_ENCRYPT_=0    --LUA脚本加密

--数据类型定义
VT_LW = 1    --变量地址
VT_RW = 2    --FLASH存储


local useraddr ={                    --用户地址，对应工程设定的地址表
driver         = 0x1003,             --设备名称
temp_warn      = 0x1150,             --文字滚动
temp_value     = 0x100C,             --温度
time_set       = 0x101B,             --时间确认
master_addr    = 0x1028,             --仪表值
textback_addr  = 0x1020,             --文本背景色
channel_minus  = 0x1102,             --通道翻页-
channel_add    = 0x1103,             --通道翻页+
channel_no1tx  = 0x1106,             --通道号1
channel_no2tx  = 0x1110,             --通道号2
channel_no3tx  = 0x111A,             --通道号3
music_id1state = 0x1021,             --音频1
music_id2state = 0x1026,             --音频2
music_progress = 0x1027,             --音频进度
beep           = 0x120E,             --蜂鸣器
switch_codes   = 0x2000,             --二维码切换
codes_addr     = 0x2001,             --告警显示
button_addr    = 0x1210,
videos_addr    = 0x1211,
error_addr     = 0x3001,
}
 
local sysaddr ={                     --系统参数地址，对应工程系统地址表
screen_idaddr     = 0x0100,          --画面ID
music_sndplay     = 0x0141,          --音频播放ID
music_sndstate    = 0x0142,          --音频播放控制
music_playTime    = 0x0143,          --音频已播放时间
music_TotalTime   = 0x0144,          --音频总时间
SysLang           = 0x0119,          --系统语言
Sysbidtime        = 0x0153,          --工程编译时间   
}

local cur_screen ={                 --画面ID
No0  = 0,
No1  = 1,
No2  = 2,
No3  = 3,
No10 = 10,
No12 = 12,
No15  = 15,
No16  = 16,
No21  = 21,
No23  = 23,
No35  = 35,

}

local LED1 = 0x0314
local LED2 = 0x0315


