clock.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. #include "clock.h"
  2. /* type defs */
  3. typedef struct t_btn {
  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. //convert linear bright level to logariphmic
  20. const uint8_t cie[8] = { 0, 5, 14, 33, 64, 109, 172, 255 };
  21. volatile static uint8_t dispWDT = 0;
  22. static in15_pin_t symToFade = 0;
  23. static brigh_level_t BrightLevel;
  24. /* function prototypes */
  25. static void check_DayNight(void);
  26. static void MinusFadeIn(void);
  27. static void MinusFadeOut(void);
  28. static void PlusFadeIn(void);
  29. static void PercentFadeIn(void);
  30. static void PressureFadeIn(void);
  31. static void IN15_FadeIn(void);
  32. static void IN15_FadeOut(void);
  33. static void valIncrease(uint8_t * val, uint8_t max);
  34. static void valDecrease(uint8_t * val, uint8_t max);
  35. static void HSV2LED(const uint8_t hue, const uint8_t sat, const uint8_t val);
  36. /* funcions */
  37. void Clock_Init(void) {
  38. RTC_ReadAll(&Clock);
  39. showTime();
  40. check_DayNight();
  41. }
  42. static void check_DayNight(void) {
  43. if ((Clock.Hr >= MORNING_HOUR) && (Clock.Hr < EVENING_HOUR)) {
  44. Flag.Now_Day = 1;
  45. BrightLevel = cie[DAY_BR_LVL];
  46. } else {
  47. Flag.Now_Day = 0;
  48. BrightLevel = cie[NIGHT_BR_LVL];
  49. }
  50. tube_BrightLevel(Tube_All, BrightLevel);
  51. //TUBES_BRIGHT(BrightLevel);
  52. }
  53. /**
  54. * @brief Обработка кнопок.
  55. * @param : None
  56. * @retval : None
  57. */
  58. void btnProcess(void) {
  59. /* get pin state */
  60. uint32_t pins = BTNS_STATE;
  61. int i;
  62. for (i=0; i<BTN_NUM; i++) {
  63. if ((pins & Button[i].pin) == 0) {
  64. /* button pressed */
  65. Button[i].time ++;
  66. if (Button[i].time >= (BTN_TIME_HOLDED/BTN_SCAN_PERIOD)) {
  67. Button[i].time -= (BTN_TIME_REPEATED/BTN_SCAN_PERIOD);
  68. if (Button[i].holded == Button[i].pressed) {
  69. /* if pressed and holded - same function, then button pressed auto repeat */
  70. ES_PlaceEvent(Button[i].pressed);
  71. }
  72. }
  73. } else if (Button[i].time != 0) {
  74. /* button released */
  75. if (Button[i].time >= ((BTN_TIME_HOLDED - BTN_TIME_REPEATED)/BTN_SCAN_PERIOD)) {
  76. /* process long press */
  77. ES_PlaceEvent(Button[i].holded);
  78. } else if (Button[i].time >= (BTN_TIME_PRESSED/BTN_SCAN_PERIOD)) {
  79. /* process short press */
  80. ES_PlaceEvent(Button[i].pressed);
  81. }
  82. Button[i].time = 0;
  83. RTOS_SetTask(btnProcess, BTN_SCAN_PAUSE, BTN_SCAN_PERIOD);
  84. }
  85. } /* end FOR */
  86. }
  87. void new_Second(void) {
  88. RTC_ReadAll(&Clock);
  89. // new hour
  90. if (Clock.Min == 0 && Clock.Sec == 0) {
  91. check_DayNight();
  92. }
  93. // check display watch dog timer
  94. if (dispWDT != 0) {
  95. dispWDT--;
  96. if (dispWDT == 0) {
  97. Flag.Blink_1 = 0;
  98. Flag.Blink_2 = 0;
  99. Flag.Blink_3 = 0;
  100. Flag.Blink_4 = 0;
  101. Flag.Blink_5 = 0;
  102. Blink_Stop();
  103. ES_PlaceEvent(evDisplayWDT);
  104. }
  105. }
  106. }
  107. /**
  108. * On/off symbols on IN-15 tube.
  109. */
  110. void in15Off(void) {
  111. IN15_OFF;
  112. TUBE_C_OFF;
  113. }
  114. void in15Minus(void) {
  115. IN15_OFF;
  116. IN15_Minus;
  117. TUBE_C_ON;
  118. }
  119. void in15Plus(void) {
  120. IN15_OFF;
  121. IN15_Plus;
  122. TUBE_C_ON;
  123. }
  124. void in15Percent(void) {
  125. IN15_OFF;
  126. IN15_Percent;
  127. TUBE_C_ON;
  128. }
  129. void in15P(void) {
  130. IN15_OFF;
  131. IN15_P;
  132. TUBE_C_ON;
  133. }
  134. /** 'Faded' funcions */
  135. static void MinusFadeIn(void) {
  136. static uint8_t on = FADE_START;
  137. static uint8_t off = FADE_STOP;
  138. static uint8_t st = 0;
  139. if (st == 0) {
  140. st = 1;
  141. IN15_Minus;
  142. on += FADE_STEP;
  143. if (on < FADE_STOP) {
  144. RTOS_SetTask(MinusFadeIn, on, 0);
  145. } else {
  146. on = FADE_START; off = FADE_STOP; st = 0;
  147. }
  148. } else {
  149. st = 0;
  150. IN15_OFF;
  151. off -= FADE_STEP;
  152. RTOS_SetTask(MinusFadeIn, off, 0);
  153. }
  154. }
  155. static void MinusFadeOut(void) {
  156. static uint8_t off = FADE_START;
  157. static uint8_t on = FADE_STOP;
  158. static uint8_t st = 0;
  159. if (st == 0) {
  160. st = 1;
  161. IN15_OFF;
  162. off += FADE_STEP;
  163. if (off < FADE_STOP) {
  164. RTOS_SetTask(MinusFadeOut, off, 0);
  165. } else {
  166. off = FADE_START; on = FADE_STOP; st = 0;
  167. }
  168. } else {
  169. st = 0;
  170. IN15_Minus;
  171. on -= FADE_STEP;
  172. RTOS_SetTask(MinusFadeOut, on, 0);
  173. }
  174. }
  175. static void PlusFadeIn(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_Plus;
  182. on += FADE_STEP;
  183. if (on < FADE_STOP) {
  184. RTOS_SetTask(PlusFadeIn, 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(PlusFadeIn, off, 0);
  193. }
  194. }
  195. static void PercentFadeIn(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_Percent;
  202. on += FADE_STEP;
  203. if (on < FADE_STOP) {
  204. RTOS_SetTask(PercentFadeIn, 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(PercentFadeIn, off, 0);
  213. }
  214. }
  215. static void PressureFadeIn(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. IN15_P;
  222. on += FADE_STEP;
  223. if (on < FADE_STOP) {
  224. RTOS_SetTask(PressureFadeIn, on, 0);
  225. } else {
  226. on = FADE_START; off = FADE_STOP; st = 0;
  227. }
  228. } else {
  229. st = 0;
  230. IN15_OFF;
  231. off -= FADE_STEP;
  232. RTOS_SetTask(PressureFadeIn, off, 0);
  233. }
  234. }
  235. static void IN15_FadeIn(void) {
  236. static uint8_t on = FADE_START;
  237. static uint8_t off = FADE_STOP;
  238. static uint8_t st = 0;
  239. if (symToFade != 0) {
  240. if (st == 0) {
  241. st = 1;
  242. GPIOA->BSRR = symToFade;
  243. on += FADE_STEP;
  244. if (on < FADE_STOP) {
  245. RTOS_SetTask(IN15_FadeIn, on, 0);
  246. } else {
  247. on = FADE_START; off = FADE_STOP; st = 0; symToFade = 0;
  248. }
  249. } else {
  250. st = 0;
  251. IN15_OFF;
  252. off -= FADE_STEP;
  253. RTOS_SetTask(IN15_FadeIn, off, 0);
  254. }
  255. }
  256. }
  257. static void IN15_FadeOut(void) {
  258. static uint8_t off = FADE_START;
  259. static uint8_t on = FADE_STOP;
  260. static uint8_t st = 0;
  261. if (symToFade != 0) {
  262. if (st == 0) {
  263. st = 1;
  264. IN15_OFF;
  265. off += FADE_STEP;
  266. if (off < FADE_STOP) {
  267. RTOS_SetTask(IN15_FadeOut, off, 0);
  268. } else {
  269. off = FADE_START; on = FADE_STOP; st = 0; symToFade = 0;
  270. }
  271. } else {
  272. st = 0;
  273. GPIOA->BSRR = symToFade;
  274. on -= FADE_STEP;
  275. RTOS_SetTask(IN15_FadeOut, on, 0);
  276. }
  277. }
  278. }
  279. /**
  280. * @brief HSV to RGB convertion
  281. * @param hue: 0-(6*256)-1 /59/, sat: 0-255, val (lightness): 0-255
  282. * @return none. RGB value output direct to LED.
  283. */
  284. static void HSV2LED(const uint8_t hue, const uint8_t sat, const uint8_t val) {
  285. #ifdef TEST_TEST_TEST
  286. int base;
  287. uint32_t r=0, g=0, b=0;
  288. if (sat == 0)
  289. { // Achromatic color (gray).
  290. r = val;
  291. g = val;
  292. b = val;
  293. } else {
  294. base = ((255 - sat) * val) >> 8;
  295. switch (hue / 10) {
  296. case 0:
  297. r = val;
  298. g = (((val - base) * hue) / 10) + base;
  299. b = base;
  300. break;
  301. case 1:
  302. r = (((val - base) * (10 - (hue % 10))) / 10) + base;
  303. g = val;
  304. b = base;
  305. break;
  306. case 2:
  307. r = base;
  308. g = val;
  309. b = (((val - base) * (hue % 10)) / 10) + base;
  310. break;
  311. case 3:
  312. r = base;
  313. g = (((val - base) * (10 - (hue % 10))) / 10) + base;
  314. b = val;
  315. break;
  316. case 4:
  317. r = (((val - base) * (hue % 10)) / 10) + base;
  318. g = base;
  319. b = val;
  320. break;
  321. case 5:
  322. r = val;
  323. g = base;
  324. b = (((val - base) * (10 - (hue % 10))) / 10) + base;
  325. break;
  326. }
  327. }
  328. COLOR_R((uint8_t)r);
  329. COLOR_G((uint8_t)g);
  330. COLOR_B((uint8_t)b);
  331. }
  332. #endif
  333. uint8_t r, g, b;
  334. if (sat == 0) {
  335. r = val;
  336. g = val;
  337. b = val;
  338. } else {
  339. uint8_t sextant = hue >> 8;
  340. uint8_t tmp;
  341. // Swap pointers depending which sextant we are in
  342. if((sextant) & 2) {
  343. tmp = r; r = b; b = tmp;
  344. }
  345. if((sextant) & 4) {
  346. tmp = g; g = b; b = tmp;
  347. }
  348. if(!((sextant) & 6)) {
  349. if(!((sextant) & 1)) {
  350. tmp = r; r = g; g = tmp;
  351. }
  352. } else {
  353. if((sextant) & 1) {
  354. tmp = r; r = g; g = tmp;
  355. }
  356. }
  357. g = val; // Top level
  358. // Perform actual calculations
  359. /*
  360. * Bottom level: v * (1.0 - s)
  361. * --> (v * (255 - s) + error_corr + 1) / 256
  362. */
  363. uint16_t ww; // Intermediate result
  364. ww = val * (255 - sat); // We don't use ~s to prevent size-promotion side effects
  365. ww += 1; // Error correction
  366. ww += ww >> 8; // Error correction
  367. b = ww >> 8;
  368. uint8_t h_fraction = hue & 0xff; // 0...255
  369. uint32_t d; // Intermediate result
  370. if (!(sextant & 1))
  371. {
  372. // r = ...slope_up...;
  373. d = val * (uint32_t)((255 << 8) - (uint16_t)(sat * (256 - h_fraction)));
  374. d += d >> 8; // Error correction
  375. d += val; // Error correction
  376. r = d >> 16;
  377. } else {
  378. // r = ...slope_down...;
  379. d = val * (uint32_t)((255 << 8) - (uint16_t)(sat * h_fraction));
  380. d += d >> 8; // Error correction
  381. d += val; // Error correction
  382. r = d >> 16;
  383. }
  384. }
  385. COLOR_R(r);
  386. COLOR_G(g);
  387. COLOR_B(b);
  388. }
  389. /**
  390. * Show info on tubes.
  391. */
  392. void showTime(void) {
  393. MinusFadeIn();
  394. RTOS_SetTask(MinusFadeOut, 500, 0);
  395. if (Flag.Now_Day != 0) {
  396. // new hsv2led
  397. uint16_t hue = bcd2bin(Clock.Sec) * 256 / 10;
  398. HSV2LED(hue, 255, BrightLevel);
  399. } else {
  400. HSV2LED(COLOUR_NIXIE, 255, BrightLevel);
  401. }
  402. tube4_t buf;
  403. buf.s8.tA = Clock.Hr >> 4;
  404. buf.s8.tB = Clock.Hr & 0xf;
  405. buf.s8.tD = Clock.Min >> 4;
  406. buf.s8.tE = Clock.Min & 0xf;
  407. showDigits(buf);
  408. }
  409. void showMMSS(void) {
  410. RTOS_DeleteTask(MinusFadeOut);
  411. IN15_Minus;
  412. uint8_t hue = bcd2bin(Clock.Sec);
  413. HSV2LED(hue, 255, BrightLevel);
  414. tube4_t buf;
  415. buf.s8.tA = Clock.Min >> 4;
  416. buf.s8.tB = Clock.Min & 0xf;
  417. buf.s8.tD = Clock.Sec >> 4;
  418. buf.s8.tE = Clock.Sec & 0xf;
  419. showDigits(buf);
  420. }
  421. void showWD(void) {
  422. dispWDT = DISP_WDT_TIME;
  423. IN15_OFF;
  424. tube4_t buf;
  425. buf.s8.tA = 0xf;
  426. buf.s8.tB = Clock.WD & 0xf;
  427. buf.s8.tD = 0xf;
  428. buf.s8.tE = 0xf;
  429. showDigits(buf);
  430. }
  431. void showDayMon(void) {
  432. dispWDT = DISP_WDT_TIME;
  433. IN15_OFF;
  434. tube4_t buf;
  435. buf.s8.tA = Clock.Day >> 4;
  436. buf.s8.tB = Clock.Day & 0xf;
  437. buf.s8.tD = Clock.Mon >> 4;
  438. buf.s8.tE = Clock.Mon & 0xf;
  439. showDigits(buf);
  440. }
  441. void showYear(void) {
  442. dispWDT = DISP_WDT_TIME;
  443. IN15_OFF;
  444. tube4_t buf;
  445. buf.s8.tA = 2;
  446. buf.s8.tB = 0;
  447. buf.s8.tD = Clock.Year >> 4;
  448. buf.s8.tE = Clock.Year & 0xf;
  449. showDigits(buf);
  450. }
  451. void showHumidity(void) {
  452. dispWDT = DISP_WDT_TIME;
  453. HSV2LED(COLOUR_BLUE, 255, BrightLevel);
  454. //in15Percent();
  455. symToFade = sym_Percent;
  456. IN15_FadeIn();
  457. //PercentFadeIn();
  458. tube4_t buf;
  459. buf.s8.tA = Humidity / 10;
  460. buf.s8.tB = Humidity % 10;
  461. buf.s8.tD = 0xf;
  462. buf.s8.tE = 0xf;
  463. showDigits(buf);
  464. }
  465. void showTemperature(void) {
  466. dispWDT = DISP_WDT_TIME;
  467. HSV2LED(COLOUR_RED, 255, BrightLevel);
  468. //in15Plus();
  469. symToFade = sym_Plus;
  470. IN15_FadeIn();
  471. //PlusFadeIn();
  472. tube4_t buf;
  473. buf.s8.tA = 0xf;
  474. buf.s8.tB = 0xf;
  475. buf.s8.tD = Temperature / 10;
  476. buf.s8.tE = Temperature % 10;
  477. showDigits(buf);
  478. }
  479. void showPressure(void) {
  480. dispWDT = DISP_WDT_TIME;
  481. HSV2LED(COLOUR_GREEN, 255, cie[Lvl_Mdl]); // GREEN
  482. //in15P();
  483. symToFade = sym_Pressure;
  484. IN15_FadeIn();
  485. //PressureFadeIn();
  486. tube4_t buf;
  487. int tmp;
  488. buf.s8.tA = 0xf;
  489. buf.s8.tB = Pressure / 100;
  490. tmp = Pressure % 100;
  491. buf.s8.tD = tmp / 10;
  492. buf.s8.tE = tmp % 10;
  493. showDigits(buf);
  494. }
  495. /* Simple function for cyclic show all sensor data */
  496. void showSensorData(void) {
  497. RTOS_DeleteTask(MinusFadeOut);
  498. showTemperature();
  499. tdelay_ms(3000);
  500. showHumidity();
  501. tdelay_ms(3000);
  502. showPressure();
  503. tdelay_ms(2700);
  504. ES_SetState(stShowTime);
  505. // showTime();
  506. }
  507. void setTimeShow(void) {
  508. dispWDT = DISP_WDT_TIME;
  509. tube4_t buf;
  510. buf.s8.tA = setClock.Hr >> 4;
  511. buf.s8.tB = setClock.Hr & 0xf;
  512. buf.s8.tD = setClock.Min >> 4;
  513. buf.s8.tE = setClock.Min & 0xf;
  514. showDigits(buf);
  515. }
  516. void setTimeBegin(void) {
  517. dispWDT = DISP_WDT_TIME;
  518. in15Minus();
  519. HSV2LED(COLOUR_NIXIE, 255, BrightLevel);
  520. RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
  521. RTC_ReadAll(&setClock);
  522. }
  523. void setHHBegin(void) {
  524. dispWDT = DISP_WDT_TIME;
  525. Flag.Blink_1 = 1;
  526. Flag.Blink_2 = 1;
  527. Flag.Blink_4 = 0;
  528. Flag.Blink_5 = 0;
  529. Blink_Start();
  530. setTimeShow();
  531. }
  532. void setHHInc(void) {
  533. valIncrease(&setClock.Hr, 23);
  534. }
  535. void setHHDec(void) {
  536. valDecrease(&setClock.Hr, 23);
  537. }
  538. void setMMBegin(void) {
  539. dispWDT = DISP_WDT_TIME;
  540. Flag.Blink_1 = 0;
  541. Flag.Blink_2 = 0;
  542. Flag.Blink_4 = 1;
  543. Flag.Blink_5 = 1;
  544. Blink_Start();
  545. setTimeShow();
  546. }
  547. void setMMInc(void) {
  548. valIncrease(&setClock.Min, 59);
  549. }
  550. void setMMDec(void) {
  551. valDecrease(&setClock.Min, 59);
  552. }
  553. void setTimeEnd(void) {
  554. dispWDT = 0;
  555. RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
  556. setClock.Sec = 0;
  557. RTC_WriteTime(&setClock);
  558. Flag.Blink_1 = 0;
  559. Flag.Blink_2 = 0;
  560. Flag.Blink_4 = 0;
  561. Flag.Blink_5 = 0;
  562. Blink_Stop();
  563. RTC_ReadAll(&Clock);
  564. }
  565. void setDateBegin(void) {
  566. dispWDT = DISP_WDT_TIME;
  567. IN15_OFF;
  568. RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
  569. RTC_ReadAll(&setClock);
  570. }
  571. void setDateEnd(void) {
  572. dispWDT = 0;
  573. RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
  574. RTC_WriteCalendar(&setClock);
  575. Flag.Blink_1 = 0;
  576. Flag.Blink_2 = 0;
  577. Flag.Blink_4 = 0;
  578. Flag.Blink_5 = 0;
  579. Blink_Stop();
  580. RTC_ReadAll(&Clock);
  581. }
  582. void setWDBegin(void) {
  583. dispWDT = DISP_WDT_TIME;
  584. Flag.Blink_1 = 0;
  585. Flag.Blink_2 = 1;
  586. Flag.Blink_4 = 0;
  587. Flag.Blink_5 = 0;
  588. Blink_Start();
  589. setWDShow();
  590. }
  591. void setWDShow(void) {
  592. dispWDT = DISP_WDT_TIME;
  593. tube4_t buf;
  594. buf.s8.tA = 0xf;
  595. buf.s8.tB = setClock.WD & 0xf;
  596. buf.s8.tD = 0xf;
  597. buf.s8.tE = 0xf;
  598. showDigits(buf);
  599. }
  600. void setDMShow(void) {
  601. dispWDT = DISP_WDT_TIME;
  602. tube4_t buf;
  603. buf.s8.tA = setClock.Day >> 4;
  604. buf.s8.tB = setClock.Day & 0xf;
  605. buf.s8.tD = setClock.Mon >> 4;
  606. buf.s8.tE = setClock.Mon & 0xf;
  607. showDigits(buf);
  608. }
  609. void setYearShow(void) {
  610. dispWDT = DISP_WDT_TIME;
  611. tube4_t buf;
  612. buf.s8.tA = 2;
  613. buf.s8.tB = 0;
  614. buf.s8.tD = setClock.Year >> 4;
  615. buf.s8.tE = setClock.Year & 0xf;
  616. showDigits(buf);
  617. }
  618. void setMDBegin(void) {
  619. dispWDT = DISP_WDT_TIME;
  620. Flag.Blink_1 = 1;
  621. Flag.Blink_2 = 1;
  622. Flag.Blink_4 = 0;
  623. Flag.Blink_5 = 0;
  624. Blink_Start();
  625. setDMShow();
  626. }
  627. void setMonthBegin(void) {
  628. dispWDT = DISP_WDT_TIME;
  629. Flag.Blink_1 = 0;
  630. Flag.Blink_2 = 0;
  631. Flag.Blink_4 = 1;
  632. Flag.Blink_5 = 1;
  633. Blink_Start();
  634. setDMShow();
  635. }
  636. void setYearBegin(void) {
  637. dispWDT = DISP_WDT_TIME;
  638. Flag.Blink_1 = 0;
  639. Flag.Blink_2 = 0;
  640. Flag.Blink_4 = 1;
  641. Flag.Blink_5 = 1;
  642. Blink_Start();
  643. setYearShow();
  644. }
  645. void setIncWDay(void) {
  646. valIncrease(&setClock.WD, 7);
  647. }
  648. void setIncMDay(void) {
  649. valIncrease(&setClock.Day, 31);
  650. }
  651. void setIncMonth(void) {
  652. valIncrease(&setClock.Mon, 12);
  653. }
  654. void setIncYear(void) {
  655. valIncrease(&setClock.Year, 99);
  656. }
  657. void setDecWDay(void) {
  658. valDecrease(&setClock.WD, 7);
  659. }
  660. void setDecMDay(void) {
  661. valDecrease(&setClock.Day, 31);
  662. }
  663. void setDecMonth(void) {
  664. valDecrease(&setClock.Mon, 12);
  665. }
  666. void setDecYear(void) {
  667. valDecrease(&setClock.Year, 99);
  668. }
  669. /**
  670. * @brief Increase BCD value.
  671. * @param : val, max
  672. * @retval : None
  673. */
  674. static void valIncrease(uint8_t * val, uint8_t max) {
  675. uint8_t bin = 10 * (*val >> 4) + (*val & 0x0f);
  676. if (bin < max) {
  677. bin ++;
  678. } else {
  679. bin = 0;
  680. }
  681. *val = ((bin / 10 ) << 4) | (bin % 10);
  682. }
  683. /**
  684. * @brief Decrease BCD value.
  685. * @param : value, max
  686. * @retval : None
  687. */
  688. static void valDecrease(uint8_t * val, uint8_t max) {
  689. uint8_t bin = 10 * (*val >> 4) + (*val & 0x0f);
  690. if (bin > 0) {
  691. bin --;
  692. } else {
  693. bin = max;
  694. }
  695. *val = ((bin / 10 ) << 4) | (bin % 10);
  696. }