test.lua 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. local cacheTable = {}
  2. local rxCache = ""
  3. local vbackup = gpio.setup(24, 1)
  4. local gnssReset = gpio.setup(27, 1)
  5. local gnssEnvPower = gpio.setup(26, 1)
  6. local es8311Power = gpio.setup(25, 1)
  7. local gnssPower = gpio.setup(2, 1)
  8. local blueLed = gpio.setup(1, 0)
  9. local redLed = gpio.setup(16, 0, nil, nil, 4)
  10. local powerKeyTest = false
  11. pm.request(pm.IDLE)
  12. local transUartId = 1
  13. if not fskv.get("pcbaDone") then
  14. transUartId = 1
  15. else
  16. transUartId = uart.VUART_0
  17. end
  18. local gnssUartId = 2
  19. local gnssIsDownload = false
  20. local gnssIsTrans = false
  21. local gnssTransFlag = false
  22. uart.setup(transUartId, 115200)
  23. local Gsensori2cId = 1
  24. local da267Addr = 0x26
  25. local intPin = 39
  26. local ES8311i2cId = 0
  27. local function NMEA2UART1(id, len)
  28. local result
  29. while 1 do
  30. local data = uart.read(gnssUartId, len)
  31. if not data or #data == 0 then
  32. break
  33. end
  34. if gnssTransFlag then
  35. table.insert(cacheTable, data)
  36. sys.publish("UART1_SEND")
  37. end
  38. end
  39. end
  40. -- amr数据存放buffer,尽可能地给大一些
  41. local amr_buff = zbuff.create(20 * 1024)
  42. -- 创建一个amr的encoder
  43. local encoder = nil
  44. audio.on(0, function(id, event, buff)
  45. log.info("audio.on", id, event)
  46. -- 使用play来播放文件时只有播放完成回调
  47. if event == audio.RECORD_DATA then -- 录音数据
  48. codec.encode(encoder, buff, amr_buff)
  49. elseif event == audio.RECORD_DONE then -- 录音完成
  50. sys.publish("AUDIO_RECORD_DONE")
  51. else
  52. local succ, stop, file_cnt = audio.getError(0)
  53. if not succ then
  54. if stop then
  55. log.info("用户停止播放")
  56. else
  57. log.info("第", file_cnt, "个文件解码失败")
  58. end
  59. end
  60. log.info("播放完成一个音频")
  61. sys.publish("AUDIO_PLAY_DONE")
  62. end
  63. end)
  64. local recordPath = "/record.amr"
  65. sys.taskInit(function()
  66. mcu.altfun(mcu.I2C, Gsensori2cId, 23, 2, 0)
  67. mcu.altfun(mcu.I2C, Gsensori2cId, 24, 2, 0)
  68. mcu.altfun(mcu.I2C, ES8311i2cId, 13, 2, 0)
  69. mcu.altfun(mcu.I2C, ES8311i2cId, 14, 2, 0)
  70. local multimedia_id = 0
  71. local i2s_id = 0
  72. local i2s_mode = 0
  73. local i2s_sample_rate = 16000
  74. local i2s_bits_per_sample = 16
  75. local i2s_channel_format = i2s.MONO_R
  76. local i2s_communication_format = i2s.MODE_LSB
  77. local i2s_channel_bits = 16
  78. local pa_pin = 23
  79. local pa_on_level = 1
  80. local pa_delay = 100
  81. local power_pin = 255
  82. local power_on_level = 1
  83. local power_delay = 3
  84. local power_time_delay = 100
  85. local voice_vol = 100
  86. local mic_vol = 80
  87. local find_es8311 = false
  88. i2c.setup(Gsensori2cId, i2c.SLOW)
  89. i2c.setup(ES8311i2cId, i2c.FAST)
  90. if i2c.send(ES8311i2cId, 0x18, 0xfd) == true then
  91. find_es8311 = true
  92. end
  93. log.info("find_es8311?", find_es8311)
  94. i2s.setup(i2s_id, i2s_mode, i2s_sample_rate, i2s_bits_per_sample, i2s_channel_format, i2s_communication_format, i2s_channel_bits)
  95. audio.config(multimedia_id, pa_pin, pa_on_level, power_delay, pa_delay, power_pin, power_on_level, power_time_delay)
  96. audio.setBus(multimedia_id, audio.BUS_I2S, {
  97. chip = "es8311",
  98. i2cid = ES8311i2cId,
  99. i2sid = i2s_id,
  100. voltage = audio.VOLTAGE_1800
  101. }) -- 通道0的硬件输出通道设置为I2S
  102. audio.vol(multimedia_id, 80)
  103. audio.micVol(multimedia_id, 80)
  104. while true do
  105. local result, param1, param2, param3 = sys.waitUntil("CONTROL")
  106. log.info("CONTROL", param1, param2, param3)
  107. if param1 == "GNSS" then
  108. gnssReset(0)
  109. sys.wait(10)
  110. gnssReset(1)
  111. elseif param1 == "GSENSOR" then
  112. i2c.send(Gsensori2cId, da267Addr, 0x01, 1)
  113. local data = i2c.recv(Gsensori2cId, da267Addr, 1)
  114. if not data or data == "" or string.byte(data) ~= 0x13 then
  115. table.insert(cacheTable, "ERROR#")
  116. else
  117. table.insert(cacheTable, "OK#")
  118. end
  119. sys.publish("UART1_SEND")
  120. elseif param1 == "RECORD" then
  121. local err = audio.record(0, audio.AMR, 5, 7, recordPath)
  122. result = sys.waitUntil("AUDIO_RECORD_DONE", 10000)
  123. if result then
  124. table.insert(cacheTable, "OK#")
  125. else
  126. table.insert(cacheTable, "ERROR#")
  127. end
  128. sys.publish("UART1_SEND")
  129. elseif param1 == "PLAY" then
  130. local err = audio.play(0, recordPath)
  131. result = sys.waitUntil("AUDIO_PLAY_DONE", 10000)
  132. if result then
  133. table.insert(cacheTable, "OK#")
  134. else
  135. table.insert(cacheTable, "ERROR#")
  136. end
  137. sys.publish("UART1_SEND")
  138. end
  139. end
  140. end)
  141. local function powerOff()
  142. pm.shutdown()
  143. end
  144. local function powerKeyCb()
  145. if gpio.get(46) == 1 then
  146. if powerKeyTest then
  147. table.insert(cacheTable, "POWERKEY_RELEASE#")
  148. end
  149. if sys.timerIsActive(powerOff) then
  150. sys.timerStop(powerOff)
  151. end
  152. else
  153. if powerKeyTest then
  154. table.insert(cacheTable, "POWERKEY_PRESS#")
  155. end
  156. sys.timerStart(powerOff, 3000)
  157. end
  158. if powerKeyTest then
  159. sys.publish("UART1_SEND")
  160. end
  161. end
  162. gpio.debounce(46, 100)
  163. gpio.setup(46, powerKeyCb, gpio.PULLUP, gpio.BOTH)
  164. local function proc(data)
  165. local item = nil
  166. local find = true
  167. local needNowReply = true
  168. local h = string.find(data, "#")
  169. if h then
  170. local cmd = string.sub(data, 1, h - 1)
  171. if string.find(cmd, "VERSION") then
  172. item = PROJECT .. "_" .. VERSION
  173. elseif string.find(cmd, "LED") then
  174. local onoff = string.match(cmd, "LED,(%d)")
  175. item = "OK"
  176. if onoff == "0" then
  177. blueLed(0)
  178. redLed(0)
  179. elseif onoff == "1" then
  180. blueLed(1)
  181. redLed(1)
  182. else
  183. item = "ERROR"
  184. end
  185. elseif string.find(cmd, "IMEI") then
  186. item = mobile.imei()
  187. elseif string.find(cmd, "IMSI") then
  188. item = mobile.imsi()
  189. elseif string.find(cmd, "ICCID") then
  190. item = mobile.iccid()
  191. elseif string.find(cmd, "CSQ") then
  192. item = mobile.csq()
  193. elseif string.find(cmd, "MUID") then
  194. item = mobile.muid()
  195. elseif string.find(cmd, "GPSTEST") then
  196. local onoff = string.match(cmd, "GPSTEST,(%d)")
  197. log.info("flag", onoff)
  198. item = "OK"
  199. if onoff == "0" then
  200. gnssTransFlag = false
  201. uart.close(gnssUartId)
  202. elseif onoff == "1" then
  203. gnssTransFlag = true
  204. uart.on(gnssUartId, "receive", NMEA2UART1)
  205. uart.setup(gnssUartId, 115200)
  206. else
  207. item = "ERROR"
  208. end
  209. elseif string.find(cmd, "GPSDOWNLOAD") then
  210. local onoff = string.match(cmd, "GPSDOWNLOAD,(%d)")
  211. item = "OK"
  212. if onoff == "0" then
  213. gpio.close(12)
  214. gpio.close(13)
  215. elseif onoff == "1" then
  216. gpio.setup(12)
  217. gpio.setup(13)
  218. sys.publish("CONTROL", "GNSS")
  219. else
  220. item = "ERROR"
  221. end
  222. elseif string.find(cmd, "GS_STATE") then
  223. sys.publish("CONTROL", "GSENSOR")
  224. needNowReply = false
  225. elseif string.find(cmd, "RECORD") then
  226. sys.publish("CONTROL", "RECORD")
  227. needNowReply = false
  228. elseif string.find(cmd, "PLAY") then
  229. sys.publish("CONTROL", "PLAY")
  230. needNowReply = false
  231. elseif string.find(cmd, "POWERKEY") then
  232. local onoff = string.match(cmd, "POWERKEY,(%d)")
  233. item = "OK"
  234. if onoff == "0" then
  235. powerKeyTest = false
  236. elseif onoff == "1" then
  237. powerKeyTest = true
  238. else
  239. item = "ERROR"
  240. end
  241. elseif string.find(cmd, "ECNPICFG") then
  242. mobile.nstOnOff(true, transUartId)
  243. mobile.nstInput("AT+ECNPICFG?\r\n")
  244. mobile.nstInput(nil)
  245. mobile.nstOnOff(false, transUartId)
  246. needNowReply = false
  247. elseif string.find(cmd, "VBAT") then
  248. adc.open(adc.CH_VBAT)
  249. local vbat = adc.get(adc.CH_VBAT)
  250. adc.close(adc.CH_VBAT)
  251. item = tostring(vbat)
  252. elseif string.find(cmd, "PCBA_TEST_DONE") then
  253. item = "OK"
  254. fskv.set("pcbaDone", true)
  255. elseif string.find(cmd, "TEST_DONE") then
  256. item = "OK"
  257. fskv.set("allDone", true)
  258. sys.timerStart(pm.reboot, 3000)
  259. else
  260. find = false
  261. item = "ERROR"
  262. end
  263. if find then
  264. if not item then
  265. item = " "
  266. end
  267. end
  268. if needNowReply then
  269. item = item .. "#"
  270. table.insert(cacheTable, item)
  271. sys.publish("UART1_SEND")
  272. end
  273. return true, data:sub(h + 1)
  274. else
  275. return false, data
  276. end
  277. end
  278. uart.on(transUartId, "receive", function(id, len)
  279. local result
  280. while 1 do
  281. local data = uart.read(transUartId, 512)
  282. if not data or #data == 0 then
  283. break
  284. end
  285. rxCache = rxCache .. data
  286. while true do
  287. result, rxCache = proc(rxCache)
  288. if not result then
  289. break
  290. end
  291. end
  292. end
  293. end)
  294. uart.on(transUartId, "sent", function()
  295. sys.publish("SEND_DOWN")
  296. end)
  297. sys.taskInit(function()
  298. while true do
  299. while #cacheTable > 0 do
  300. uart.write(transUartId, table.remove(cacheTable, 1))
  301. sys.waitUntil("SEND_DOWN")
  302. end
  303. sys.waitUntil("UART1_SEND", 1000)
  304. end
  305. end)
  306. sys.taskInit(function()
  307. sys.wait(1000)
  308. while true do
  309. mobile.reqCellInfo(15)
  310. sys.waitUntil("CELL_INFO_UPDATE", 15000)
  311. sys.wait(1000)
  312. end
  313. end)