|

楼主 |
发表于 2025-6-7 15:49:36
|
显示全部楼层
本帖最后由 nietao1 于 2025-6-9 15:28 编辑
addr=0x00000000
-- 数值转换函数:将存储的数值转换为有符号的实际值
-- 规则:0=0, 255=-1, 254=-2...173=-82
local function convert_stored_to_signed(stored_value)
stored_value = tonumber(stored_value) or 0 -- 确保是数字
if stored_value == 0 then
return 0
elseif stored_value >= 173 and stored_value <= 255 then
return stored_value - 256
else
-- 1~172保持原值(如果有)
return stored_value
end
end
function on_init()
uart_free_protocol=1 read_data=read_flash(addr,100)
set_value(5,2,read_data[0] )--控件减
set_value(5,3,read_data[1])--控件加
set_text(5, 4, tostring(convert_stored_to_signed(read_data[2])))
end
local output1_vloume={ }
output1_vloume[0]=0xEF
output1_vloume[1]=0x85
output1_vloume[2]=0x20
output1_vloume[3]=0x02
output1_vloume[4]=0xD0
output1_vloume[5]=0x02
output1_vloume[6]=0xD0
output1_vloume[7]=0x00
output1_vloume[8]=0x00
output1_vloume[9]=0xFF
output1_vloume[10]=0xFE
output1_vloume[11]=0xFF
output1_vloume[12]=0xFF function on_control_notify(screen,control,value)
local high_byte3=(output1_vloume[4] >> 4) & 0x0F
local low_byte3= output1_vloume[4] & 0x0F
local add3=high_byte3 * 16+low_byte3
local output1_vloume_ten=output1_vloume[3]*256+add3
if screen == 17 and value == 1 then
if control == 1 then
local save = {}
save[0] = get_value(5,2)--控件减
save[1] = get_value(5,3)--进度加
save[2] = string.format("%d", get_text(5,4)) -- 文本控件 write_flash(addr, save)
local write_jsonStr = cjson.encode(save)
print("DBUG: encode Jsonstr -> "..write_jsonStr)
end
end
if screen==5 and control==2 then
if output1_vloume_ten>0 then
output1_vloume_ten=output1_vloume_ten-10
output1_vloume[3] = math.floor(output1_vloume_ten / 256) -- 高字节
output1_vloume[4] = output1_vloume_ten % 256 -- 低字节
uart_send_data(output1_vloume)
if get_value(5,4)>-72 then
set_value(5,4,get_value(5,4)-1)
get_text(5,4)
print("OUT1:"..output1_vloume_ten)
endend
end
if screen==5 and control==3 then
if output1_vloume_ten<720 then
output1_vloume_ten=output1_vloume_ten +10
output1_vloume[3] = math.floor(output1_vloume_ten / 256) -- 高字节
output1_vloume[4] = output1_vloume_ten % 256 -- 低字节
uart_send_data(output1_vloume)
if get_value(5,4)<0 then
set_value(5,4,get_value(5,4)+1)
get_text(5,4)
print("OUT1:"..output1_vloume_ten)
end
end
end 这个是关于保存部分的代码
|
|