------------------------------------
-- 提示
-- 如果使用其他Lua编辑工具编辑此文档，请将VisualTFT软件中打开的此文件编辑器视图关闭，
-- 因为VisualTFT具有自动保存功能，其他软件修改时不能同步到VisualTFT编辑视图，
-- VisualTFT定时保存时，其他软件修改的内容将被恢复。
--
-- Attention
-- If you use other Lua Editor to edit this file, please close the file editor view 
-- opened in the VisualTFT, Because VisualTFT has an automatic save function, 
-- other Lua Editor cannot be synchronized to the VisualTFT edit view when it is modified.
-- When VisualTFT saves regularly, the content modified by other Lua Editor will be restored.
------------------------------------

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


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

--定时器超时回调函数，当设置的定时器超时时，执行此回调函数，timer_id为对应的定时器ID
--function on_timer(timer_id)
--end

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

--当画面切换时，执行此回调函数，screen为目标画面。
--function on_page.change(screen)
--end
local page = {
    main1 = 0,              --主界面一
    main2 = 1,              --主界面二
    main3 = 2,              --主界面三
    main4 = 3,              --主界面四
    rtc1 = 4,               --日期
    rtc2 = 5,               --时间
    video = 7,              --按钮
    progress = 8,           --进度条
    meter = 11,             --仪表
    selector = 12,          --选择控件
    animation = 13,         --动画
    language = 14,          --多语言
    slider = 15,            --滑块
    setting = 16,           --设置
    standby1 = 17,          --待机时钟1
    standby2 = 18,          --待机时钟2
    boot_animation = 19,    --开机动画
    theme_setting = 20,     --主题选择
    test = 28,              --测试
}


--定时器
local meter_timer = 0   --仪表
local touch_timer = 1   --时间
local user2_timer = 4   --双串口


local can_cmd_id = 10   --CAN ID

local standby_time = 20000

local meter_value = 0
local theme_flag = 17
local touch_flag = 0


local theme_flag_addr = 0x2000

local find_cnt = 0

run_roal_flag = 0
run_roal_state = 0
runtime = 0


function on_init()
    set_button_notify_release()         --设置按键通知
    set_value(page.slider,2,get_backlight())    --设置亮度
    set_value(page.slider,5,get_backlight())    --设置湿度
    if get_language() == 0 then         --当前语言为中文
        set_value(page.language,1,1)
        set_value(page.language,2,0)
    elseif get_language() == 0x81 then  --当前语言为英文
        set_value(page.language,1,0)
        set_value(page.language,2,1)
    end

    local theme_flag_str = read_flash_string(theme_flag_addr)   --读flash(切换待机时钟)
    if theme_flag_str ~= nil then
        theme_flag = tonumber(theme_flag_str)   --字符转数字
    elseif theme_flag_str == nil then
        theme_flag_str = tostring(theme_flag)   --数字转字符
        write_flash_string(theme_flag_addr,theme_flag_str)
    end

    start_timer(touch_timer,standby_time,0,1)   --20s屏幕无触摸进入待机界面
    
    uart_set_baudrate2(19200)    --双串口初始化
    canbus_open(0,125,0,0)      --CAN初始化
end


--定时回调函数，系统每隔1秒钟自动调用。
function on_systick()
    if run_roal_flag == 0 then
        run_roal_state = (run_roal_state + 1) % 2
        set_visiable(page.main1,9,run_roal_state)
    end
end


