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

--uart_free_protocol = 1

--[[
--使用PC虚拟串口屏调试4G模块时使用
--屏幕需要升级工程 UART3_TO_UART1
--使用虚拟屏与实体屏联调时，设置air_debug_mode = 1
--使用实体屏允许时，设置air_debug_mode = 0
--]]
local zgb_debug_mode = 0

--uart_free_protocol = zgb_debug_mode      --自由串口协议


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 Bootanimation_id = 41
screen_zgbsh = 23
screen_zgbtest = 24
screen_zgbrec = 25

--[[***************************************************************************
** Function name:  my_log
** Descriptions:   打印虚拟屏调试信息，设置 en_debug_msg = 1
                   注意：实体屏运行时， en_debug_msg 不能等于 1
***************************************************************************--]]
local en_debug_msg = 1 --0-不显示调试信息;1-虚拟屏; 2-实体屏，串口1输出
function my_log(debug_msg)
    if en_debug_msg == 1
    then
        print(debug_msg)
    elseif en_debug_msg == 2
    then
        uart_send_string(debug_msg)
        uart_send_string('\r\n')
    end
end

function on_init()
    
    --wifi
    dofile('esp32at.lua')                            --加载文件
    dofile('esp32_main.lua')                         --加载文件
    uart_set_baudrate3(115200)                       --设置与4G模块通讯的串口3的波特率为115200
    
    dofile('E180_ZG120B.lua')                                                   --加载  文件
    dofile('e180_uart_data.lua')
    dofile('expack1.lua')
    uart_set_baudrate2(115200)                                                  --设置与4G模块通讯的串口3的波特率为115200
    
    esp32_init()
    
    e180.set_callback(on_e180_send_cb,on_e180_config_callback,on_e180_log_cb)     --设置4G库函数的命令发送函数，命令回调函数、调试信息打印函数
    eud.on_init()
    eud.init_end_device()
    
	set_button_notify_release()
	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
end

--[[***************************************************************************
** Function name:  on_zgb_send_cb
** Descriptions:   发送数据
***************************************************************************--]]
function on_e180_send_cb(data)
    if zgb_debug_mode==1  
    then
        uart_send_data(data)                       -- air_debug_mode==1 ，使用虚拟屏调试，通过串口1发送数据
        --my_zgb_uart_log(data)
        on_e180_log_cb('OUT:'..e180.table2str16(data))
    else
        uart_send_data2(data)                      -- air_debug_mode==0 ，使用实体屏调试，通过串口3发送数据
        
        --my_zgb_uart_log('out: '..data..'\r\n')
        on_e180_log_cb('OUT:'..e180.table2str16(data))
    end
end

test_re_en = 1    --测试，数据记录开关
function on_e180_log_cb(msg)
    if test_re_en == 1
    then
        --数据打印到记录控件
        record_add(screen_zgbrec,9,string.sub(msg,1,100))
    end
end

function on_e180_config_callback(key,value)
end

function on_timer(timer_id)
    on_esp32_timer(timer_id)
    e180.on_timer(timer_id)                                  --4G库函数的定时处理
    eud.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	
end

function on_control_notify(screen,control,value)
    on_esp32_control_notify(screen,control,value)
    eud.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
end

function on_video_notify(msg,v1,v2)
	local anim_id = get_current_screen()
    on_esp32_video_notify(msg,v1,v2)
	if anim_id == Bootanimation_id and msg == 0 then
		change_screen(0)
	end
end

function on_audio_callback(state)
	set_value(Audioscreen_id,3,0)
	set_value(Audioscreen_id,4,0)
    on_esp32_audio_callback(state)
end
--[[function on_systick()
	local Curve_value = math.random(1,254)
	print("debug:Curve_value =" ..Curve_value)
	set_history_graph_value(Historicalcurve_screenid,1,Curve_value)
end--]]

function on_draw(screen)
    on_esp32_draw(screen)
end

--[[***************************************************************************
** Function name:  on_uart_recv_data2
** Descriptions:   接收串口2的数据的回调函数。串口2是连接ZIGBEE模块。
***************************************************************************--]]
function on_uart_recv_data2(packet)
    on_e180_log_cb('mainin:'..e180.table2str16(packet))
    my_log('mainin:'..e180.table2str16(packet))
    if zgb_debug_mode == 0
    then
        e180.response( packet )
    end
end

function on_print_hook(str)
    on_e180_log_cb(str)
end


