--下面列出了常用的回调函数
--更多功能请阅读<<LUA脚本API.pdf>>
--uart_free_protocol = 1
--初始化函数
--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 Progress_screenid = 5
local Meter_screenid = 6
local Slider_screendid = 7
local RTC_screenid = 8
local Seltor_screenid = 13
local Historicalcurve_screenid = 16
local Audioscreen_id = 17
local Languagescreen_id = 18
local VideoScreen_Id = 20
local Bootanimation_id = 39
local Expansion_id = 40

local IO0_PE5 = 0x0405
local IO1_PE9 = 0x0409
local IO2_PE10 = 0x040A
local IO3_PD21 = 0x0315
local POWER_EN_PD20 = 0x0314

local IO_test_mode = 0      --0：无，1：输出，2：输入
local timerid_IO_test = 11
local timer_IO_test_bit = 0
local timerid_ADC_test = 12

local pwm_freq = 100
local pwm_duty = 0

-- ADC VALUE 
-- 1
-- 8
-- 14
-- 20
-- 26
-- 32
-- 39
-- 45
-- 51
-- 57


local timerid_UART_test = 13
local timerid_UART_test_timeout = 14
local UART_test_data = {0x05,0xA1,0xB2,0xC3,0xD4,0xF5}
UART_test_data[0] = 0x5A

local cmd_head     = 0x5A           --帧头
local buff         = {}             --缓冲区
local cmd_length   = 0              --帧长度
local data_length  = 0
local cmd_head_tag = 0	

local CatTime = 7

function on_init()
	set_button_notify_release()
	uart_set_baudrate3(115200)                 --串口3
    
	start_timer(0,1000,0,0)
	local language_id = get_language()
	if language_id == 0 then
		set_value(Languagescreen_id,1,1)
		set_value(Languagescreen_id,2,0)
	end
	if language_id == 1 then
		set_value(Languagescreen_id,2,1)
		set_value(Languagescreen_id,1,0)
	end
	--start_timer(2,6000,0,1)
	gpio_set_out (POWER_EN_PD20)
	gpio_set_value(POWER_EN_PD20,1)
	start_timer(timerid_ADC_test,200,0,0)
    
    set_enable(Expansion_id,8,0)
    set_enable(Expansion_id,9,0)
    set_enable(Expansion_id,10,0)
    set_enable(Expansion_id,11,0)
end

function on_timer(timer_id)
	if timer_id==1
	then
		if meter_value <=0                                                       --小于0时，数值加
		then
			meter_flag = 0
		end
		if meter_value >=270                                                     --大于0时，数值减
		then
			meter_flag = 1
		end
		if meter_flag  ==0
		then
		set_value(Meter_screenid,1,meter_value)
		set_value(Meter_screenid,5,meter_value)                                               --指针转动
		meter_value = meter_value+2                                              --数值增加
		end
		if meter_flag ==1
		then
		set_value(Meter_screenid,1,meter_value)
		set_value(Meter_screenid,5,meter_value)                                               --指针转动
		meter_value = meter_value-2                                              --数值增加
		end  
	end
	if timer_id == 0 
	then
        local Curve_value = math.random(1,254)
        set_history_graph_value(Historicalcurve_screenid,1,Curve_value)
	end
--[[	if timer_id == 2 then
		if get_current_screen() == Bootanimation_id then
			CatTime = CatTime - 1
			set_value(Bootanimation_id,3,CatTime)
			if CatTime == 0 then
				change_screen(0)
			end
		end	
	end--]]
