Led.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /**********************************************************************************
  2. * File Name: Led.c
  3. * Function Describe:The task for LED display
  4. * Relate Module:
  5. * Explain: 控制指示灯显示任务。在其他任务中可通过SetLedFlash函数设置显示方式
  6. * Date: 2019.2.25
  7. LED1 绿色 电源指示灯,MCU工作常亮
  8. LED2 红色 wifi指示灯
  9. LED3 蓝色 LAN指示灯
  10. LED4 stat绿色 指示与外部设备通讯状态,收到一包数据并解析成功,闪烁1次
  11. LED2 红色 wifi模式下工作指示灯
  12. wifi硬件故障,或非wifi模式下 常灭
  13. wifi连接服务器失败 单闪
  14. 已经正常连接 双闪 //收到一包数据
  15. LED3 蓝色 有线模式下工作指示灯
  16. 网络芯片硬件故障 ,或非有线模式, 常灭
  17. 未插网线或未分配内部IP 单闪
  18. 已正常连接 双闪
  19. **********************************************************************************/
  20. #define THIS_FILE_ID 9
  21. //--------------------------------------------------------------------------------
  22. #include "includes.h"
  23. static unsigned char sucRedLedFlash=0;
  24. static unsigned char sucRedLedSleep=0;
  25. static unsigned char sucBlueLedFlash=0;
  26. static unsigned char sucBlueLedSleep=0;
  27. static unsigned char sucLedFlash[3]={0,0,0};
  28. static unsigned char sucLedSleep[3]={0,0,0};
  29. void LedOn(int i);
  30. void LedOff(int i);
  31. void LedInit(void)
  32. {
  33. int i;
  34. GPIO_InitTypeDef GPIO_InitStructure;
  35. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC, ENABLE);
  36. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  37. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  38. GPIO_InitStructure.GPIO_Pin = WARN_LED1_PIN;
  39. GPIO_Init(WARN_LED1_PORT, &GPIO_InitStructure);
  40. GPIO_InitStructure.GPIO_Pin = WARN_LED2_PIN;
  41. GPIO_Init(WARN_LED2_PORT, &GPIO_InitStructure);
  42. GPIO_InitStructure.GPIO_Pin = WARN_LED3_PIN;
  43. GPIO_Init(WARN_LED3_PORT, &GPIO_InitStructure);
  44. GPIO_InitStructure.GPIO_Pin = WARN_BEEP_PIN;
  45. GPIO_Init(WARN_BEEP_PORT, &GPIO_InitStructure);
  46. GPIO_InitStructure.GPIO_Pin = MODEM_LED4_PIN;
  47. GPIO_Init(MODEM_LED4_PORT, &GPIO_InitStructure);
  48. for(i=0;i<4;i++) LedOff(i);
  49. FEED_EXTWATCHDOG();//喂狗
  50. IWDG_ReloadCounter();//喂狗
  51. for(i=0;i<4;i++){
  52. LedOn(i);
  53. os_dly_wait(80);
  54. FEED_EXTWATCHDOG();//喂狗
  55. IWDG_ReloadCounter();//喂狗
  56. }
  57. FEED_EXTWATCHDOG();//喂狗
  58. IWDG_ReloadCounter();//喂狗
  59. for(i=0;i<4;i++)LedOff(i);
  60. }
  61. void LedOn(int i)
  62. {
  63. if(i==0)WARN_LED1_HIGH;
  64. else if(i==1)WARN_LED2_HIGH;
  65. else if(i==2)WARN_LED3_HIGH;
  66. else MODEM_LED4_HIGH;
  67. }
  68. void LedOff(int i)
  69. {
  70. if(i==0)WARN_LED1_LOW;
  71. else if(i==1)WARN_LED2_LOW;
  72. else if(i==2)WARN_LED3_LOW;
  73. else MODEM_LED4_LOW;
  74. }
  75. void LedProcess(unsigned char reset)
  76. {
  77. int i;
  78. static unsigned char LedCt[3]={0,0,0};
  79. static unsigned char LedMaxCt[3]={0,0,0};
  80. static unsigned char LedFlash2[3]={0,0,0};
  81. static unsigned char LastLedFlash[3]={0,0,0};
  82. static unsigned char LastLedSleep[3]={0,0,0};
  83. static unsigned char sucCt=0;
  84. static unsigned char Uart1LedCt=0;
  85. if(reset){
  86. for(i=0;i<3;i++){
  87. LedCt[i]=0;
  88. LedMaxCt[i]=0;
  89. LedFlash2[i]=0;
  90. LastLedFlash[i]=0;
  91. LastLedSleep[i]=0;
  92. LedOff(i);
  93. }
  94. sucCt=0;
  95. Uart1LedCt=0;
  96. }
  97. //控制200ms执行一次
  98. if(++sucCt>20)sucCt=0;
  99. else return;
  100. //LED1~LED3
  101. for(i=0;i<3;i++){
  102. if(sucLedFlash[i]==0){ //长灭
  103. LedOff(i);
  104. }else if(sucLedSleep[i]==0){//长亮
  105. LedOn(i);
  106. }else{
  107. if(LastLedFlash[i]!=sucLedFlash[i] || LastLedSleep[i]!=sucLedSleep[i]){
  108. LedCt[i]=0;
  109. LastLedFlash[i]=sucLedFlash[i];
  110. LastLedSleep[i]=sucLedSleep[i];
  111. LedFlash2[i]=sucLedFlash[i]*2;
  112. LedMaxCt[i]=(LedFlash2[i]+LastLedSleep[i])*2;
  113. }
  114. if(++LedCt[i]>LedMaxCt[i])LedCt[i]=0;
  115. if(LedCt[i]<LedFlash2[i] && (LedCt[i]&0x01)==0x01)LedOn(i);
  116. else LedOff(i);
  117. }
  118. }
  119. //LED4 串口指示灯,来一包数据闪烁一下
  120. if(g_ucUart1Activated == 1){//串口1有数据来就闪灯
  121. g_ucUart1Activated=0;
  122. Uart1LedCt=0;
  123. MODEM_LED4_HIGH;
  124. }else if(++Uart1LedCt>0){//串口1无数据来就灭灯 //
  125. Uart1LedCt=0;
  126. MODEM_LED4_LOW;
  127. }
  128. }
  129. ///********************************************************************
  130. //SetLed
  131. //设置内部运行参数
  132. //*********************************************************************/
  133. void SetLed(unsigned char LedId,unsigned char flash,unsigned char sleep)
  134. {
  135. //合法性判断
  136. int t=(flash+sleep)*2;
  137. if(t>255){
  138. return;
  139. }
  140. if(LedId>2)return;
  141. //设置运行参数
  142. sucLedFlash[LedId]=flash;
  143. sucLedSleep[LedId]=sleep;
  144. }
  145. void SetLedStatus(unsigned char LedId,LED_STATUS LedStatus)
  146. {
  147. switch(LedStatus){
  148. case NotBright:
  149. SetLed(LedId,0,0); //常灭
  150. break;
  151. case Bright:
  152. SetLed(LedId,1,0); //常亮
  153. break;
  154. case Flash:
  155. SetLed(LedId,1,1);
  156. break;
  157. case OneFlashFast:
  158. SetLed(LedId,1,6);
  159. break;
  160. case TowFlashFast:
  161. SetLed(LedId,2,6);
  162. break;
  163. case ThreeFlashFast:
  164. SetLed(LedId,3,6);
  165. break;
  166. case OneFlashSlow:
  167. SetLed(LedId,1,20);
  168. break;
  169. case TowFlashSlow:
  170. SetLed(LedId,2,20);
  171. break;
  172. case ThreeFlashSlow:
  173. SetLed(LedId,3,20);
  174. break;
  175. }
  176. }
  177. //void SetLedIndicator(void)
  178. //{
  179. // if(sutTcpStatus.NetMode==1){//4G 模式
  180. // SetLedStatus(1,NotBright);//常灭
  181. // if(sutModemStatus.Power==CLOSED){
  182. // SetLedStatus(0,NotBright);//常灭
  183. // }else if(sutModemStatus.CardStatus!=OPENED){
  184. // SetLedStatus(0,Bright);//常亮
  185. // }else if(sutModemStatus.CREG!=OPENED){
  186. // SetLedStatus(0,Flash);//均匀闪烁
  187. // }else if(sutModemStatus.PPPStatus!=OPENED){
  188. // SetLedStatus(0,OneFlashFast);//1闪
  189. // }else if(sutTcpStatus.ServerStatus!=OPENED){
  190. // SetLedStatus(0,TowFlashFast);//2闪
  191. // }else{
  192. // SetLedStatus(0,ThreeFlashFast);//3闪
  193. // }
  194. // }else if(sutTcpStatus.NetMode==2){//LAN模式
  195. // SetLedStatus(0,NotBright);//常灭
  196. // if(sutWLanStatus.NetFlag==0){//网络芯片未完成初始化
  197. // SetLedStatus(1,NotBright);//常灭
  198. // }else if(sutWLanStatus.NetFlag==1){//网络IC初始化成功但网线未链接
  199. // SetLedStatus(1,Bright);//常亮
  200. // }else if(sutWLanStatus.NetFlag==2){//网线已插入但DHCP未分配IP
  201. // SetLedStatus(1,Flash);//均匀闪烁
  202. // }else if(sutWLanStatus.Connected==0){//DHCP已成功分配IP,但未连接服务器
  203. // SetLedStatus(1,OneFlashFast);//1闪
  204. // }else if(sutTcpStatus.ServerStatus!=OPENED){//已连接服务器但未成功登陆
  205. // SetLedStatus(1,TowFlashFast);//2闪
  206. // }else{//已成功登陆服务器
  207. // SetLedStatus(1,ThreeFlashFast);//3闪
  208. // }
  209. // }else{//无效模式
  210. // SetLedStatus(0,NotBright);//常灭
  211. // SetLedStatus(1,NotBright);//常灭
  212. // }
  213. //
  214. //}
  215. /***********************************************************************************/