netLed.lua 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. --- 模块功能:网络指示灯模块
  2. -- @module netLed
  3. -- @author openLuat
  4. -- @license MIT
  5. -- @copyright openLuat
  6. -- @release 2018.03.14
  7. module(..., package.seeall)
  8. require "pins"
  9. require "sim"
  10. --SIM卡状态:true为异常,false或者nil为正常
  11. local simError
  12. --是否处于飞行模式:true为是,false或者nil为否
  13. local flyMode
  14. --是否注册上GSM网络,true为是,false或者nil为否
  15. local gsmRegistered
  16. --是否附着上GPRS网络,true为是,false或者nil为否
  17. local gprsAttached
  18. --是否有socket连接上后台,true为是,false或者nil为否
  19. local socketConnected
  20. --网络指示灯表示的工作状态
  21. --NULL:功能关闭状态
  22. --FLYMODE:飞行模式
  23. --SIMERR:未检测到SIM卡或者SIM卡锁pin码等SIM卡异常
  24. --IDLE:未注册GSM网络
  25. --GSM:已注册GSM网络
  26. --GPRS:已附着GPRS数据网络
  27. --SCK:socket已连接上后台
  28. local ledState = "NULL"
  29. local ON,OFF = 1,2
  30. --各种工作状态下配置的点亮、熄灭时长(单位毫秒)
  31. local ledBlinkTime =
  32. {
  33. NULL = {0,0xFFFF}, --常灭
  34. FLYMODE = {0,0xFFFF}, --常灭
  35. SIMERR = {300,5700}, --亮300毫秒,灭5700毫秒
  36. IDLE = {300,3700}, --亮300毫秒,灭3700毫秒
  37. GSM = {300,1700}, --亮300毫秒,灭1700毫秒
  38. GPRS = {300,700}, --亮300毫秒,灭700毫秒
  39. SCK = {100,100}, --亮100毫秒,灭100毫秒
  40. }
  41. --网络指示灯开关,true为打开,false或者nil为关闭
  42. local ledSwitch = false
  43. --网络指示灯默认PIN脚(GPIO33)
  44. local ledPin = pio.P1_1
  45. --[[
  46. -- 模块功能:更新网络指示灯表示的工作状态
  47. -- 参数:无
  48. -- 返回值:无
  49. --]]
  50. local function updateState()
  51. --log.info("netLed.updateState",ledSwitch,ledState,flyMode,simError,gsmRegistered,gprsAttached,socketConnected)
  52. if ledSwitch then
  53. local newState = "IDLE"
  54. if flyMode then
  55. newState = "FLYMODE"
  56. elseif simError then
  57. newState = "SIMERR"
  58. elseif socketConnected then
  59. newState = "SCK"
  60. elseif gprsAttached then
  61. newState = "GPRS"
  62. elseif gsmRegistered then
  63. newState = "GSM"
  64. end
  65. --指示灯状态发生变化
  66. if newState~=ledState then
  67. ledState = newState
  68. sys.publish("NET_LED_UPDATE")
  69. end
  70. end
  71. end
  72. --[[
  73. -- 模块功能:网络指示灯模块的运行任务
  74. -- 参数:
  75. ledPinSetFunc:指示灯GPIO的设置函数
  76. -- 返回值:无
  77. --]]
  78. local function taskLed(ledPinSetFunc)
  79. while true do
  80. --log.info("netLed.taskLed",ledPinSetFunc,ledSwitch,ledState)
  81. if ledSwitch then
  82. local onTime,offTime = ledBlinkTime[ledState][ON],ledBlinkTime[ledState][OFF]
  83. if onTime>0 then
  84. ledPinSetFunc(1)
  85. if not sys.waitUntil("NET_LED_UPDATE", onTime) then
  86. if offTime>0 then
  87. ledPinSetFunc(0)
  88. sys.waitUntil("NET_LED_UPDATE", offTime)
  89. end
  90. end
  91. else if offTime>0 then
  92. ledPinSetFunc(0)
  93. sys.waitUntil("NET_LED_UPDATE", offTime)
  94. end
  95. end
  96. else
  97. ledPinSetFunc(0)
  98. break
  99. end
  100. end
  101. end
  102. --- 配置网络指示灯并且立即执行配置后的动作
  103. -- @bool flag,是否打开网络指示灯功能,true为打开,false为关闭
  104. -- @param pin,number或者function类型
  105. -- 为number类型时,表示控制网络指示灯闪烁的GPIO引脚,例如pio.P1_1表示GPIO33
  106. -- 为function类型时,表示控制网络指示灯闪烁的回调函数,回调函数的调用形式为:
  107. -- pin(value)
  108. -- value:number类型,1表示点亮,0表示熄灭
  109. -- @return nil
  110. -- @usage setup(true,pio.P1_1)表示打开网络指示灯功能,GPIO33控制指示灯
  111. -- @usage setup(false)表示关闭网络指示灯功能
  112. function setup(flag,pin)
  113. --log.info("netLed.setup",flag,pin,ledSwitch)
  114. local oldSwitch = ledSwitch
  115. if flag~=ledSwitch then
  116. ledSwitch = flag
  117. sys.publish("NET_LED_UPDATE")
  118. end
  119. if flag and not oldSwitch then
  120. if type(pin)=="function" then pin(0) end
  121. sys.taskInit(taskLed, type(pin)=="function" and pin or pins.setup(pin or ledPin, 0))
  122. end
  123. end
  124. --- 配置某种工作状态下指示灯点亮和熄灭的时长(如果用户不配置,使用netLed.lua中ledBlinkTime配置的默认值)
  125. -- @string state,某种工作状态,仅支持"FLYMODE"、"SIMERR"、"IDLE"、"GSM"、"GPRS"、"SCK"
  126. -- @number on,指示灯点亮时长,单位毫秒,0xFFFF表示常亮,0表示常灭
  127. -- @number off,指示灯熄灭时长,单位毫秒,0xFFFF表示常灭,0表示常亮
  128. -- @return nil
  129. -- @usage updateBlinkTime("FLYMODE",1000,500)表示飞行模式工作状态下,指示灯闪烁规律为:亮1秒,灭0.5秒
  130. -- @usage updateBlinkTime("SCK",0xFFFF,0)表示有socket连接上后台的工作状态下,指示灯闪烁规律为:常亮
  131. -- @usage updateBlinkTime("SIMERR",0,0xFFFF)表示SIM卡异常状态下,指示灯闪烁规律为:常灭
  132. function updateBlinkTime(state,on,off)
  133. if not ledBlinkTime[state] then log.error("netLed.updateBlinkTime") return end
  134. local updated
  135. if on and ledBlinkTime[state][ON]~=on then
  136. ledBlinkTime[state][ON] = on
  137. updated = true
  138. end
  139. if off and ledBlinkTime[state][OFF]~=off then
  140. ledBlinkTime[state][OFF] = off
  141. updated = true
  142. end
  143. --log.info("netLed.updateBlinkTime",state,on,off,updated)
  144. if updated then sys.publish("NET_LED_UPDATE") end
  145. end
  146. sys.subscribe("FLYMODE", function(mode) if flyMode~=mode then flyMode=mode updateState() end end)
  147. sys.subscribe("SIM_IND", function(para) if simError~=(para~="RDY") then simError=(para~="RDY") updateState() end end)
  148. sys.subscribe("NET_STATE_UNREGISTER", function() if gsmRegistered then gsmRegistered=false updateState() end end)
  149. sys.subscribe("NET_STATE_REGISTERED", function() if not gsmRegistered then gsmRegistered=true updateState() end end)
  150. sys.subscribe("GPRS_ATTACH", function(attach) if gprsAttached~=attach then gprsAttached=attach updateState() end end)
  151. sys.subscribe("SOCKET_ACTIVE", function(active) if socketConnected~=active then socketConnected=active updateState() end end)