-- ADC VALUE 
-- 1
-- 8
-- 14
-- 20
-- 26
-- 32
-- 39
-- 45
-- 51
-- 57
	if timer_id == timerid_ADC_test 
	then
		local status,data = get_key_adc_data()
		set_text(Expansion_id,3,data)
        --set_text(Expansion_id,3,status..';'..data)
		
		if status == 1
		then
			if data>=0 and data<5
			then
				set_text(Expansion_id,4,'1')
			elseif data>=5 and data <11
			then
				set_text(Expansion_id,4,'2')
			elseif data>=11 and data <17
			then
				set_text(Expansion_id,4,'3')
			elseif data>=17 and data <23
			then
				set_text(Expansion_id,4,'4')
			elseif data>=23 and data <29
			then
				set_text(Expansion_id,4,'5')
			elseif data>=29 and data <35
			then
				set_text(Expansion_id,4,'6')
			elseif data>=35 and data <42
			then
				set_text(Expansion_id,4,'7')
			elseif data>=42 and data <48
			then
				set_text(Expansion_id,4,'8')
			elseif data>=48 and data <54
			then
				set_text(Expansion_id,4,'9')
			elseif data>=54 and data <60
			then
				set_text(Expansion_id,4,'10')
			end
            set_text(Expansion_id,5,'按下')
		elseif status == 17
		then
            set_text(Expansion_id,5,'弹起')
		end
	end
	
	if timer_id == timerid_IO_test 
	then
		
		--输入
		if IO_test_mode == 2
		then
			local IO0_num = gpio_get_value (IO0_PE5)
			if IO0_num == 0
			then
                set_value(Expansion_id,8,0)
                set_value(Expansion_id,9,1)
                set_value(Expansion_id,10,1)
                set_value(Expansion_id,11,1)
				
			end
			local IO1_num = gpio_get_value (IO1_PE9)
			if IO1_num == 0
			then
                set_value(Expansion_id,8,1)
                set_value(Expansion_id,9,0)
                set_value(Expansion_id,10,1)
                set_value(Expansion_id,11,1)
				
			end
			local IO2_num = gpio_get_value (IO2_PE10)
			if IO2_num == 0
			then
                set_value(Expansion_id,8,1)
                set_value(Expansion_id,9,1)
                set_value(Expansion_id,10,0)
                set_value(Expansion_id,11,1)
				
			end
			local IO3_num = gpio_get_value (IO3_PD21)
			if IO3_num == 0
			then
                set_value(Expansion_id,8,1)
                set_value(Expansion_id,9,1)
                set_value(Expansion_id,10,1)
                set_value(Expansion_id,11,0)
				
			end
			if IO0_num == 1 and IO1_num == 1 and IO2_num == 1 and IO3_num == 1
			then
				set_value(Expansion_id,8,1)
                set_value(Expansion_id,9,1)
                set_value(Expansion_id,10,1)
                set_value(Expansion_id,11,1)
			end
			
		end
		
	end
	
end

