|
uart_free_protocol = 1
--[[--LUA脚本开启自由串口协议时(uart_free_protocol = 1),
屏幕接收数据会自动触发on_uart_recv_data(packet)函数
--]]
local sc_Home = 0
local Func_lampState = 0x01
local Func_lampLight = 0x02
local cmd_head = 0x5A
local cmd_end = 0xA55AA5A5
local buff = {}
local cmd_length = 0
local cmd_end_tag = 0
local judge_lamp1_light = 0
--calculate CRC16
--@data : t, data to be verified
--@n : number of verified
--@return : check result
function add_crc16(start, n, data)
local carry_flag, a = 0
local result = 0xffff
local i = start
while(true)
do
result = result ~ data[i]
for j = 0, 7
do
a = result
carry_flag = a & 0x0001
result = result >> 1
if carry_flag == 1
then
result = result ~ 0xa001
end
end
i = i + 1
if i == start + n
then
break
end
end
return result
end
-- Calculate checksum
-- @buffTb: check table
function my_cal_checksum(buffTb)
local check_sum = 0
for i = 0, #(buffTb)-1
do
check_sum = check_sum + buffTb[i]
end
return (check_sum & 0xFF)
end
--set the lamp state
--@xth_lamp:which lamp
--@xth_lamp_style: state of lamp, 0-colse,1-open
function my_set_lamp_state(xth_lamp, xth_lamp_state)
if xth_lamp == 1
then
set_value(sc_Home, 1, xth_lamp_state)
end
end
--set the style of the lamp
--@xth_lamp : which lamp
--@xth_lamp_style: style of lamp
function my_set_lamp_light(xth_lamp, xth_lamp_light)
if xth_lamp == 1
then
set_value(sc_Home, 2, xth_lamp_light)
end
end
--send notify of lamp status
--@xth_lamp: which lamp
--@state: state of lamp, 0-colse,1-open
function my_uartsend_lampstate_notify(xth_lamp,state)
local lamp_state_notify = {}
local send_crc16 = 0
lamp_state_notify[0] = 0xA5
lamp_state_notify[1] = Func_lampState
lamp_state_notify[2] = xth_lamp
lamp_state_notify[3] = state
send_crc16 = add_crc16(1, 3, lamp_state_notify)
lamp_state_notify[4] = (send_crc16 >> 8) & 0xFF
lamp_state_notify[5] = (send_crc16 >> 0) & 0xFF
lamp_state_notify[6] = 0x5A
lamp_state_notify[7] = 0xA5
lamp_state_notify[8] = 0x5A
lamp_state_notify[9] = 0x5A
uart_send_data(lamp_state_notify)
end
--send notify of lamp brightness
function my_uartsend_lamplight_notify(xth_lamp)
local xth_lamp_light = 0
local lamp_light_notify = {}
local send_crc16 = 0
if xth_lamp == 1
then
xth_lamp_light = get_value(sc_Home, 2)
end
lamp_light_notify[0] = 0xA5
lamp_light_notify[1] = Func_lampLight
lamp_light_notify[2] = xth_lamp
lamp_light_notify[3] = xth_lamp_light
send_crc16 = add_crc16(1, 3, lamp_light_notify)
lamp_light_notify[4] = (send_crc16 >> 8) & 0xFF
lamp_light_notify[5] = (send_crc16 >> 0) & 0xFF
lamp_light_notify[6] = 0x5A
lamp_light_notify[7] = 0xA5
lamp_light_notify[8] = 0x5A
lamp_light_notify[9] = 0x5A
uart_send_data(lamp_light_notify)
end
--Instruction analysis
--@msg:data table
function my_processmessage(msg)
local funccode = msg[1]
if funccode == Func_lampState
then
local xth_lamp = msg[2]
local xth_lamp_state = msg[3]
my_set_lamp_state(xth_lamp, xth_lamp_state)
elseif funccode == Func_lampLight
then
local xth_lamp = msg[2]
local xth_lamp_light = msg[3]
my_set_lamp_light(xth_lamp, xth_lamp_light)
end
end
-- 系统函数: 初始化
function on_init()
uart_set_timeout(0, 0)
uart_setup(115200, 0, 1.5, 8)
end
local cmd_head = 0x5A --帧头
local cmd_end = 0xA55AA5A5 --帧尾
local buff = {} --缓冲区
local cmd_length = 0 --帧长度
local cmd_head_tag = 0
local cmd_end_tag = 0 --帧尾标识
-- 系统函数: 串口接收函数
function on_uart_recv_data(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
cmd_end_tag = (cmd_end_tag << 8) | (packet[i])
if (cmd_end_tag & cmd_end)== cmd_end
then
check16 = ((buff[cmd_length - 6] << 8) | buff[cmd_length - 5]) & 0xFFFF
print('check16 = '..string.format('%04X', check16))
print('result = '..string.format('%04X', add_crc16(1, cmd_length - 7, buff)))
if check16 == add_crc16(1, cmd_length - 7, buff)
then
my_processmessage(buff)
buff = {}
cmd_length = 0
cmd_end_tag = 0
cmd_head_tag = 0
else
buff = {}
cmd_length = 0
cmd_end_tag = 0
cmd_head_tag = 0
end
end
end
end
end
-- 系统函数: 触摸回调函数
function on_press(state,x,y)
local cur_screen = get_current_screen()
if state == 0 and cur_screen == sc_Home
then
if judge_lamp1_light == 1
then
my_uartsend_lamplight_notify(1)
judge_lamp1_light = 0
end
end
end
--用户通过触摸修改控件后,执行此回调函数。
--点击按钮控件,修改文本控件、修改滑动条都会触发此事件。
function on_control_notify(screen,control,value)
--开关灯设置
if screen == sc_Home and control == 1
then
my_uartsend_lampstate_notify(1, value)
--亮度设置
elseif screen == sc_Home and control == 2
then
judge_lamp1_light = 1
end
end
|
|