<template>
    <el-form :model="device">

        <el-form-item label="模块地址">
            <el-input-number v-model="device.ADDR" :min="1" :max="99999" />
        </el-form-item>

        <el-divider>
            <el-icon><star-filled /></el-icon>
            DI输入点配置
            <el-icon><star-filled /></el-icon>
        </el-divider>

        <el-form-item v-for="(dix, key, index) in device.DIGITAL_INPUT" :key="index" :label="key">
            <el-select v-model="dix.Func">
                <el-option v-for="(func_label, func_value, func_index) in DIGITAL_INPUT_FUNCTION" :key="func_index"
                    :label="func_label" :value="func_value" />
            </el-select>
            <div v-if="dix.Func === 'RingBell'">
                <el-form-item label="类型">
                    <el-radio-group v-model="dix.ConfFlags">
                        <el-radio :value="DefaultValue.NormallyOpen">常开</el-radio>
                        <el-radio :value="DefaultValue.NormallyClose">常闭</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="扩展配置">
                    <el-input-number v-model="dix.Options" :min="1" :max="99999" />
                </el-form-item>
            </div>
            <div v-if="dix.Func === 'CommonDoor' ||
        dix.Func === 'ConnectingDoor' ||
        dix.Func === 'EntryDoor' ||
        dix.Func === 'KeyTagSensor' ||
        dix.Func === 'MinBarDoor' ||
        dix.Func === 'PIR_MotionSensor' ||
        dix.Func === 'SmokeSensor' ||
        dix.Func === 'WindowBalcony'
        ">
                <el-form-item label="类型">
                    <el-radio-group v-model="dix.ConfFlags">
                        <el-radio :value="DefaultValue.NormallyOpen">常开</el-radio>
                        <el-radio :value="DefaultValue.NormallyClose">常闭</el-radio>
                    </el-radio-group>
                </el-form-item>
            </div>
            <div v-if="dix.Func === 'LightScene'">
                灯光场景配置
                <el-select v-model="dix.LightScene.Name">
                    <el-option v-for="(scene_name, scene_value, scene_index) in DIGITAL_INPUT_LIGHT_SCENE"
                        :key="scene_index" :label="scene_name" :value="scene_value" />
                </el-select>
                <div v-if="dix.LightScene.Name === 'LightsAllOn_Suite' ||
        dix.LightScene.Name === 'LightsAllOff_Suite'
        ">
                    <el-form-item label="套房房号">
                        <el-input-number v-model="dix.LightScene.SuiteID" :min="1" :max="99999" />
                    </el-form-item>
                </div>
                <div v-if="dix.LightScene.Name === 'DimSet'">
                    <el-form-item>
                        <span>信号发送地址</span><el-input-number v-model="dix.P5Addr" :min="0" :max="99999" />
                        <span>LCflag</span><el-input-number v-model="dix.LightScene.LCFlag" :min="1" :max="99999" />
                        <span>回路</span><el-input-number v-model="dix.LightScene.Circuit" :min="1" :max="99999" />
                        <span>Level</span><el-input-number v-model="dix.LightScene.Level" :min="1" :max="99999" />
                    </el-form-item>
                </div>
                <div v-if="dix.LightScene.Name === 'DimToggle'">
                    <el-form-item>
                        <span>信号发送地址</span><el-input-number v-model="dix.P5Addr" :min="0" :max="99999" />
                        <span>回路</span><el-input-number v-model="dix.LightScene.Circuit" :min="1" :max="99999" />
                    </el-form-item>
                </div>
                <!-- <div v-if="dix.LightScene.Name === 'LightCtr'">
                    <el-form-item>
                        <span>信号发送地址</span><el-input-number v-model="dix.P5Addr" :min="0" :max="99999" />
                        <span>LCflag</span><el-input-number v-model="dix.P5Addr" :min="1" :max="99999" />
                        <span>回路</span><el-input-number v-model="dix.P5Addr" :min="1" :max="99999" />
                        <span>Level</span><el-input-number v-model="dix.P5Addr" :min="1" :max="99999" />
                    </el-form-item>
                </div> -->
                <div v-if="dix.LightScene.Name === 'LightSceneClear' || dix.LightScene.Name === 'LightSceneSet'">
                    <el-form-item>
                        <span>场景ID</span><el-input-number v-model="dix.LightScene.SceneID" :min="0" :max="48" />
                    </el-form-item>
                </div>
                <!-- <div v-if="dix.LightScene.Name === 'SceneClear'">
                    <el-form-item>
                        <span>信号发送地址</span><el-input-number v-model="dix.P5Addr" :min="0" :max="99999" />
                        <span>LCflag</span><el-input-number v-model="dix.P5Addr" :min="1" :max="99999" />
                        <span>回路</span><el-input-number v-model="dix.P5Addr" :min="1" :max="99999" />
                        <span>Level</span><el-input-number v-model="dix.P5Addr" :min="1" :max="99999" />
                    </el-form-item>
                </div> -->
                <div v-if="dix.LightScene.Name === 'SmartCircuitToggle'">
                    <el-form-item>
                        <span>信号发送地址</span><el-input-number v-model="dix.P5Addr" :min="0" :max="99999" />
                        <span>逻辑回路ID</span><el-input-number v-model="dix.LightScene.LogicalCircuitID" :min="1"
                            :max="99999" />
                        <span>参考回路ID</span><el-input-number v-model="dix.LightScene.GuideID" :min="1" :max="16" />
                    </el-form-item>
                </div>
                <div v-if="dix.LightScene.Name === 'SmartSceneRotate'">
                    <el-form-item>
                        <span>信号发送地址</span><el-input-number v-model="dix.P5Addr" :min="0" :max="99999" />
                        <span>场景数量</span><el-input-number v-model="dix.LightScene.RotateSceneCnt" :min="2" :max="4" />
                        <span>场景1</span><el-input-number v-model="dix.LightScene.RotateSceneID[0]" :min="0" :max="48" />
                        <span>场景2</span><el-input-number v-model="dix.LightScene.RotateSceneID[1]" :min="0" :max="48" />

                        <span v-if="dix.LightScene.RotateSceneCnt > 2">场景3</span><el-input-number
                            v-model="dix.LightScene.RotateSceneID[2]" :min="0" :max="48"
                            v-if="dix.LightScene.RotateSceneCnt > 2" />
                        <span v-if="dix.LightScene.RotateSceneCnt > 3">场景4</span><el-input-number
                            v-model="dix.LightScene.RotateSceneID[3]" :min="0" :max="48"
                            v-if="dix.LightScene.RotateSceneCnt > 3" />

                        <span>参考回路ID</span><el-input-number v-model="dix.LightScene.GuideID" :min="1" :max="16" />
                    </el-form-item>
                </div>
                <div v-if="dix.LightScene.Name === 'SmartSceneToggle'">
                    <el-form-item>
                        <span>信号发送地址</span><el-input-number v-model="dix.P5Addr" :min="0" :max="99999" />
                        <span>场景ID</span><el-input-number v-model="dix.LightScene.SceneID" :min="0" :max="48" />
                        <span>参考回路ID</span><el-input-number v-model="dix.LightScene.GuideID" :min="1" :max="16" />
                    </el-form-item>
                </div>
            </div>
        </el-form-item>

        <el-divider>
            <el-icon><star-filled /></el-icon>
            DO输入点配置
            <el-icon><star-filled /></el-icon>
        </el-divider>

        <el-form-item v-for="(dix, key, index) in device.DIGITAL_OUTPUT" :key="index" :label="key">
            <el-select v-model="dix.Func">
                <el-option v-for="(func_label, func_value, func_index) in DIGITAL_OUTPUT_FUNCTION" :key="func_index"
                    :label="func_label" :value="func_value" />
            </el-select>
            <div v-if="dix.Func === 'DigitalOutput'">
                <el-form-item>
                    <span>PI5_Ofs</span>
                    <el-input-number v-model="dix.DigitalOutput.PI5_Ofs" :min="0" :max="99999" :controls="false" />
                    <span>BitMask</span>
                    <el-input-number v-model="dix.DigitalOutput.BitMask" :min="0" :max="99999" :controls="false" />
                    <span>ConfFlags</span>
                    <el-input-number v-model="dix.DigitalOutput.ConfFlags" :min="0" :max="99999" :controls="false" />
                </el-form-item>

            </div>
            <div v-if="dix.Func === 'LightOutput'">
                <el-form-item>
                    <span>欢迎模式</span>
                    <el-switch v-model="dix.LightOutput.Welcome" inline-prompt active-text="参与" inactive-text="取消" />
                    <span>员工欢迎模式</span>
                    <el-switch v-model="dix.LightOutput.WelcomeStaff" inline-prompt active-text="参与"
                        inactive-text="取消" />
                    <el-input-number v-if="dix.LightOutput.WelcomeStaff" v-model="dix.LightOutput.WelcomeStaffDelayMin"
                        :min="0" :max="99999" :controls="false" />
                    <span>客人欢迎模式</span>
                    <el-switch v-model="dix.LightOutput.WelcomeGuest" inline-prompt active-text="参与"
                        inactive-text="取消" />
                    <el-input-number v-if="dix.LightOutput.WelcomeGuest" v-model="dix.LightOutput.WelcomeGuestDelayMin"
                        :min="0" :max="99999" :controls="false" />
                </el-form-item>
                <el-form-item>
                    <span v-for="(_, index) in dix.LightOutput.LogicalCircuitList" :key="index">
                        参与的第{{ index }}个逻辑回路ID
                        <el-input-number v-model="dix.LightOutput.LogicalCircuitList[index]" :min="0" :max="99999" />
                    </span>
                </el-form-item>
                <el-form-item>
                    <span v-for="(_, index) in dix.LightOutput.SceneList" :key="index">
                        场景{{ index.toString().padStart(2, '0') }}
                        <el-switch v-model="dix.LightOutput.SceneList[index].Enable" inline-prompt active-text="参与"
                            inactive-text="取消" />
                        <el-switch v-if="dix.LightOutput.SceneList[index].Enable"
                            v-model="dix.LightOutput.SceneList[index].Value" inline-prompt active-text="开灯"
                            inactive-text="关灯" style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949" />
                    </span>
                </el-form-item>
            </div>
        </el-form-item>

        <el-divider>
            <el-icon><star-filled /></el-icon>
            虚拟回路配置
            <el-icon><star-filled /></el-icon>
        </el-divider>

        <el-form-item v-for="(dix, key, index) in device.VIRTUAL_OUTPUT" :key="index" :label="key">
            <el-select v-model="dix.Func">
                <el-option v-for="(func_label, func_value, func_index) in VIRTUAL_OUTPUT_FUNCTION" :key="func_index"
                    :label="func_label" :value="func_value" />
            </el-select>
            <div v-if="dix.Func === 'GuideOutput'">
                <el-form-item>
                    <span>欢迎模式</span>
                    <el-switch v-model="dix.LightOutput.Welcome" inline-prompt active-text="参与" inactive-text="取消" />
                    <span>员工欢迎模式</span>
                    <el-switch v-model="dix.LightOutput.WelcomeStaff" inline-prompt active-text="参与"
                        inactive-text="取消" />
                    <el-input-number v-if="dix.LightOutput.WelcomeStaff" v-model="dix.LightOutput.WelcomeStaffDelayMin"
                        :min="0" :max="99999" :controls="false" />
                    <span>客人欢迎模式</span>
                    <el-switch v-model="dix.LightOutput.WelcomeGuest" inline-prompt active-text="参与"
                        inactive-text="取消" />
                    <el-input-number v-if="dix.LightOutput.WelcomeGuest" v-model="dix.LightOutput.WelcomeGuestDelayMin"
                        :min="0" :max="99999" :controls="false" />
                </el-form-item>
                <el-form-item>
                    <span v-for="(_, index) in dix.LightOutput.LogicalCircuitList" :key="index">
                        参与的第{{ index }}个逻辑回路ID
                        <el-input-number v-model="dix.LightOutput.LogicalCircuitList[index]" :min="0" :max="99999" />
                    </span>
                </el-form-item>
                <el-form-item>
                    <span v-for="(_, index) in dix.LightOutput.SceneList" :key="index">
                        场景{{ index.toString().padStart(2, '0') }}
                        <el-switch v-model="dix.LightOutput.SceneList[index].Enable" inline-prompt active-text="参与"
                            inactive-text="取消" />
                        <el-switch v-if="dix.LightOutput.SceneList[index].Enable"
                            v-model="dix.LightOutput.SceneList[index].Value" inline-prompt active-text="开灯"
                            inactive-text="关灯" style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949" />
                    </span>
                </el-form-item>
            </div>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="onSubmit">生成</el-button>
        </el-form-item>
    </el-form>

    <el-dialog v-model="dialogVisible" title="生成代码">
        <div class="hljs-container" codetype="Personality.c">
            <el-button type="primary" @click="iar_code_copy">复制</el-button>
            <highlightjs language="c" :autodetect="false" :code="code"></highlightjs>
        </div>
    </el-dialog>
