--_ENCRYPT_=0    --LUA脚本加密

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

local screen_info = {}
screen_info.timer = 1
screen_info.id = 0
screen_info.set = 0
screen_info.last = 0
screen_info.now = 0

local mode_fast = {}
mode_fast.id = 0
mode_fast.time = 600
mode_fast.bise = (600 / 100)
mode_fast.perc = 100

local mode_mixed = {}
mode_mixed.id = 1
mode_mixed.time = {}
mode_mixed.time.wash = 600
mode_mixed.bise = {}
mode_mixed.bise.wash = (600 / 100)
mode_mixed.perc = {}
mode_mixed.perc.wash = 100
mode_mixed.cnt = 0

local mode_wool = {}
mode_wool.id = 2
mode_wool.time = 1200
mode_wool.bise = (1200 / 100)
mode_wool.perc = 100

local mode_sweater = {}
mode_sweater.id = 3
mode_sweater.time = 2400
mode_sweater.bise = (2400 / 100)
mode_sweater.perc = 100

local mode_eider = {}
mode_eider.id = 4
mode_eider.time = 1800
mode_eider.bise = (1800 / 100)
mode_eider.perc = 100


local mode_setting = {}
mode_setting.id = 5
mode_setting.cnt = 0

mode_setting.light = 0


local mode_wash = {}

mode_wash.cnt = 0
mode_wash.water = 1
mode_wash.time = 10



local all_page = 5


Key = {
	key1 ={
		pin = 0x0303,
	},
}


