--- 模块功能 HMI -- @module -- @author lcg -- @license MIT -- @copyright yf -- @release 2019.08.01 require 'ril' require "misc" require "AM2320" require "audio" module(..., package.seeall) local uart_id local hmi_task HMI_FILE = "/hmi.info" -- { -- "cmd": "paramconf", -- "ext": { -- "wind_sw": 0, -- "coll_time": ["7-9", "10-11"], -- "set_temp": 25, -- "imgres": 0, -- "datt": 0, -- "drop_time": 0, -- "cul_time": 24 -- } -- } hmiconf = { passWord = 8888, --设置初始化密码 thsp = 300, --默认熄屏时间5分钟 vol = 7, -- 默认音量 datt = 20, -- 数据上传时间间隔 分钟 wind_sw = 0, on_off = 1,-- 设备开关:注意:1为开;0为关 -- coll_time = '["9-10","14-15"]', -- coll_time = '[ " 6- 7", "8 - 10","12 -15","1 6 -2 0","21 -2 1"]', -- coll_time = '[ "8 - 10", "1 6 -2 0"," 6- 7", "12 -15","21 -2 1"]', -- coll_time = '[ "8 - 8", "1 6 -2 0"," 6- 7", "12 -15","21 -2 1","9 - 10"]', -- coll_time = '[ "8 - 8"," 6- 7", "12 -15","9 - 10"]', -- coll_time = ["7-9","10-11"], -- coll_time = {"8 - 8"," 6- 7", "12 -15","9 - 10"}, -- coll_time = {"8 - 9"," 4- 5", "14 -19","10 - 11"}, -- coll_time = {"8 - 9"," 4- 5", "14 -20","10 - 11"}, -- coll_time = {"8-9","4-5","10-20"}, coll_time = {"8-10","14-16"}, cold_sw = 0,--制冷开关 set_temp = 25, -- 保温仓设定温度 imgres = 0,-- 分辨率 drop_time = 1,-- 滴液秒数/10 cul_time = 1, -- 设定培养时间 } hmista = { work_sta = 0,-- 工作状态:0待机;1收集;2培养;3拍照; collect_fan = 0,--收集风扇:0停;1转 cool_fan = 0,--散热风机:0停;1转 heat_sta = 0,--加热状态:0停止;1加热 cold_sta = 0,--制冷状态:0停止;1加热 -- 机器内部温度: box_tem = 25, -- 环境温湿度: envTem = 25, -- 环境温度 envHum = 30, -- 环境湿度 heatTem = 25, --保温仓实时温度 vbat = "",--电池电压 bat_sta = 0,--电池低压保护状态 staytime = 0,--已经培养的时间(小时) pre_temp = 30,-- 保温仓当前温度 usb_sta = 0,--相机状态 imgres = 0,--分辨率 wind_sw = 0,--风机状态 at = 0,--温度 ah = 0,--湿度 rainStatus = 0,--雨控 stm8_v = '',--stm8代码版本 } local function read_line() while true do local s = uart.read(uart_id, '*l') if s ~= '' then return s end coroutine.yield() end end function write(s) uart.write(uart_id, s .. string.fromhex("FFFFFF")) end local function on_wait_event_timeout() coroutine.resume(hmi_task, 'TIEMOUT') end local function wait_event(event, timeout) if timeout then sys.timer_start(on_wait_event_timeout, timeout) end while true do local receive_event = coroutine.yield() if receive_event == event then sys.timer_stop(on_wait_event_timeout) return elseif receive_event == 'TIMEOUT' then write('WAIT EVENT ' .. event .. 'TIMEOUT\r\n') return end end end local function main_loop() local cache_data = '' local wait_event_flag while true do -- 读取输入 local new_data = read_line('*l') -- 输出回显 -- write(new_data) -- 拼接之前未成行的剩余数据 cache_data = cache_data .. new_data -- 去掉回车换行 local line = cache_data:match('(.+)\r\n') if line then -- 收到一整行的数据 清除缓冲数据 cache_data = '' -- 用xpcall执行用户输入的脚本,可以捕捉脚本的错误 xpcall(function() if line == 'button' then audio.play('/ldata/button.mp3') elseif line == 'cancel' then audio.play("/ldata/button.mp3") elseif line == 'getFullData' then getFullData() elseif line:find("saveClockSet") then saveClockSet(line:match("=(.+)")) elseif line:find("saveUploadSet") then saveUploadSet(line:match("=(.+)")) elseif line:find('saveSystemSet') then saveSystemSet(line:match("=(.+)")) elseif line == "turn" then turn() elseif line == "turn_init" then turn_init() elseif line:find("putTest") then putTest(line:match("%.(.+)=(.+)")) elseif line:find("resetDefault") then resetDefault() end end, function()log.info(debug.traceback()) end) if wait_event_flag then wait_event(wait_event_flag, 3000) wait_event_flag = nil end end end end --- console.setup -- @param id 串口id -- @param baudrate 串口波特率 -- @return 无 -- @usage console.setup(1, 115200) function setup(id, baudrate) -- 默认串口1 uart_id = id or 1 -- 默认波特率115200 baudrate = baudrate or 115200 -- 创建console处理的协程 hmi_task = coroutine.create(main_loop) -- 初始化串口 uart.setup(uart_id, baudrate, 8, uart.PAR_NONE, uart.STOP_1) -- 串口收到数据时唤醒console协程 uart.on(uart_id, 'receive', function()coroutine.resume(hmi_task) end) coroutine.resume(hmi_task) end function getFullData() -- workstatus page local c = misc.getClock() local date = string.format('"%00004d-%02d-%02d %02d:%02d:%02d"', c.year, c.month, c.day, c.hour, c.min, c.sec) -- 注意:防止imei的后八位的前几位连续位0:要进行8位格式化: write("workSta.equipid.txt=" .. string.format('"%06d"',string.sub(misc.getimei(), -6, -1))) write("workSta.rtc.txt=" .. date) write("workSta.csq.txt=" .. string.format('"%02d"',net.getRssi()))-- 信号强度) write("workSta.vbat.txt=" .. hmista.vbat) write("workSta.bat_sta.val=" .. hmista.bat_sta) write("workSta.on_off.val=" .. hmiconf.on_off) write("workSta.work_sta.val=" .. hmista.work_sta) write("workSta.collect_fan.val=" .. hmista.collect_fan) write("workSta.cool_fan.val=" .. hmista.cool_fan) write("workSta.box_tmp.txt=" .. hmista.box_tem) write("workSta.env_tem.val=" .. (hmista.envTem + 5) / 10) write("workSta.env_hum.val=" .. (hmista.envHum + 5) / 10) write("workSta.heat_sta.val=" .. hmista.heat_sta) write("workSta.cold_sw.val=" .. hmiconf.cold_sw) write("workSta.cold_sta.val=" .. hmista.cold_sta) write("workSta.set_temp.val=" .. hmiconf.set_temp) write("workSta.pre_temp.val=" .. hmista.pre_temp) write("workSta.cul_time.val=" .. hmiconf.cul_time) write("workSta.staytime.val=" .. hmista.staytime) -- 温湿度 page write("temHum.box_tmp.txt=" .. hmista.box_tem) -- write("temHum.box_hum.val=" .. (hmista.box_hum + 5) / 10) write("temHum.env_tem.val=" .. (hmista.envTem + 5) / 10) write("temHum.env_hum.val=" .. (hmista.envHum + 5) / 10) write("temHum.cool_fan.val=" .. hmista.cool_fan) -- timer page -- local table_tim1 = control.lua_string_split(hmiconf.coll_time[1], "-") -- local table_tim2 = control.lua_string_split(hmiconf.coll_time[2], "-") local table_tim1 = control.lua_string_split(string.gsub(hmiconf.coll_time[1], '[ "]', ''), "-") local table_tim2 = control.lua_string_split(string.gsub(hmiconf.coll_time[2], '[ "]', ''), "-") -- log.info("clockSet.start1.val=" .. table_tim1[1]) -- log.info("clockSet.end1.val=" .. table_tim1[2]) -- log.info("clockSet.start2.val=" .. table_tim2[1]) -- log.info("clockSet.end2.val=" .. table_tim2[2]) write("clockSet.start1.val=" .. table_tim1[1]) write("clockSet.end1.val=" .. table_tim1[2]) write("clockSet.start2.val=" .. table_tim2[1]) write("clockSet.end2.val=" .. table_tim2[2]) write("clockSet.drop.val=" .. hmiconf.drop_time) write("clockSet.stay.val=" .. hmiconf.cul_time) -- -- upload page -- write("uploadSet.imgres.val=" .. hmiconf.imgres) -- write("uploadSet.imgupl.val=" .. hmiconf.imgupl) -- write("uploadSet.datt.val=" .. hmiconf.datt) -- system page -- write("sysSet.thsp=" .. hmiconf.thsp) write("sysSet.vol.val=" .. hmiconf.vol or 7) write("sysSet.keep.val=" .. hmiconf.set_temp) write("sysSet.cold_sw.val=" .. hmiconf.cold_sw) write("sysSet.datt.val=" .. hmiconf.datt) -- log.info("sysSet.keep.val=" .. hmiconf.set_temp) -- log.info("sysSet.datt.val=" .. hmiconf.datt) -- -- password page -- write("inputPasswd.pwdData.txt=" .. hmiconf.passWord) end -- 定时模式设置保存 function saveClockSet(dat) local idx,coll_time = 1,{} local start1,end1,start2,end2 idx, start1 = pack.unpack(dat, "') control.collect_fan(1) sys.wait(10000) control.collect_fan(0) write("sysTest.switchCollect.val=0") write('sysTest.auto_flag.txt="collect0"') end) -- 散热风机测试: elseif cmd == "switchCool" then log.info("~~~~~~~~~~switchCool",dat) sys.taskInit(function() log.info('<----- switchCool! ----->') control.cool(1) -- hmi.hmista.cool_fan = 1 -- log.warn("hmi.hmista.cool_fan:",hmi.hmista.cool_fan) sys.wait(10000) control.cool(0) write("sysTest.switchCool.val=0") write('sysTest.auto_flag.txt="cool0"') -- hmi.hmista.cool_fan = 0 -- log.warn("hmi.hmista.cool_fan:",hmi.hmista.cool_fan) -- write("sysTest.switchCool.val=0") -- write("temHum.cool_fan.val=0") end) --[[ -- 步进电机脱机测试: elseif cmd == "switchMoff" then log.info("~~~~~~~~~~switchMoff",dat) -- 1为使能;非脱机;0为脱机; if dat == 0 or dat == '0' then if haikang.up_step == 1 or haikang.down_step == 1 then haikang.stop_step = 1 end -- haikang.up_step = 0 -- haikang.down_step = 0 end haikang.motor_en(dat) -- control.ctrl_12v(dat) --]] -- 粘附电机测试: elseif cmd == "switchFsl" then log.info("~~~~~~~~~~switchFsl",dat) sys.taskInit(function() control.add_fsl(dat,5) sys.wait(10*1000) write('sysTest.switchFsl.val=0') end) -- 对焦拍照测试: elseif cmd == "switchAutopic" then log.info("~~~~~~~~~~switchAutopic",dat) if dat == 1 or dat == '1' then if auto_pic_flag == 1 then log.error("hmi auto_pic_flag = 1,auto_takepic is running!!!") return end sys.taskInit(function() -- 对焦拍照测试标志位置 1 auto_pic_flag = 1 log.info('<----- switchPic! ----->') -- haikang.hk_takephoto() audio.play('/ldata/takephoto.mp3') haikang.autotakepic2() -- 对焦拍照测试标志位 清零 auto_pic_flag = 0 write('sysTest.switchAutopic.val=0') write('sysTest.auto_flag.txt="auto_photo0"') end) end -- 立即拍照测试: elseif cmd == "switchPic" then log.info("~~~~~~~~~~switchPic",dat) if dat == 1 or dat == '1' then sys.taskInit(function() log.info('<----- switchPic! ----->') audio.play('/ldata/takephoto.mp3') -- haikang.hk_takephoto() haikang.test_takephoto() sys.wait(10000) -- haikang.photo_flag = 1 haikang.http_chose_photo_task() write('sysTest.switchPic.val=0') write('sysTest.auto_flag.txt="photo0"') end) end -- 步进电机上行测试: elseif cmd == "switchUp" then log.info("~~~~~~~~~~switchUp",dat) if dat == 1 or dat == '1' then sys.taskInit(haikang.motor_reset) elseif dat == 0 or dat == '0' then -- if haikang.up_step == 1 or haikang.down_step == 1 then if haikang.up_step_flag == 1 then haikang.stop_step = 1 end end -- 步进电机下行测试: elseif cmd == "switchDown" then log.info("~~~~~~~~~~switchDown",dat) if dat == 1 or dat == '1' then sys.taskInit(haikang.motor_down) elseif dat == 0 or dat == '0' then -- if haikang.up_step == 1 or haikang.down_step == 1 then if haikang.down_step_flag == 1 then haikang.stop_step = 1 end end -- 滴液测试: elseif cmd == "switchDrop" then log.info("~~~~~~~~~~switchDrop",dat) if dat == 1 or dat == '1' then sys.taskInit(control.drop) -- sys.taskInit(function() -- print("----------drop time is ...",t) -- for i=1,3,1 do -- print("----------droping...",i) -- pins.setup(pio.P1_1,1) -- sys.wait(5000) -- pins.setup(pio.P1_1,0) -- sys.wait(5000) -- end -- write("sysTest.switchDrop.val=0") -- end) end -- 补光灯测试: elseif cmd == "switchLed" then log.info("~~~~~~~~~~switchLed",dat) haikang.light_led(dat) -- sys.taskInit(haikang.http_chose_photo) -- haikang.http_chose_photo_task() -- control.ctrl_12v(dat) -- 加热测试: elseif cmd == "switchHeat" then log.info("~~~~~~~~~~switchHeat",dat) if dat == 1 or dat == '1' then sys.taskInit(function() log.info('<----- switchHeat! ----->') control.heat(1) sys.wait(20000) control.heat(0) write("sysTest.switchHeat.val=0") write('sysTest.auto_flag.txt="heat0"') end) end -- 制冷测试: elseif cmd == "switchCold" then log.info("~~~~~~~~~~switchCold",dat) if dat == 1 or dat == '1' then sys.taskInit(function() log.info('<----- switchCold! ----->') control.cold(1) sys.wait(20000) control.cold(0) write("sysTest.switchCold.val=0") write('sysTest.auto_flag.txt="cold0"') end) end end end function resetDefault() audio.play('/ldata/save.mp3') -- 恢复参数为默认值 hmiconf.passWord = 8888 --设置初始化密码 hmiconf.thsp = 300 --默认熄屏时间5分钟 hmiconf.vol = 7 -- 默认音量 hmiconf.datt = 20 -- 数据上传时间间隔 分钟 hmiconf.wind_sw = 0 hmiconf.coll_time = {"8-10","14-16"} hmiconf.cold_sw = 0 --制冷开关 hmiconf.set_temp = 25 -- 保温仓设定温度 hmiconf.imgres = 0 -- 分辨率 hmiconf.drop_time = 1 -- 滴液秒数 hmiconf.cul_time = 1 -- 设定培养时间 audio.setVolume(hmiconf.vol) -- 将参数保存 local str = json.encode(hmiconf) io.writefile(HMI_FILE, str) log.info("resetDefault is Action!", str) end -- 保存参数到flash中 sys.taskInit(function() local str = io.readfile(HMI_FILE) log.info("paramconf default value: ", str) if str then local cnf, result, err = json.decode(str) -- local str = json.encode(cnf) -- log.warn("read paramconf is:-----------------:",str) log.info("==========cnf",cnf) log.info("==========result",result) log.info("==========err",err) if result then hmiconf = cnf end log.info("==========hmi.hmiconf.wind_sw",hmi.hmiconf.wind_sw) log.info("==========hmi.hmiconf.coll_time",hmi.hmiconf.coll_time) log.info("==========hmi.hmiconf.set_temp",hmi.hmiconf.set_temp) log.info("==========hmi.hmiconf.imgres",hmi.hmiconf.imgres) log.info("==========hmi.hmiconf.datt",hmi.hmiconf.datt) log.info("==========hmi.hmiconf.drop_time",hmi.hmiconf.drop_time) log.info("==========hmi.hmiconf.cul_time",hmi.hmiconf.cul_time) end audio.setVolume(hmiconf.vol) -- audio.play_utf8_tts("欢迎使用物联网孢子捕捉仪") audio.play('/ldata/pwron.mp3') end) function remote_auto_pic() if auto_pic_flag == 1 then log.error("remote_auto_pic auto_pic_flag = 1,auto_takepic is running!!!") return end sys.taskInit(function() -- 对焦拍照测试标志位置 1 auto_pic_flag = 1 log.info('<----- switchPic! ----->') -- haikang.hk_takephoto() audio.play('/ldata/takephoto.mp3') haikang.autotakepic2() -- 对焦拍照测试标志位 清零 auto_pic_flag = 0 write('sysTest.switchAutopic.val=0') write('sysTest.auto_flag.txt="auto_photo0"') end) end