main.lua 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. PROJECT = "startupv13"
  2. VERSION = "1.0.0"
  3. PRODUCT_KEY = "29uptfBkJMwFC7x7QeW10UPO3LecPYFu"
  4. log.info("main", PROJECT, VERSION)
  5. -- sys库是标配
  6. sys = require("sys")
  7. local airlcd = require "airlcd"
  8. local airgps = require "airgps"
  9. local airsta = require "airsta"
  10. local airmusic = require "airmusic"
  11. local airap = require "airap"
  12. local airtts = require "airtts"
  13. local airlan = require "airlan"
  14. local airwan = require "airwan"
  15. local airsms = require "airsms"
  16. local aircall = require "aircall"
  17. local airgsensor = require "airgsensor"
  18. local airpower = require "airpower"
  19. local airtf = require "airtf"
  20. local airaudio = require "airaudio"
  21. local aircamera = require "aircamera"
  22. local airrus = require "russia"
  23. local airstatus = require "statusbar"
  24. local airble = require "airble"
  25. local airtestwlan = require "test_wlan"
  26. local airbuzzer = require "airbuzzer"
  27. local multi_network = require "multi_network"
  28. local talk = require "talk"
  29. local airrecord = require "airrecord"
  30. local taskName = "MAIN"
  31. local fota_wifi = require("fota_wifi")
  32. local sid = 0
  33. libfota2 = require "libfota2"
  34. -- 按键事件类型:
  35. -- "switch": 短按开机键切换菜单;
  36. -- "enter": 长按开机键进入功能菜单或者退出功能菜单
  37. -- 当前功能界面
  38. -- "main": 九宫格主界面
  39. -----------页面1------------------
  40. -- "gps": gps定位
  41. -- "wifiap": wifiap,可实现4g转wifi 用作热点传出
  42. -- "wifista": 链接wifi路由器
  43. -- "camera": 摄像头预览
  44. -- "call": 拨打电话
  45. -- "bt": 蓝牙
  46. -- "sms": 短信
  47. -- "tts": 文字转语音
  48. ------------页面2------------------
  49. -- "record": 录音
  50. -- "music": 播放音乐
  51. -- "tf": 驱动TF卡
  52. -- "gsensor": 运动传感器
  53. -- "pm": 电源管理
  54. -- "lan": 以太网lan 通讯,通过air8000 给以太网设备上网
  55. -- "wan": 以太网wan 通讯,通过以太网给air8000 上网
  56. ---------页面3------------------
  57. -- "multi_network": 多网融合演示
  58. -- "485": 485 通讯
  59. -- "can": can 通讯
  60. -- "onewire": onewire 通讯
  61. -- "pwm": 输出PWM 波形
  62. -- "uart": 串口输出
  63. -- "232": 232
  64. local cur_fun = "main"
  65. -- local cur_sel = 0
  66. -- local funlist = {
  67. -- "gps", "wifiap","wifista","camera", "call","bt","sms","tts",
  68. -- "record","music","tf","gsensor","pm","lan","wan",
  69. -- "multi_network","485","can","onewire","pwm","uart","232"
  70. -- }
  71. _G.bkcolor = lcd.rgb565(99, 180, 245,false)
  72. fskv.init()
  73. local function wdtInit()
  74. -- 添加硬狗防止程序卡死
  75. if wdt then
  76. wdt.init(9000) -- 初始化watchdog设置为9s
  77. sys.timerLoopStart(wdt.feed, 3000) -- 3s喂一次狗
  78. end
  79. end
  80. local function update()
  81. if cur_fun == "russia" then
  82. airrus.updaterus()
  83. end
  84. if airstatus.data.bat_level == 0 then
  85. airstatus.get_bat_level()
  86. end
  87. end
  88. local lock_push = 1
  89. local function main_local(x,y)
  90. if x > 0 and x < 100 and y > 64 and y < 192 then
  91. return 1
  92. elseif x > 100 and x < 200 and y > 64 and y < 192 then
  93. return 2
  94. elseif x > 200 and x < 300 and y > 64 and y < 192 then
  95. return 3
  96. elseif x > 0 and x < 100 and y > 192 and y < 320 then
  97. return 4
  98. elseif x > 100 and x < 200 and y > 192 and y < 320 then
  99. return 5
  100. elseif x > 200 and x < 300 and y > 192 and y < 320 then
  101. return 6
  102. elseif x > 0 and x < 100 and y > 320 and y < 448 then
  103. return 7
  104. elseif x > 100 and x < 200 and y > 320 and y < 448 then
  105. return 8
  106. elseif x > 200 and x < 300 and y > 320 and y < 448 then
  107. return 9
  108. end
  109. end
  110. local function home_first_page(x,y)
  111. key = main_local(x,y)
  112. log.info("tp_handal key",key)
  113. if key == 1 then
  114. cur_fun = "airgps"
  115. elseif key == 2 then
  116. cur_fun = "airap"
  117. elseif key == 3 then
  118. cur_fun = "airsta"
  119. elseif key == 4 then
  120. cur_fun = "aircamera"
  121. elseif key == 5 then
  122. cur_fun = "aircall"
  123. elseif key == 6 then
  124. cur_fun = "airble"
  125. elseif key == 7 then
  126. cur_fun = "airsms"
  127. elseif key == 8 then -- tts
  128. cur_fun = "airtts"
  129. elseif key == 9 then
  130. cur_fun = "main1"
  131. end
  132. end
  133. local function home_second_page(x,y)
  134. key = main_local(x,y)
  135. log.info("tp_handal key",key)
  136. if key == 1 then
  137. cur_fun = "airrecord"
  138. elseif key == 2 then
  139. elseif key == 3 then
  140. cur_fun = "airtf"
  141. elseif key == 4 then
  142. cur_fun = "airgsensor"
  143. elseif key == 5 then
  144. cur_fun = "airpower"
  145. elseif key == 6 then
  146. cur_fun = "airlan"
  147. elseif key == 7 then
  148. cur_fun = "main"
  149. elseif key == 8 then
  150. cur_fun = "airwan"
  151. elseif key == 9 then
  152. cur_fun = "main2"
  153. end
  154. end
  155. local function home_third_page(x,y)
  156. key = main_local(x,y)
  157. log.info("tp_handal key",key)
  158. if key == 1 then
  159. cur_fun = "multi_network"
  160. elseif key == 2 then
  161. elseif key == 3 then
  162. elseif key == 4 then
  163. elseif key == 5 then
  164. elseif key == 6 then
  165. elseif key == 7 then
  166. cur_fun = "main1"
  167. elseif key == 8 then
  168. elseif key == 9 then
  169. cur_fun = "main3"
  170. end
  171. end
  172. local function home_fourth_page(x,y)
  173. key = main_local(x,y)
  174. log.info("tp_handal key",key)
  175. if key == 1 then
  176. cur_fun = "talk"
  177. elseif key == 2 then
  178. elseif key == 3 then
  179. elseif key == 4 then
  180. elseif key == 5 then
  181. elseif key == 6 then
  182. elseif key == 7 then
  183. cur_fun = "main2"
  184. elseif key == 8 then
  185. elseif key == 9 then
  186. cur_fun = "main"
  187. end
  188. end
  189. local function tp_handal(tp_device,tp_data)
  190. -- log.info("tp_handal",tp_data[1].x,tp_data[1].y,tp_data[1].event)
  191. if tp_data[1].event == 1 then
  192. lock_push = 0
  193. end
  194. if tp_data[1].event == 2 and lock_push == 0 then
  195. if cur_fun == "main" then
  196. home_first_page(tp_data[1].x,tp_data[1].y)
  197. elseif cur_fun == "main1" then
  198. home_second_page(tp_data[1].x,tp_data[1].y)
  199. elseif cur_fun == "main2" then
  200. home_third_page(tp_data[1].x,tp_data[1].y)
  201. elseif cur_fun == "main3" then
  202. home_fourth_page(tp_data[1].x,tp_data[1].y)
  203. elseif cur_fun == "airtts" then
  204. airtts.tp_handal(tp_data[1].x,tp_data[1].y,tp_data[1].event)
  205. elseif cur_fun == "aircamera" then
  206. aircamera.tp_handal(tp_data[1].x,tp_data[1].y,tp_data[1].event)
  207. elseif cur_fun == "airgps" then
  208. airgps.tp_handal(tp_data[1].x,tp_data[1].y,tp_data[1].event)
  209. elseif cur_fun == "airap" then
  210. airap.tp_handal(tp_data[1].x,tp_data[1].y,tp_data[1].event)
  211. elseif cur_fun == "airsta" then
  212. airsta.tp_handal(tp_data[1].x,tp_data[1].y,tp_data[1].event)
  213. elseif cur_fun == "multi_network" then
  214. multi_network.tp_handal(tp_data[1].x,tp_data[1].y,tp_data[1].event)
  215. elseif cur_fun == "airwan" then
  216. airwan.tp_handal(tp_data[1].x,tp_data[1].y,tp_data[1].event)
  217. elseif cur_fun == "airlan" then
  218. airlan.tp_handal(tp_data[1].x,tp_data[1].y,tp_data[1].event)
  219. elseif cur_fun == "airsms" then
  220. airsms.tp_handal(tp_data[1].x,tp_data[1].y,tp_data[1].event)
  221. elseif cur_fun == "aircall" then
  222. aircall.tp_handal(tp_data[1].x,tp_data[1].y,tp_data[1].event)
  223. elseif cur_fun == "airtf" then
  224. airtf.tp_handal(tp_data[1].x,tp_data[1].y,tp_data[1].event)
  225. elseif cur_fun == "airgsensor" then
  226. airgsensor.tp_handal(tp_data[1].x,tp_data[1].y,tp_data[1].event)
  227. elseif cur_fun == "airpower" then
  228. airpower.tp_handal(tp_data[1].x,tp_data[1].y,tp_data[1].event)
  229. elseif cur_fun == "airble" then
  230. airble.tp_handal(tp_data[1].x,tp_data[1].y,tp_data[1].event)
  231. elseif cur_fun == "talk" then
  232. talk.tp_handal(tp_data[1].x,tp_data[1].y,tp_data[1].event)
  233. elseif cur_fun == "airrecord" then
  234. airrecord.tp_handal(tp_data[1].x,tp_data[1].y,tp_data[1].event)
  235. end
  236. lock_push = 1
  237. end
  238. end
  239. -- 画状态栏
  240. local function draw_statusbar()
  241. lcd.showImage(0, 0, "/luadb/signal" .. airstatus.data.sig_stren .. ".jpg")
  242. lcd.showImage(64, 0, "/luadb/power" .. airstatus.data.bat_level .. ".jpg")
  243. lcd.showImage(128, 0, "/luadb/temp.jpg")
  244. lcd.showImage(192, 0, "/luadb/humidity.jpg")
  245. lcd.showImage(0, 448, "/luadb/Lbottom.jpg")
  246. if airstatus.data.weather.result then
  247. lcd.setFont(lcd.font_opposansm32_chinese)
  248. lcd.drawStr(172, 30, tostring(airstatus.data.weather.temp))
  249. lcd.drawStr(236, 30, tostring(airstatus.data.weather.humidity))
  250. lcd.showImage(256, 0, airstatus.data.weather.text)
  251. else
  252. lcd.showImage(256, 0, "/luadb/default.jpg")
  253. end
  254. end
  255. --画九宫格界面
  256. local function draw_main()
  257. local i = 0
  258. local j = 0
  259. local x,y
  260. local fname
  261. local sel = 0
  262. for i = 1,3 do
  263. for j = 1,3 do
  264. x = 64 + (i-1)*128 + 24
  265. y = (j-1)*106 + 13
  266. sel = j+(i-1)*3
  267. fname = "/luadb/" .. "A" .. sel .. ".jpg"
  268. -- log.info("fname:", fname, x,y,sel,cur_sel)
  269. lcd.showImage(y,x,fname)
  270. end
  271. end
  272. end
  273. local function draw_main1()
  274. local i = 0
  275. local j = 0
  276. local x,y
  277. local fname
  278. local sel = 0
  279. for i = 1,3 do
  280. for j = 1,3 do
  281. x = 64 + (i-1)*128 + 24
  282. y = (j-1)*106 + 13
  283. sel = j+(i-1)*3
  284. fname = "/luadb/" .. "B" .. sel .. ".jpg"
  285. -- log.info("fname:", fname, x,y,sel,cur_sel)
  286. lcd.showImage(y,x,fname)
  287. end
  288. end
  289. end
  290. local function draw_main2()
  291. local i = 0
  292. local j = 0
  293. local x,y
  294. local fname
  295. local sel = 0
  296. for i = 1,3 do
  297. for j = 1,3 do
  298. x = 64 + (i-1)*128 + 24
  299. y = (j-1)*106 + 13
  300. sel = j+(i-1)*3
  301. fname = "/luadb/" .. "C" .. sel .. ".jpg"
  302. -- log.info("fname:", fname, x,y,sel,cur_sel)
  303. lcd.showImage(y,x,fname)
  304. end
  305. end
  306. end
  307. local function draw_main3()
  308. local i = 0
  309. local j = 0
  310. local x,y
  311. local fname
  312. local sel = 0
  313. for i = 1,3 do
  314. for j = 1,3 do
  315. x = 64 + (i-1)*128 + 24
  316. y = (j-1)*106 + 13
  317. sel = j+(i-1)*3
  318. fname = "/luadb/" .. "D" .. sel .. ".jpg"
  319. -- log.info("fname:", fname, x,y,sel,cur_sel)
  320. lcd.showImage(y,x,fname)
  321. end
  322. end
  323. end
  324. local function draw_pic()
  325. lcd.showImage(0,64,"/luadb/P1.jpg")
  326. end
  327. local function draw_tts()
  328. if airtts.run() then
  329. cur_fun = "main"
  330. end
  331. end
  332. local function draw_camera()
  333. if aircamera.run() then
  334. cur_fun = "main"
  335. end
  336. end
  337. local function draw_gps()
  338. aircamera.close()
  339. if airgps.run() then
  340. cur_fun = "main"
  341. end
  342. end
  343. local function draw_ap()
  344. if airap.run() then
  345. cur_fun = "main"
  346. end
  347. end
  348. local function draw_sta()
  349. if airsta.run() then
  350. cur_fun = "main"
  351. end
  352. end
  353. local function draw_wan()
  354. if airwan.run() then
  355. cur_fun = "main"
  356. end
  357. end
  358. local function draw_lan()
  359. if airlan.run() then
  360. cur_fun = "main"
  361. end
  362. end
  363. local function draw_sms()
  364. if airsms.run() then
  365. cur_fun = "main"
  366. end
  367. end
  368. local function draw_call()
  369. if aircall.run() then
  370. cur_fun = "main"
  371. end
  372. end
  373. local function draw_tf()
  374. if airtf.run() then
  375. cur_fun = "main1"
  376. end
  377. end
  378. local function draw_gsensor()
  379. if airgsensor.run() then
  380. cur_fun = "main1"
  381. end
  382. end
  383. local function draw_power()
  384. if airpower.run() then
  385. cur_fun = "main1"
  386. end
  387. end
  388. local function draw_multi_network()
  389. if multi_network.run() then
  390. cur_fun = "main"
  391. end
  392. end
  393. local function draw_airble()
  394. if airble.run() then
  395. cur_fun = "main"
  396. end
  397. end
  398. local function draw_talk()
  399. if talk.run() then
  400. cur_fun = "main"
  401. end
  402. end
  403. local function draw_airrecord()
  404. if airrecord.run() then
  405. cur_fun = "main"
  406. end
  407. end
  408. local function draw()
  409. if cur_fun == "camshow" then
  410. return
  411. end
  412. lcd.clear(_G.bkcolor)
  413. draw_statusbar()
  414. if cur_fun == "main" then
  415. draw_main()
  416. elseif cur_fun == "main1" then
  417. draw_main1()
  418. elseif cur_fun == "main2" then
  419. draw_main2()
  420. elseif cur_fun == "main3" then
  421. draw_main3()
  422. elseif cur_fun == "airtts" then
  423. draw_tts()
  424. elseif cur_fun == "aircamera" then
  425. draw_camera()
  426. elseif cur_fun == "airgps" then
  427. draw_gps()
  428. elseif cur_fun == "airap" then
  429. draw_ap()
  430. elseif cur_fun == "airsta" then
  431. draw_sta()
  432. elseif cur_fun == "airwan" then
  433. draw_wan()
  434. elseif cur_fun == "airlan" then
  435. draw_lan()
  436. elseif cur_fun == "airsms" then
  437. draw_sms()
  438. elseif cur_fun == "aircall" then
  439. draw_call()
  440. elseif cur_fun == "airtf" then
  441. draw_tf()
  442. elseif cur_fun == "airgsensor" then
  443. draw_gsensor()
  444. elseif cur_fun == "airpower" then
  445. draw_power()
  446. elseif cur_fun == "multi_network" then
  447. draw_multi_network()
  448. elseif cur_fun == "airble" then
  449. draw_airble()
  450. elseif cur_fun == "talk" then
  451. draw_talk()
  452. elseif cur_fun == "airrecord" then
  453. draw_airrecord()
  454. end
  455. lcd.showImage(0,448,"/luadb/Lbottom.jpg")
  456. lcd.flush()
  457. end
  458. local function update_airstatus()
  459. airstatus.get_sig_strength()
  460. airstatus.get_weather()
  461. end
  462. wdtInit()
  463. function ip_ready_handle(ip, adapter)
  464. log.info("ip_ready_handle",ip, adapter)
  465. if adapter == socket.LWIP_GP then
  466. sysplus.taskInitEx(update_airstatus, "update_airstatus")
  467. end
  468. end
  469. local function UITask()
  470. sys.wait(1000)
  471. log.info("合宙 8000 startup v11111")
  472. -- aircamera.init()
  473. airlcd.lcd_init("AirLCD_1001")
  474. sys.subscribe("TP",tp_handal)
  475. while 1 do
  476. update()
  477. draw()
  478. if ((sid % 10) == 0) then
  479. log.info("sid: ", sid, cur_fun)
  480. end
  481. sid = sid + 1
  482. sys.wait(10)
  483. end
  484. end
  485. local function wififota()
  486. fota_wifi.request()
  487. end
  488. local function fota_cb(ret)
  489. log.info("fota", ret)
  490. if ret == 0 then
  491. log.info("升级包下载成功,重启模块")
  492. rtos.reboot()
  493. elseif ret == 1 then
  494. log.info("连接失败", "请检查url拼写或服务器配置(是否为内网)")
  495. elseif ret == 2 then
  496. log.info("url错误", "检查url拼写")
  497. elseif ret == 3 then
  498. log.info("服务器断开", "检查服务器白名单配置")
  499. elseif ret == 4 then
  500. log.info("接收报文错误", "检查模块固件或升级包内文件是否正常")
  501. elseif ret == 5 then
  502. log.info("版本号书写错误", "iot平台版本号需要使用xxx.yyy.zzz形式")
  503. else
  504. log.info("不是上面几种情况 ret为", ret)
  505. end
  506. end
  507. local ota_opts = {}
  508. sys.taskInit(function()
  509. -- 等待网络就行后开始检查升级
  510. sys.waitUntil("IP_READY")
  511. log.info("开始检查升级")
  512. sys.wait(500)
  513. libfota2.request(fota_cb, ota_opts)
  514. end)
  515. local function hardware_start ()
  516. gpio.setup(140, 1, gpio.PULLUP) -- 打开CH390 电源
  517. sys.wait(50) -- 等待电源稳定
  518. gpio.setup(12,1)--上拉ch390使用spi的cs引脚避免干扰
  519. sys.wait(10)--等待稳定
  520. gpio.setup(20, 1) -- 上拉tf 卡 cs 管脚
  521. end
  522. -- 演示定时自动升级, 每隔4小时自动检查一次
  523. sys.timerLoopStart(libfota2.request, 4 * 3600000, fota_cb, ota_opts)
  524. sysplus.taskInitEx(hardware_start,"hardware_start")
  525. sysplus.taskInitEx(wififota,"fota_wifi")
  526. sys.subscribe("IP_READY", ip_ready_handle)
  527. sysplus.taskInitEx(UITask, taskName)
  528. -- 当前是给camera 表的变量赋值让camera 退出。 未来可以让 UI task 发消息给camera 任务,让camera 任务关闭摄像头,释放LCD
  529. -- 用户代码已结束---------------------------------------------
  530. -- 结尾总是这一句
  531. sys.run()
  532. -- sys.run()之后后面不要加任何语句!!!!!