--下面列出了常用的回调函数
--更多功能请阅读<<LUA脚本API.pdf>>

--初始化函数
--function on_init()
--end

--定时回调函数，系统每隔1秒钟自动调用。
--function on_systick()
--end

--用户通过触摸修改控件后，执行此回调函数。
--点击按钮控件，修改文本控件、修改滑动条都会触发此事件。
--function on_control_notify(screen,control,value)
--end

--当画面切换时，执行此回调函数，screen为目标画面。
--function on_screen_change(screen)
--end

local progress = 0
local meter_value = 0
local meter_flag = 0

local page = {  --界面页面
    main1          = 0,  --主界面一
    main2          = 1,  --主界面二
    main3          = 2,  --主界面三
    button         = 3,  --按钮
    text           = 4,  --文本
    progress       = 5,  --进度条
    meter          = 6,  --仪表
    slider         = 7,  --滑块
    rtc            = 8,  --时间
    animation      = 9,  --动画
    icon           = 10, --图标
    graph          = 11, --曲线
    menu           = 12, --下拉菜单
    selector       = 13, --滚轮选择
    qrcode         = 14, --二维码
    record         = 15, --数据记录
    history_graph  = 16, --历史曲线
    audio          = 17, --音频
    language       = 18, --多语言
    video          = 20, --视频
    test           = 30, --测试
    boot_animation = 39, --开机视频
}

local control_id = {    --测试界面测试信息控件ID
    can = 16,
    usb = 12,
    sd = 14,
    uart2 = 15,
}

local can_cmd_id = 10   --CAN ID

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

function on_init()

    on_time = get_tick_count() --初始上电时间

    if type(canbus_open) == nil then
        set_visiable(page.main1,101,0) 
    end

    local cur_lang = get_language()
    if cur_lang == 0         --当前语言为中文
    then
        set_visiable(page.main1,101,0)   --隐藏英文时间显示
        set_value(page.language,1,1)
        set_value(page.language,2,0)
    elseif cur_lang == 0x81         --当前语言为英文
    then
        set_visiable(page.main1,100,0)   --隐藏中文时间显示
        set_value(page.language,2,1)
        set_value(page.language,1,0)
    end
    
    set_button_notify_release() --设置按键通知

    start_timer(8,1000,0,0)     --历史曲线
    start_timer(2,5000,0,1)     --虚拟屏定时退出

    uart_set_baudrate3(19200)   --双串口初始化

    canbus_open(0,125,0,0)      --CAN初始化
end

function on_timer(timer_id)  
    if timer_id == 1    --仪表
    then
        if meter_value <= 0     --小于0时，数值加
        then
            meter_flag = 0
        end

        if meter_value >= 260   --大于0时，数值减
        then
            meter_flag = 1
        end

        if meter_flag  == 0
        then
            set_value(page.meter, 1, meter_value)   --指针转动
            set_value(page.meter, 5, meter_value)
            meter_value = meter_value + 4   --数值增加
        end

        if meter_flag == 1
        then
            set_value(page.meter, 1, meter_value)   --指针转动
            set_value(page.meter, 5, meter_value)
            meter_value = meter_value - 4   --数值增加
        end
    end

    if timer_id == 2
    then
        change_screen(0)
    end

    if timer_id == 8 then       --历史曲线
        local Curve_value = math.random(1,254)
        set_history_graph_value(page.history_graph, 1, Curve_value)
    end

    if timer_id == 4    --串口2发送
    then
        local uart2_buffer = {0x00,0x12,0x34,0xFF,0xFC,0xFF,0xFF}
        uart2_buffer[0] = 0xEE
        uart_send_data2(uart2_buffer)
    end
end


function on_audio_callback (state)  --音频结束回调
    set_value(page.audio, 3, 0)
    set_value(page.audio, 4, 0)
end

