event-system.c 11 KB

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