_ENCRYPT_=0    --LUA脚本加密

--数据类型定义
--VTYPE

--系统加载LUA脚本文件之后,立即调用此回调函数,通常用于执行初始化操作
function on_init()
	--numvar = 0
	volume = 0x20
	dataRecordId = 0
	meterNum = 0
	meterBackNum = 0
	sTimerNum = 0
	mTimerNum = 0
	hTimerNum = 0
	set_text(5, 4, "0")
	meterPlayerFlag = 0
	timerPlayerFlag = 0
	--set_text(7, 13, 32)
	--顺时针驱动仪表指令
	--meterClockwiseCmd = {0xAA, 0xB1, 0x1C, 0x00, 0x05, 0x00, 0x02, 0x20, 0xCC, 0x33, 0xC3, 0x3C}
	meterClockwiseCmd = {0xAA, 0xB1, 0x1C, 0x00, 0x05, 0x00, 0x02, 0x17, 0xCC, 0x33, 0xC3, 0x3C}
	--逆时针驱动仪表指令
	--meterAnticlockwiseCmd = {0xAA, 0xB1, 0x1D, 0x00, 0x05, 0x00, 0x02, 0x10, 0xCC, 0x33, 0xC3, 0x3C}
	meterAnticlockwiseCmd = {0xAA, 0xB1, 0x1D, 0x00, 0x05, 0x00, 0x02, 0x17, 0xCC, 0x33, 0xC3, 0x3C}
	audioCmd = {0xAA, 0x93, 0x20, 0xCC, 0x33, 0xC3, 0x3C}

	--秒针
	sTimerClockInitCmd = {0xAA, 0xB1, 0x10, 0x00, 0x0F, 0x00, 0x02, 0x00, 0x00, 0x00, 0x0A, 0xCC, 0x33, 0xC3, 0x3C}
	sTimerClockCmd = {0xAA, 0xB1, 0x1C, 0x00, 0x0F, 0x00, 0x02, 0x0A, 0xCC, 0x33, 0xC3, 0x3C}

	--分针
	mTimerClockInitCmd = {0xAA, 0xB1, 0x10, 0x00, 0x0F, 0x00, 0x03, 0x00, 0x00, 0x00, 0x0A, 0xCC, 0x33, 0xC3, 0x3C}
	mTimerClockCmd = {0xAA, 0xB1, 0x1C, 0x00, 0x0F, 0x00, 0x03, 0x0A, 0xCC, 0x33, 0xC3, 0x3C}
	
	--时针
	hTimerClockInitCmd = {0xAA, 0xB1, 0x10, 0x00, 0x0F, 0x00, 0x04, 0x00, 0x00, 0x00, 0x0A, 0xCC, 0x33, 0xC3, 0x3C}
	hTimerClockCmd = {0xAA, 0xB1, 0x1C, 0x00, 0x0F, 0x00, 0x04, 0x0A, 0xCC, 0x33, 0xC3, 0x3C}
end

--系统每隔1秒钟自动调用此回调函数
function on_systick()

end

--当画面需要切换时,执行此回调函数,screen为目标画面.
function on_screen_change(screen)
	if screen == 15 then
		if (timerPlayerFlag == 1 ) then
			--start_timer(1, 1000, 0, 0)
			--start_timer(2, 60*1000, 0, 0)
			--start_timer(3,  60*60*1000, 0, 0)
			
			start_timer(1, 1000, 0, 0)
			start_timer(2, 25000, 0, 0)
			start_timer(3, 60000, 0, 0)

			timerPlayerFlag = 0
		end
	end
end

--函数功能：用户触摸修改控件后,执行此回调函数.
function on_control_notify(screen, control, value)
	--[[if (screen == 5) and (control == 3) then
		if (value == 1) then
			if (meterPlayerFlag == 1) then
				meterPlayerFlag = 0
			else
				meterPlayerFlag = 1
			end
		end
	end--]]
	
	if (screen == 17) and (control == 5) then
		local progressValue = get_value(17, 4)
		set_text(17, 7, progressValue)
	end
	
	if (screen == 17) and (control == 6) then
		local progressValue = get_value(17, 4)
		set_text(17, 7, progressValue)
	end
	
	if (screen == 0) and (control == 23) then
		if (value == 1) then
			timerPlayerFlag = 1
		end
	end
	
	if (screen == 5) and (control == 3) then
		if (value == 0) then
			meterPlayerFlag = 0
			stop_timer(0)
		else
			meterPlayerFlag = 1
			start_timer(0, 100, 0, 0)
		end
	end
	
	if (screen == 3) and (control == 2) then
		local progressValue = get_value(3, 2)
		set_text(3, 9, progressValue)
	end
	
		if (screen == 3) and (control == 4) then
		local progressValue = get_value(3, 4)
		set_text(3, 10, progressValue)
	end
	
	--增加数据记录
	add_data_record(screen, control, value)
	
	if (screen == 6) then
		audio_player(screen, control, value)
	end
	
	if (screen == 7) then
		video_player(screen, control, value)
	end
    
	if (screen == 50) then
        local text
        if control == 2 then
            text = get_text(50,6)
            set_text(48,1,text)
        elseif control == 3 then
            text = get_text(50,7)
            set_text(48,1,text)
        elseif control == 4 then
            text = get_text(50,8)
            set_text(48,1,text)
        elseif control == 5 then
            text = get_text(50,9)
            set_text(48,1,text)           
        end

	end    
