LED_ATY.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /**
  2. * @file LED_ATY.c
  3. *
  4. * @param Project DEVICE_GENERAL_ATY_LIB
  5. *
  6. * @author ATY
  7. *
  8. * @copyright
  9. * - Copyright 2017 - 2023 MZ-ATY
  10. * - This code follows:
  11. * - MZ-ATY Various Contents Joint Statement -
  12. * <a href="https://mengze.top/MZ-ATY_VCJS">
  13. * https://mengze.top/MZ-ATY_VCJS</a>
  14. * - CC 4.0 BY-NC-SA -
  15. * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
  16. * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
  17. * - Your use will be deemed to have accepted the terms of this statement.
  18. *
  19. * @brief functions of LED for all embedded device
  20. *
  21. * @version
  22. * - 1_01_220901 > ATY
  23. * -# Preliminary version, first Release
  24. ********************************************************************************
  25. */
  26. #ifndef __LED_ATY_C
  27. #define __LED_ATY_C
  28. #include "LED_ATY.h"
  29. /******************************* For user *************************************/
  30. /******************************************************************************/
  31. uint32_t ledBlinkStep = 0;
  32. uint8_t ledBlinkType = 20;
  33. /**
  34. * @brief set sysled blink ledBlinkType
  35. * @note put this function at timer IT callback, change
  36. * @note 1: light on and off cycle
  37. * 10: light on and off cycle long time ver
  38. * 2: light on blink twice
  39. * 20: light off blink twice
  40. * 3: light on blink thrice
  41. * 30: light off blink thrice
  42. * 40: breath
  43. * @note if(ledBlinkType - 40 >= 10) at large time cycle
  44. * if(ledBlinkType - 40 < 10) at small time cycle(breath type is 40-49)
  45. */
  46. void SysLedBlink(void)
  47. {
  48. static uint8_t ledBlinkLastType = 0;
  49. if(ledBlinkLastType != ledBlinkType)
  50. {
  51. ledBlinkLastType = ledBlinkType;
  52. ledBlinkStep = 0;
  53. }
  54. if(ledBlinkType == 1)
  55. {
  56. if(ledBlinkStep == 0)
  57. {
  58. ledBlinkStep = 1;
  59. GPIO_SET_L(SYSLED_PORT, SYSLED_PIN);
  60. }
  61. else if(ledBlinkStep != 0)
  62. {
  63. ledBlinkStep = 0;
  64. GPIO_SET_H(SYSLED_PORT, SYSLED_PIN);
  65. #ifdef __DEBUG_LED_ATY
  66. // printf("\r\nSysLed Blink.");
  67. #endif /* __DEBUG_LED_ATY */
  68. }
  69. }
  70. else if(ledBlinkType == 10)
  71. {
  72. ledBlinkStep++;
  73. if(ledBlinkStep < 10)
  74. GPIO_SET_L(SYSLED_PORT, SYSLED_PIN);
  75. else if(ledBlinkStep >= 10 && ledBlinkStep < 20)
  76. GPIO_SET_H(SYSLED_PORT, SYSLED_PIN);
  77. else
  78. {
  79. ledBlinkStep = 0;
  80. }
  81. }
  82. // Type 2 & 3 put at short timer is better, like 100ms
  83. else if(ledBlinkType == 2)
  84. {
  85. if(ledBlinkStep == 0)
  86. {
  87. ledBlinkStep = 1;
  88. GPIO_SET_L(SYSLED_PORT, SYSLED_PIN);
  89. }
  90. else if(ledBlinkStep == 1)
  91. {
  92. ledBlinkStep = 2;
  93. GPIO_SET_H(SYSLED_PORT, SYSLED_PIN);
  94. }
  95. else if(ledBlinkStep == 2)
  96. {
  97. ledBlinkStep = 3;
  98. GPIO_SET_L(SYSLED_PORT, SYSLED_PIN);
  99. }
  100. else if(ledBlinkStep == 3)
  101. {
  102. ledBlinkStep = 4;
  103. GPIO_SET_H(SYSLED_PORT, SYSLED_PIN);
  104. }
  105. else if((ledBlinkStep >= 4) && (ledBlinkStep < LED_LONG_STEPS))
  106. {
  107. ledBlinkStep++;
  108. }
  109. else if(ledBlinkStep == LED_LONG_STEPS)
  110. {
  111. ledBlinkStep = 0;
  112. #ifdef __DEBUG_LED_ATY
  113. // printf("\r\nSysLed Blink.");
  114. #endif /* __DEBUG_LED_ATY */
  115. }
  116. }
  117. else if(ledBlinkType == 20)
  118. {
  119. if(ledBlinkStep == 0)
  120. {
  121. ledBlinkStep = 1;
  122. GPIO_SET_H(SYSLED_PORT, SYSLED_PIN);
  123. }
  124. else if(ledBlinkStep == 1)
  125. {
  126. ledBlinkStep = 2;
  127. GPIO_SET_L(SYSLED_PORT, SYSLED_PIN);
  128. }
  129. else if(ledBlinkStep == 2)
  130. {
  131. ledBlinkStep = 3;
  132. GPIO_SET_H(SYSLED_PORT, SYSLED_PIN);
  133. }
  134. else if(ledBlinkStep == 3)
  135. {
  136. ledBlinkStep = 4;
  137. GPIO_SET_L(SYSLED_PORT, SYSLED_PIN);
  138. }
  139. else if((ledBlinkStep >= 4) && (ledBlinkStep < LED_LONG_STEPS))
  140. {
  141. ledBlinkStep++;
  142. }
  143. else if(ledBlinkStep == LED_LONG_STEPS)
  144. {
  145. ledBlinkStep = 0;
  146. #ifdef __DEBUG_LED_ATY
  147. // printf("\r\nSysLed Blink.");
  148. #endif /* __DEBUG_LED_ATY */
  149. }
  150. }
  151. else if(ledBlinkType == 3)
  152. {
  153. if(ledBlinkStep == 0)
  154. {
  155. ledBlinkStep = 1;
  156. GPIO_SET_L(SYSLED_PORT, SYSLED_PIN);
  157. }
  158. else if(ledBlinkStep == 1)
  159. {
  160. ledBlinkStep = 2;
  161. GPIO_SET_H(SYSLED_PORT, SYSLED_PIN);
  162. }
  163. else if(ledBlinkStep == 2)
  164. {
  165. ledBlinkStep = 3;
  166. GPIO_SET_L(SYSLED_PORT, SYSLED_PIN);
  167. }
  168. else if(ledBlinkStep == 3)
  169. {
  170. ledBlinkStep = 4;
  171. GPIO_SET_H(SYSLED_PORT, SYSLED_PIN);
  172. }
  173. else if(ledBlinkStep == 4)
  174. {
  175. ledBlinkStep = 5;
  176. GPIO_SET_L(SYSLED_PORT, SYSLED_PIN);
  177. }
  178. else if(ledBlinkStep == 5)
  179. {
  180. ledBlinkStep = 6;
  181. GPIO_SET_H(SYSLED_PORT, SYSLED_PIN);
  182. }
  183. else if((ledBlinkStep >= 6) && (ledBlinkStep < LED_LONG_STEPS))
  184. {
  185. ledBlinkStep++;
  186. }
  187. else if(ledBlinkStep == LED_LONG_STEPS)
  188. {
  189. ledBlinkStep = 0;
  190. #ifdef __DEBUG_LED_ATY
  191. // printf("\r\nSysLed Blink.");
  192. #endif /* __DEBUG_LED_ATY */
  193. }
  194. }
  195. else if(ledBlinkType == 30)
  196. {
  197. if(ledBlinkStep == 0)
  198. {
  199. ledBlinkStep = 1;
  200. GPIO_SET_H(SYSLED_PORT, SYSLED_PIN);
  201. }
  202. else if(ledBlinkStep == 1)
  203. {
  204. ledBlinkStep = 2;
  205. GPIO_SET_L(SYSLED_PORT, SYSLED_PIN);
  206. }
  207. else if(ledBlinkStep == 2)
  208. {
  209. ledBlinkStep = 3;
  210. GPIO_SET_H(SYSLED_PORT, SYSLED_PIN);
  211. }
  212. else if(ledBlinkStep == 3)
  213. {
  214. ledBlinkStep = 4;
  215. GPIO_SET_L(SYSLED_PORT, SYSLED_PIN);
  216. }
  217. else if(ledBlinkStep == 4)
  218. {
  219. ledBlinkStep = 5;
  220. GPIO_SET_H(SYSLED_PORT, SYSLED_PIN);
  221. }
  222. else if(ledBlinkStep == 5)
  223. {
  224. ledBlinkStep = 6;
  225. GPIO_SET_L(SYSLED_PORT, SYSLED_PIN);
  226. }
  227. else if((ledBlinkStep >= 6) && (ledBlinkStep < LED_LONG_STEPS))
  228. {
  229. ledBlinkStep++;
  230. }
  231. else if(ledBlinkStep == LED_LONG_STEPS)
  232. {
  233. ledBlinkStep = 0;
  234. #ifdef __DEBUG_LED_ATY
  235. // printf("\r\nSysLed Blink.");
  236. #endif /* __DEBUG_LED_ATY */
  237. }
  238. }
  239. else if(ledBlinkType == 40) // put at 1ms cycle, cycle more faster, led animation more breathly
  240. {
  241. // ms when cycle is 1ms // 3000 at 1ms cycle, 60000 at 24MHz main while cycle
  242. #define FLOW_CYCLE_COUNT ((uint32_t)60000)
  243. // #define FLOW_CYCLE_COUNT (3000)
  244. // led brghtness split // 10 at 1ms cycle, 1000 at 24MHz main while cycle
  245. #define ONE_PULSE_COUNT (60)
  246. // #define ONE_PULSE_COUNT (10)
  247. static uint16_t cycleCount = 0;
  248. if(cycleCount < ONE_PULSE_COUNT)
  249. cycleCount++;
  250. else
  251. cycleCount = 0;
  252. ledBlinkStep++;
  253. // UartSendFloatStr(cycleCount);
  254. if(ledBlinkStep < FLOW_CYCLE_COUNT){
  255. if(cycleCount < (ONE_PULSE_COUNT * ledBlinkStep / FLOW_CYCLE_COUNT))
  256. GPIO_SET_H(SYSLED_PORT, SYSLED_PIN);
  257. else
  258. GPIO_SET_L(SYSLED_PORT, SYSLED_PIN);
  259. }
  260. else if(ledBlinkStep >= FLOW_CYCLE_COUNT && ledBlinkStep < FLOW_CYCLE_COUNT * 2){
  261. if(cycleCount < (ONE_PULSE_COUNT * (ledBlinkStep - FLOW_CYCLE_COUNT) / FLOW_CYCLE_COUNT))
  262. GPIO_SET_L(SYSLED_PORT, SYSLED_PIN);
  263. else
  264. GPIO_SET_H(SYSLED_PORT, SYSLED_PIN);
  265. }
  266. if(ledBlinkStep >= FLOW_CYCLE_COUNT * 2)
  267. ledBlinkStep = 0;
  268. }
  269. }
  270. #endif /* __LED_ATY_C */
  271. /******************************** End Of File *********************************/