main.lua 15 KB

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