_ENCRYPT_ = 0    --LUA脚本加密

--数据类型定义
VT_LW = 1    --变量地址
VT_RW = 2    --FLASH存储

EN_ON_UPDATE_API_CB = 1 --全局变量，其他回调函数设置寄存器时，为1执行，为0直接退出


--用户地址，对应工程设定的地址表
useraddr = {
    switch            = 0x1001,    --开关按键
    driver            = 0x1003,    --设备名称
    temp_value        = 0x100C,    --温度
    speed             = 0x1019,    --时速
    animation         = 0x101A,    --动画
    time_set          = 0x101B,    --时间确认
    music_id1state    = 0x1021,    --音频1
    slide             = 0x1022,    --滑动按键
    music_id2state    = 0x1026,    --音频2
    master_addr       = 0x1028,    --仪表值
    enable_collection = 0x1105,    --资料采集使能
    temp_warn         = 0x1150,    --文字滚动
    flow_block_speed  = 0x121F,    --流动块
    can_text          = 0x1220,    --can检测显示文本
    uart2_text        = 0x1221,    --串口检测显示文本
    usb_text          = 0x1222,    --U盘插入检测文本
    sd_text           = 0x1223,    --SD卡插入检测文本
    switch_codes      = 0x2000,    --二维码切换
    codes_addr        = 0x2001,    --告警显示
    data              = 0x2100,    --数据记录
}

--系统参数地址，对应工程系统地址(系统地址不可更改)
sysaddr = {
    SysLang     = 0x0119,    --系统语言
    SysSndVol   = 0x0140,    --喇叭音量
    SysSndPlay  = 0x0141,    --音频播放ID
    SysSndState = 0x0142,    --音频播放控制
}

--画面ID
page ={
    animation       = 10,    --动画
    flow_block      = 12,    --流动块
    record          = 15,    --数据记录-表格
    graph           = 16,    --数据记录-曲线
    audio_play_back = 21,    --音频
    test            = 23,    --测试
}

--双串口检测
local cmd = {
    head_tag = 0, --检测头标识
    head     = 0, --存放头字节
    tail     = 0, --存放尾字节
    buff     = {}, --指令缓存
    len      = 0, --指令长度
    datalen  = 0  --内容长度
}


