3 @summary netLed 网络状态指示灯
8 --注意:因使用了sys.wait()所有api需要在协程中使用
10 local netLed = require ("netLed")
12 local LEDA = gpio.setup(27,1,gpio.PULLUP) --LED引脚判断赋值结束
13 sys.taskInit(function()
15 sys.wait(5080)--延时5秒等待网络注册
16 log.info("mobile.status()", mobile.status())
18 if mobile.status() == 1 then--已注册
20 netLed.setupBreateLed(LEDA)
28 local sys
= require("sys")
31 local simError
--SIM卡状态:true为异常,false或者nil为正常
32 local flyMode
--是否处于飞行模式:true为是,false或者nil为否
33 local gprsAttached
--是否附着上GPRS网络,true为是,false或者nil为否
34 local socketConnected
--是否有socket连接上后台,true为是,false或者nil为否
40 SIMERR:未检测到SIM卡或者SIM卡锁pin码等SIM卡异常
45 local ledState
= "NULL"
47 --各种工作状态下配置的点亮、熄灭时长(单位毫秒)
50 NULL
= {0,0xFFFF}, --常灭
51 FLYMODE
= {0,0xFFFF}, --常灭
52 SIMERR
= {300,5700}, --亮300毫秒,灭5700毫秒
53 IDLE
= {300,3700}, --亮300毫秒,灭3700毫秒
54 GPRS
= {300,700}, --亮300毫秒,灭700毫秒
55 SCK
= {100,100}, --亮100毫秒,灭100毫秒
59 local ledSwitch
= false --网络指示灯开关,true为打开,false或者nil为关闭
60 local LEDPIN
= 27 --网络指示灯默认PIN脚(GPIO27)
61 local lteSwitch
= false --LTE指示灯开关,true为打开,false或者nil为关闭
62 local LTEPIN
= 26 --LTE指示灯默认PIN脚(GPIO26)
72 function netLed
.setState()
73 log.info("netLed.setState",ledSwitch
,ledState
,flyMode
,simError
,gprsAttached
,socketConnected
)
75 local newState
= "IDLE"
80 elseif socketConnected
then
82 elseif gprsAttached
then
86 if newState
~=ledState
then
88 sys
.publish("NET_LED_UPDATE")
95 @api netLed.taskLed(ledPinSetFunc)
98 local LEDA = gpio.setup(27,1,gpio.PULLUP) --LED引脚判断赋值结束
101 function netLed
.taskLed(ledPinSetFunc
)
103 --log.info("netLed.taskLed",ledPinSetFunc,ledSwitch,ledState)
105 local onTime
,offTime
= ledBlinkTime
[ledState
][ON
],ledBlinkTime
[ledState
][OFF
]
108 if not sys
.waitUntil("NET_LED_UPDATE", onTime
) then
111 sys
.waitUntil("NET_LED_UPDATE", offTime
)
114 else if offTime
>0 then
116 sys
.waitUntil("NET_LED_UPDATE", offTime
)
128 @api netLed.taskLte(ledPinSetFunc)
131 local LEDA = gpio.setup(27,1,gpio.PULLUP) --LED引脚判断赋值结束
134 function netLed
.taskLte(ledPinSetFunc
)
136 local _
,arg
= sys
.waitUntil("LTE_LED_UPDATE")
138 ledPinSetFunc(arg
and 1 or 0)
144 配置网络指示灯和LTE指示灯并且立即执行配置后的动作
145 @api netLed.setup(flag,ledpin,ltepin)
146 @bool flag 是否打开网络指示灯和LTE指示灯功能,true为打开,false为关闭
147 @number ledPin 控制网络指示灯闪烁的GPIO引脚,例如pio.P0_1表示GPIO1
148 @number ltePin 控制LTE指示灯闪烁的GPIO引脚,例如pio.P0_4表示GPIO4
151 netLed.setup(true,27,0)
153 function netLed
.setup(flag
,ledPin
,ltePin
)
154 --log.info("netLed.setup",flag,pin,ledSwitch)
155 local oldSwitch
= ledSwitch
156 if flag
~=ledSwitch
then
158 sys
.publish("NET_LED_UPDATE")
160 if flag
and not oldSwitch
then
161 sys
.taskInit(netLed
.taskLed
, gpio
.setup(ledPin
or LEDPIN
, 0))
163 if flag
~=lteSwitch
then
166 if flag
and ltePin
and not oldSwitch
then
167 sys
.taskInit(netLed
.taskLte
, gpio
.setup(ltePin
, 0))
172 配置某种工作状态下指示灯点亮和熄灭的时长(如果用户不配置,使用netLed.lua中ledBlinkTime配置的默认值)
173 @api netLed.setBlinkTime(state,on,off)
174 @string state 某种工作状态,仅支持"FLYMODE"、"SIMERR"、"IDLE"、"GSM"、"GPRS"、"SCK"
175 @number on 指示灯点亮时长,单位毫秒,0xFFFF表示常亮,0表示常灭
176 @number off 指示灯熄灭时长,单位毫秒,0xFFFF表示常灭,0表示常亮
179 netLed.setBlinkTime(("FLYMODE",1000,500) --表示飞行模式工作状态下,指示灯闪烁规律为: 亮1秒,灭8.5秒
181 function netLed
.setBlinkTime(state
,on
,off
)
182 if not ledBlinkTime
[state
] then log.error("netLed.setBlinkTime") return end
184 if on
and ledBlinkTime
[state
][ON
]~=on
then
185 ledBlinkTime
[state
][ON
] = on
188 if off
and ledBlinkTime
[state
][OFF
]~=off
then
189 ledBlinkTime
[state
][OFF
] = off
192 --log.info("netLed.setBlinkTime",state,on,off,updated)
193 if updated
then sys
.publish("NET_LED_UPDATE") end
198 @api netLed.setupBreateLed(ledPin)
199 @function ledPin 呼吸灯的ledPin(1)用pins.setup注册返回的方法
202 local netLed = require ("netLed")
203 local LEDA = gpio.setup(27,1,gpio.PULLUP) --LED引脚判断赋值结束
204 sys.taskInit(function()
206 sys.wait(5080)--延时5秒等待网络注册
207 log.info("mobile.status()", mobile.status())
209 if mobile.status() == 1 then--已注册
211 netLed.setupBreateLed(LEDA)
216 function netLed
.setupBreateLed(ledPin
)
218 local bLighting
, bDarking
, LED_PWM
= false, true, 18
220 for i
= 1, LED_PWM
- 1 do
224 sys
.wait(LED_PWM
- i
)
232 for i
= 1, LED_PWM
- 1 do
234 sys
.wait(LED_PWM
- i
)
245 sys
.subscribe("FLYMODE", function(mode
) if flyMode
~=mode
then flyMode
=mode netLed
.setState() end end)
246 sys
.subscribe("SIM_IND", function(para
) if simError
~=(para
~="RDY") and simError
~=(para
~="GET_NUMBER") then simError
=(para
~="RDY") netLed
.setState() end log.info("sim status", para
) end)
247 sys
.subscribe("IP_LOSE", function() if gprsAttached
then gprsAttached
=false netLed
.setState() end log.info("mobile", "IP_LOSE", (adapter
or -1) == socket
.LWIP_GP
) end)
248 sys
.subscribe("IP_READY", function(ip
, adapter
) if gprsAttached
~=adapter
then gprsAttached
=adapter netLed
.setState() end log.info("mobile", "IP_READY", ip
, (adapter
or -1) == socket
.LWIP_GP
) end)
249 sys
.subscribe("SOCKET_ACTIVE", function(active
) if socketConnected
~=active
then socketConnected
=active netLed
.setState() end end)