function on_control_notify(screen,control,value)
    stop_timer(touch_timer)
    start_timer(touch_timer,standby_time,0,1)
    if screen == page.main1 and control == 1 then
        change_screen(theme_flag)
    end
    if screen == page.rtc2 and control == 2 then
        local year,mon,day,hour,min,sec
        year = get_value(page.rtc1,1)
        mon = get_value(page.rtc1,2)+1
        day = get_value(page.rtc1,3)+1
        hour = get_value(page.rtc2,1)
        min = get_value(page.rtc2,3)
        sec = get_value(page.rtc2,4)
        print("mon = "..mon.."day = "..day)
        set_date_time(year,mon,day,hour,min,sec)
    end
    if screen == page.progress and control == 1 then
        meter_value = math.floor(value)%65536
        if meter_value >= 128 and meter_value <= 360 then
            meter_value = meter_value - 128
        elseif meter_value >=0 and meter_value <= 45 then
            meter_value = meter_value + 232
        end
        set_value(page.progress,3,meter_value)
    end
    if screen == page.meter then
        if control == 3 then
            stop_timer(meter_timer)
        end
    end
    if screen == page.selector then
        if control == 1 then
            set_value(page.selector,4,value)
        elseif control == 3 then
            set_value(page.selector,5,value)
        end
    end
    if screen == page.slider then
        if control == 2 then
            set_value(screen,5,value)
            set_backlight(value)
        end
        if control == 4 then
            set_value(screen,6,value)
        end
    end
    if screen == page.setting then
        if control == 8 then
            local year,mon,day,hour,min,sec,week = get_date_time()
            --print("mon = "..mon.."day = "..day)
            set_value(page.rtc1,1,year)
            set_value(page.rtc1,2,mon-1)
            set_value(page.rtc1,3,day-1)
            set_value(page.rtc2,1,hour)
            set_value(page.rtc2,3,min)
            set_value(page.rtc2,4,sec)
        end
    end
    if screen == page.standby1 and control == 4 then
        set_backlight(255)
    end
    if screen == page.standby2 and control == 5 then
        set_backlight(255)
    end
    if screen == page.theme_setting then
        if control == 1 or control == 2  then
            theme_flag = control + 16
            local theme_flag_str = tostring(theme_flag)
            write_flash_string(theme_flag_addr,theme_flag_str)
        end
    end
    if screen == page.boot_animation then
        if control == 1   then
            change_screen(page.main1)
        end
    end
end


local meter_flag = 0
function on_timer(timer_id)
    if timer_id == meter_timer then     --仪表
        if meter_value <= 0 then
            meter_flag = 0
        elseif meter_value >= 269 then
            meter_flag = 1
        end
        if meter_flag == 0 then
            meter_value = meter_value + 5
        elseif meter_flag == 1 then
            meter_value = meter_value - 5
        end
        set_value(page.meter,1,meter_value)
        set_value(page.meter,2,meter_value)
    end

    if timer_id == touch_timer then     --时间
        change_screen(theme_flag)
        set_backlight(20)
        stop_timer(meter_timer)
    end
    
    if timer_id == user2_timer then    --串口2发送
       local uart2_buffer = {0x00,0x12,0x34,0xFF,0xFC,0xFF,0xFF}
       uart2_buffer[0] = 0xEE
       uart_send_data2(uart2_buffer)
    end
end


function on_screen_change(screen)
    if screen == page.meter then    --仪表
        start_timer(meter_timer,1,0,0)
    end

    if screen == page.standby1 then --待机时钟一
        play_animation(screen,5)
    end

    if screen == page.standby2 then --待机时钟二
        play_animation(screen,1)
    end

    if screen == page.video then    --视频
        set_value(page.video,2,1)
    end

    if screen == page.test then     --进入测试界面
        start_timer(user2_timer,1000,0,0)
    else
        stop_timer(user2_timer)
    end
end


function on_press(state,x,y)
    if get_current_screen() == page.main1 then
        set_visiable(page.main1,9,0)
        run_roal_flag = 1
    end

    if state == 0 then  --定时待机时钟
        stop_timer(touch_timer)
        start_timer(touch_timer,standby_time,0,1)
    end
end


function on_usb_inserted(driver)
    set_text(page.test, 20, 'SD卡检测：正常')
end


function on_sd_inserted(dir)
    set_text(page.test, 21, 'U盘检测：正常')
end




function on_video_notify(msg,v1,v2)
    if get_current_screen() == page.boot_animation then
        if msg == 0 then
            change_screen(page.main1)
        end
    end
end

function on_uart_recv_data2(packet)     --串口2

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

        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 and cmd.tail ~= 0xFFFCFFFF then
            cmd.buff[cmd.datalen] = packet[i]
            cmd.datalen = cmd.datalen + 1
            cmd.tail = ((cmd.tail << 8) & 0xFFFFFFFF) | packet[i]
        end
    end

    if cmd.datalen >= 6 and cmd.tail == 0xFFFCFFFF then    --指令长度判定
        set_text(page.test, control_id.uart2, "串口2通信:成功")
        stop_timer(user2_timer)
    else
        print('uart2 test error size')
    end

    cmd = {    --初始化，清空串口相关标识，缓冲区
        head_tag = 0,
        head     = 0,
        tail     = 0,
        buff     = {},
        len      = 0,
        datalen  = 0
    }
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, 23, "CAN通信:正常")
        end
    end
end