clock.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. static void PlusFadeIn(void);
  16. static void PercentFadeIn(void);
  17. static void PressureFadeIn(void);
  18. //static void IN15_FadeIn(in15_pin_t pin);
  19. //static void IN15_FadeOut(in15_pin_t pin);
  20. /* funcions */
  21. /**
  22. * @brief Вывод HEX значений цвета в таймер.
  23. * @param : RGB value in range 0x00-0xFF
  24. * @retval : None
  25. */
  26. static void Color_RGB(uint8_t r, uint8_t g, uint8_t b) {
  27. COLOR_R(r);
  28. COLOR_G(g);
  29. COLOR_B(b);
  30. }
  31. /**
  32. * @brief Обработка кнопок.
  33. * @param : None
  34. * @retval : None
  35. */
  36. void btnProcess(void) {
  37. /* get pin state */
  38. uint32_t pins = BTNS_STATE;
  39. int i;
  40. for (i=0; i<BTN_NUM; i++) {
  41. if ((pins & Button[i].pin) == 0) {
  42. /* button pressed */
  43. Button[i].time ++;
  44. if (Button[i].time >= (BTN_TIME_HOLDED/BTN_SCAN_PERIOD)) {
  45. Button[i].time -= (BTN_TIME_REPEATED/BTN_SCAN_PERIOD);
  46. if (Button[i].holded == Button[i].pressed) {
  47. /* if pressed and holded - same function, then button pressed auto repeat */
  48. ES_PlaceEvent(Button[i].pressed);
  49. }
  50. }
  51. } else if (Button[i].time != 0) {
  52. /* button released */
  53. if (Button[i].time >= ((BTN_TIME_HOLDED - BTN_TIME_REPEATED)/BTN_SCAN_PERIOD)) {
  54. /* process long press */
  55. ES_PlaceEvent(Button[i].holded);
  56. } else if (Button[i].time >= (BTN_TIME_PRESSED/BTN_SCAN_PERIOD)) {
  57. /* process short press */
  58. ES_PlaceEvent(Button[i].pressed);
  59. }
  60. Button[i].time = 0;
  61. RTOS_SetTask(btnProcess, BTN_SCAN_PAUSE, BTN_SCAN_PERIOD);
  62. }
  63. } /* end FOR */
  64. }
  65. void new_Second(void) {
  66. // Blink_Start(); // !!! TODO
  67. RTC_ReadAll(&Clock);
  68. if (dispWDT != 0) {
  69. dispWDT--;
  70. if (dispWDT == 0) {
  71. ES_PlaceEvent(evDisplayWDT);
  72. }
  73. }
  74. }
  75. /**
  76. * On/off symbols on IN-15 tube.
  77. */
  78. void in15Off(void) {
  79. IN15_OFF;
  80. TUBE_C_OFF;
  81. }
  82. void in15Minus(void) {
  83. IN15_OFF;
  84. IN15_Minus;
  85. TUBE_C_ON;
  86. }
  87. void in15Plus(void) {
  88. IN15_OFF;
  89. IN15_Plus;
  90. TUBE_C_ON;
  91. }
  92. void in15Percent(void) {
  93. IN15_OFF;
  94. IN15_Percent;
  95. TUBE_C_ON;
  96. }
  97. void in15P(void) {
  98. IN15_OFF;
  99. IN15_P;
  100. TUBE_C_ON;
  101. }
  102. /** 'Faded' funcions */
  103. static void MinusFadeIn(void) {
  104. static uint8_t on = FADE_START;
  105. static uint8_t off = FADE_STOP;
  106. static uint8_t st = 0;
  107. if (st == 0) {
  108. st = 1;
  109. IN15_Minus;
  110. on += FADE_STEP;
  111. if (on < FADE_STOP) {
  112. RTOS_SetTask(MinusFadeIn, on, 0);
  113. } else {
  114. on = FADE_START; off = FADE_STOP; st = 0;
  115. }
  116. } else {
  117. st = 0;
  118. IN15_OFF;
  119. off -= FADE_STEP;
  120. RTOS_SetTask(MinusFadeIn, off, 0);
  121. }
  122. }
  123. static void MinusFadeOut(void) {
  124. static uint8_t off = FADE_START;
  125. static uint8_t on = FADE_STOP;
  126. static uint8_t st = 0;
  127. if (st == 0) {
  128. st = 1;
  129. IN15_OFF;
  130. off += FADE_STEP;
  131. if (off < FADE_STOP) {
  132. RTOS_SetTask(MinusFadeOut, off, 0);
  133. } else {
  134. off = FADE_START; on = FADE_STOP; st = 0;
  135. }
  136. } else {
  137. st = 0;
  138. IN15_Minus;
  139. on -= FADE_STEP;
  140. RTOS_SetTask(MinusFadeOut, on, 0);
  141. }
  142. }
  143. static void PlusFadeIn(void) {
  144. static uint8_t on = FADE_START;
  145. static uint8_t off = FADE_STOP;
  146. static uint8_t st = 0;
  147. if (st == 0) {
  148. st = 1;
  149. IN15_Plus;
  150. on += FADE_STEP;
  151. if (on < FADE_STOP) {
  152. RTOS_SetTask(PlusFadeIn, on, 0);
  153. } else {
  154. on = FADE_START; off = FADE_STOP; st = 0;
  155. }
  156. } else {
  157. st = 0;
  158. IN15_OFF;
  159. off -= FADE_STEP;
  160. RTOS_SetTask(PlusFadeIn, off, 0);
  161. }
  162. }
  163. static void PercentFadeIn(void) {
  164. static uint8_t on = FADE_START;
  165. static uint8_t off = FADE_STOP;
  166. static uint8_t st = 0;
  167. if (st == 0) {
  168. st = 1;
  169. IN15_Percent;
  170. on += FADE_STEP;
  171. if (on < FADE_STOP) {
  172. RTOS_SetTask(PercentFadeIn, on, 0);
  173. } else {
  174. on = FADE_START; off = FADE_STOP; st = 0;
  175. }
  176. } else {
  177. st = 0;
  178. IN15_OFF;
  179. off -= FADE_STEP;
  180. RTOS_SetTask(PercentFadeIn, off, 0);
  181. }
  182. }
  183. static void PressureFadeIn(void) {
  184. static uint8_t on = FADE_START;
  185. static uint8_t off = FADE_STOP;
  186. static uint8_t st = 0;
  187. if (st == 0) {
  188. st = 1;
  189. IN15_P;
  190. on += FADE_STEP;
  191. if (on < FADE_STOP) {
  192. RTOS_SetTask(PressureFadeIn, on, 0);
  193. } else {
  194. on = FADE_START; off = FADE_STOP; st = 0;
  195. }
  196. } else {
  197. st = 0;
  198. IN15_OFF;
  199. off -= FADE_STEP;
  200. RTOS_SetTask(PressureFadeIn, off, 0);
  201. }
  202. }
  203. #ifdef NEW_SHED
  204. static void IN15_FadeIn(in15_pin_t pin) {
  205. static uint8_t on = FADE_START;
  206. static uint8_t off = FADE_STOP;
  207. static uint8_t st = 0;
  208. if (st == 0) {
  209. st = 1;
  210. GPIOA->BSRR = pin;
  211. on += FADE_STEP;
  212. if (on < FADE_STOP) {
  213. RTOS_SetTask(IN15_FadeIn, on, 0);
  214. } else {
  215. on = FADE_START; off = FADE_STOP; st = 0;
  216. }
  217. } else {
  218. st = 0;
  219. IN15_OFF;
  220. off -= FADE_STEP;
  221. RTOS_SetTask(IN15_FadeIn, off, 0);
  222. }
  223. }
  224. static void IN15_FadeOut(in15_pin_t pin) {
  225. static uint8_t off = FADE_START;
  226. static uint8_t on = FADE_STOP;
  227. static uint8_t st = 0;
  228. if (st == 0) {
  229. st = 1;
  230. IN15_OFF;
  231. off += FADE_STEP;
  232. if (off < FADE_STOP) {
  233. RTOS_SetTask(IN15_FadeOut, off, 0);
  234. } else {
  235. off = FADE_START; on = FADE_STOP; st = 0;
  236. }
  237. } else {
  238. st = 0;
  239. GPIOA->BSRR = pin;
  240. on -= FADE_STEP;
  241. RTOS_SetTask(IN15_FadeOut, on, 0);
  242. }
  243. }
  244. #endif /* NewShed */
  245. /**
  246. * Show info on tubes.
  247. */
  248. void showTime(void) {
  249. MinusFadeIn();
  250. RTOS_SetTask(MinusFadeOut, 500, 0);
  251. uint8_t hue = bcd2bin(Clock.Sec);
  252. HSV2LED(hue, 255, cie[Lvl_Mdl]);
  253. tube4_t buf;
  254. /*
  255. buf.s8.tA = Clock.Hr >> 4;
  256. buf.s8.tB = Clock.Hr & 0xf;
  257. buf.s8.tD = Clock.Min >> 4;
  258. buf.s8.tE = Clock.Min & 0xf;
  259. */
  260. buf.s8.tA = Clock.Min >> 4;
  261. buf.s8.tB = Clock.Min & 0xf;
  262. buf.s8.tD = Clock.Sec >> 4;
  263. buf.s8.tE = Clock.Sec & 0xf;
  264. showDigits(buf);
  265. }
  266. void showWD(void) {
  267. dispWDT = DISP_WDT_TIME;
  268. IN15_OFF;
  269. tube4_t buf;
  270. buf.s8.tA = 0xf;
  271. buf.s8.tB = Clock.WD & 0xf;
  272. buf.s8.tD = 0xf;
  273. buf.s8.tE = 0xf;
  274. showDigits(buf);
  275. }
  276. void showDay(void) {
  277. dispWDT = DISP_WDT_TIME;
  278. IN15_OFF;
  279. tube4_t buf;
  280. buf.s8.tA = Clock.Day >> 4;
  281. buf.s8.tB = Clock.Day & 0xf;
  282. buf.s8.tD = 0xf;
  283. buf.s8.tE = 0xf;
  284. showDigits(buf);
  285. }
  286. void showMonth(void) {
  287. dispWDT = DISP_WDT_TIME;
  288. IN15_OFF;
  289. tube4_t buf;
  290. buf.s8.tA = 0xf;
  291. buf.s8.tB = 0xf;
  292. buf.s8.tD = Clock.Mon >> 4;
  293. buf.s8.tE = Clock.Mon & 0xf;
  294. showDigits(buf);
  295. }
  296. void showDayMon(void) {
  297. dispWDT = DISP_WDT_TIME;
  298. IN15_OFF;
  299. tube4_t buf;
  300. buf.s8.tA = Clock.Day >> 4;
  301. buf.s8.tB = Clock.Day & 0xf;
  302. buf.s8.tD = Clock.Mon >> 4;
  303. buf.s8.tE = Clock.Mon & 0xf;
  304. showDigits(buf);
  305. }
  306. void showYear(void) {
  307. dispWDT = DISP_WDT_TIME;
  308. IN15_OFF;
  309. tube4_t buf;
  310. buf.s8.tA = 2;
  311. buf.s8.tB = 0;
  312. buf.s8.tD = Clock.Year >> 4;
  313. buf.s8.tE = Clock.Year & 0xf;
  314. showDigits(buf);
  315. }
  316. void showHumidity(void) {
  317. dispWDT = DISP_WDT_TIME;
  318. //in15Percent();
  319. //IN15_FadeIn(sym_Percent);
  320. PercentFadeIn();
  321. tube4_t buf;
  322. buf.s8.tA = Humidity / 10;
  323. buf.s8.tB = Humidity % 10;
  324. buf.s8.tD = 0xf;
  325. buf.s8.tE = 0xf;
  326. showDigits(buf);
  327. }
  328. void showTemperature(void) {
  329. dispWDT = DISP_WDT_TIME;
  330. //in15Plus();
  331. //IN15_FadeIn(sym_Plus);
  332. PlusFadeIn();
  333. tube4_t buf;
  334. buf.s8.tA = 0xf;
  335. buf.s8.tB = 0xf;
  336. buf.s8.tD = Temperature / 10;
  337. buf.s8.tE = Temperature % 10;
  338. showDigits(buf);
  339. }
  340. void showPressure(void) {
  341. dispWDT = DISP_WDT_TIME;
  342. //in15P();
  343. //IN15_FadeIn(sym_Pressure);
  344. PressureFadeIn();
  345. tube4_t buf;
  346. int tmp;
  347. buf.s8.tA = 0xf;
  348. buf.s8.tB = Pressure / 100;
  349. tmp = Pressure % 100;
  350. buf.s8.tD = tmp / 10;
  351. buf.s8.tE = tmp % 10;
  352. showDigits(buf);
  353. }
  354. /* Simple function for cyclic show all sensor data */
  355. void showSensorData(void) {
  356. ES_SetState(stShowSensorData);
  357. RTOS_DeleteTask(MinusFadeOut);
  358. //HSV2LED(1, 255, cie[Lvl_Mdl]); // Nixie color
  359. HSV2LED(0, 255, cie[Lvl_Mdl]); // RED
  360. showTemperature();
  361. tdelay_ms(3000);
  362. HSV2LED(40, 255, cie[Lvl_Mdl]); // BLUE
  363. showHumidity();
  364. tdelay_ms(3000);
  365. HSV2LED(20, 255, cie[Lvl_Mdl]); // GREEN
  366. showPressure();
  367. tdelay_ms(2700);
  368. ES_SetState(stShowTime);
  369. // showTime();
  370. }