test.lua 9.3 KB

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