function on_control_notify(screen,control,value)
	
	if screen == Meter_screenid then
		if control == 4 and value == 1 then
			start_timer(1,15,0,0)
		end
		if control == 4 and value == 0 then
			stop_timer(1)
		end
	end
	
	if screen == Seltor_screenid 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 == RTC_screenid 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 == Progress_screenid 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 == Slider_screendid 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 == Expansion_id 
	then
        --IO OUT
		if control == 6 and value == 1
		then
            stop_timer(timerid_IO_test)
			gpio_set_out (IO0_PE5)
			gpio_set_out (IO1_PE9)
			gpio_set_out (IO2_PE10)
			gpio_set_out (IO3_PD21)
            gpio_set_value ( IO0_PE5, 1)
            gpio_set_value ( IO1_PE9, 1)
            gpio_set_value ( IO2_PE10, 1)
            gpio_set_value ( IO3_PD21, 1)
            
			set_value(Expansion_id,7,0)
            set_value(Expansion_id,8,1)
            set_value(Expansion_id,9,1)
            set_value(Expansion_id,10,1)
            set_value(Expansion_id,11,1)
            
            set_enable(Expansion_id,8,1)
            set_enable(Expansion_id,9,1)
            set_enable(Expansion_id,10,1)
            set_enable(Expansion_id,11,1)

            
			IO_test_mode = 1                    --模式1，用于在定时器中判断
        elseif control == 6 and value == 0
		then
            IO_test_mode = 0
            
        elseif IO_test_mode == 1 and control>=8 and control<=11
        then
            --按钮控制Io输出状态
            if control == 8
            then
                gpio_set_value ( IO0_PE5, value)
            elseif control == 9
            then
                gpio_set_value ( IO1_PE9, value)
            elseif control == 10
            then
                gpio_set_value ( IO2_PE10, value)
            elseif control == 11
            then
                gpio_set_value ( IO3_PD21, value)
            end
            
            
        --IO IN
		elseif control == 7 and value == 1
		then
            
			gpio_set_in (IO0_PE5)
			gpio_set_in (IO1_PE9)
			gpio_set_in (IO2_PE10)
			gpio_set_in (IO3_PD21)
            
			set_value(Expansion_id,6,0)
            set_value(Expansion_id,8,1)
            set_value(Expansion_id,9,1)
            set_value(Expansion_id,10,1)
            set_value(Expansion_id,11,1)
            
            set_enable(Expansion_id,8,0)
            set_enable(Expansion_id,9,0)
            set_enable(Expansion_id,10,0)
            set_enable(Expansion_id,11,0)
            
			IO_test_mode = 2
			stop_timer(timerid_IO_test)
			start_timer(timerid_IO_test,200,0,0)
        elseif control == 7 and value == 0
		then
			stop_timer(timerid_IO_test)
            IO_test_mode = 0
            
        --PWM
        elseif control == 13
		then
			pwm_freq = value
			set_pwm(pwm_freq,pwm_duty)                  --设置PWM输出参数
            set_text(Expansion_id,14,math.ceil(value))
        elseif control == 14
		then
			pwm_freq = value
			set_pwm(pwm_freq,pwm_duty)                  --设置PWM输出参数
            set_value( Expansion_id, 12, pwm_freq )
            set_value( Expansion_id, 13, pwm_freq )
		elseif control == 16
		then
			pwm_duty = value
			set_pwm(pwm_freq,pwm_duty)                  --设置PWM输出参数
            set_text(Expansion_id,17,math.ceil(value))
        elseif control == 17
		then
			pwm_duty = value
			set_pwm(pwm_freq,pwm_duty)                  --设置PWM输出参数
            set_value( Expansion_id, 15, pwm_duty )
            set_value( Expansion_id, 16, pwm_duty )
            
        --power
		elseif control == 2 
		then
			if value == 1
			then
				gpio_set_value(POWER_EN_PD20,0)     --关闭电源
			elseif value == 0
			then
				gpio_set_value(POWER_EN_PD20,1)     --打开电源
			end
            
            
        --uart
		elseif control == 20 and value == 0
		then
            
            local us_string = get_text( Expansion_id,18 )
            local us_data={}
            us_data[0] = 0x5A                               --帧头
            us_data[1] = #us_string                         --获取文本长度
            
            for i=0,#us_string-1
            do
                us_data[i+2] = string.byte(us_string,i+1)   --把文本框的内容写入数组
            end
            
            uart_send_data3(us_data)                        --发送数组
		end
	end
	
end

function on_video_notify(msg,v1,v2)
--[[	if msg == 0 then
		change_screen(0)
	end--]]
	local anim_id = get_current_screen()
	if anim_id == Bootanimation_id and msg == 0 then
		change_screen(0)
	end
	--[[for i = 3, 5 do
		set_visiable(Bootanimation_id,i,0)
	end--]]
end

function on_audio_callback(state)
	set_value(Audioscreen_id,3,0)
	set_value(Audioscreen_id,4,0)
end

function on_screen_change(screen)
--[[	if screen == Bootanimation_id then
		start_timer(2,1000,0,4)
		set_value(screen,3,CatTime)
	end--]]
	if screen == VideoScreen_Id then
		set_value(VideoScreen_Id,2,1)
		set_value(VideoScreen_Id,3,0)
		set_value(VideoScreen_Id,4,0)
	end
	
end

--Instruction analysis
--@msg:data table
function my_processmessage(msg)
    local recv_string = ''
	for i=2,#msg
	do
        recv_string = recv_string..string.char( msg[i] )
		set_text( Expansion_id, 19, recv_string )
	end
	
end

-- 系统函数: 串口接收函数
function on_uart_recv_data3(packet)

	local recv_packet_size = (#(packet))
	local check16          = 0
	
	for i = 0, recv_packet_size 
	do
		if packet[i] == cmd_head and cmd_head_tag == 0
		then
			cmd_head_tag = 1
		end
		
		if cmd_head_tag == 1
		then
			buff[cmd_length] = packet[i]
			cmd_length = cmd_length + 1
			
			if cmd_length == 2
			then 
				data_length = packet[i]
			end
			
			if (data_length + 2) <= cmd_length and  cmd_length >= 2
			then
				my_processmessage(buff)

				buff         = {}
				cmd_length   = 0
				data_length  = 0
				cmd_head_tag = 0
			end
		end
	end
end


