ccVolte.lua 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. local pcmFile = "/luadb/10.pcm"
  2. local amrFile = "/luadb/10.amr"
  3. local ringTimer
  4. local hangUpCb = function()
  5. cc.hangUp(0)
  6. end
  7. local acceptCb = function()
  8. if ringTimer then
  9. sys.timerStop(ringTimer)
  10. ringTimer = nil
  11. end
  12. if not audio.isEnd(0) then
  13. log.info("手动关闭")
  14. audio.playStop(0)
  15. end
  16. local result = cc.accept(0)
  17. log.info("cc accept", result)
  18. sys.subscribe("POWERKEY_PRESSED", hangUpCb)
  19. end
  20. local cnt = 0
  21. sys.subscribe("CC_IND", function(state)
  22. log.info("CC_IND", state)
  23. if state == "READY" then
  24. sys.publish("CC_READY")
  25. elseif state == "INCOMINGCALL" then
  26. cnt = cnt + 1
  27. if cnt > 1 then
  28. if ringTimer then return end
  29. sys.subscribe("POWERKEY_PRESSED", acceptCb)
  30. attributes.set("callStatus", "通话中")
  31. attributes.set("audioStatus", "通话中")
  32. if ringTimer then
  33. sys.timerStop(ringTimer)
  34. ringTimer = nil
  35. end
  36. if not audio.isEnd(0) then
  37. log.info("手动关闭")
  38. audio.playStop(0)
  39. end
  40. local buff = io.readFile(pcmFile)
  41. audio.start(0, audio.PCM, 1, 8000, 16)
  42. audio.write(0, buff)
  43. ringTimer = sys.timerLoopStart(function()
  44. if not audio.isEnd(0) then
  45. log.info("手动关闭")
  46. audio.playStop(0)
  47. end
  48. audio.write(0, buff)
  49. end, 400)
  50. end
  51. elseif state == "HANGUP_CALL_DONE" or state == "MAKE_CALL_FAILED" or state == "DISCONNECTED" then
  52. pcall(sys.unsubscribe, "POWERKEY_PRESSED", acceptCb)
  53. pcall(sys.unsubscribe, "POWERKEY_PRESSED", hangUpCb)
  54. if ringTimer then
  55. sys.timerStop(ringTimer)
  56. ringTimer = nil
  57. end
  58. attributes.set("callStatus", "已就绪")
  59. attributes.set("audioStatus", "空闲")
  60. sys.publish("CC_DONE")
  61. audio.pm(0, audio.STANDBY)
  62. -- audio.pm(0,audio.SHUTDOWN) --低功耗可以选择SHUTDOWN或者POWEROFF,如果codec无法断电用SHUTDOWN
  63. end
  64. end)
  65. local ccReady = false
  66. local es8311Power = gpio.setup(25, 1)
  67. sys.taskInit(function()
  68. local multimedia_id = 0
  69. local i2c_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 = 70
  85. local mic_vol = 90
  86. local find_es8311 = false
  87. mcu.altfun(mcu.I2C, i2c_id, 13, 2, 0)
  88. mcu.altfun(mcu.I2C, i2c_id, 14, 2, 0)
  89. i2c.setup(i2c_id, i2c.FAST)
  90. sys.wait(10)
  91. if i2c.send(i2c_id, 0x18, 0xfd) == true then
  92. find_es8311 = true
  93. end
  94. if not find_es8311 then
  95. while true do
  96. log.info("not find es8311")
  97. sys.wait(1000)
  98. end
  99. end
  100. i2s.setup(i2s_id, i2s_mode, i2s_sample_rate, i2s_bits_per_sample, i2s_channel_format, i2s_communication_format, i2s_channel_bits)
  101. audio.config(multimedia_id, pa_pin, pa_on_level, power_delay, pa_delay, power_pin, power_on_level, power_time_delay)
  102. audio.setBus(multimedia_id, audio.BUS_I2S, {
  103. chip = "es8311",
  104. i2cid = i2c_id,
  105. i2sid = i2s_id,
  106. voltage = audio.VOLTAGE_1800
  107. }) -- 通道0的硬件输出通道设置为I2S
  108. audio.vol(multimedia_id, voice_vol)
  109. audio.micVol(multimedia_id, mic_vol)
  110. cc.init(multimedia_id)
  111. audio.pm(0, audio.STANDBY)
  112. sys.publish("AUDIO_SETUP_DONE")--音频初始化完毕了
  113. sys.waitUntil("CC_READY")
  114. ccReady = true
  115. attributes.set("callStatus", "已就绪")
  116. sys.wait(100)
  117. end)
  118. sys.taskInit(function()
  119. sys.waitUntil("AUDIO_SETUP_DONE")
  120. log.info("audio", "ready")
  121. while true do
  122. local _,cmd,param = sys.waitUntil("AUDIO_CMD_RECEIVED")
  123. log.info("audio", cmd, param)
  124. --低功耗模式就别放了
  125. if attributes.get("sleepMode") then
  126. log.info("audio", "sleepMode cancel play")
  127. elseif cmd == "call" then
  128. if ccReady then
  129. cc.dial(0,param) --拨打电话
  130. attributes.set("callStatus", "通话中")
  131. attributes.set("audioStatus", "通话中")
  132. sys.subscribe("POWERKEY_PRESSED", hangUpCb)
  133. sys.waitUntil("CC_DONE")
  134. else
  135. log.info("audio", "cc not ready")
  136. end
  137. elseif cmd == "music" then
  138. local result = audio.play(0, amrFile)
  139. attributes.set("audioStatus", "播放中")
  140. log.info("audio", "play music",result)
  141. sys.wait(1000)
  142. if not audio.isEnd(0) then
  143. log.info("手动关闭")
  144. audio.playStop(0)
  145. end
  146. audio.pm(0, audio.STANDBY)
  147. attributes.set("audioStatus", "空闲")
  148. end
  149. end
  150. end)
  151. audio.on(0, function(audio_id, msg)
  152. log.info("msg", audio_id, msg)
  153. end)