--正弦波
local sine_wave = {0x21,0x1D,0x19,0x16,0x13,0x0F,0x0D,0x0A,0x08,0x06,0x04,0x03,0x02,0x01,0x01,0x01,0x01,0x01,0x02,0x03,0x04,0x06,0x08,0x0A,0x0D,0x0F,0x13,0x16,0x19,0x1D,0x21,0x26,0x2A,0x2F,0x34,0x39,0x3E,0x44,0x49,0x4F,0x55,0x5B,0x61,0x67,0x6D,0x73,0x79,0x7F,0x86,0x8C,0x92,0x98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98,0x92,0x8C,0x86,0x80,0x79,0x73,0x6D,0x67,0x61,0x5B,0x55,0x4F,0x49,0x44,0x3E,0x39,0x34,0x2F,0x2A,0x26}
--锯齿波
local zigzag_vave = {0xCC,0xD4,0xDD,0xE5,0xEE,0xF6,0x00,0x08,0x11,0x19,0x22,0x2A,0x33,0x3B,0x44,0x4C,0x55,0x5D,0x66,0x6E,0x77,0x7F,0x88,0x90,0x99,0xA1,0xAA,0xB2,0xBB,0xC3,0xCC,0xD4,0xDD,0xE5,0xEE,0xF6,0x00,0x08,0x11,0x19,0x22,0x2A,0x33,0x3B,0x44,0x4C,0x55,0x5D,0x66,0x6E,0x77,0x7F,0x88,0x90,0x99,0xA1,0xAA,0xB2,0xBB,0xC3,0xCC,0xD4,0xDD,0xE5,0xEE,0xF6,0x00,0x08,0x11,0x19,0x22,0x2A,0x33,0x3B,0x44,0x4C,0x55,0x5D,0x66,0x6E,0x77,0x7F,0x88,0x90,0x99,0xA1,0xAA,0xB2,0xBB,0xC3,0xCC,0xD4,0xDD,0xE5,0xEE,0xF6,0x00,0x08,0x11,0x19,0x22,0x2A,0x33,0x3B,0x44,0x4C,0x55,0x5D,0x66,0x6E,0x77,0x7F,0x88,0x90,0x99,0xA1,0xAA,0xB2,0xBB,0xC3,0xCC,0xD4,0xDD,0xE5,0xEE,0xF6,0x00,0x08,0x11,0x19,0x22,0x2A,0x33,0x3B,0x44,0x4C,0x55,0x5D,0x66,0x6E,0x77,0x7F,0x88,0x90,0x99,0xA1,0xAA,0xB2,0xBB,0xC3,0xCC,0xD4,0xDD,0xE5,0xEE,0xF6,0x00,0x08,0x11,0x19,0x22,0x2A,0x33,0x3B,0x44,0x4C,0x55,0x5D,0x66,0x6E,0x77,0x7F,0x88,0x90,0x99,0xA1,0xAA,0xB2,0xBB,0xC3,0xCC,0xD4,0xDD,0xE5,0xEE,0xF6,0x00,0x08,0x11,0x19,0x22,0x2A,0x33,0x3B,0x44,0x4C,0x55,0x5D,0x66,0x6E,0x77,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E,0xF6,0x00,0x08,0x11,0x19,0x22,0x2A,0x33,0x3B,0x44,0x4C,0x55,0x5D,0x66,0x6E,0x77,0x7F,0x88,0x90,0x99,0xA1,0xAA,0xB2,0xBB,0xC3,0xCC,0xD4,0xDD,0xE5,0xEE,0xF6,0x00,0x08,0x11,0x19,0x22,0x2A,0x33,0x3B,0x44,0x4C,0x55,0x5D,0x66,0x6E,0x77,0x7F,0x88,0x90,0x99,0xA1,0xAA,0xB2,0xBB,0xC3,0xCC,0xD4,0xDD,0xE5,0xEE,0xF6,0x00,0x08,0x11,0x19,0x22,0x2A,0x33,0x3B,0x44,0x4C,0x55,0x5D,0x66,0x6E,0x77,0x7F,0x88,0x90,0x99,0xA1,0xAA,0xB2,0xBB,0xC3,0xCC,0xD4}
--矩形波
local square_wave = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}



local current_screen = 0          --当前画面ID
local master_value = 0            --仪表值
local master_countdown = 0        --仪表倒计时
local init_flag = 0               --初始化标志位
local pause_flag = 0               --暂停标志位
local musicprogress = -1           --音频播放进度
local channel_offset = 0          --通道偏移起始
local wave_count = 0              --曲线
local square_count = 0            --曲线偏移
local power_on = 0                --开机动画时长
local red_led = 0                
local music_tab = {0x1021,0x1026}

-- 保留n位小数
function keepDecimal(num, n)
    if type(num) ~= "number" then
        return num    
    end
    n = n or 2
    if num < 0 then
        return -(math.abs(num) - math.abs(num) % 0.1 ^ n)
    else
        return num - num % 0.1 ^ n
    end