--正弦波
local sine_wave = {
    0x21,0x1D,0x19,0x16,0x13,0x0F,0x0D,0x0A,0x08,0x06,
    0x04,0x03,0x02,0x01,0x01,0x01,0x01,0x01,0x02,0x03,
    0x04,0x06,0x08,0x0A,0x0D,0x0F,0x13,0x16,0x19,0x1D,
    0x21,0x26,0x2A,0x2F,0x34,0x39,0x3E,0x44,0x49,0x4F,
    0x55,0x5B,0x61,0x67,0x6D,0x73,0x79,0x7F,0x86,0x8C,
    0x92,0x98,0x9E,0xA4,0xAA,0xB0,0xB6,0xBB,0xC1,0xC6,
    0xCB,0xD0,0xD5,0xD9,0xDE,0xE2,0xE6,0xE9,0xEC,0xF0,
    0xF2,0xF5,0xF7,0xF9,0xFB,0xFC,0xFD,0xFE,0xFE,0xFE,
    0xFE,0xFE,0xFD,0xFC,0xFB,0xF9,0xF7,0xF5,0xF2,0xF0,
    0xEC,0xE9,0xE6,0xE2,0xDE,0xD9,0xD5,0xD0,0xCB,0xC6,
    0xC1,0xBB,0xB6,0xB0,0xAA,0xA4,0x9E,0x98,0x92,0x8C,
    0x86,0x80,0x79,0x73,0x6D,0x67,0x61,0x5B,0x55,0x4F,
    0x49,0x44,0x3E,0x39,0x34,0x2F,0x2A,0x26,0x21,0x1D,
    0x19,0x16,0x13,0x0F,0x0D,0x0A,0x08,0x06,0x04,0x03,
    0x02,0x01,0x01,0x01,0x01,0x01,0x02,0x03,0x04,0x06,
    0x08,0x0A,0x0D,0x0F,0x13,0x16,0x19,0x1D,0x21,0x26,
    0x2A,0x2F,0x34,0x39,0x3E,0x44,0x49,0x4F,0x55,0x5B,
    0x61,0x67,0x6D,0x73,0x79,0x7F,0x86,0x8C,0x92,0x98,
    0x9E,0xA4,0xAA,0xB0,0xB6,0xBB,0xC1,0xC6,0xCB,0xD0,
    0xD5,0xD9,0xDE,0xE2,0xE6,0xE9,0xEC,0xF0,0xF2,0xF5,
    0xF7,0xF9,0xFB,0xFC,0xFD,0xFE,0xFE,0xFE,0xFE,0xFE,
    0xFD,0xFC,0xFB,0xF9,0xF7,0xF5,0xF2,0xF0,0xEC,0xE9,
    0xE6,0xE2,0xDE,0xD9,0xD5,0xD0,0xCB,0xC6,0xC1,0xBB,
    0xB6,0xB0,0xAA,0xA4,0x9E,0x98,0x92,0x8C,0x86,0x80,
    0x79,0x73,0x6D,0x67,0x61,0x5B,0x55,0x4F,0x49,0x44,
    0x3E,0x39,0x34,0x2F,0x2A,0x26,0x21,0x1D,0x19,0x16,
    0x13,0x0F,0x0D,0x0A,0x08,0x06,0x04,0x03,0x02,0x01,
    0x01,0x01,0x01,0x01,0x02,0x03,0x04,0x06,0x08,0x0A,
    0x0D,0x0F,0x13,0x16,0x19,0x1D,0x21,0x26,0x2A,0x2F,
    0x34,0x39,0x3E,0x44,0x49,0x4F,0x55,0x5B,0x61,0x67,
    0x6D,0x73,0x79,0x7F,0x86,0x8C,0x92,0x98,0x9E,0xA4,
    0xAA,0xB0,0xB6,0xBB,0xC1,0xC6,0xCB,0xD0,0xD5,0xD9,
    0xDE,0xE2,0xE6,0xE9,0xEC,0xF0,0xF2,0xF5,0xF7,0xF9,
    0xFB,0xFC,0xFD,0xFE,0xFE,0xFE,0xFE,0xFE,0xFD,0xFC,
    0xFB,0xF9,0xF7,0xF5,0xF2,0xF0,0xEC,0xE9,0xE6,0xE2,
    0xDE,0xD9,0xD5,0xD0,0xCB,0xC6,0xC1,0xBB,0xB6,0xB0,
    0xAA,0xA4,0x9E,0x98,0x92,0x8C,0x86,0x80,0x79,0x73,
    0x6D,0x67,0x61,0x5B,0x55,0x4F,0x49,0x44,0x3E,0x39,
    0x34,0x2F,0x2A,0x26,0x21,0x1D,0x19,0x16,0x13,0x0F,
    0x0D,0x0A,0x08,0x06,0x04,0x03,0x02,0x01,0x01,0x01,
    0x01,0x01,0x02,0x03,0x04,0x06,0x08,0x0A,0x0D,0x0F,
    0x13,0x16,0x19,0x1D,0x21,0x26,0x2A,0x2F,0x34,0x39,
    0x3E,0x44,0x49,0x4F,0x55,0x5B,0x61,0x67,0x6D,0x73,
    0x79,0x7F,0x86,0x8C,0x92,0x98,0x9E,0xA4,0xAA,0xB0,
    0xB6,0xBB,0xC1,0xC6,0xCB,0xD0,0xD5,0xD9,0xDE,0xE2,
    0xE6,0xE9,0xEC,0xF0,0xF2,0xF5,0xF7,0xF9,0xFB,0xFC,
    0xFD,0xFE,0xFE,0xFE,0xFE,0xFE,0xFD,0xFC,0xFB,0xF9,
    0xF7,0xF5,0xF2,0xF0,0xEC,0xE9,0xE6,0xE2,0xDE,0xD9,
    0xD5,0xD0,0xCB,0xC6,0xC1,0xBB,0xB6,0xB0,0xAA,0xA4,
    0x9E,0x98,0x92,0x8C,0x86,0x80,0x79,0x73,0x6D,0x67,
    0x61,0x5B,0x55,0x4F,0x49,0x44,0x3E,0x39,0x34,0x2F,
    0x2A,0x26
}

