event-system.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /**
  2. * Author(s)...: Pashgan http://ChipEnable.Ru
  3. * Target(s)...: любой микроконтроллер mega
  4. * Compiler....: IAR 5.11A
  5. * Description.: Заготовка для событийной системы на таблицах
  6. * Data........: 30.09.12
  7. */
  8. #include "event-system.h"
  9. static void EmptyFunc(void);
  10. /* ring buffer */
  11. static volatile es_event_t cycleBuf[SIZE_BUF];
  12. static volatile uint8_t tailBuf = 0;
  13. static volatile uint8_t headBuf = 0;
  14. static volatile uint8_t countBuf = 0;
  15. static volatile es_state_t _State;
  16. typedef struct {
  17. es_state_t state;
  18. es_event_t event;
  19. es_state_t nextState;
  20. void (*pStateFunc1)(void);
  21. void (*pStateFunc2)(void);
  22. } table_state_t;
  23. /**
  24. * Sate Table - heart of state machine
  25. */
  26. const table_state_t stateTable[] = {
  27. /* STATE EVENT NEXT STATE STATE_FUNC1 STATE_FUNC2 */
  28. /* show date, time, ... */
  29. // 'Select' Button pressed
  30. {stShowTime, evBTN1Pressed, stShowWDay, showWD, NULL},
  31. {stShowWDay, evBTN1Pressed, stShowDM, showDayMon, NULL},
  32. {stShowDM, evBTN1Pressed, stShowYear, showYear, NULL},
  33. {stShowYear, evBTN1Pressed, stShowDNhours, showDNhour, NULL},
  34. {stShowDNhours, evBTN1Pressed, stShowDNbright, showDNbright, NULL},
  35. {stShowDNbright, evBTN1Pressed, stShowDNmode, showDNmode, NULL},
  36. {stShowDNmode, evBTN1Pressed, stShowDNcolour, showDNcolour, NULL},
  37. {stShowDNcolour, evBTN1Pressed, stShowTime, showTime, NULL},
  38. // '+' Button pressed
  39. {stShowTime, evBTN2Pressed, stShowMMSS, showMMSS, NULL},
  40. {stShowMMSS, evBTN2Pressed, stShowTime, showTime, NULL},
  41. /*
  42. // Top Buton pressed
  43. {stShowTime, evBTN4Pressed, stShowTemperature, showTemperature, NULL},
  44. {stShowTemperature, evBTN4Pressed, stShowHumidity, showHumidity, NULL},
  45. {stShowHumidity, evBTN4Pressed, stShowPressure, showPressure, NULL},
  46. {stShowPressure, evBTN4Pressed, stShowTime, showTime, NULL},
  47. */
  48. /* display wdt events */
  49. {stShowWDay, evDisplayWDT, stShowTime, showTime, NULL},
  50. {stShowDM, evDisplayWDT, stShowTime, showTime, NULL},
  51. {stShowYear, evDisplayWDT, stShowTime, showTime, NULL},
  52. {stShowHumidity, evDisplayWDT, stShowTime, showTime, NULL},
  53. {stShowTemperature, evDisplayWDT, stShowTime, showTime, NULL},
  54. {stShowPressure, evDisplayWDT, stShowTime, showTime, NULL},
  55. {stShowDNhours, evDisplayWDT, stShowTime, showTime, NULL},
  56. {stShowDNbright, evDisplayWDT, stShowTime, showTime, NULL},
  57. {stShowDNmode, evDisplayWDT, stShowTime, showTime, NULL},
  58. {stShowDNcolour, evDisplayWDT, stShowTime, showTime, NULL},
  59. /* refresh output */
  60. {stShowTime, evNewSecond, stNoChange, showTime, NULL},
  61. {stShowMMSS, evNewSecond, stNoChange, showMMSS, NULL},
  62. /* set time */
  63. {stShowTime, evBTN1Holded, stSetHH, setTimeBegin, setHHBegin},
  64. {stSetHH, evBTN1Holded, stShowTime, setTimeEnd, showTime},
  65. {stSetMM, evBTN1Holded, stShowTime, setTimeEnd, showTime},
  66. {stSetHH, evBTN1Pressed, stSetMM, setMMBegin, NULL},
  67. {stSetMM, evBTN1Pressed, stSetHH, setHHBegin, NULL},
  68. {stSetHH, evBTN2Pressed, stNoChange, setHHInc, setTimeShow},
  69. {stSetHH, evBTN3Pressed, stNoChange, setHHDec, setTimeShow},
  70. {stSetMM, evBTN2Pressed, stNoChange, setMMInc, setTimeShow},
  71. {stSetMM, evBTN3Pressed, stNoChange, setMMDec, setTimeShow},
  72. {stSetHH, evDisplayWDT, stShowTime, showTime, NULL},
  73. {stSetMM, evDisplayWDT, stShowTime, showTime, NULL},
  74. /* set calendar */
  75. // begin
  76. {stShowWDay, evBTN1Holded, stSetWD, setDateBegin, setWDBegin},
  77. {stShowDM, evBTN1Holded, stSetMD, setDateBegin, setMDBegin},
  78. {stShowYear, evBTN1Holded, stSetYear, setDateBegin, setYearBegin},
  79. // end
  80. {stSetWD, evBTN1Holded, stShowWDay, setDateEnd, showWD},
  81. {stSetMD, evBTN1Holded, stShowDM, setDateEnd, showDayMon},
  82. {stSetMon, evBTN1Holded, stShowDM, setDateEnd, showDayMon},
  83. {stSetYear, evBTN1Holded, stShowYear, setDateEnd, showYear},
  84. // next
  85. {stSetWD, evBTN1Pressed, stSetMD, setMDBegin, NULL},
  86. {stSetMD, evBTN1Pressed, stSetMon, setMonthBegin, NULL},
  87. {stSetMon, evBTN1Pressed, stSetYear, setYearBegin, NULL},
  88. {stSetYear, evBTN1Pressed, stSetWD, setWDBegin, NULL},
  89. // change
  90. {stSetWD, evBTN2Pressed, stNoChange, setIncWDay, setWDShow},
  91. {stSetWD, evBTN3Pressed, stNoChange, setDecWDay, setWDShow},
  92. {stSetMD, evBTN2Pressed, stNoChange, setIncMDay, setDMShow},
  93. {stSetMD, evBTN3Pressed, stNoChange, setDecMDay, setDMShow},
  94. {stSetMon, evBTN2Pressed, stNoChange, setIncMonth, setDMShow},
  95. {stSetMon, evBTN3Pressed, stNoChange, setDecMonth, setDMShow},
  96. {stSetYear, evBTN2Pressed, stNoChange, setIncYear, setYearShow},
  97. {stSetYear, evBTN3Pressed, stNoChange, setDecYear, setYearShow},
  98. // WDT
  99. {stSetWD, evDisplayWDT, stShowTime, showTime, NULL},
  100. {stSetMD, evDisplayWDT, stShowTime, showTime, NULL},
  101. {stSetMon, evDisplayWDT, stShowTime, showTime, NULL},
  102. {stSetYear, evDisplayWDT, stShowTime, showTime, NULL},
  103. /* set lighting */
  104. // begin
  105. {stShowDNhours, evBTN1Holded, stSetDayHour, setDNbegin, setDayHourBegin},
  106. {stShowDNbright, evBTN1Holded, stSetDayBright, setDNbegin, setDayBrightBegin},
  107. {stShowDNmode, evBTN1Holded, stSetDayMode, setDNbegin, setDayModeBegin},
  108. {stShowDNcolour, evBTN1Holded, stSetDayColour, setDNbegin, setDayColourBegin},
  109. // end
  110. {stSetDayHour, evBTN1Holded, stShowDNhours, setDNend, showDNhour},
  111. {stSetNightHour, evBTN1Holded, stShowDNhours, setDNend, showDNhour},
  112. {stSetDayBright, evBTN1Holded, stShowDNbright, setDNend, showDNbright},
  113. {stSetNightBright, evBTN1Holded, stShowDNbright, setDNend, showDNbright},
  114. {stSetDayMode, evBTN1Holded, stShowDNmode, setDNend, showDNmode},
  115. {stSetNightMode, evBTN1Holded, stShowDNmode, setDNend, showDNmode},
  116. {stSetDayColour, evBTN1Holded, stShowDNcolour, setDNend, showDNcolour},
  117. {stSetNightColour, evBTN1Holded, stShowDNcolour, setDNend, showDNcolour},
  118. // next
  119. {stSetDayHour, evBTN1Pressed, stSetNightHour, setNightHourBegin, NULL},
  120. {stSetNightHour, evBTN1Pressed, stSetDayBright, setDayBrightBegin, NULL},
  121. {stSetDayBright, evBTN1Pressed, stSetNightBright, setNightBrightBegin, NULL},
  122. {stSetNightBright, evBTN1Pressed, stSetDayMode, setDayModeBegin, NULL},
  123. {stSetDayMode, evBTN1Pressed, stSetNightMode, setNightModeBegin, NULL},
  124. {stSetNightMode, evBTN1Pressed, stSetDayColour, setDayColourBegin, NULL},
  125. {stSetDayColour, evBTN1Pressed, stSetNightColour, setNightColourBegin, NULL},
  126. {stSetNightColour, evBTN1Pressed, stSetDayHour, setDayHourBegin, NULL},
  127. // change
  128. {stSetDayHour, evBTN2Pressed, stNoChange, setIncDayHour, showDNhour},
  129. {stSetDayHour, evBTN3Pressed, stNoChange, setDecDayHour, showDNhour},
  130. {stSetNightHour, evBTN2Pressed, stNoChange, setIncNightHour, showDNhour},
  131. {stSetNightHour, evBTN3Pressed, stNoChange, setDecNightHour, showDNhour},
  132. {stSetDayBright, evBTN2Pressed, stNoChange, setIncDayBright, showDNbright},
  133. {stSetDayBright, evBTN3Pressed, stNoChange, setDecDayBright, showDNbright},
  134. {stSetNightBright, evBTN2Pressed, stNoChange, setIncNightBright, showDNbright},
  135. {stSetNightBright, evBTN3Pressed, stNoChange, setDecNightBright, showDNbright},
  136. {stSetDayMode, evBTN2Pressed, stNoChange, setIncDayMode, showDNmode},
  137. {stSetDayMode, evBTN3Pressed, stNoChange, setDecDayMode, showDNmode},
  138. {stSetNightMode, evBTN2Pressed, stNoChange, setIncNightMode, showDNmode},
  139. {stSetNightMode, evBTN3Pressed, stNoChange, setDecNightMode, showDNmode},
  140. {stSetDayColour, evBTN2Pressed, stNoChange, setIncDayColour, showDNcolour},
  141. {stSetDayColour, evBTN3Pressed, stNoChange, setDecDayColour, showDNcolour},
  142. {stSetNightColour, evBTN2Pressed, stNoChange, setIncNightColour, showDNcolour},
  143. {stSetNightColour, evBTN3Pressed, stNoChange, setDecNightColour, showDNcolour},
  144. // WDT
  145. {stSetDayHour, evDisplayWDT, stShowTime, setDNbreak, showTime},
  146. {stSetNightHour, evDisplayWDT, stShowTime, setDNbreak, showTime},
  147. {stSetDayBright, evDisplayWDT, stShowTime, setDNbreak, showTime},
  148. {stSetNightBright, evDisplayWDT, stShowTime, setDNbreak, showTime},
  149. {stSetDayMode, evDisplayWDT, stShowTime, setDNbreak, showTime},
  150. {stSetNightMode, evDisplayWDT, stShowTime, setDNbreak, showTime},
  151. {stSetDayColour, evDisplayWDT, stShowTime, setDNbreak, showTime},
  152. {stSetNightColour, evDisplayWDT, stShowTime, setDNbreak, showTime},
  153. /* mandatory empty table row */
  154. {stNoChange, eventNull, stNoChange, EmptyFunc, EmptyFunc}
  155. };
  156. /**
  157. * @brief Take event.
  158. * @param None
  159. * @retval Event code
  160. */
  161. es_event_t ES_GetEvent(void)
  162. {
  163. es_event_t event;
  164. if (countBuf > 0) {
  165. event = cycleBuf[headBuf];
  166. countBuf --;
  167. headBuf = (headBuf + 1) & (SIZE_BUF - 1);
  168. return event;
  169. }
  170. return eventNull;
  171. }
  172. /**
  173. * @brief Place event.
  174. * @param Event
  175. * @retval None
  176. */
  177. void ES_PlaceEvent(es_event_t event)
  178. {
  179. if (countBuf < SIZE_BUF) {
  180. cycleBuf[tailBuf] = event;
  181. tailBuf = (tailBuf + 1) & (SIZE_BUF - 1);
  182. countBuf ++;
  183. /* сигнализация переполнения буфера событий */
  184. } else {
  185. // tube power off
  186. TIM1->CCER &= ~(TIM_CCER_CC1E);
  187. TIM3->CCER &= ~(TIM_CCER_CC4E);
  188. TIM3->CCER &= ~(TIM_CCER_CC3E);
  189. TIM3->CCER &= ~(TIM_CCER_CC2E);
  190. TIM3->CCER &= ~(TIM_CCER_CC1E);
  191. GPIOA->BSRR = 0x10;
  192. // color - pink
  193. TIM1->CCR2 = 0xff;
  194. TIM1->CCR3 = 0x14;
  195. TIM1->CCR4 = 0x93;
  196. while(1);
  197. }
  198. }
  199. /**
  200. * @brief Initialize event system.
  201. * @param Start Event
  202. * @retval None
  203. */
  204. void ES_Init(es_state_t init_state)
  205. {
  206. tailBuf = 0;
  207. headBuf = 0;
  208. countBuf = 0;
  209. _State = init_state;
  210. }
  211. /**
  212. * @brief Fake function.
  213. * @param None
  214. * @retval None
  215. */
  216. static void EmptyFunc(void)
  217. {
  218. }
  219. /**
  220. * @brief Dispatcher of event system.
  221. * @param Event
  222. * @retval None
  223. */
  224. void ES_Dispatch(es_event_t event)
  225. {
  226. uint8_t i;
  227. void (*pStateFunc1)(void);
  228. void (*pStateFunc2)(void);
  229. pStateFunc1 = NULL;
  230. pStateFunc2 = NULL;
  231. /* get next state */
  232. for (i=0; stateTable[i].state; i++) {
  233. /* if the current state and the received event match */
  234. if (stateTable[i].state == _State && stateTable[i].event == event) {
  235. /* change current state if needed */
  236. if (stateTable[i].nextState != stNoChange) {
  237. _State = stateTable[i].nextState;
  238. }
  239. /* get functions */
  240. pStateFunc1 = stateTable[i].pStateFunc1;
  241. pStateFunc2 = stateTable[i].pStateFunc2;
  242. break;
  243. }
  244. }
  245. if (pStateFunc1) {
  246. pStateFunc1();
  247. }
  248. if (pStateFunc2) {
  249. pStateFunc2();
  250. }
  251. }
  252. /**
  253. * @brief Return current state code.
  254. * @param None
  255. * @retval State code
  256. */
  257. es_state_t ES_GetState(void)
  258. {
  259. return _State;
  260. }
  261. /**
  262. * @brief Set current state to given code .
  263. * @param State
  264. * @retval None
  265. */
  266. void ES_SetState(es_state_t new_state)
  267. {
  268. _State = new_state;
  269. }