</template>
<script setup>
import { ref, reactive } from "vue";
import { StarFilled } from "@element-plus/icons-vue";
import { DEVICE_MODEL, NEW_DIGITAL_INPUT, DIGITAL_INPUT_FUNCTION, DefaultValue, DIGITAL_INPUT_LIGHT_SCENE, NEW_DIGITAL_OUTPUT, DIGITAL_OUTPUT_FUNCTION, VIRTUAL_OUTPUT_FUNCTION } from '../js/INNcom.js'
import { ElMessage } from 'element-plus'
const dialogVisible = ref(false);
// 设备信息
const device = reactive({
    ADDR: 1,
    MODEL: DEVICE_MODEL['X10'],
    DIGITAL_INPUT: {
        DI1: NEW_DIGITAL_INPUT(1),
        DI2: NEW_DIGITAL_INPUT(2),
        DI3: NEW_DIGITAL_INPUT(3),
        DI4: NEW_DIGITAL_INPUT(4),
        DI5: NEW_DIGITAL_INPUT(5),
        DI6: NEW_DIGITAL_INPUT(6),
        DI7: NEW_DIGITAL_INPUT(7),
        DI8: NEW_DIGITAL_INPUT(8),
    },
    DIGITAL_OUTPUT: {
        DO1: NEW_DIGITAL_OUTPUT(1),
        DO2: NEW_DIGITAL_OUTPUT(2),
        DO3: NEW_DIGITAL_OUTPUT(3),
        DO4: NEW_DIGITAL_OUTPUT(4),
        DO5: NEW_DIGITAL_OUTPUT(5),
        DO6: NEW_DIGITAL_OUTPUT(6),
        DO7: NEW_DIGITAL_OUTPUT(7),
        DO8: NEW_DIGITAL_OUTPUT(8),
    },
    VIRTUAL_OUTPUT: {
        VIR1: NEW_DIGITAL_OUTPUT(9),
        VIR2: NEW_DIGITAL_OUTPUT(10),
        VIR3: NEW_DIGITAL_OUTPUT(11),
        VIR4: NEW_DIGITAL_OUTPUT(12),
        VIR5: NEW_DIGITAL_OUTPUT(13),
        VIR6: NEW_DIGITAL_OUTPUT(14),
        VIR7: NEW_DIGITAL_OUTPUT(15),
        VIR8: NEW_DIGITAL_OUTPUT(16),
    }
});
let code = '';
const onSubmit = () => {
    let code71 = '';
    Object.values(device.DIGITAL_INPUT).forEach((dix) => {
        switch (dix.Func) {
            case 'LightScene':
                switch (dix.LightScene) {
                    case 'LightsAllOn_Room':
                        code71 += `REG_HELPER_LowVoltageInput_LightsAllOn_Room(${dix.Pin});\n    `
                        break;
                    case 'LightsAllOff_Room':
                        code71 += `REG_HELPER_LowVoltageInput_LightsAllOff_Room(${dix.Pin});\n    `
                        break;
                    case 'LightsAllOn_Suite':
                        code71 += `REG_HELPER_LowVoltageInput_LightsAllOn_Suite(${dix.Pin},${dix.SuiteID});\n    `
                        break;
                    case 'LightsAllOff_Suite':
                        code71 += `REG_HELPER_LowVoltageInput_LightsAllOff_Suite(${dix.Pin},${dix.SuiteID});\n    `
                        break;
                    case 'DimSet':
                        code71 += `REG_HELPER_LowVoltageInput_DimSet(${dix.Pin},${dix.P5Addr},${dix.LCFlag},${dix.Circuit},${dix.Level});\n    `
                        break;
                    case 'DimToggle':
                        code71 += `REG_HELPER_LowVoltageInput_DimToggle(${dix.Pin},${dix.P5Addr},${dix.Circuit});\n    `
                        break;
                    //     case 'LightCtr':
                    // code71 += `REG_HELPER_LowVoltageInput_LightCtr(${dix.Pin},${dix.P5Addr},${dix.Circuit});\n    `
                    //     break;
                    case 'LightSceneClear':
                        code71 += `REG_HELPER_LowVoltageInput_LightSceneClear(${dix.Pin},${dix.SceneID});\n    `
                        break;
                    case 'LightSceneSet':
                        code71 += `REG_HELPER_LowVoltageInput_LightSceneSet(${dix.Pin},${dix.SceneID});\n    `
                        break;
                    //     case 'SceneClear':
                    // code71 += `REG_HELPER_LowVoltageInput_LightSceneSet(${dix.Pin},${dix.SceneID});\n    `
                    //     break;
                    case 'SmartCircuitToggle':
                        code71 += `REG_HELPER_LowVoltageInput_SmartCircuitToggle(${dix.Pin},${dix.P5Addr},${dix.LogicalCircuitID},${dix.GuideID});\n    `
                        break;
                    case 'SmartSceneRotate':
                        code71 += `REG_HELPER_LowVoltageInput_SmartSceneRotate(${dix.Pin},${dix.P5Addr},0,${dix.RotateSceneCnt},${dix.GuideID},${dix.RotateSceneID[0]},${dix.RotateSceneID[1]},${dix.RotateSceneID[2]},${dix.RotateSceneID[3]});\n    `
                        break;
                    case 'SmartSceneToggle':
                        code71 += `REG_HELPER_LowVoltageInput_SmartSceneToggle(${dix.Pin},${dix.P5Addr},0,${dix.SceneID},${dix.GuideID});\n    `
                        break;
                    default:
                        break;
                }
                break;
            case 'EntryDoor':
                code71 += `REG_HELPER_LowVoltage_Input_StdCommonCtrl_EntryDoor(${dix.Pin},${dix.Pin},${dix.ConfFlags});\n    `
                break;
            case 'RingBell':
                code71 += `REG_HELPER_LowVoltage_Input_RingBell(${dix.Pin},${dix.Pin},${dix.P5Addr},${dix.ConfFlags},${dix.Options});\n    `
                break;
            case 'PIR_MotionSensor':
                code71 += `REG_HELPER_LowVoltage_Input_StdCommonCtrl_PIR_MotionSensor(${dix.Pin},${dix.Pin},${dix.ConfFlags});\n    `
                break;
            case 'WindowBalcony':
                code71 += `REG_HELPER_LowVoltage_Input_StdCommonCtrl_WindowBalcony(${dix.Pin},${dix.Pin},${dix.ConfFlags});\n    `
                break;
            case 'SmokeSensor':
                code71 += `REG_HELPER_LowVoltage_Input_StdCommonCtrl_SmokeSensor(${dix.Pin},${dix.Pin},${dix.ConfFlags});\n    `
                break;
            case 'MinBarDoor':
                code71 += `REG_HELPER_LowVoltage_Input_StdCommonCtrl_MinBarDoor(${dix.Pin},${dix.Pin},${dix.ConfFlags});\n    `
                break;
            case 'KeyTagSensor':
                code71 += `REG_HELPER_LowVoltage_Input_StdCommonCtrl_KeyTagSensor(${dix.Pin},${dix.Pin},${dix.ConfFlags});\n    `
                break;
            case 'CommonDoor':
                code71 += `REG_HELPER_LowVoltage_Input_StdCommonCtrl_CommonDoor(${dix.Pin},${dix.Pin},${dix.ConfFlags});\n    `
                break;
            case 'ConnectingDoor':
                code71 += `REG_HELPER_LowVoltage_Input_StdCommonCtrl_ConnectingDoor(${dix.Pin},${dix.Pin},${dix.ConfFlags});\n    `
                break;
            default:
                break;
        }
    });
    let code72 = '';
    let code73 = '';
    Object.values(device.DIGITAL_OUTPUT).forEach((dix) => {
        switch (dix.Func) {
            case 'DigitalOutput':
                code72 += `REG_HELPER_Output_PI5_Bit(${dix.Pin}, ${dix.PI5_Ofs}, ${dix.BitMask}, ${dix.ConfFlags});\n    `
                break;
            case 'LightOutput':
                code73 += `
    Reg.BaseOutput.DriverIndex              = CBL32_LO_DRIVER_INDEX_LV_INDICATOR;
    Reg.BaseOutput.OutputSetup              = CBL32_DO_OUTPUT_SETUP_DEFAULT;
    Reg.BaseOutput.DriverPinIndex           = P5_HELPER_MapOutput_CBL_to_Pin(${dix.Pin});
    Reg.BaseOutput.WelcomeSetup             = ${dix.LightOutput.Welcome ? 0x80 : 0x00};
    Reg.BaseOutput.WelcomeStaff             = ${dix.LightOutput.WelcomeStaff ? 0x80 : 0x00};
    Reg.BaseOutput.WelcomeGuest             = ${dix.LightOutput.WelcomeGuest ? 0x80 : 0x00};
    Reg.BaseOutput.Min_GracePeriodStaff     = ${dix.LightOutput.WelcomeStaffDelayMin};
    Reg.BaseOutput.Min_GracePeriodGuest     = ${dix.LightOutput.WelcomeGuestDelayMin};
    Reg.DimSetup                            = 0;\n    `
                dix.LightOutput.LogicalCircuitList.forEach((s, index) => {
                    if (s > 0) {
                        code73 += `Reg.LogCircuit[${index}]                       = ${s};\n    `
                    }
                });
                dix.LightOutput.SceneList.forEach((s, index) => {
                    if (s.Enable) {
                        code73 += `Reg.SceneLevel[${index<10?' ':''}${index}]                      = ${s.Enable ? 0x80 : 0x00}  + CBL32_LS_LEVEL_PERCENT(${s.Value ? 100 : 0});\n    `
                    }
                });
                code73 += `APPMAP_RegistryCreate(REG_GRPKEY_LIGHT_OUTPUT, ${dix.Pin}, (BYTE *)&Reg, sizeof(Reg));\n    `
                break;
            default:
                break;
        }
    });
    Object.values(device.VIRTUAL_OUTPUT).forEach((dix) => {
        if (dix.Func === 'GuideOutput') {
            code73 += `
    Reg.BaseOutput.DriverIndex              = CBL32_LO_DRIVER_INDEX_LV_INDICATOR;
    Reg.BaseOutput.OutputSetup              = CBL32_DO_OUTPUT_SETUP_DEFAULT;
    Reg.BaseOutput.DriverPinIndex           = P5_HELPER_MapOutput_CBL_to_Pin(${dix.Pin});
    Reg.BaseOutput.WelcomeSetup             = ${dix.LightOutput.Welcome ? 0x80 : 0x00};
    Reg.BaseOutput.WelcomeStaff             = ${dix.LightOutput.WelcomeStaff ? 0x80 : 0x00};
    Reg.BaseOutput.WelcomeGuest             = ${dix.LightOutput.WelcomeGuest ? 0x80 : 0x00};
    Reg.BaseOutput.Min_GracePeriodStaff     = ${dix.LightOutput.WelcomeStaffDelayMin};
    Reg.BaseOutput.Min_GracePeriodGuest     = ${dix.LightOutput.WelcomeGuestDelayMin};
    Reg.DimSetup                            = 0;\n    `
            dix.LightOutput.LogicalCircuitList.forEach((s, index) => {
                if (s > 0) {
                    code73 += `Reg.LogCircuit[${index}]                       = ${s};\n    `
                }
            });
            dix.LightOutput.SceneList.forEach((s, index) => {
                if (s.Enable) {
                    code73 += `Reg.SceneLevel[${index<10?' ':''}${index}]                      = ${s.Enable ? 0x80 : 0x00}  + CBL32_LS_LEVEL_PERCENT(${s.Value ? 100 : 0});\n    `
                }
            });
            code73 += `APPMAP_RegistryCreate(REG_GRPKEY_LIGHT_OUTPUT, ${dix.Pin}, (BYTE *)&Reg, sizeof(Reg));\n    `
        }
    });
    code = `//**********************************************************************************************************************
// FileName   : Personality.c
// Author(s)  : Philipp Roosli, Michael Sobanko
// Description: Template for the personalization of a device.
//**********************************************************************************************************************
// Version History:
// 2011/10/14 Base release.
//**********************************************************************************************************************

#include "string.h"
#include "APPMAP_Main.h"
#include "Personality.h"

//**********************************************************************************************************************
// 设备型号及地址
//**********************************************************************************************************************

#define APP_ID_DEVICE_TYPE \
    ${ device.MODEL }
#define P5_ADDRESS \
    ${ device.ADDR }

//**********************************************************************************************************************
// Constants
//**********************************************************************************************************************
const char LogicalDeviceIdentifier[] = "X10.r4";

void RunObject(TYPE_CBL32_OBJECT *ptrObj)
{
    TYPE_REG_ENTRY *RegEntry;
    TYPE_CBL32_LIGHT_OUTPUT_DATA *LOData;
    if (ptrObj)
    {
        RegEntry = ptrObj->ptrRegEntry;
        LOData = (TYPE_CBL32_LIGHT_OUTPUT_DATA *)ptrObj->ptrData;
        switch (RegEntry->RegGroup)
        {
        case (REG_GRPKEY_IO >> 8):
            switch (RegEntry->RegKey)
            {
            case (REG_GRPKEY_DIGITAL_INPUT & 0xFF):
            case (REG_GRPKEY_ANALOG_OUTPUT & 0xFF):
                CBL_HELPER_DefaultIO_RunObject(ptrObj);
                break;
            case (REG_GRPKEY_DIGITAL_OUTPUT & 0xFF):
            {
                if (LOData->BOData.DriverCtrlCmd == CBL32_IRAS_CTRL_CMD_SET_VALUE)
                {
                    switch (RegEntry->RegIdx)
                    {
                    case 1: // Pin  3
                    case 2: // Pin  4
                    case 3: // Pin  5
                    case 4: // Pin  6
                    case 5: // Pin  7
                    case 6: // Pin  8
                    case 7: // Pin  9
                    case 8: // Pin 10
                            // HW_API_02_9593_DigitalOut_Control(P5_HELPER_MapOutput_CBL_to_Pin(RegEntry->RegIdx), (BOOL)LOData->BOData.DriverCtrlValue, BOConfig->DriverConfFlags);
                        break;
                    }
                }
            }
            break;
            case (REG_GRPKEY_LIGHT_OUTPUT & 0xFF):
                break;
            case (REG_GRPKEY_ANALOG_INPUT & 0xFF):
                break;
            }
            break;
        }
    }
}

void BootRegistry7_1(BYTE IOMAP)
{
    ${ code71 }
}

void BootRegistry7_2(BYTE IOMAP)
{
    ${ code72 }
}

void BootRegistry7_3(BYTE IOMAP)
{
    TYPE_CBL32REG_7_3_LIGHT_OUTPUT Reg;
    memcpy(&Reg, (BYTE *)&REGBOOT_7_3_DEFAULT, sizeof(Reg));
    ${ code73 }
}

void ProcessVFI(TYPE_VFI *VFI)
{}

void ApplicationStartup(void)
{
    BOOL flagBootAppRegistry = TRUE;
    TYPE_REG_ENTRY *RegEntry = (TYPE_REG_ENTRY *)APPMAP_WRAPPER_BootLoaderSendMsg(CBL32_MSG_REGISTRY_SEARCH_UNIQUE, (REG_GRPKEY_EXT_DEV_NAME << 8), 0);
    if (RegEntry)
    {
        if (strcmp(LogicalDeviceIdentifier, (char *)(RegEntry->RegData)) == 0)
        {
            flagBootAppRegistry = FALSE;
        }
    }
    if (flagBootAppRegistry)
    {
        BootRegistry(0);
    }
}

void ProcessP5Reply(BYTE *Data, BYTE DataLen)
{}

void ProcessPAE(BYTE P5Addr, USHORT Param)
{}

void ProcessP5Interceptor(TYPE_CBL32_P5_PRETTY_PACKET *P5PP, ULONG Flags)
{}

ULONG ProcessMsg(ULONG Msg, ULONG P1, ULONG P2)
{
    switch (Msg)
    {
    case CBL32_MSG_4MSEC_TICK:
        break;
    case CBL32_MSG_32MSEC_TICK:
        break;
    case CBL32_MSG_PROCESS_VFI:
        ProcessVFI((TYPE_VFI *)P1);
        break;
    }
    return (CBL_HELPER_DefaultIO_ProcessMsg(Msg, P1, P2));
}

void ReconfRegistry(BYTE ReconfOp)
{}

void BootRegistry(BYTE IOMAP)
{
    if (IOMAP < 1)
    {
        APPMAP_EEPROM_Write(EEPROM_OFS_STD_NSPB_BLOCK_CACHED_NV + P5STM_NSPB_CACHE_FLAG0, 0x10);
        APPMAP_EEPROM_Write(EEPROM_OFS_STD_NSPB_BLOCK_CACHED_NV + P5STM_NSPB_CACHE_FLAG1, 0x0F);
        APPMAP_EEPROM_Write(EEPROM_OFS_STD_NSPB_BLOCK_CACHED_NV + P5STM_NSPB_CACHE_STATION_ADDR, P5_ADDRESS);
        APPMAP_EEPROM_Write(EEPROM_OFS_STD_NSPB_BLOCK_CACHED_NV + P5STM_NSPB_CACHE_FLAG3, 0x0F);
        APPMAP_EEPROM_Write(EEPROM_OFS_STD_NSPB_BLOCK_CACHED_NV + P5STM_NSPB_CACHE_MULTICAST_MAP, 0xFF);
        APPMAP_EEPROM_Write(EEPROM_OFS_STD_NSPB_BLOCK_CACHED_NV + P5STM_NSPB_CACHE_MC_FOCUS_0, 0x00);
        APPMAP_EEPROM_Write(EEPROM_OFS_STD_NSPB_BLOCK_CACHED_NV + P5STM_NSPB_CACHE_MC_FOCUS_1, 0x00);
        APPMAP_EEPROM_Write(EEPROM_OFS_STD_NSPB_BLOCK_CACHED_NV + P5STM_NSPB_CACHE_APP_SERVER_0 + 0, 0x00);
        APPMAP_EEPROM_Write(EEPROM_OFS_STD_NSPB_BLOCK_CACHED_NV + P5STM_NSPB_CACHE_APP_SERVER_0 + 1, 0x00);
        APPMAP_EEPROM_Write(EEPROM_OFS_STD_NSPB_BLOCK_CACHED_NV + P5STM_NSPB_CACHE_APP_SERVER_0 + 2, 0x00);
        APPMAP_EEPROM_Write(EEPROM_OFS_STD_NSPB_BLOCK_CACHED_NV + P5STM_NSPB_CACHE_APP_SERVER_0 + 3, 0x00);
        REG_HELPER_FormatRegistry();
    }
    BootRegistry7_1(IOMAP);
    BootRegistry7_2(IOMAP);
    BootRegistry7_3(IOMAP);
    APPMAP_RegistryCreateUKey_String((REG_GRPKEY_PROPERTY_ID << 8), (const char *)("2.17.3.15"));
    APPMAP_RegistryCreateUKey_String((REG_GRPKEY_PART_NO << 8), (const char *)("01-8201.C"));
    APPMAP_RegistryCreateUKey_String((REG_GRPKEY_ENGINEER << 8), (const char *)("Greg.Howard@inncom.com"));
    APPMAP_RegistryCreateUKey_String((REG_GRPKEY_ENG_MEMO << 8), (const char *)(""));
    APPMAP_RegistryCreateUKey_String((REG_GRPKEY_EXT_DEV_NAME << 8), LogicalDeviceIdentifier);
    APPMAP_RegistryCreateUKey_String((REG_GRPKEY_INNCONTROL_OPTIONS << 8), (const char *)("Dev_LVIO_2_17_3_15.cfg"));
}
`
    dialogVisible.value = true;
}