--锯齿波
local zigzag_wave = {
    0xCC,0xD4,0xDD,0xE5,0xEE,0xF6,0x00,0x08,0x11,0x19,
    0x22,0x2A,0x33,0x3B,0x44,0x4C,0x55,0x5D,0x66,0x6E,
    0x77,0x7F,0x88,0x90,0x99,0xA1,0xAA,0xB2,0xBB,0xC3,
    0xCC,0xD4,0xDD,0xE5,0xEE,0xF6,0x00,0x08,0x11,0x19,
    0x22,0x2A,0x33,0x3B,0x44,0x4C,0x55,0x5D,0x66,0x6E,
    0x77,0x7F,0x88,0x90,0x99,0xA1,0xAA,0xB2,0xBB,0xC3,
    0xCC,0xD4,0xDD,0xE5,0xEE,0xF6,0x00,0x08,0x11,0x19,
    0x22,0x2A,0x33,0x3B,0x44,0x4C,0x55,0x5D,0x66,0x6E,
    0x77,0x7F,0x88,0x90,0x99,0xA1,0xAA,0xB2,0xBB,0xC3,
    0xCC,0xD4,0xDD,0xE5,0xEE,0xF6,0x00,0x08,0x11,0x19,
    0x22,0x2A,0x33,0x3B,0x44,0x4C,0x55,0x5D,0x66,0x6E,
    0x77,0x7F,0x88,0x90,0x99,0xA1,0xAA,0xB2,0xBB,0xC3,
    0xCC,0xD4,0xDD,0xE5,0xEE,0xF6,0x00,0x08,0x11,0x19,
    0x22,0x2A,0x33,0x3B,0x44,0x4C,0x55,0x5D,0x66,0x6E,
    0x77,0x7F,0x88,0x90,0x99,0xA1,0xAA,0xB2,0xBB,0xC3,
    0xCC,0xD4,0xDD,0xE5,0xEE,0xF6,0x00,0x08,0x11,0x19,
    0x22,0x2A,0x33,0x3B,0x44,0x4C,0x55,0x5D,0x66,0x6E,
    0x77,0x7F,0x88,0x90,0x99,0xA1,0xAA,0xB2,0xBB,0xC3,
    0xCC,0xD4,0xDD,0xE5,0xEE,0xF6,0x00,0x08,0x11,0x19,
    0x22,0x2A,0x33,0x3B,0x44,0x4C,0x55,0x5D,0x66,0x6E,
    0x77,0x7F,0x88,0x90,0x99,0xA1,0xAA,0xB2,0xBB,0xC3,
    0xCC,0xD4,0xDD,0xE5,0xEE,0xF6,0x00,0x08,0x11,0x19,
    0x22,0x2A,0x33,0x3B,0x44,0x4C,0x55,0x5D,0x66,0x6E,
    0x77,0x7F,0x88,0x90,0x99,0xA1,0xAA,0xB2,0xBB,0xC3,
    0xCC,0xD4,0xDD,0xE5,0xEE,0xF6,0x00,0x08,0x11,0x19,
    0x22,0x2A,0x33,0x3B,0x44,0x4C,0x55,0x5D,0x66,0x6E,
    0x77,0x7F,0x88,0x90,0x99,0xA1,0xAA,0xB2,0xBB,0xC3,
    0xCC,0xD4,0xDD,0xE5,0xEE,0xF6,0x00,0x08,0x11,0x19,
    0x22,0x2A,0x33,0x3B,0x44,0x4C,0x55,0x5D,0x66,0x6E,
    0x77,0x7F,0x88,0x90,0x99,0xA1,0xAA,0xB2,0xBB,0xC3,
    0xCC,0xD4,0xDD,0xE5,0xEE,0xF6,0x00,0x08,0x11,0x19,
    0x22,0x2A,0x33,0x3B,0x44,0x4C,0x55,0x5D,0x66,0x6E,
    0x77,0x7F,0x88,0x90,0x99,0xA1,0xAA,0xB2,0xBB,0xC3,
    0xCC,0xD4,0xDD,0xE5,0xEE,0xF6,0x00,0x08,0x11,0x19,
    0x22,0x2A,0x33,0x3B,0x44,0x4C,0x55,0x5D,0x66,0x6E,
    0x77,0x7F,0x88,0x90,0x99,0xA1,0xAA,0xB2,0xBB,0xC3,
    0xCC,0xD4,0xDD,0xE5,0xEE,0xF6,0x00,0x08,0x11,0x19,
    0x22,0x2A,0x33,0x3B,0x44,0x4C,0x55,0x5D,0x66,0x6E,
    0x77,0x7F,0x88,0x90,0x99,0xA1,0xAA,0xB2,0xBB,0xC3,
    0xCC,0xD4,0xDD,0xE5,0xEE,0xF6,0x00,0x08,0x11,0x19,
    0x22,0x2A,0x33,0x3B,0x44,0x4C,0x55,0x5D,0x66,0x6E,
    0x77,0x7F,0x88,0x90,0x99,0xA1,0xAA,0xB2,0xBB,0xC3,
    0xCC,0xD4,0xDD,0xE5,0xEE,0xF6,0x00,0x08,0x11,0x19,
    0x22,0x2A,0x33,0x3B,0x44,0x4C,0x55,0x5D,0x66,0x6E,
    0x77,0x7F,0x88,0x90,0x99,0xA1,0xAA,0xB2,0xBB,0xC3,
    0xCC,0xD4,0xDD,0xE5,0xEE,0xF6,0x00,0x08,0x11,0x19,
    0x22,0x2A,0x33,0x3B,0x44,0x4C,0x55,0x5D,0x66,0x6E,
    0x77,0x7F,0x88,0x90,0x99,0xA1,0xAA,0xB2,0xBB,0xC3,
    0xCC,0xD4,0xDD,0xE5,0xEE,0xF6,0x00,0x08,0x11,0x19,
    0x22,0x2A,0x33,0x3B,0x44,0x4C,0x55,0x5D,0x66,0x6E,
    0x77,0x7F,0x88,0x90,0x99,0xA1,0xAA,0xB2,0xBB,0xC3,
    0xCC,0xD4
}

