event-system.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. {stShowTime, evBTN1Pressed, stShowWDay, showWD, NULL},
  30. {stShowWDay, evBTN1Pressed, stShowDM, showDayMon, NULL},
  31. {stShowDM, evBTN1Pressed, stShowYear, showYear, NULL},
  32. {stShowYear, evBTN1Pressed, stShowTime, showTime, NULL},
  33. {stShowTime, evBTN2Pressed, stShowMMSS, showMMSS, NULL},
  34. {stShowMMSS, evBTN2Pressed, stShowTime, showTime, NULL},
  35. {stShowTime, evBTN4Pressed, stShowSensorData, showSensorData, NULL},
  36. {stShowSensorData, evBTN4Pressed, stShowTime, showTime, NULL},
  37. /* display wdt */
  38. {stShowWDay, evDisplayWDT, stShowTime, showTime, NULL},
  39. {stShowDM, evDisplayWDT, stShowTime, showTime, NULL},
  40. {stShowMDay, evDisplayWDT, stShowTime, showTime, NULL},
  41. {stShowMon, evDisplayWDT, stShowTime, showTime, NULL},
  42. {stShowYear, evDisplayWDT, stShowTime, showTime, NULL},
  43. //{stShowBright, evDisplayWDT, stShowTime, showTime, NULL},
  44. {stShowHumidity, evDisplayWDT, stShowTime, showTime, NULL},
  45. {stShowTemperature, evDisplayWDT, stShowTime, showTime, NULL},
  46. {stShowPressure, evDisplayWDT, stShowTime, showTime, NULL},
  47. /* refresh output */
  48. {stShowTime, evNewSecond, stNoChange, showTime, NULL},
  49. {stShowMMSS, evNewSecond, stNoChange, showMMSS, NULL},
  50. {stShowWDay, evRefreshCal, stNoChange, showWD, NULL},
  51. {stShowDM, evRefreshCal, stNoChange, showDayMon, NULL},
  52. {stShowMDay, evRefreshCal, stNoChange, showDay, NULL},
  53. {stShowMon, evRefreshCal, stNoChange, showMonth, NULL},
  54. {stShowYear, evRefreshCal, stNoChange, showYear, NULL},
  55. // {stShowYear, evBTN1Pressed, stShowBright, showBright, NULL},
  56. {stShowBright, evBTN1Pressed, stShowTime, showTime, NULL},
  57. /* set time */
  58. {stShowTime, evBTN1Holded, stSetHH, setTimeBegin, setHHBegin},
  59. {stSetHH, evBTN1Holded, stShowTime, setTimeEnd, showTime},
  60. {stSetMM, evBTN1Holded, stShowTime, setTimeEnd, showTime},
  61. {stSetHH, evBTN1Pressed, stSetMM, setMMBegin, NULL},
  62. {stSetMM, evBTN1Pressed, stSetHH, setHHBegin, NULL},
  63. {stSetHH, evBTN3Pressed, stNoChange, setHHInc, setTimeShow},
  64. {stSetHH, evBTN2Pressed, stNoChange, setHHDec, setTimeShow},
  65. {stSetMM, evBTN3Pressed, stNoChange, setMMInc, setTimeShow},
  66. {stSetMM, evBTN2Pressed, stNoChange, setMMDec, setTimeShow},
  67. {stSetHH, evNewSecond, stNoChange, setTimeShow, NULL},
  68. {stSetMM, evNewSecond, stNoChange, setTimeShow, NULL},
  69. {stSetHH, evDisplayWDT, stShowTime, showTime, NULL},
  70. {stSetMM, evDisplayWDT, stShowTime, showTime, NULL},
  71. /* set calendar */
  72. /*
  73. {stShowWDM, evBTN1Holded, stSetWDay, setDateBegin, setWDayBegin},
  74. {stSetWDay, evBTN1Holded, stShowWDM, setDateEnd, showWDM},
  75. {stSetMDay, evBTN1Holded, stShowWDM, setDateEnd, showWDM},
  76. {stSetMon, evBTN1Holded, stShowWDM, setDateEnd, showWDM},
  77. {stSetWDay, evBTN1Pressed, stSetMDay, setMDayBegin, NULL},
  78. {stSetMDay, evBTN1Pressed, stSetMon, setMonthBegin, NULL},
  79. {stSetMon, evBTN1Pressed, stSetWDay, setWDayBegin, NULL},
  80. {stSetWDay, evBTN2Pressed, stNoChange, setDecWDay, setWDMShow},
  81. {stSetWDay, evBTN3Pressed, stNoChange, setIncWDay, setWDMShow},
  82. {stSetMDay, evBTN2Pressed, stNoChange, setDecMDay, setWDMShow},
  83. {stSetMDay, evBTN3Pressed, stNoChange, setIncMDay, setWDMShow},
  84. {stSetMon, evBTN2Pressed, stNoChange, setDecMonth, setWDMShow},
  85. {stSetMon, evBTN3Pressed, stNoChange, setIncMonth, setWDMShow},
  86. {stShowYear, evBTN1Holded, stSetYear, setDateBegin, setYearBegin},
  87. {stSetYear, evBTN1Holded, stShowYear, setDateEnd, showYear},
  88. {stSetYear, evBTN2Pressed, stNoChange, setDecYear, setYearShow},
  89. {stSetYear, evBTN3Pressed, stNoChange, setIncYear, setYearShow},
  90. {stSetWDay, evDisplayWDT, stShowTime, showTime, NULL},
  91. {stSetMDay, evDisplayWDT, stShowTime, showTime, NULL},
  92. {stSetMon, evDisplayWDT, stShowTime, showTime, NULL},
  93. {stSetYear, evDisplayWDT, stShowTime, showTime, NULL},
  94. {stShowWDay, evBTN3Pressed, stNoChange, incWDay, showWDM},
  95. {stShowMDay, evBTN3Pressed, stNoChange, incMDay, showWDM},
  96. {stShowMon, evBTN3Pressed, stNoChange, incMonth, showWDM},
  97. {stShowYear, evBTN3Pressed, stNoChange, incYear, showYear},
  98. {stShowWDay, evBTN2Pressed, stNoChange, decWDay, showWDM},
  99. {stShowMDay, evBTN2Pressed, stNoChange, decMDay, showWDM},
  100. {stShowMon, evBTN2Pressed, stNoChange, decMonth, showWDM},
  101. {stShowYear, evBTN2Pressed, stNoChange, decYear, showYear},
  102. */
  103. /* set bright */
  104. // {stShowBright, evBTN3Pressed, stNoChange, incBright, showBright},
  105. // {stShowBright, evBTN2Pressed, stNoChange, decBright, showBright},
  106. /* mandatory empty table row */
  107. {stNoChange, eventNull, stNoChange, EmptyFunc, EmptyFunc}
  108. };
  109. /**
  110. * @brief Take event.
  111. * @param None
  112. * @retval Event code
  113. */
  114. es_event_t ES_GetEvent(void)
  115. {
  116. es_event_t event;
  117. if (countBuf > 0) {
  118. event = cycleBuf[headBuf];
  119. countBuf --;
  120. headBuf = (headBuf + 1) & (SIZE_BUF - 1);
  121. return event;
  122. }
  123. return eventNull;
  124. }
  125. /**
  126. * @brief Place event.
  127. * @param Event
  128. * @retval None
  129. */
  130. void ES_PlaceEvent(es_event_t event)
  131. {
  132. if (countBuf < SIZE_BUF) {
  133. cycleBuf[tailBuf] = event;
  134. tailBuf = (tailBuf + 1) & (SIZE_BUF - 1);
  135. countBuf ++;
  136. /* сигнализация переполнения буфера событий */
  137. } else {
  138. // tube power off
  139. TIM1->CCER &= ~(TIM_CCER_CC1E);
  140. TIM3->CCER &= ~(TIM_CCER_CC4E);
  141. TIM3->CCER &= ~(TIM_CCER_CC3E);
  142. TIM3->CCER &= ~(TIM_CCER_CC2E);
  143. TIM3->CCER &= ~(TIM_CCER_CC1E);
  144. GPIOA->BSRR = 0x10;
  145. // color - pink
  146. TIM1->CCR2 = 0xff;
  147. TIM1->CCR3 = 0x14;
  148. TIM1->CCR4 = 0x93;
  149. while(1);
  150. }
  151. }
  152. /**
  153. * @brief Initialize event system.
  154. * @param Start Event
  155. * @retval None
  156. */
  157. void ES_Init(es_state_t init_state)
  158. {
  159. tailBuf = 0;
  160. headBuf = 0;
  161. countBuf = 0;
  162. _State = init_state;
  163. }
  164. /**
  165. * @brief Fake function.
  166. * @param None
  167. * @retval None
  168. */
  169. static void EmptyFunc(void)
  170. {
  171. }
  172. /**
  173. * @brief Dispatcher of event system.
  174. * @param Event
  175. * @retval None
  176. */
  177. void ES_Dispatch(es_event_t event)
  178. {
  179. uint8_t i;
  180. void (*pStateFunc1)(void);
  181. void (*pStateFunc2)(void);
  182. pStateFunc1 = NULL;
  183. pStateFunc2 = NULL;
  184. /* get next state */
  185. for (i=0; stateTable[i].state; i++) {
  186. /* if the current state and the received event match */
  187. if (stateTable[i].state == _State && stateTable[i].event == event) {
  188. /* change current state if needed */
  189. if (stateTable[i].nextState != stNoChange) {
  190. _State = stateTable[i].nextState;
  191. }
  192. /* get functions */
  193. pStateFunc1 = stateTable[i].pStateFunc1;
  194. pStateFunc2 = stateTable[i].pStateFunc2;
  195. break;
  196. }
  197. }
  198. if (pStateFunc1) {
  199. pStateFunc1();
  200. }
  201. if (pStateFunc2) {
  202. pStateFunc2();
  203. }
  204. }
  205. /**
  206. * @brief Return current state code.
  207. * @param None
  208. * @retval State code
  209. */
  210. es_state_t ES_GetState(void)
  211. {
  212. return _State;
  213. }
  214. /**
  215. * @brief Set current state to given code .
  216. * @param State
  217. * @retval None
  218. */
  219. void ES_SetState(es_state_t new_state)
  220. {
  221. _State = new_state;
  222. }