--下面列出了常用的回调函数
--更多功能请阅读<<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 Languagescreen_id = 18
local Bootanimation_id = 32

local CAN_CMD_ID = 10

local recv3_buffer = {}     --串口三
local cmd_head = 0
local recv_buffer3_count = 0

function on_init()

    if type(canbus_open) == nil then
        set_visiable(0,24,0) 
    end
    
    local cur_lang = get_language()
    if cur_lang == 0         --当前语言为中文
    then
        set_visiable(0,24,0)        --隐藏英文时间显示
        set_value(Languagescreen_id,1,1)
		set_value(Languagescreen_id,2,0)
    elseif cur_lang == 0x81         --当前语言为英文
    then
        set_visiable(0,9,0)        --隐藏中文时间显示
        set_value(Languagescreen_id,2,1)
		set_value(Languagescreen_id,1,0)
    end
    
    set_button_notify_release()
    
    start_timer(4,300,0,0)
	start_timer(0,1000,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(6,1,meter_value)                                               --指针转动
            meter_value = meter_value+4                                              --数值增加
        end
        if meter_flag ==1
        then
            set_value(6,1,meter_value)                                               --指针转动
            meter_value = meter_value-4                                              --数值增加
        end  
    end	
  
    if timer_id==4
    then
        local uart3_buffer= {0x04,0xff,0xfc,0xff,0xff}
        uart3_buffer[0] = 0xee

        uart_send_data3(uart3_buffer)

    elseif timer_id==0 
    then
        set_text(20,15,"")
    end
end




function on_control_notify(screen,control,value)
	if screen == 6 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 == 13 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 == 8 then
		if control == 8 and value == 0 then
			local year,mon,day,hour,min,sec,week = get_date_time()
			set_text(screen,4,hour)
			set_text(screen,5,min)
			set_text(screen,6,sec)
		end
	end
	if screen == 5 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 == 7 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
    
    if screen == 20 --测试CAN
    then
        if control == 13
        then
            local uart3_buffer= {0xAA,0x55,0x00 ,0x08 ,0xF2 ,0x01 ,0x00 ,0x01 ,0x00 ,0x00 ,0xCC ,0xCC}
            canbus_write(0,CAN_CMD_ID,2,0,0,uart3_buffer)
        end
    end
end

function on_video_notify(msg,v1,v2)
	local anim_id = get_current_screen()
	if anim_id == Bootanimation_id and msg == 0 then
    print('video OK-------------------------------------------')
		change_screen(0)
    print('change_screen OK-------------------------------------------')
	end
end

function on_uart_recv_data3(packet)     --串口3

	for i = 0,(#(packet)) do
		cmd_head = packet[i]
		cmd_head = cmd_head&0xff
		
		if cmd_head==0XEE then  
			
			recv3_buffer = {}
			recv_buffer3_count = 1
			
			recv3_buffer[0] = 0xee

		else
			recv3_buffer[recv_buffer3_count] = packet[i]
			recv_buffer3_count = recv_buffer3_count+1
		end

		if recv_buffer3_count >= 6 then                --指令长度判定
			if recv3_buffer[0] == 0xee then
				set_text(20,15,"串口3通信成功")
				stop_timer(0)
				start_timer(0,1000,0,1)
			end
		end
	end
end

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

	if CAN_CMD_ID == identifier then
		if data[1] ~= nil then
			local show_value = (data[1]<<8) + data[2] 
			set_text(20,16,"CAN 通信正常")
			canbus_write(0,CAN_CMD_ID,2,0,0,data)
			print('-------------------CAN OK----------')
		end
	end
end

function on_usb_inserted(driver)    --U盘插入
    set_text(20,12,'U盘插入')
end

function on_usb_removed()           --U盘拔出
    set_text(20,12,'U盘拔出')
end
