event-system.c 7.2 KB

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