event-system.c 7.1 KB

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