end
--变更语言显示
function change_langue()
		if get_uint16(VT_LW, sysaddr.SysLang) == 0x00 then
			set_string(VT_LW, useraddr.channel_no1tx, '通道'..(channel_offset + 1))
			set_string(VT_LW, useraddr.channel_no2tx, '通道'..(channel_offset + 2))
			set_string(VT_LW, useraddr.channel_no3tx, '通道'..(channel_offset + 3))
			set_string(VT_LW,useraddr.temp_warn , "设定温度过高")
		elseif get_uint16(VT_LW, sysaddr.SysLang) == 0x01 then
			set_string(VT_LW, useraddr.channel_no1tx, 'channel'..(channel_offset + 1))
			set_string(VT_LW, useraddr.channel_no2tx, 'channel'..(channel_offset + 2))
			set_string(VT_LW, useraddr.channel_no3tx, 'channel'..(channel_offset + 3))
			set_string(VT_LW,useraddr.temp_warn , "Setting temperature is too high！")
		elseif get_uint16(VT_LW, sysaddr.SysLang) == 0x02 then
			set_string(VT_LW, useraddr.channel_no1tx, 'チャネル'..(channel_offset + 1))
			set_string(VT_LW, useraddr.channel_no2tx, 'チャネル'..(channel_offset + 2))
			set_string(VT_LW, useraddr.channel_no3tx, 'チャネル'..(channel_offset + 3))
			set_string(VT_LW,useraddr.temp_warn , "設定温度が高すぎる！")
		elseif get_uint16(VT_LW, sysaddr.SysLang) == 0x03 then
			set_string(VT_LW, useraddr.channel_no1tx, '채널 '..(channel_offset + 1))
			set_string(VT_LW, useraddr.channel_no2tx, '채널 '..(channel_offset + 2))
			set_string(VT_LW, useraddr.channel_no3tx, '채널 '..(channel_offset + 3))
			set_string(VT_LW,useraddr.temp_warn , "> 30 ℃에서 경고 메시지를 표시합니다!")
		end
end

--上电系统初始化回调函数
function on_init()
    init_flag = 1
	set_string(VT_LW,useraddr.driver , "客厅空调")
	set_string(VT_LW,useraddr.codes_addr , "欢迎使用HMI触摸屏")
	set_uint16(VT_LW, useraddr.temp_value, 26)

	change_langue()
	set_beep_interrupt(1)
	start_timer(30,800,0,3)
	
	set_uint16(VT_LW, 0x120D, 3)

end

--系统定时回调(一个系统周期调用一次，回调速度手工程复杂程度影响。最快20ms)
function on_run(screen)
	
	if get_screen() == cur_screen.No16 or get_screen() == cur_screen.No15 then
		set_notify(0)
		for i = 3 , 9 do
			local Curve_value = math.random(-20,255)
			
			set_uint16(VT_LW,0x2100+i,Curve_value)	
			
		end
		wave_count = wave_count + 1 
		if wave_count > #sine_wave  then
			wave_count = 1
		end
		set_uint16(VT_LW,0x2100,sine_wave[wave_count])	
		set_uint16(VT_LW,0x2101,zigzag_vave[wave_count])	
		
		square_count = square_count +1 
		if  square_count > #square_wave  then
			square_count = 1
		end
		set_uint16(VT_LW,0x2102,square_wave[square_count])	
		set_notify(1)
	end
	if get_screen() == cur_screen.No21  and get_uint16(VT_LW, sysaddr.music_sndstate) == 0x80 then
		local music_playTime  = get_uint16(VT_LW, sysaddr.music_playTime) 
		print("music_playTime = "..music_playTime)
		local music_TotalTime  = get_uint16(VT_LW, sysaddr.music_TotalTime) 
		
		musicprogress = math.modf(keepDecimal(music_playTime / music_TotalTime,2)*100)    
		print("musicprogress = "..musicprogress)
		
		set_uint16(VT_LW,useraddr.music_progress,musicprogress)

	end
	if get_screen() == cur_screen.No21  and get_uint16(VT_LW, sysaddr.music_sndstate) == 0x00 and musicprogress ~= -1  then
		set_uint16(VT_LW,useraddr.music_progress,100)
		local curr_id = get_uint16(VT_LW, sysaddr.music_sndplay)
		set_uint16(VT_LW,music_tab[curr_id],0)
		set_uint16(VT_LW,sysaddr.music_sndplay,0)
		
		musicprogress = -1 
	end
end