function Key.key1.on_SinglePress()
	print("key1 press !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
end

function Key.key1.on_SingleRelease()
	print("key1.on_SingleRelease")
	if get_screen() == 0 then
		if screen_info.id == mode_wool.id then
			set_string(VT_LW, 0x2011, "")
			set_screen(1)
			mode_wool.time = 1200
			set_uint16(VT_LW, 0x2001, mode_wool.time // 60)
			set_uint16(VT_LW, 0x2002, mode_wool.time % 60)
			set_uint16(VT_LW, 0x2003, 100)
			start_timer(screen_info.timer,1000,0,0)
			set_string(VT_LW, 0x2027, "洗衣中...")
		elseif screen_info.id == mode_sweater.id then
			set_string(VT_LW, 0x2011, "")
			set_screen(1)
			mode_sweater.time = 2400
			set_uint16(VT_LW, 0x2001, mode_sweater.time // 60)
			set_uint16(VT_LW, 0x2002, mode_sweater.time % 60)
			set_uint16(VT_LW, 0x2003, 100)
			start_timer(screen_info.timer,1000,0,0)
			set_string(VT_LW, 0x2027, "洗衣中...")
		elseif screen_info.id == mode_eider.id then
			set_string(VT_LW, 0x2011, "")
			set_screen(1)
			mode_eider.time = 1800
			set_uint16(VT_LW, 0x2001, mode_eider.time // 60)
			set_uint16(VT_LW, 0x2002, mode_eider.time % 60)
			set_uint16(VT_LW, 0x2003, 100)
			start_timer(screen_info.timer,1000,0,0)
			set_string(VT_LW, 0x2027, "洗衣中...")
		elseif screen_info.id == mode_fast.id then
			set_string(VT_LW, 0x2011, "")
			set_screen(1)
			mode_fast.time = 600
			set_uint16(VT_LW, 0x2001, mode_fast.time // 60)
			set_uint16(VT_LW, 0x2002, mode_fast.time % 60)
			set_uint16(VT_LW, 0x2003, 100)
			start_timer(screen_info.timer,1000,0,0)
			set_string(VT_LW, 0x2027, "洗衣中...")
		elseif screen_info.id == mode_mixed.id then
			set_screen(2)
		elseif screen_info.id == mode_setting.id then
			set_screen(7)
		end
		--set_screen(screen_info.id + 1)
	elseif get_screen() == 1
	then
		set_screen(0)
	elseif get_screen() == 2
	then
		if mode_mixed.cnt == 0 
		then
			set_screen(3)
		elseif mode_mixed.cnt == 1
		then
			set_string(VT_LW, 0x2011, "")
			set_screen(1)
			mode_mixed.time.dehy = 200
			mode_mixed.bise.dehy = (200 / 100)
			mode_mixed.perc.dehy = 100
			set_uint16(VT_LW, 0x2001, mode_mixed.time.dehy // 60)
			set_uint16(VT_LW, 0x2002, mode_mixed.time.dehy % 60)
			set_uint16(VT_LW, 0x2003, 100)
			start_timer(screen_info.timer,1000,0,0)
			set_string(VT_LW, 0x2027, "脱水中...")
		elseif mode_mixed.cnt == 2
		then
			set_string(VT_LW, 0x2011, "")
			set_screen(1)
			mode_mixed.time.stov = 1500
			mode_mixed.bise.stov = (1500 / 100)
			mode_mixed.perc.stov = 100
			set_uint16(VT_LW, 0x2001, mode_mixed.time.stov // 60)
			set_uint16(VT_LW, 0x2002, mode_mixed.time.stov % 60)
			set_uint16(VT_LW, 0x2003, 100)
			start_timer(screen_info.timer,1000,0,0)
			set_string(VT_LW, 0x2027, "烘干中...")
		elseif mode_mixed.cnt == 3
		then
			set_string(VT_LW, 0x2011, "")
			set_screen(1)
			mode_mixed.time.clean = 5 *60
			mode_mixed.bise.clean = (5 *60 / 100)
			mode_mixed.perc.clean = 100
			set_uint16(VT_LW, 0x2001, mode_mixed.time.clean // 60)
			set_uint16(VT_LW, 0x2002, mode_mixed.time.clean % 60)
			set_uint16(VT_LW, 0x2003, 100)
			start_timer(screen_info.timer,1000,0,0)
			set_string(VT_LW, 0x2027, "自洁中...")
		end
	elseif get_screen() == 3
	then
		if mode_wash.cnt == 0 
		then
			mode_wash.water = 1
			set_uint16(VT_LW, 0x2007, 12)
			set_screen(5)
		elseif mode_wash.cnt == 1
		then
			mode_wash.time = 10
			set_uint16(VT_LW, 0x2008, mode_wash.time)
			set_screen(6)
		end
	elseif get_screen() == 5 
	then
		set_screen(6)
		mode_wash.time = 10
		set_uint16(VT_LW, 0x2008, mode_wash.time)
	elseif get_screen() == 6 
	then
		set_string(VT_LW, 0x2011, "")
		set_screen(1)
		set_uint16(VT_LW, 0x2001, mode_mixed.time.wash // 60)
		set_uint16(VT_LW, 0x2002, mode_mixed.time.wash % 60)
		mode_mixed.perc.wash = 100
		set_uint16(VT_LW, 0x2003, 100)
		start_timer(screen_info.timer,1000,0,0)
		set_string(VT_LW, 0x2027, "洗衣中...")
	elseif get_screen() == 7
	then
		if mode_setting.cnt == 0 
		then
			set_screen(8)
		elseif mode_setting.cnt == 1
		then
			set_screen(9)
		elseif mode_setting.cnt == 2
		then
			set_screen(16)
		end
	elseif get_screen() == 8
	then
				set_screen(7)
	elseif get_screen() == 9
	then
		set_screen(10)
	elseif get_screen() == 10
	then
		set_screen(11)
	elseif get_screen() == 11
	then
		set_screen(12)
	elseif get_screen() == 12
	then
		set_screen(13)
	elseif get_screen() == 13
	then
		set_screen(14)
	elseif get_screen() == 14
	then
		set_screen(15)
	elseif get_screen() == 15
	then
		set_screen(7)
	elseif get_screen() == 16
	then
		set_screen(7)
	end
end

function Key.key1.on_LongPress()
	print("key1 press long.....................................................................")
end

function Key.key1.on_LongRelease()
	print("key1.on_LongRelease")
	local cur_id = get_screen()
	if cur_id == 1 or cur_id == 2 or cur_id == 7
	then
		set_screen(0)
	elseif cur_id == 3
	then
		set_screen(2)
	elseif cur_id == 5 or cur_id == 6
	then
		set_screen(3)
	elseif cur_id >=8 and cur_id <= 16
	then
		set_screen(7)
	end
end


function on_init()
	dofile("bspKey.lua")
	-- 编码器按键注册
	registerKey(Key.key1.pin,
		0,
		Key.key1.on_SinglePress,
		Key.key1.on_SingleRelease,
		Key.key1.on_LongPress,
		Key.key1.on_LongRelease,
		100  
	)
	uart_setup(1,115200,8,0,0)
	set_notify(0)
	set_rotate_decoder(0,0x0300,0x0302,0) --PD0 PD2
	start_rotate_decoder()
	set_run_cycle(10)
	start_timer(0, 100, 0, 1)
	mode_setting.light = get_uint16(VT_LW, 0x0121)
	set_uint16(VT_LW, 0x2004, 0)
end

function on_run(screen)
	updateKeys()
	if num_cnt == 1 then
		screen_info.now = get_int16(VT_LW, 0x0240)
		if screen_info.now ~= screen_info.last
		then
			print("screen_info.now = "..screen_info.now)
		end
		
		local step = screen_info.now - screen_info.last
		screen_info.last = screen_info.now
		if screen == 0 then
			screen_info.id = screen_info.id + step
			if screen_info.id < 0 then
				screen_info.id = all_page
			elseif screen_info.id > all_page then
				screen_info.id = 0
			end
			set_uint16(VT_LW, 0x2000, screen_info.id)
			--set_screen(screen_info.id)
		elseif screen == 2 then 
			mode_mixed.cnt = mode_mixed.cnt + step
			if mode_mixed.cnt < 0 then
				mode_mixed.cnt = 3
			elseif mode_mixed.cnt > 3 then
				mode_mixed.cnt = 0
			end
			set_uint16(VT_LW, 0x2004, mode_mixed.cnt)
		elseif screen == 3 then 
			mode_wash.cnt = mode_wash.cnt + step
			if mode_wash.cnt < 0 then
				mode_wash.cnt = 1
			elseif mode_wash.cnt > 1 then
				mode_wash.cnt = 0
			end
			set_uint16(VT_LW, 0x2005, mode_wash.cnt)
		elseif screen == 5 then 
			mode_wash.water = mode_wash.water + step
			if mode_wash.water < 1 then
				mode_wash.water = 1
			elseif mode_wash.water > 4 then
				mode_wash.water = 4
			end
			if mode_wash.water == 1
			then
				set_uint16(VT_LW, 0x2007, 12)
			elseif mode_wash.water == 2
			then
				set_uint16(VT_LW, 0x2007, 18)
			elseif mode_wash.water == 3
			then
				set_uint16(VT_LW, 0x2007, 24)
			elseif mode_wash.water == 4
			then
				set_uint16(VT_LW, 0x2007, 36)
			end
		elseif screen == 6 then 
			mode_wash.time = mode_wash.time + (step * 10)
			if mode_wash.time < 10 then
				mode_wash.time = 10
			elseif mode_wash.time > 40 then
				mode_wash.time = 40
			end
			mode_mixed.time.wash = mode_wash.time * 60
			set_uint16(VT_LW, 0x2008, mode_wash.time)
		elseif screen == 7 then
			mode_setting.cnt = mode_setting.cnt + step
			if mode_setting.cnt < 0 then
				mode_setting.cnt = 2
			elseif mode_setting.cnt > 2 then
				mode_setting.cnt = 0
			end
			set_uint16(VT_LW, 0x2009, mode_setting.cnt)
		elseif screen == 8 then
			mode_setting.light = mode_setting.light + (step * 10)
			if mode_setting.light < 0 then
				mode_setting.light = 0
			elseif mode_setting.light > 100 then
				mode_setting.light = 100
			end
			if mode_setting.light <= 100
			then
				set_uint16(VT_LW, 0x0121, mode_setting.light)
			end
		end
	end
end


function on_update(slave,vtype,addr)
	
end

function on_draw(screen_id,control_id)
end

function on_systick()
	
end


local meter_cnt = 0
local Dtimeflag = 0
local Drunflag = 0
function on_timer(timer_id)
	if timer_id == 11 
	then
		Dtimeflag = (Dtimeflag + 1) % 2
		set_uint16(VT_LW, 0x2010, Dtimeflag)
	end
	if timer_id == 0 then
		num_cnt = 1
		set_uint16(VT_LW, 0x0240, 0)
		set_uint16(VT_LW, 0x2000, 0)
	end
	if screen_info.timer == timer_id 
	then
		Drunflag = Drunflag + 1
		Drunflag = Drunflag % 4
		if screen_info.id ==0 or screen_info.id == 2 or screen_info.id == 3 or screen_info.id == 4
		then
			if Drunflag == 0
			then
				set_string(VT_LW, 0x2027, "洗衣中")
			elseif Drunflag == 1
			then
				set_string(VT_LW, 0x2027, "洗衣中.")
			elseif Drunflag == 2
			then
				set_string(VT_LW, 0x2027, "洗衣中..")
			elseif Drunflag == 3
			then
				set_string(VT_LW, 0x2027, "洗衣中...")
			end
		elseif screen_info.id == mode_mixed.id
		then
			if mode_mixed.cnt == 0
			then
				if Drunflag == 0
				then
					set_string(VT_LW, 0x2027, "洗衣中")
				elseif Drunflag == 1
				then
					set_string(VT_LW, 0x2027, "洗衣中.")
				elseif Drunflag == 2
				then
					set_string(VT_LW, 0x2027, "洗衣中..")
				elseif Drunflag == 3
				then
					set_string(VT_LW, 0x2027, "洗衣中...")
				end
			elseif mode_mixed.cnt == 1
			then
				if Drunflag == 0
				then
					set_string(VT_LW, 0x2027, "脱水中")
				elseif Drunflag == 1
				then
					set_string(VT_LW, 0x2027, "脱水中.")
				elseif Drunflag == 2
				then
					set_string(VT_LW, 0x2027, "脱水中..")
				elseif Drunflag == 3
				then
					set_string(VT_LW, 0x2027, "脱水中...")
				end
			elseif mode_mixed.cnt == 2
			then
				if Drunflag == 0
				then
					set_string(VT_LW, 0x2027, "烘干中")
				elseif Drunflag == 1
				then
					set_string(VT_LW, 0x2027, "烘干中.")
				elseif Drunflag == 2
				then
					set_string(VT_LW, 0x2027, "烘干中..")
				elseif Drunflag == 3
				then
					set_string(VT_LW, 0x2027, "烘干中...")
				end
			elseif mode_mixed.cnt == 3
			then
				if Drunflag == 0
				then
					set_string(VT_LW, 0x2027, "自洁中")
				elseif Drunflag == 1
				then
					set_string(VT_LW, 0x2027, "自洁中.")
				elseif Drunflag == 2
				then
					set_string(VT_LW, 0x2027, "自洁中..")
				elseif Drunflag == 3
				then
					set_string(VT_LW, 0x2027, "自洁中...")
				end
			end
		end
		if screen_info.id == mode_wool.id then
			mode_wool.time = mode_wool.time - 1
			if mode_wool.time % mode_wool.bise == 0 then
				mode_wool.perc = mode_wool.perc - 1
			end
			if mode_wool.perc <= 0 then
				--set_screen(0)
				set_string(VT_LW, 0x2011, "洗衣已完成")
				stop_timer(screen_info.timer)
			end
			set_uint16(VT_LW, 0x2001, mode_wool.time // 60)
			set_uint16(VT_LW, 0x2002, mode_wool.time % 60)
			set_uint16(VT_LW, 0x2003, mode_wool.perc)
		elseif screen_info.id == mode_sweater.id then
			mode_sweater.time = mode_sweater.time - 1
			if mode_sweater.time % mode_sweater.bise == 0 then
				mode_sweater.perc = mode_sweater.perc - 1
			end
			if mode_sweater.perc <= 0 then
				--set_screen(0)
				set_string(VT_LW, 0x2011, "洗衣已完成")
				stop_timer(screen_info.timer)
			end
			set_uint16(VT_LW, 0x2001, mode_sweater.time // 60)
			set_uint16(VT_LW, 0x2002, mode_sweater.time % 60)
			set_uint16(VT_LW, 0x2003, mode_sweater.perc)
		elseif screen_info.id == mode_eider.id then
			mode_eider.time = mode_eider.time - 1
			if mode_eider.time % mode_eider.bise == 0 then
				mode_eider.perc = mode_eider.perc - 1
			end
			if mode_eider.perc <= 0 then
				--set_screen(0)
				set_string(VT_LW, 0x2011, "洗衣已完成")
				stop_timer(screen_info.timer)
			end
			set_uint16(VT_LW, 0x2001, mode_eider.time // 60)
			set_uint16(VT_LW, 0x2002, mode_eider.time % 60)
			set_uint16(VT_LW, 0x2003, mode_eider.perc)
		elseif screen_info.id == mode_fast.id then
			mode_fast.time = mode_fast.time - 1
			if mode_fast.time % mode_fast.bise == 0 then
				mode_fast.perc = mode_fast.perc - 1
			end
			if mode_fast.perc <= 0 then
				--set_screen(0)
				set_string(VT_LW, 0x2011, "洗衣已完成")
				stop_timer(screen_info.timer)
			end
			set_uint16(VT_LW, 0x2001, mode_fast.time // 60)
			set_uint16(VT_LW, 0x2002, mode_fast.time % 60)
			set_uint16(VT_LW, 0x2003, mode_fast.perc)
		elseif screen_info.id == mode_mixed.id 
		then
			if mode_mixed.cnt == 0 
			then
				mode_mixed.time.wash = mode_mixed.time.wash - 1
				if mode_mixed.time.wash % mode_mixed.bise.wash == 0 then
					mode_mixed.perc.wash = mode_mixed.perc.wash - 1
				end
				if mode_mixed.perc.wash <= 0 then
					--set_screen(0)
					set_string(VT_LW, 0x2011, "洗衣已完成")
					stop_timer(screen_info.timer)
				end
				set_uint16(VT_LW, 0x2001, mode_mixed.time.wash // 60)
				set_uint16(VT_LW, 0x2002, mode_mixed.time.wash % 60)
				set_uint16(VT_LW, 0x2003, mode_mixed.perc.wash)
			elseif mode_mixed.cnt == 1
			then
				mode_mixed.time.dehy = mode_mixed.time.dehy - 1
				if mode_mixed.time.dehy % mode_mixed.bise.dehy == 0 then
					mode_mixed.perc.dehy = mode_mixed.perc.dehy - 1
				end			
				if mode_mixed.perc.dehy <= 0 then
					--set_screen(0)
					set_string(VT_LW, 0x2011, "脱水已完成")
					stop_timer(screen_info.timer)
				end
				set_uint16(VT_LW, 0x2001, mode_mixed.time.dehy // 60)
				set_uint16(VT_LW, 0x2002, mode_mixed.time.dehy % 60)
				set_uint16(VT_LW, 0x2003, mode_mixed.perc.dehy)
			elseif mode_mixed.cnt == 2
			then
				mode_mixed.time.stov = mode_mixed.time.stov - 1
				if mode_mixed.time.stov % mode_mixed.bise.stov == 0 then
					mode_mixed.perc.stov = mode_mixed.perc.stov - 1
				end
				if mode_mixed.perc.stov <= 0 then
					--set_screen(0)
					set_string(VT_LW, 0x2011, "烘干已完成")
					stop_timer(screen_info.timer)
				end
				set_uint16(VT_LW, 0x2001, mode_mixed.time.stov // 60)
				set_uint16(VT_LW, 0x2002, mode_mixed.time.stov % 60)
				set_uint16(VT_LW, 0x2003, mode_mixed.perc.stov)
			elseif mode_mixed.cnt == 3
			then
				mode_mixed.time.clean = mode_mixed.time.clean - 1
				if mode_mixed.time.clean % mode_mixed.bise.clean == 0 then
					mode_mixed.perc.clean = mode_mixed.perc.clean - 1
				end
				if mode_mixed.perc.clean <= 0 then
					--set_screen(0)
					set_string(VT_LW, 0x2011, "自洁已完成")
					stop_timer(screen_info.timer)
				end
				set_uint16(VT_LW, 0x2001, mode_mixed.time.clean // 60)
				set_uint16(VT_LW, 0x2002, mode_mixed.time.clean % 60)
				set_uint16(VT_LW, 0x2003, mode_mixed.perc.clean)
			end
		end
	end
end


function on_screen_change(screen)
	if screen ~= 1 then
		stop_timer(screen_info.timer)
		mode_wool.time = 1200
		mode_sweater.time = 2400
		mode_eider.time = 1800
		mode_fast.time = 600
		
		mode_wool.perc = 100
		mode_sweater.perc = 100
		mode_eider.perc = 100
		mode_fast.perc = 100
		stop_timer(11)
	end
	if screen == 1
	then
		start_timer(11, 500, 0, 0)
		set_string(VT_LW, 0x2011, "")
	end
	if screen == 5 
	then
		
	end
	if screen == 6
	then
		set_uint16(VT_LW, 0x2008, 10)
	end
end

function on_uart_recv(ch,packet)
	uart_send(ch,packet)
end