test.lua 9.2 KB

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