--变量变化回调函数
function on_update(slave,vtype,addr)

	if useraddr.master_addr == addr then
		local masterv = get_uint16(VT_LW, useraddr.master_addr)
		if masterv == 1 then
			start_timer(0,10,1,0)
		else
			stop_timer(0)
		end

	elseif useraddr.textback_addr == addr then
		if get_uint16(VT_LW, useraddr.textback_addr)== 1 then
			wgt_set_bcolor(5,6, 0x1b53)
		elseif get_uint16(VT_LW, useraddr.textback_addr)== 0 then
			wgt_set_bcolor(5,6, 0xFFFF)
		end
	elseif  useraddr.switch_codes == addr then
		if get_uint16(VT_LW, useraddr.switch_codes)== 1 then
			set_string(VT_LW,useraddr.codes_addr , "https://pay.weixin.qq.com/index.php/core/home/login?return_url=%2F")
		elseif get_uint16(VT_LW, useraddr.switch_codes)== 2 then
			set_string(VT_LW,useraddr.codes_addr , "https://auth.alipay.com/login/index.htm?")
		end
	elseif useraddr.time_set == addr then
		print("useraddr.time_set = "..useraddr.time_set)
		local year = get_uint16(VT_LW, 0X0160)
		local mon  = get_uint16(VT_LW, 0X0161)
		local day  = get_uint16(VT_LW, 0X0162)
		local hour = get_uint16(VT_LW, 0X0164)
		local min  = get_uint16(VT_LW, 0X0165)
		local sec  = get_uint16(VT_LW, 0X0166)
		print(" data timer  = year ="..2019+year .. "mon = "..mon .. "day = ".. day .. "hour =".. hour .."min = "..min .. "sec =" ..sec )
		
		set_date_time (2019+year,mon+1,day+1,hour,min,sec)
		 
      elseif  addr >= useraddr.channel_minus and addr <= useraddr.channel_add 
        then
			
            local state = get_uint16(vtype, addr)

                if addr == useraddr.channel_minus
                then
					print("channel_minus ")
                    channel_offset = channel_offset - 3
                    if channel_offset < 0
                    then
                        channel_offset = 6
                    end
                else
					print("channel_add ")
                    channel_offset = channel_offset + 3
                    if channel_offset >= 9
                    then
                        channel_offset = 0
                    end
                end

				change_langue()
                wgt_set_param(cur_screen.No15,100,0x30,channel_offset)
   

	elseif sysaddr.music_sndplay == addr then
		musicprogress = 0
	elseif sysaddr.SysLang  == addr then
		change_langue()
	elseif useraddr.music_id1state  == addr then
		if get_uint16(VT_LW, useraddr.music_id1state) == 1 then
			set_uint16(VT_LW,useraddr.music_id2state,0)
			set_uint16(VT_LW,sysaddr.music_sndplay,1)
			set_uint16(VT_LW,sysaddr.music_sndstate,1)
		else
			set_uint16(VT_LW,sysaddr.music_sndplay,0)
			set_uint16(VT_LW,sysaddr.music_sndstate,3)
		end
	elseif useraddr.music_id2state  == addr then
		if get_uint16(VT_LW, useraddr.music_id2state) == 2 then
			set_uint16(VT_LW,useraddr.music_id1state,0)
			set_uint16(VT_LW,sysaddr.music_sndplay,2)
			set_uint16(VT_LW,sysaddr.music_sndstate,1)
		else
			set_uint16(VT_LW,sysaddr.music_sndplay,0)
			set_uint16(VT_LW,sysaddr.music_sndstate,3)
		end		
	elseif useraddr.beep == addr then	
		local value = get_uint16(VT_LW, useraddr.beep) 
		if value == 1 then
			beep(100)
		end
	
	elseif useraddr.button_addr == addr then
		print("useraddr.button_addr")
		local value = get_uint16(VT_LW, useraddr.button_addr) 
		if value == 0 then
			gpio_set_value(LED1,1)
			gpio_set_value(LED2,1)
		elseif value == 1 then
			gpio_set_value(LED1,0)
			gpio_set_value(LED2,0)			
		end
	elseif useraddr.error_addr == addr then
		print("useraddr.button_addr")
		local value = get_uint16(VT_LW, useraddr.error_addr) 
		if value == 0 then
			gpio_set_value(LED2,1)
			stop_timer(2)
			red_led = 0
		elseif value == 15 then
			start_timer(2,300,1,0)
		end
	elseif useraddr.videos_addr == addr then	
		local value = get_uint16(VT_LW, useraddr.videos_addr) 
		if value == 1 then
			stop_video()
			play_video("3:/videos/1.mp4",0,66,800,353)
		elseif value == 2 then
			
			if pause_flag == 0 then
				pause_flag = 1 
				pause_video()
			elseif pause_flag == 1 then
				pause_flag = 0 
				resume_video()
				set_uint16(VT_LW, 0x1211, 1)	
			end
			
		elseif value == 4 then
			stop_video()
		end
	elseif addr == 0x1022  then
			stop_video()
		
    end
	