function on_control_notify(screen,control,value)

    if screen == page.meter    --仪表
    then
        if control == 4 and value == 1
        then
            start_timer(1,1,0,0)
        end

        if control == 4 and value == 0
        then
            stop_timer(1)
        end
    end

    if screen == page.selector     --滚轮
    then
        if control == 1
        then
            if value >= 0 and value <= 6
            then
                set_value(screen,3,0)
            end

            if value > 6 and value <= 12
            then
                set_value(screen,3,1)
            end

            if value > 12 and value <= 18
            then
                set_value(screen,3,2)
            end

            if value > 18 and value <= 21
            then
                set_value(screen,3,3)
            end

            if value > 21 and value <= 23
            then
                set_value(screen,3,4)
            end
        end
    end

    if screen == page.rtc   --时间
    then
        if control == 8 and value == 0
        then
            local year,mon,day,hour,min,sec,week = get_date_time()
            if week == 0 then week = "日" end
            if week == 1 then week = "一" end
            if week == 2 then week = "二" end
            if week == 3 then week = "三" end
            if week == 4 then week = "四" end
            if week == 5 then week = "五" end
            if week == 6 then week = "六" end
            set_text(screen,1,year)
            set_text(screen,2,mon)
            set_text(screen,3,day)
            set_text(screen,4,hour)
            set_text(screen,5,min)
            set_text(screen,6,sec)
            set_text(screen,7,week)
        end
    end

    if screen == page.progress  --进度条
    then
        if control == 3
        then
            if value == 0 or value == 2
            then
                progress = progress - 1
                if progress <= 0
                then
                    progress = 0
                end
                set_value(screen, 1, progress)
                set_value(screen, 2, progress)
            end

        elseif control == 1
        then
            set_value(screen, 2, value)
            progress = value

        elseif control == 4
        then
            if value == 0 or value == 2
            then
                progress = progress + 1
                if progress >= 100
                then
                    progress = 100
                end

                set_value(screen, 1, progress)
                set_value(screen, 2, progress)
            end
        end
    end

    if screen == page.slider    --滑块
    then
        if control == 2
        then
            set_value(screen, 3, value)
            set_backlight(value)
        end

        if control == 5
        then
            set_value(screen, 6, value)
        end
    end
end


--切换页面
function on_screen_change(screen)
    if screen == page.test then --进入测试界面
        stop_timer(4)
        start_timer(4,1000,0,0)
    else
        stop_timer(4)
    end
end


function on_video_notify(msg,v1,v2)

    local anim_id = get_current_screen()
    if anim_id == page.boot_animation and msg == 0  --开机动画界面
    then
        stop_timer(2)
        change_screen(0)
    end
end


function on_uart_recv_data2(packet)     --串口2

    for i = 0,(#(packet))
    do
        cmd.head = (cmd.head | packet[i]) & 0xFF

        if cmd.head == 0xEE and cmd.head_tag == 0 then
            cmd.buff = {}
            cmd.datalen = 1
            cmd.buff[0] = 0xEE
            cmd.head_tag = 1
        end

        if cmd.head_tag == 1 then
            cmd.buff[cmd.datalen] = packet[i]
            cmd.datalen = cmd.datalen + 1
        end

        if cmd.tail ~= 0xFFFCFFFF and cmd.head_tag == 1 then
            cmd.tail = ((cmd.tail << 8) & 0xFFFFFFFF) | packet[i]
        end

        if cmd.datalen >= 6 and cmd.tail == 0xFFFCFFFF then    --指令长度判定
            set_text(peage.test, control_id.uart2, "串口2通信:成功")
            cmd = {    --初始化，清空串口相关标识，缓冲区
                head_tag = 0,
                head     = 0,
                tail     = 0,
                buff     = {},
                len      = 0,
                datalen  = 0
            }

            stop_timer(4)
        end
    end
end

function on_canbus_recv (index,identifier,dlc,rtr,ide,data)     --CAN通讯

    if can_cmd_id == identifier then
        if data[0] == 0xA0 then
            canbus_write(0, can_cmd_id, 8, 0, 0, {[0] = (data[1] + 1),0x55,0x00,0x08,0xF2,0x01,0xCC,0xCC})
        elseif data[0] == 0xA2 then
            set_text(page.test, control_id.can, "CAN通信:正常")
        end
    end
end


function on_usb_inserted(driver)    --U盘插入
    set_text(page.test, control_id.usb, 'U盘检测：成功')
end


function on_sd_inserted(dir)    --SD卡插入
    set_text(page.test, control_id.sd, 'SD卡检测：成功')
end