--矩形波
local square_wave = {
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
    0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
    0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,
    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
    0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
}


local can_cmd_id           = 10  --CAN-ID
local instrument_value     = 0   --仪表值
local instrument_countdown = 0   --仪表倒计时
local graph_idx            = 0   --曲线
local graph_idx_offset     = 0   --曲线偏移
local uart_ch              = 1   --串口测试通道



--变更语言显示
function change_langue()
    local tempeMsg = {[0] ="设定温度过高", "Setting temperature is too high！", "設定温度が高すぎる！", "> 30 ���에서 경고 메시지를 표시합니다!"}
    set_string(VT_LW, useraddr.temp_warn, tempeMsg[get_uint16(VT_LW, sysaddr.SysLang)])
end

--上电系统初始化回调函数
function on_init()

    uart_output(0)
    set_string(VT_LW, useraddr.codes_addr , "欢迎使用HMI触摸屏")    --初始化二维码
    set_string(VT_LW, useraddr.driver , "客厅空调")    --初始化文本显示文字
    set_uint16(VT_LW, useraddr.temp_value, 26)    --初始化文本显示数字

    change_langue()

    set_uint16(VT_LW, useraddr.switch, 1)    --初始化开关按键
    set_uint16(VT_LW, useraddr.slide, 1)    --初始化滑动按键

    set_uint16(VT_LW, sysaddr.SysSndVol, 85)    --设置初始喇叭音量
    uart_setup(uart_ch, 19200, 8, 0, 0)    --设置串口参数

    if get_platform() ~= 'WIN32'
    then
        canbus_open(0, 125, 0, 0)    --CAN设置
    end
    uart_output(1)