end


--绘图回调函数，每次刷新画面都会触发
function on_draw(screen_id,control_id)


end

--定时器超时回调函数
function on_timer(timer_id)
	if timer_id == 0  then
		if master_countdown == 0 then
			master_value = master_value +3
			if master_value >= 240 then
	           master_value = 240
			   master_countdown = 1
			end
		
		else
			master_value = master_value -3
			if master_value <= 0 then
	           master_value = 0
			   master_countdown = 0
			end
		
		end
		set_uint16(VT_LW, 0x1019, master_value)
	elseif timer_id == 1  then	
		gpio_set_value(LED2,1)
	elseif timer_id == 2 and current_screen == 17  then	
		gpio_set_value(LED2,red_led)		
		red_led  = red_led + 1 
		if red_led > 1 then
			red_led = 0
		end
		print("red_led"..red_led)
	
	elseif timer_id == 30  then
		power_on = power_on + 1 
		set_uint16(VT_LW, 0x120D, (3-power_on))
		print("power_on = " ..(3-power_on))
		if power_on >= 3 then
			set_uint16(VT_LW, 0x120C, 1)
		end
		local rtcerr = rtc_is_online()
		if rtcerr == true then
			set_uint16(VT_LW, 0x1212, 1)
		else
			set_uint16(VT_LW, 0x1212, 0)
		end
		
	end
end

--画面切换回调函数
function on_screen_change(screen)
	current_screen = screen
	stop_video()
		
	gpio_set_value(LED2,1)		

	if screen == cur_screen.No0 and  init_flag == 1 then
		play_video("3:/videos/0.mp4",0,0,800,480)
		init_flag = 0
		gpio_set_out(LED1)
		gpio_set_out(LED2)
		gpio_set_value(LED1,0)
		gpio_set_value(LED2,0)
		start_timer(1,1000,1,1)

	end
	if screen == cur_screen.No15 or screen == cur_screen.No16 then
		change_langue()
		set_uint16(VT_LW, 0x1105, 1)
	elseif screen == cur_screen.No10 then
		set_uint16(VT_LW, 0x101A, 1)
	elseif screen == cur_screen.No12 then	
		set_uint16(VT_LW, 0x121F, 15)
		
	elseif screen == cur_screen.No23 then	
		gpio_set_value(LED1,1)
		gpio_set_value(LED2,1)
		set_uint16(VT_LW, 0x1210, 0)
	elseif screen == cur_screen.No35 then	
	    stop_video()
		set_uint16(VT_LW, 0x1211, 1)		
		
		
	end
	
end

function on_video_notify(msg,v1,v2) 

	if current_screen ==cur_screen.No35 and msg == 0  then
				
		stop_video()
	
		play_video("3:/videos/1.mp4",0,66,800,353)
	end

end


function on_usb_inserted(dir)
	set_uint16(VT_LW, 0x120C, 1)
	stop_video()
	
end