end

--定时器方面的几个函数使用
--LUA定时器中断函数,timer_id值范围为0-31
function on_timer(timer_id)
	if (timer_id == 0) then
		if (meterPlayerFlag == 1) then
			if (meterNum >= 0) and (meterNum <= 16) and (meterBackNum == 0) then
				run_HexCmd(meterClockwiseCmd)
				meterNum = (meterNum + 1)
				set_text(5, 4, meterNum * 10)
				
				if (meterNum == 17) then
					meterBackNum = 32
				end
			end
			
			if (meterBackNum > 0) and (meterBackNum <= 32) then
				--meterNum = (meterNum + 1)
				if (meterBackNum - meterNum) >= 0 then
					set_text(5, 4, (meterBackNum - meterNum) * 10)
					meterBackNum = meterBackNum - 1
				else
					meterNum = 0
					meterBackNum = 0
				end
				run_HexCmd(meterAnticlockwiseCmd)
			end 
		end
	end
	
	if (timer_id == 1) then
		if (sTimerNum >= 0) and (sTimerNum <= 59) then
			run_HexCmd(sTimerClockCmd)
			sTimerNum = (sTimerNum + 1)
			if (sTimerNum == 59) then
				run_HexCmd(sTimerClockInitCmd)
				sTimerNum = 0
			end
		end
	end
	

	if (timer_id == 2) then
		if (mTimerNum >= 0) and (mTimerNum <= 59) then
			run_HexCmd(mTimerClockCmd)
			mTimerNum = (mTimerNum + 1)
			if (mTimerNum == 59) then
				run_HexCmd(mTimerClockInitCmd)
				mTimerNum = 0
			end
		end
	end
	
	if (timer_id == 3) then
		if (hTimerNum >= 0) and (hTimerNum <= 59) then
			run_HexCmd(hTimerClockCmd)
			hTimerNum = (hTimerNum + 1)
			if (hTimerNum == 59) then
				run_HexCmd(hTimerClockInitCmd)
				hTimerNum = 0
			end
		end
	end
	
end


function on_run(screen)
end

function on_update(slave, vtype, addr)
end

function on_draw(screen_id, control_id)
end


function add_data_record(screen, control, value)
	if (screen == 8) and (control == 8) then
		--AA B1 52 00 08 00 05 31 3B B8 DF CE C2 3B 32 30 32 35 2D 30 36 2D 31 33 20 
		--31 31 3A 32 33 3A 30 32 3B 32 30 32 35 2D 30 36 2D 31 33 20 31 31 3A 32 36 
		--3A 31 36 CC 33 C3 3C
		dataRecordId = dataRecordId + 1	
		local strId = string.format("%02d", dataRecordId)
		local tmpCmd = {}
		tmpCmd[1] = 0xAA
		tmpCmd[2] = 0xB1
		tmpCmd[3] = 0x52
		tmpCmd[4] = 0x00
		tmpCmd[5] = 0x08
		tmpCmd[6] = 0x00
		tmpCmd[7] = 0x05
        
		--数据记录ID
		for i = 1,  string.len(strId) do
			tmpCmd[7 + i] = "0x" .. string.format("%02x", string.byte(strId, i))
			index = 7 + i + 1
		end
		tmpCmd[index] = 0x3B
        
		tmpCmd[index + 1] = 0xB8
		tmpCmd[index + 2] = 0xDF
		tmpCmd[index + 3] = 0xCE
		tmpCmd[index + 4] = 0xC2
		tmpCmd[index + 5] = 0x3B
        
		tmpCmd[index + 6] = 0x31
		tmpCmd[index + 7] = 0x31
		tmpCmd[index + 8] = 0x3A
		tmpCmd[index + 9] = 0x32
		tmpCmd[index + 10] = 0x33
		tmpCmd[index + 11] = 0x3A
		tmpCmd[index + 12] = 0x30
		tmpCmd[index + 13] = 0x32
		tmpCmd[index + 14] = 0x3B
        
		tmpCmd[index + 15] = 0x31
		tmpCmd[index + 16] = 0x31
		tmpCmd[index + 17] = 0x3A
		tmpCmd[index + 18] = 0x32
		tmpCmd[index + 19] = 0x36       
		tmpCmd[index + 20] = 0x3A
		tmpCmd[index + 21] = 0x31
		tmpCmd[index + 22] = 0x36
        tmpCmd[index + 23] = 0x3B
        
		tmpCmd[index + 24] = 0xCC
		tmpCmd[index + 25] = 0x33
		tmpCmd[index + 26] = 0xC3
		tmpCmd[index + 27] = 0x3C

		run_HexCmd(tmpCmd)
	end
end

function audio_player(screen, control, value)
	if (screen == 6) and (control == 15) then
		volume = get_value(screen, control) 
		audioCmd[3] = volume
		run_HexCmd(audioCmd)
	end
end

function video_player(screen, control, value)
	if (screen == 7) and (control == 11) then
		volume = get_value(screen, control) 
		audioCmd[3] = volume
		run_HexCmd(audioCmd)
	end
end
