clock.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. #include "clock.h"
  2. /* type defs */
  3. typedef struct {
  4. uint8_t time;
  5. es_event_t pressed;
  6. es_event_t holded;
  7. uint32_t pin;
  8. //(GPIO_TypeDef *) GPIOA; // ?->IDR
  9. } btn_t;
  10. /* variables */
  11. rtc_t Clock;
  12. static rtc_t setClock;
  13. static btn_t Button[BTN_NUM] = {
  14. {0, evBTN1Pressed, evBTN1Holded, BTN1_PIN},
  15. {0, evBTN2Pressed, evBTN2Pressed, BTN2_PIN},
  16. {0, evBTN3Pressed, evBTN3Pressed, BTN3_PIN},
  17. {0, evBTN4Pressed, evBTN4Holded, BTN4_PIN}
  18. };
  19. volatile static uint8_t dispWDT = 0;
  20. static in15_pin_t symToFade = 0;
  21. /* function prototypes */
  22. //static void Color_RGB(const uint8_t r, const uint8_t g, const uint8_t b);
  23. static void MinusFadeIn(void);
  24. static void MinusFadeOut(void);
  25. static void PlusFadeIn(void);
  26. static void PercentFadeIn(void);
  27. static void PressureFadeIn(void);
  28. static void IN15_FadeIn(void);
  29. static void IN15_FadeOut(void);
  30. static void valIncrease(uint8_t * val, uint8_t max);
  31. static void valDecrease(uint8_t * val, uint8_t max);
  32. /* funcions */
  33. /**
  34. * @brief Вывод HEX значений цвета в таймер.
  35. * @param : RGB value in range 0x00-0xFF
  36. * @retval : None
  37. */
  38. static void Color_RGB(uint8_t r, uint8_t g, uint8_t b) {
  39. COLOR_R(r);
  40. COLOR_G(g);
  41. COLOR_B(b);
  42. }
  43. /**
  44. * @brief Обработка кнопок.
  45. * @param : None
  46. * @retval : None
  47. */
  48. void btnProcess(void) {
  49. /* get pin state */
  50. uint32_t pins = BTNS_STATE;
  51. int i;
  52. for (i=0; i<BTN_NUM; i++) {
  53. if ((pins & Button[i].pin) == 0) {
  54. /* button pressed */
  55. Button[i].time ++;
  56. if (Button[i].time >= (BTN_TIME_HOLDED/BTN_SCAN_PERIOD)) {
  57. Button[i].time -= (BTN_TIME_REPEATED/BTN_SCAN_PERIOD);
  58. if (Button[i].holded == Button[i].pressed) {
  59. /* if pressed and holded - same function, then button pressed auto repeat */
  60. ES_PlaceEvent(Button[i].pressed);
  61. }
  62. }
  63. } else if (Button[i].time != 0) {
  64. /* button released */
  65. if (Button[i].time >= ((BTN_TIME_HOLDED - BTN_TIME_REPEATED)/BTN_SCAN_PERIOD)) {
  66. /* process long press */
  67. ES_PlaceEvent(Button[i].holded);
  68. } else if (Button[i].time >= (BTN_TIME_PRESSED/BTN_SCAN_PERIOD)) {
  69. /* process short press */
  70. ES_PlaceEvent(Button[i].pressed);
  71. }
  72. Button[i].time = 0;
  73. RTOS_SetTask(btnProcess, BTN_SCAN_PAUSE, BTN_SCAN_PERIOD);
  74. }
  75. } /* end FOR */
  76. }
  77. void new_Second(void) {
  78. // Blink_Start(); // !!! TODO
  79. RTC_ReadAll(&Clock);
  80. if (dispWDT != 0) {
  81. dispWDT--;
  82. if (dispWDT == 0) {
  83. ES_PlaceEvent(evDisplayWDT);
  84. }
  85. }
  86. }
  87. /**
  88. * On/off symbols on IN-15 tube.
  89. */
  90. void in15Off(void) {
  91. IN15_OFF;
  92. TUBE_C_OFF;
  93. }
  94. void in15Minus(void) {
  95. IN15_OFF;
  96. IN15_Minus;
  97. TUBE_C_ON;
  98. }
  99. void in15Plus(void) {
  100. IN15_OFF;
  101. IN15_Plus;
  102. TUBE_C_ON;
  103. }
  104. void in15Percent(void) {
  105. IN15_OFF;
  106. IN15_Percent;
  107. TUBE_C_ON;
  108. }
  109. void in15P(void) {
  110. IN15_OFF;
  111. IN15_P;
  112. TUBE_C_ON;
  113. }
  114. /** 'Faded' funcions */
  115. static void MinusFadeIn(void) {
  116. static uint8_t on = FADE_START;
  117. static uint8_t off = FADE_STOP;
  118. static uint8_t st = 0;
  119. if (st == 0) {
  120. st = 1;
  121. IN15_Minus;
  122. on += FADE_STEP;
  123. if (on < FADE_STOP) {
  124. RTOS_SetTask(MinusFadeIn, on, 0);
  125. } else {
  126. on = FADE_START; off = FADE_STOP; st = 0;
  127. }
  128. } else {
  129. st = 0;
  130. IN15_OFF;
  131. off -= FADE_STEP;
  132. RTOS_SetTask(MinusFadeIn, off, 0);
  133. }
  134. }
  135. static void MinusFadeOut(void) {
  136. static uint8_t off = FADE_START;
  137. static uint8_t on = FADE_STOP;
  138. static uint8_t st = 0;
  139. if (st == 0) {
  140. st = 1;
  141. IN15_OFF;
  142. off += FADE_STEP;
  143. if (off < FADE_STOP) {
  144. RTOS_SetTask(MinusFadeOut, off, 0);
  145. } else {
  146. off = FADE_START; on = FADE_STOP; st = 0;
  147. }
  148. } else {
  149. st = 0;
  150. IN15_Minus;
  151. on -= FADE_STEP;
  152. RTOS_SetTask(MinusFadeOut, on, 0);
  153. }
  154. }
  155. static void PlusFadeIn(void) {
  156. static uint8_t on = FADE_START;
  157. static uint8_t off = FADE_STOP;
  158. static uint8_t st = 0;
  159. if (st == 0) {
  160. st = 1;
  161. IN15_Plus;
  162. on += FADE_STEP;
  163. if (on < FADE_STOP) {
  164. RTOS_SetTask(PlusFadeIn, on, 0);
  165. } else {
  166. on = FADE_START; off = FADE_STOP; st = 0;
  167. }
  168. } else {
  169. st = 0;
  170. IN15_OFF;
  171. off -= FADE_STEP;
  172. RTOS_SetTask(PlusFadeIn, off, 0);
  173. }
  174. }
  175. static void PercentFadeIn(void) {
  176. static uint8_t on = FADE_START;
  177. static uint8_t off = FADE_STOP;
  178. static uint8_t st = 0;
  179. if (st == 0) {
  180. st = 1;
  181. IN15_Percent;
  182. on += FADE_STEP;
  183. if (on < FADE_STOP) {
  184. RTOS_SetTask(PercentFadeIn, on, 0);
  185. } else {
  186. on = FADE_START; off = FADE_STOP; st = 0;
  187. }
  188. } else {
  189. st = 0;
  190. IN15_OFF;
  191. off -= FADE_STEP;
  192. RTOS_SetTask(PercentFadeIn, off, 0);
  193. }
  194. }
  195. static void PressureFadeIn(void) {
  196. static uint8_t on = FADE_START;
  197. static uint8_t off = FADE_STOP;
  198. static uint8_t st = 0;
  199. if (st == 0) {
  200. st = 1;
  201. IN15_P;
  202. on += FADE_STEP;
  203. if (on < FADE_STOP) {
  204. RTOS_SetTask(PressureFadeIn, on, 0);
  205. } else {
  206. on = FADE_START; off = FADE_STOP; st = 0;
  207. }
  208. } else {
  209. st = 0;
  210. IN15_OFF;
  211. off -= FADE_STEP;
  212. RTOS_SetTask(PressureFadeIn, off, 0);
  213. }
  214. }
  215. static void IN15_FadeIn(void) {
  216. static uint8_t on = FADE_START;
  217. static uint8_t off = FADE_STOP;
  218. static uint8_t st = 0;
  219. if (st == 0) {
  220. st = 1;
  221. GPIOA->BSRR = symToFade;
  222. on += FADE_STEP;
  223. if (on < FADE_STOP) {
  224. RTOS_SetTask(IN15_FadeIn, on, 0);
  225. } else {
  226. on = FADE_START; off = FADE_STOP; st = 0; symToFade = 0;
  227. }
  228. } else {
  229. st = 0;
  230. IN15_OFF;
  231. off -= FADE_STEP;
  232. RTOS_SetTask(IN15_FadeIn, off, 0);
  233. }
  234. }
  235. static void IN15_FadeOut(void) {
  236. static uint8_t off = FADE_START;
  237. static uint8_t on = FADE_STOP;
  238. static uint8_t st = 0;
  239. if (st == 0) {
  240. st = 1;
  241. IN15_OFF;
  242. off += FADE_STEP;
  243. if (off < FADE_STOP) {
  244. RTOS_SetTask(IN15_FadeOut, off, 0);
  245. } else {
  246. off = FADE_START; on = FADE_STOP; st = 0; symToFade = 0;
  247. }
  248. } else {
  249. st = 0;
  250. GPIOA->BSRR = symToFade;
  251. on -= FADE_STEP;
  252. RTOS_SetTask(IN15_FadeOut, on, 0);
  253. }
  254. }
  255. /**
  256. * Show info on tubes.
  257. */
  258. void showTime(void) {
  259. MinusFadeIn();
  260. RTOS_SetTask(MinusFadeOut, 500, 0);
  261. uint8_t hue = bcd2bin(Clock.Sec);
  262. HSV2LED(hue, 255, cie[Lvl_Mdl]);
  263. tube4_t buf;
  264. buf.s8.tA = Clock.Hr >> 4;
  265. buf.s8.tB = Clock.Hr & 0xf;
  266. buf.s8.tD = Clock.Min >> 4;
  267. buf.s8.tE = Clock.Min & 0xf;
  268. showDigits(buf);
  269. }
  270. void showMMSS(void) {
  271. RTOS_DeleteTask(MinusFadeOut);
  272. IN15_Minus;
  273. uint8_t hue = bcd2bin(Clock.Sec);
  274. HSV2LED(hue, 255, cie[Lvl_Mdl]);
  275. tube4_t buf;
  276. buf.s8.tA = Clock.Min >> 4;
  277. buf.s8.tB = Clock.Min & 0xf;
  278. buf.s8.tD = Clock.Sec >> 4;
  279. buf.s8.tE = Clock.Sec & 0xf;
  280. showDigits(buf);
  281. }
  282. void showWD(void) {
  283. dispWDT = DISP_WDT_TIME;
  284. IN15_OFF;
  285. tube4_t buf;
  286. buf.s8.tA = 0xf;
  287. buf.s8.tB = Clock.WD & 0xf;
  288. buf.s8.tD = 0xf;
  289. buf.s8.tE = 0xf;
  290. showDigits(buf);
  291. }
  292. void showDay(void) {
  293. dispWDT = DISP_WDT_TIME;
  294. IN15_OFF;
  295. tube4_t buf;
  296. buf.s8.tA = Clock.Day >> 4;
  297. buf.s8.tB = Clock.Day & 0xf;
  298. buf.s8.tD = 0xf;
  299. buf.s8.tE = 0xf;
  300. showDigits(buf);
  301. }
  302. void showMonth(void) {
  303. dispWDT = DISP_WDT_TIME;
  304. IN15_OFF;
  305. tube4_t buf;
  306. buf.s8.tA = 0xf;
  307. buf.s8.tB = 0xf;
  308. buf.s8.tD = Clock.Mon >> 4;
  309. buf.s8.tE = Clock.Mon & 0xf;
  310. showDigits(buf);
  311. }
  312. void showDayMon(void) {
  313. dispWDT = DISP_WDT_TIME;
  314. IN15_OFF;
  315. tube4_t buf;
  316. buf.s8.tA = Clock.Day >> 4;
  317. buf.s8.tB = Clock.Day & 0xf;
  318. buf.s8.tD = Clock.Mon >> 4;
  319. buf.s8.tE = Clock.Mon & 0xf;
  320. showDigits(buf);
  321. }
  322. void showYear(void) {
  323. dispWDT = DISP_WDT_TIME;
  324. IN15_OFF;
  325. tube4_t buf;
  326. buf.s8.tA = 2;
  327. buf.s8.tB = 0;
  328. buf.s8.tD = Clock.Year >> 4;
  329. buf.s8.tE = Clock.Year & 0xf;
  330. showDigits(buf);
  331. }
  332. void showHumidity(void) {
  333. dispWDT = DISP_WDT_TIME;
  334. //in15Percent();
  335. symToFade = sym_Percent;
  336. IN15_FadeIn();
  337. //PercentFadeIn();
  338. tube4_t buf;
  339. buf.s8.tA = Humidity / 10;
  340. buf.s8.tB = Humidity % 10;
  341. buf.s8.tD = 0xf;
  342. buf.s8.tE = 0xf;
  343. showDigits(buf);
  344. }
  345. void showTemperature(void) {
  346. dispWDT = DISP_WDT_TIME;
  347. //in15Plus();
  348. symToFade = sym_Plus;
  349. IN15_FadeIn();
  350. //PlusFadeIn();
  351. tube4_t buf;
  352. buf.s8.tA = 0xf;
  353. buf.s8.tB = 0xf;
  354. buf.s8.tD = Temperature / 10;
  355. buf.s8.tE = Temperature % 10;
  356. showDigits(buf);
  357. }
  358. void showPressure(void) {
  359. dispWDT = DISP_WDT_TIME;
  360. //in15P();
  361. symToFade = sym_Pressure;
  362. IN15_FadeIn();
  363. //PressureFadeIn();
  364. tube4_t buf;
  365. int tmp;
  366. buf.s8.tA = 0xf;
  367. buf.s8.tB = Pressure / 100;
  368. tmp = Pressure % 100;
  369. buf.s8.tD = tmp / 10;
  370. buf.s8.tE = tmp % 10;
  371. showDigits(buf);
  372. }
  373. /* Simple function for cyclic show all sensor data */
  374. void showSensorData(void) {
  375. RTOS_DeleteTask(MinusFadeOut);
  376. //HSV2LED(1, 255, cie[Lvl_Mdl]); // Nixie color
  377. HSV2LED(0, 255, cie[Lvl_Mdl]); // RED
  378. showTemperature();
  379. tdelay_ms(3000);
  380. HSV2LED(40, 255, cie[Lvl_Mdl]); // BLUE
  381. showHumidity();
  382. tdelay_ms(3000);
  383. HSV2LED(20, 255, cie[Lvl_Mdl]); // GREEN
  384. showPressure();
  385. tdelay_ms(2700);
  386. ES_SetState(stShowTime);
  387. // showTime();
  388. }
  389. void setTimeShow(void) {
  390. dispWDT = DISP_WDT_TIME;
  391. in15Minus();
  392. HSV2LED(1, 255, cie[Lvl_2]);
  393. tube4_t buf;
  394. buf.s8.tA = setClock.Hr >> 4;
  395. buf.s8.tB = setClock.Hr & 0xf;
  396. buf.s8.tD = setClock.Min >> 4;
  397. buf.s8.tE = setClock.Min & 0xf;
  398. showDigits(buf);
  399. }
  400. void setTimeBegin(void) {
  401. dispWDT = DISP_WDT_TIME;
  402. RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
  403. RTC_ReadAll(&setClock);
  404. }
  405. void setHHBegin(void) {
  406. dispWDT = DISP_WDT_TIME;
  407. Flag.Blink_1 = 1;
  408. Flag.Blink_2 = 1;
  409. Flag.Blink_4 = 0;
  410. Flag.Blink_5 = 0;
  411. Blink_Start();
  412. setTimeShow();
  413. }
  414. void setHHInc(void) {
  415. dispWDT = DISP_WDT_TIME;
  416. valIncrease(&setClock.Hr, 23);
  417. }
  418. void setHHDec(void) {
  419. dispWDT = DISP_WDT_TIME;
  420. valDecrease(&setClock.Hr, 23);
  421. }
  422. void setMMBegin(void) {
  423. dispWDT = DISP_WDT_TIME;
  424. Flag.Blink_1 = 0;
  425. Flag.Blink_2 = 0;
  426. Flag.Blink_4 = 1;
  427. Flag.Blink_5 = 1;
  428. Blink_Start();
  429. setTimeShow();
  430. }
  431. void setMMInc(void) {
  432. dispWDT = DISP_WDT_TIME;
  433. valIncrease(&setClock.Min, 59);
  434. }
  435. void setMMDec(void) {
  436. dispWDT = DISP_WDT_TIME;
  437. valDecrease(&setClock.Min, 59);
  438. }
  439. void setTimeEnd(void) {
  440. RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
  441. setClock.Sec = 0;
  442. RTC_WriteTime(&setClock);
  443. Flag.Blink_1 = 0;
  444. Flag.Blink_2 = 0;
  445. Flag.Blink_4 = 0;
  446. Flag.Blink_5 = 0;
  447. Blink_Stop();
  448. RTC_ReadAll(&Clock);
  449. }
  450. void setDateBegin(void) {
  451. }
  452. void setDateEnd(void) {
  453. }
  454. void setWShow(void) {
  455. }
  456. void setDMShow(void) {
  457. }
  458. void setYearShow(void) {
  459. }
  460. void setWDayBegin(void) {
  461. }
  462. void setMDayBegin(void) {
  463. }
  464. void setMonthBegin(void) {
  465. }
  466. void setYearBegin(void) {
  467. }
  468. void setIncWDay(void) {
  469. }
  470. void setIncMDay(void) {
  471. }
  472. void setIncMonth(void) {
  473. }
  474. void setIncYear(void) {
  475. }
  476. void setDecWDay(void) {
  477. }
  478. void setDecMDay(void) {
  479. }
  480. void setDecMonth(void) {
  481. }
  482. void setDecYear(void) {
  483. }
  484. /**
  485. * @brief Increase BCD value.
  486. * @param : val, max
  487. * @retval : None
  488. */
  489. static void valIncrease(uint8_t * val, uint8_t max) {
  490. uint8_t bin = 10 * (*val >> 4) + (*val & 0x0f);
  491. if (bin < max) {
  492. bin ++;
  493. } else {
  494. bin = 0;
  495. }
  496. *val = ((bin / 10 ) << 4) | (bin % 10);
  497. }
  498. /**
  499. * @brief Decrease BCD value.
  500. * @param : value, max
  501. * @retval : None
  502. */
  503. static void valDecrease(uint8_t * val, uint8_t max) {
  504. uint8_t bin = 10 * (*val >> 4) + (*val & 0x0f);
  505. if (bin > 0) {
  506. bin --;
  507. } else {
  508. bin = max;
  509. }
  510. *val = ((bin / 10 ) << 4) | (bin % 10);
  511. }