event-system.c 7.3 KB

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