clock.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #include "clock.h"
  2. /* variables */
  3. rtc_t Clock;
  4. static btn_t Button[BTN_NUM] = {
  5. {0, evBTN1Pressed, evBTN1Holded, BTN1_PIN},
  6. {0, evBTN2Pressed, evBTN2Pressed, BTN2_PIN},
  7. {0, evBTN3Pressed, evBTN3Pressed, BTN3_PIN},
  8. {0, evBTN4Pressed, evBTN4Holded, BTN4_PIN}
  9. };
  10. static volatile uint8_t dispWDT = 0;
  11. /* function prototypes */
  12. static void Color_RGB(const uint8_t r, const uint8_t g, const uint8_t b);
  13. static void MinusFadeIn(void);
  14. static void MinusFadeOut(void);
  15. /* funcions */
  16. /**
  17. * @brief Вывод HEX значений цвета в таймер.
  18. * @param : RGB value in range 0x00-0xFF
  19. * @retval : None
  20. */
  21. static void Color_RGB(uint8_t r, uint8_t g, uint8_t b) {
  22. COLOR_R(r);
  23. COLOR_G(g);
  24. COLOR_B(b);
  25. }
  26. /**
  27. * @brief Обработка кнопок.
  28. * @param : None
  29. * @retval : None
  30. */
  31. void btnProcess(void) {
  32. /* get pin state */
  33. uint32_t pins = BTNS_STATE;
  34. int i;
  35. for (i=0; i<BTN_NUM; i++) {
  36. if ((pins & Button[i].pin) == 0) {
  37. /* button pressed */
  38. Button[i].time ++;
  39. if (Button[i].time >= (BTN_TIME_HOLDED/BTN_SCAN_PERIOD)) {
  40. Button[i].time -= (BTN_TIME_REPEATED/BTN_SCAN_PERIOD);
  41. if (Button[i].holded == Button[i].pressed) {
  42. /* if pressed and holded - same function, then button pressed auto repeat */
  43. ES_PlaceEvent(Button[i].pressed);
  44. }
  45. }
  46. } else if (Button[i].time != 0) {
  47. /* button released */
  48. if (Button[i].time >= ((BTN_TIME_HOLDED - BTN_TIME_REPEATED)/BTN_SCAN_PERIOD)) {
  49. /* process long press */
  50. ES_PlaceEvent(Button[i].holded);
  51. } else if (Button[i].time >= (BTN_TIME_PRESSED/BTN_SCAN_PERIOD)) {
  52. /* process short press */
  53. ES_PlaceEvent(Button[i].pressed);
  54. }
  55. Button[i].time = 0;
  56. RTOS_SetTask(btnProcess, BTN_SCAN_PAUSE, BTN_SCAN_PERIOD);
  57. }
  58. } /* end FOR */
  59. }
  60. void new_Second(void) {
  61. // Blink_Start(); // !!! TODO
  62. RTC_ReadAll(&Clock);
  63. if (dispWDT != 0) {
  64. dispWDT--;
  65. if (dispWDT == 0) {
  66. ES_PlaceEvent(evDisplayWDT);
  67. }
  68. }
  69. }
  70. /**
  71. * On/off symbols on IN-15 tube.
  72. */
  73. void in15Off(void) {
  74. IN15_OFF;
  75. TUBE_C_OFF;
  76. }
  77. void in15Minus(void) {
  78. IN15_OFF;
  79. IN15_Minus;
  80. TUBE_C_ON;
  81. }
  82. void in15Plus(void) {
  83. IN15_OFF;
  84. IN15_Plus;
  85. TUBE_C_ON;
  86. }
  87. void in15Percent(void) {
  88. IN15_OFF;
  89. IN15_Percent;
  90. TUBE_C_ON;
  91. }
  92. void in15P(void) {
  93. IN15_OFF;
  94. IN15_P;
  95. TUBE_C_ON;
  96. }
  97. /** 'Faded' funcions */
  98. static void MinusFadeIn(void) {
  99. static uint8_t on = 0;
  100. static uint8_t off = 20;
  101. static uint8_t st = 0;
  102. if (st == 0) {
  103. st = 1;
  104. IN15_Minus;
  105. on += 2;
  106. if (on < 20) {
  107. RTOS_SetTask(MinusFadeIn, on, 0);
  108. } else {
  109. on = 0; off = 20; st = 0;
  110. }
  111. } else {
  112. st = 0;
  113. IN15_OFF;
  114. off -= 2;
  115. RTOS_SetTask(MinusFadeIn, off, 0);
  116. }
  117. }
  118. static void MinusFadeOut(void) {
  119. static uint8_t off = 0;
  120. static uint8_t on = 20;
  121. static uint8_t st = 0;
  122. if (st == 0) {
  123. st = 1;
  124. IN15_OFF;
  125. off += 2;
  126. if (off < 20) {
  127. RTOS_SetTask(MinusFadeOut, off, 0);
  128. } else {
  129. off = 0; on = 20; st = 0;
  130. }
  131. } else {
  132. st = 0;
  133. IN15_Minus;
  134. on -= 2;
  135. RTOS_SetTask(MinusFadeOut, on, 0);
  136. }
  137. }
  138. /**
  139. * Show info on tubes.
  140. */
  141. void showTime(void) {
  142. MinusFadeIn();
  143. RTOS_SetTask(MinusFadeOut, 500, 0);
  144. uint8_t hue = bcd2bin(Clock.Sec);
  145. HSV2LED(hue, 255, cie[Lvl_Mdl]);
  146. tube4_t buf;
  147. /*
  148. buf.s8.tA = Clock.Hr >> 4;
  149. buf.s8.tB = Clock.Hr & 0xf;
  150. buf.s8.tD = Clock.Min >> 4;
  151. buf.s8.tE = Clock.Min & 0xf;
  152. */
  153. buf.s8.tA = Clock.Min >> 4;
  154. buf.s8.tB = Clock.Min & 0xf;
  155. buf.s8.tD = Clock.Sec >> 4;
  156. buf.s8.tE = Clock.Sec & 0xf;
  157. showDigits(buf);
  158. }
  159. void showWD(void) {
  160. dispWDT = DISP_WDT_TIME;
  161. IN15_OFF;
  162. tube4_t buf;
  163. buf.s8.tA = 0xf;
  164. buf.s8.tB = Clock.WD & 0xf;
  165. buf.s8.tD = 0xf;
  166. buf.s8.tE = 0xf;
  167. showDigits(buf);
  168. }
  169. void showDay(void) {
  170. dispWDT = DISP_WDT_TIME;
  171. IN15_OFF;
  172. tube4_t buf;
  173. buf.s8.tA = Clock.Day >> 4;
  174. buf.s8.tB = Clock.Day & 0xf;
  175. buf.s8.tD = 0xf;
  176. buf.s8.tE = 0xf;
  177. showDigits(buf);
  178. }
  179. void showMonth(void) {
  180. dispWDT = DISP_WDT_TIME;
  181. IN15_OFF;
  182. tube4_t buf;
  183. buf.s8.tA = 0xf;
  184. buf.s8.tB = 0xf;
  185. buf.s8.tD = Clock.Mon >> 4;
  186. buf.s8.tE = Clock.Mon & 0xf;
  187. showDigits(buf);
  188. }
  189. void showDayMon(void) {
  190. dispWDT = DISP_WDT_TIME;
  191. IN15_OFF;
  192. tube4_t buf;
  193. buf.s8.tA = Clock.Day >> 4;
  194. buf.s8.tB = Clock.Day & 0xf;
  195. buf.s8.tD = Clock.Mon >> 4;
  196. buf.s8.tE = Clock.Mon & 0xf;
  197. showDigits(buf);
  198. }
  199. void showYear(void) {
  200. dispWDT = DISP_WDT_TIME;
  201. IN15_OFF;
  202. tube4_t buf;
  203. buf.s8.tA = 2;
  204. buf.s8.tB = 0;
  205. buf.s8.tD = Clock.Year >> 4;
  206. buf.s8.tE = Clock.Year & 0xf;
  207. showDigits(buf);
  208. }
  209. void showHumidity(void) {
  210. dispWDT = DISP_WDT_TIME;
  211. in15Percent();
  212. tube4_t buf;
  213. buf.s8.tA = Humidity / 10;
  214. buf.s8.tB = Humidity % 10;
  215. buf.s8.tD = 0xf;
  216. buf.s8.tE = 0xf;
  217. showDigits(buf);
  218. }
  219. void showTemperature(void) {
  220. dispWDT = DISP_WDT_TIME;
  221. in15Plus();
  222. tube4_t buf;
  223. buf.s8.tA = 0xf;
  224. buf.s8.tB = 0xf;
  225. buf.s8.tD = Temperature / 10;
  226. buf.s8.tE = Temperature % 10;
  227. showDigits(buf);
  228. }
  229. void showPressure(void) {
  230. dispWDT = DISP_WDT_TIME;
  231. in15P();
  232. tube4_t buf;
  233. int tmp;
  234. buf.s8.tA = 0xf;
  235. buf.s8.tB = Pressure / 100;
  236. tmp = Pressure % 100;
  237. buf.s8.tD = tmp / 10;
  238. buf.s8.tE = tmp % 10;
  239. showDigits(buf);
  240. }
  241. /* Simple function for cyclic show all sensor data */
  242. void showSensorData(void) {
  243. ES_SetState(stShowSensorData);
  244. RTOS_DeleteTask(MinusFadeOut);
  245. HSV2LED(1, 255, cie[Lvl_Mdl]); // Nixie color
  246. showTemperature();
  247. tdelay_ms(3000);
  248. showHumidity();
  249. tdelay_ms(3000);
  250. showPressure();
  251. tdelay_ms(2500);
  252. ES_SetState(stShowTime);
  253. // showTime();
  254. }