const iar_code_copy = async () => {
    try {
        await navigator.clipboard.writeText(code);
        ElMessage({
            message: '已复制',
            type: 'success',
        })
    } catch (err) {
        console.error('无法复制: ', err);
    }
};

</script>
<style>
.hljs-container {


    position: relative;
    display: block;
    /* width: 600px; */
    padding: 30px 5px 2px;
    overflow-x: hidden;
    line-height: 20px;
    text-align: left;
    background: #21252b;
    box-shadow: 0 10px 30px 0 rgb(0 0 0 / 40%);
}

/** 3个点 */
.hljs-container::before {


    position: absolute;
    top: 10px;
    left: 15px;
    width: 12px;
    height: 12px;
    overflow: visible;
    font-weight: 700;
    font-size: 16px;
    line-height: 12px;
    white-space: nowrap;
    text-indent: 75px;
    background-color: #fc625d;
    border-radius: 16px;
    box-shadow: 20px 0 #fdbc40, 40px 0 #35cd4b;
    content: attr(codetype);
}

/** 滚动条 */
:deep(.hljs) {


    overflow-x: auto;
}

:deep(.hljs::-webkit-scrollbar) {


    width: 12px !important;
    height: 12px !important;
}

:deep(.hljs::-webkit-scrollbar-thumb) {


    height: 30px !important;
    background: #d1d8e6;
    background-clip: content-box;
    border: 2px solid transparent;
    border-radius: 19px;
    opacity: 0.8;
}

:deep(.hljs::-webkit-scrollbar-thumb:hover) {


    background: #a5b3cf;
    background-clip: content-box;
    border: 2px solid transparent;
}

:deep(.hljs::-webkit-scrollbar-track-piece) {


    width: 30px;
    height: 30px;
    background: #333;
}

::-webkit-scrollbar-button {


    display: none;
}

/** 复制样式 */
.hljs-copy {


    position: absolute;
    top: 50px;
    right: 30px;
    display: none;
    padding: 0 10px;
    color: #66a9ff;
    font-size: 10px;
    background-color: #ecf5ff;
    border-radius: 3px;
    cursor: pointer;
}
</style>