end

--系统定时回调(一个系统周期调用一次，回调速度受工程复杂程度影响。最快20ms)
function on_run(screen)

    uart_output(0)
    if (screen == page.graph) or (screen == page.record)            --数据记录(表格和曲线)
    then

        graph_idx = ((graph_idx + 1) > #sine_wave) and 1 or (graph_idx + 1)
        set_uint16(VT_LW, useraddr.data, sine_wave[graph_idx])                  --正弦波,通道1
        set_uint16(VT_LW, (useraddr.data + 1), zigzag_wave[graph_idx])          --锯齿波,通道2
        graph_idx_offset = ((graph_idx_offset + 1) > #square_wave) and 1 or (graph_idx_offset + 1)
        set_uint16(VT_LW, (useraddr.data + 2), square_wave[graph_idx_offset])   --矩形波,通道3

        for i = 3, 9
        do
            local val = math.random(-20, 255)
            set_uint16(VT_LW, useraddr.data + i, val)
        end
    end
    uart_output(1)

end

--变量变化回调函数
function on_update(slave,vtype,addr)

    if EN_ON_UPDATE_API_CB == 0 then return end

    if useraddr.master_addr == addr    --仪表
    then
        local Instrumentv = get_uint16(VT_LW, useraddr.master_addr)
        if Instrumentv == 1
        then
            start_timer(0, 10, 1, 0)
        else
            stop_timer(0)
        end

    elseif useraddr.switch_codes == addr    --二维码显示
    then
        local msg_data = {[0] = '', 'https://pay.weixin.qq.com/index.php/core/home/login?return_url=%2F', 'https://auth.alipay.com/login/index.htm?'}
        set_string(VT_LW, useraddr.codes_addr, msg_data[get_uint16(VT_LW, addr)])

    elseif useraddr.time_set == addr    --滚轮切换设置时间
    then
        local year = get_uint16(VT_LW, 0X0160)    --年
        local mon  = get_uint16(VT_LW, 0X0161)    --月
        local day  = get_uint16(VT_LW, 0X0162)    --日
        local hour = get_uint16(VT_LW, 0X0164)    --时
        local min  = get_uint16(VT_LW, 0X0165)    --分
        local sec  = get_uint16(VT_LW, 0X0166)    --秒
        set_date_time (2020 + year, mon + 1, day + 1, hour, min, sec)

    elseif sysaddr.SysLang == addr    --切换语言
    then
        change_langue()

    elseif sysaddr.SysSndState == addr    --退出界面关闭音频
    then
        set_uint16(VT_LW, useraddr.music_id1state, 0)
        set_uint16(VT_LW, useraddr.music_id2state, 0)

    elseif useraddr.music_id1state  == addr    --音频1
    then
        local music1 = get_uint16(VT_LW, addr)
        set_uint16(VT_LW, sysaddr.SysSndPlay, (music1 == 1) and 1 or 0)
        set_uint16(VT_LW, sysaddr.SysSndState, (music1 == 1) and 1 or 3)
        if music1 == 1 then set_uint16(VT_LW, useraddr.music_id2state, 0) end

    elseif useraddr.music_id2state  == addr    --音频2
    then
        local music2 = get_uint16(VT_LW, addr)
        set_uint16(VT_LW, sysaddr.SysSndPlay, (music2 == 2) and 2 or 0)
        set_uint16(VT_LW, sysaddr.SysSndState, (music2 == 2) and 1 or 3)
        if music2 == 2 then set_uint16(VT_LW, useraddr.music_id1state, 0) end
    end
end


--绘图回调函数，每次刷新画面都会触发
function on_draw(screen_id,control_id)
end

--定时器超时回调函数
function on_timer(timer_id)

    uart_output(0)
    if timer_id == 0    --仪表
    then
        if instrument_countdown == 0
        then
            instrument_value = instrument_value + 3
            if instrument_value >= 240
            then
               instrument_value = 240
               instrument_countdown = 1
            end

        else
            instrument_value = instrument_value - 3
            if instrument_value <= 0
            then
               instrument_value = 0
               instrument_countdown = 0
            end
        end

        set_int16(VT_LW, useraddr.speed, instrument_value)    --写入仪表值

    elseif timer_id == 3    --双串口测试发送
    then
        if get_uint16(VT_LW, useraddr.uart2_text) == 0
        then
            start_timer(3, 1000, 0, 1)
            uart_send(uart_ch,{0xAA,0x55,0x00,0x07,0xF1,0x10,0x00,0x00,0x00,0xCC,0xCC})
        end
    end
    uart_output(1)

end

--画面切换回调函数
function on_screen_change(screen)

    uart_output(0)
    if screen == page.record or screen == page.graph    --数据记录
    then
        change_langue()    --更换语言显示
        set_uint16(VT_LW, useraddr.enable_collection, 1)    --资料采集使能

    elseif screen == page.animation
    then
        set_uint16(VT_LW, useraddr.animation, 1)    --动画

    elseif screen == page.flow_block    --流动块
    then
        set_uint16(VT_LW, useraddr.flow_block_speed, 15)    --流动块
    elseif screen == page.test    --测试界面
    then
        stop_timer(3)
        start_timer(3, 1000, 0, 1)    --进入测试界面发送串口测试
    end
    uart_output(1)

end


--USB插入
function on_usb_inserted(driver)

    uart_output(0)
    set_uint16(VT_LW, useraddr.usb_text, 1)    --USB插入
    uart_output(1)

end

--SD卡插入
function on_sd_inserted(dir)

    uart_output(0)
    set_uint16(VT_LW, useraddr.sd_text, 1)    --SD卡插入
    uart_output(1)

end


--串口接收回调
function on_uart_recv(ch,packet)

    uart_output(0)

    if ch == uart_ch
    then
        for i = 1,(#(packet))
        do
            cmd.head = ((cmd.head << 8) | packet[i]) & 0xFFFF

            if cmd.head_tag == 1
            then
                cmd.buff[cmd.datalen] = packet[i]
                cmd.datalen = cmd.datalen + 1
                if cmd.datalen >= 4
                then
                    cmd.len = cmd.buff[3]    --指令长度
                end
            end

            if cmd.head == 0xAA55 and cmd.head_tag == 0
            then
                cmd.datalen = 2
                cmd.buff[0] = 0xAA
                cmd.buff[1] = 0x55
                cmd.head_tag = 1
            end

            if cmd.datalen >= cmd.len + 4    --指令长度判定
            then
                set_uint16(VT_LW, useraddr.uart2_text, 1)
                cmd = {    --初始化，清空串口相关标识，缓冲区
                    head_tag = 0,
                    head     = 0,
                    tail     = 0,
                    buff     = {},
                    len      = 0,
                    datalen  = 0
                }
            end
        end
    end
    uart_output(1)

end


--CAN接收回调
function on_canbus_recv (index,identifier,dlc,rtr,ide,data)

    uart_output(0)
    if can_cmd_id == identifier
    then
        if data[1] == 0xA0
        then
            canbus_write(0, can_cmd_id, 8, 0, 0, {(data[1] + 1),0x55,0x00,0x08,0xF2,0x01,0xCC,0xCC})

        elseif data[1] == 0xA2
        then
            set_uint16(VT_LW, useraddr.can_text, 1)
        end
    end
    uart_output(1)

end


--串口打印信息屏蔽
function uart_output(arg)

    set_notify(arg)    --使能串口通知(0禁止，1使能(默认值))
    EN_ON_UPDATE_API_CB = arg

end
