clock.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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 int 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. Blink_Stop();
  98. ES_PlaceEvent(evDisplayWDT);
  99. }
  100. }
  101. }
  102. /**
  103. * On/off symbols on IN-15 tube.
  104. */
  105. void in15Off(void) {
  106. IN15_OFF;
  107. TUBE_C_OFF;
  108. }
  109. void in15Minus(void) {
  110. IN15_OFF;
  111. IN15_Minus;
  112. TUBE_C_ON;
  113. }
  114. void in15Plus(void) {
  115. IN15_OFF;
  116. IN15_Plus;
  117. TUBE_C_ON;
  118. }
  119. void in15Percent(void) {
  120. IN15_OFF;
  121. IN15_Percent;
  122. TUBE_C_ON;
  123. }
  124. void in15P(void) {
  125. IN15_OFF;
  126. IN15_P;
  127. TUBE_C_ON;
  128. }
  129. /** 'Faded' funcions */
  130. static void MinusFadeIn(void) {
  131. static uint8_t on = FADE_START;
  132. static uint8_t off = FADE_STOP;
  133. static uint8_t st = 0;
  134. if (st == 0) {
  135. st = 1;
  136. IN15_Minus;
  137. on += FADE_STEP;
  138. if (on < FADE_STOP) {
  139. RTOS_SetTask(MinusFadeIn, on, 0);
  140. } else {
  141. on = FADE_START; off = FADE_STOP; st = 0;
  142. }
  143. } else {
  144. st = 0;
  145. IN15_OFF;
  146. off -= FADE_STEP;
  147. RTOS_SetTask(MinusFadeIn, off, 0);
  148. }
  149. }
  150. static void MinusFadeOut(void) {
  151. static uint8_t off = FADE_START;
  152. static uint8_t on = FADE_STOP;
  153. static uint8_t st = 0;
  154. if (st == 0) {
  155. st = 1;
  156. IN15_OFF;
  157. off += FADE_STEP;
  158. if (off < FADE_STOP) {
  159. RTOS_SetTask(MinusFadeOut, off, 0);
  160. } else {
  161. off = FADE_START; on = FADE_STOP; st = 0;
  162. }
  163. } else {
  164. st = 0;
  165. IN15_Minus;
  166. on -= FADE_STEP;
  167. RTOS_SetTask(MinusFadeOut, on, 0);
  168. }
  169. }
  170. static void PlusFadeIn(void) {
  171. static uint8_t on = FADE_START;
  172. static uint8_t off = FADE_STOP;
  173. static uint8_t st = 0;
  174. if (st == 0) {
  175. st = 1;
  176. IN15_Plus;
  177. on += FADE_STEP;
  178. if (on < FADE_STOP) {
  179. RTOS_SetTask(PlusFadeIn, on, 0);
  180. } else {
  181. on = FADE_START; off = FADE_STOP; st = 0;
  182. }
  183. } else {
  184. st = 0;
  185. IN15_OFF;
  186. off -= FADE_STEP;
  187. RTOS_SetTask(PlusFadeIn, off, 0);
  188. }
  189. }
  190. static void PercentFadeIn(void) {
  191. static uint8_t on = FADE_START;
  192. static uint8_t off = FADE_STOP;
  193. static uint8_t st = 0;
  194. if (st == 0) {
  195. st = 1;
  196. IN15_Percent;
  197. on += FADE_STEP;
  198. if (on < FADE_STOP) {
  199. RTOS_SetTask(PercentFadeIn, on, 0);
  200. } else {
  201. on = FADE_START; off = FADE_STOP; st = 0;
  202. }
  203. } else {
  204. st = 0;
  205. IN15_OFF;
  206. off -= FADE_STEP;
  207. RTOS_SetTask(PercentFadeIn, off, 0);
  208. }
  209. }
  210. static void PressureFadeIn(void) {
  211. static uint8_t on = FADE_START;
  212. static uint8_t off = FADE_STOP;
  213. static uint8_t st = 0;
  214. if (st == 0) {
  215. st = 1;
  216. IN15_P;
  217. on += FADE_STEP;
  218. if (on < FADE_STOP) {
  219. RTOS_SetTask(PressureFadeIn, on, 0);
  220. } else {
  221. on = FADE_START; off = FADE_STOP; st = 0;
  222. }
  223. } else {
  224. st = 0;
  225. IN15_OFF;
  226. off -= FADE_STEP;
  227. RTOS_SetTask(PressureFadeIn, off, 0);
  228. }
  229. }
  230. static void IN15_FadeIn(void) {
  231. static uint8_t on = FADE_START;
  232. static uint8_t off = FADE_STOP;
  233. static uint8_t st = 0;
  234. if (symToFade != 0) {
  235. if (st == 0) {
  236. st = 1;
  237. GPIOA->BSRR = symToFade;
  238. on += FADE_STEP;
  239. if (on < FADE_STOP) {
  240. RTOS_SetTask(IN15_FadeIn, on, 0);
  241. } else {
  242. on = FADE_START; off = FADE_STOP; st = 0; symToFade = 0;
  243. }
  244. } else {
  245. st = 0;
  246. IN15_OFF;
  247. off -= FADE_STEP;
  248. RTOS_SetTask(IN15_FadeIn, off, 0);
  249. }
  250. }
  251. }
  252. static void IN15_FadeOut(void) {
  253. static uint8_t off = FADE_START;
  254. static uint8_t on = FADE_STOP;
  255. static uint8_t st = 0;
  256. if (symToFade != 0) {
  257. if (st == 0) {
  258. st = 1;
  259. IN15_OFF;
  260. off += FADE_STEP;
  261. if (off < FADE_STOP) {
  262. RTOS_SetTask(IN15_FadeOut, off, 0);
  263. } else {
  264. off = FADE_START; on = FADE_STOP; st = 0; symToFade = 0;
  265. }
  266. } else {
  267. st = 0;
  268. GPIOA->BSRR = symToFade;
  269. on -= FADE_STEP;
  270. RTOS_SetTask(IN15_FadeOut, on, 0);
  271. }
  272. }
  273. }
  274. /**
  275. * @brief HSV to RGB convertion
  276. * @param hue: 0-59, sat: 0-255, val (lightness): 0-255
  277. * @return none. RGB value output direct to LED.
  278. */
  279. #define HUE_DEGREE 60
  280. static void HSV2LED(const int hue, const uint8_t sat, const uint8_t val) {
  281. uint8_t r, g, b;
  282. int h = hue;
  283. int s = sat;
  284. int v = val;
  285. int i = h / (60 * HUE_DEGREE);
  286. int p = (256 * v - s * v) / 256;
  287. if (i & 1)
  288. {
  289. int q = (256 * 60 * HUE_DEGREE * v - h * s * v + 60 * HUE_DEGREE * s * v * i) / (256 * 60 * HUE_DEGREE);
  290. switch (i)
  291. {
  292. case 1:
  293. r = q;
  294. g = v;
  295. b = p;
  296. break;
  297. case 3:
  298. r = p;
  299. g = q;
  300. b = v;
  301. break;
  302. case 5:
  303. r = v;
  304. g = p;
  305. b = q;
  306. break;
  307. }
  308. }
  309. else
  310. {
  311. int t = (256 * 60 * HUE_DEGREE * v + h * s * v - 60 * HUE_DEGREE * s * v * (i + 1)) / (256 * 60 * HUE_DEGREE);
  312. switch (i)
  313. {
  314. case 0:
  315. r = v;
  316. g = t;
  317. b = p;
  318. break;
  319. case 2:
  320. r = p;
  321. g = v;
  322. b = t;
  323. break;
  324. case 4:
  325. r = t;
  326. g = p;
  327. b = v;
  328. break;
  329. }
  330. }
  331. COLOR_R(r);
  332. COLOR_G(g);
  333. COLOR_B(b);
  334. }
  335. static void old_HSV2LED(const uint8_t hue, const uint8_t sat, const uint8_t val) {
  336. int base;
  337. uint32_t r=0, g=0, b=0;
  338. if (sat == 0)
  339. { // Achromatic color (gray).
  340. r = val;
  341. g = val;
  342. b = val;
  343. } else {
  344. base = ((255 - sat) * val) >> 8;
  345. switch (hue / 10) {
  346. case 0:
  347. r = val;
  348. g = (((val - base) * hue) / 10) + base;
  349. b = base;
  350. break;
  351. case 1:
  352. r = (((val - base) * (10 - (hue % 10))) / 10) + base;
  353. g = val;
  354. b = base;
  355. break;
  356. case 2:
  357. r = base;
  358. g = val;
  359. b = (((val - base) * (hue % 10)) / 10) + base;
  360. break;
  361. case 3:
  362. r = base;
  363. g = (((val - base) * (10 - (hue % 10))) / 10) + base;
  364. b = val;
  365. break;
  366. case 4:
  367. r = (((val - base) * (hue % 10)) / 10) + base;
  368. g = base;
  369. b = val;
  370. break;
  371. case 5:
  372. r = val;
  373. g = base;
  374. b = (((val - base) * (10 - (hue % 10))) / 10) + base;
  375. break;
  376. }
  377. }
  378. COLOR_R((uint8_t)r);
  379. COLOR_G((uint8_t)g);
  380. COLOR_B((uint8_t)b);
  381. }
  382. /**
  383. * Show info on tubes.
  384. */
  385. void showTime(void) {
  386. MinusFadeIn();
  387. RTOS_SetTask(MinusFadeOut, 500, 0);
  388. if (Flag.Now_Day != 0) {
  389. // new hsv2led
  390. //uint16_t hue = (uint16_t)(bcd2bin(Clock.Sec) * 256) / 10;
  391. uint8_t hue = bcd2bin(Clock.Sec);
  392. HSV2LED(hue, 255, BrightLevel);
  393. } else {
  394. HSV2LED(COLOUR_NIXIE, 255, BrightLevel);
  395. }
  396. tube4_t buf;
  397. buf.s8.tA = Clock.Hr >> 4;
  398. buf.s8.tB = Clock.Hr & 0xf;
  399. buf.s8.tD = Clock.Min >> 4;
  400. buf.s8.tE = Clock.Min & 0xf;
  401. showDigits(buf);
  402. }
  403. void showMMSS(void) {
  404. RTOS_DeleteTask(MinusFadeOut);
  405. IN15_Minus;
  406. uint8_t hue = bcd2bin(Clock.Sec);
  407. HSV2LED(hue, 255, BrightLevel);
  408. tube4_t buf;
  409. buf.s8.tA = Clock.Min >> 4;
  410. buf.s8.tB = Clock.Min & 0xf;
  411. buf.s8.tD = Clock.Sec >> 4;
  412. buf.s8.tE = Clock.Sec & 0xf;
  413. showDigits(buf);
  414. }
  415. void showWD(void) {
  416. dispWDT = DISP_WDT_TIME;
  417. IN15_OFF;
  418. tube4_t buf;
  419. buf.s8.tA = 0xf;
  420. buf.s8.tB = Clock.WD & 0xf;
  421. buf.s8.tD = 0xf;
  422. buf.s8.tE = 0xf;
  423. showDigits(buf);
  424. }
  425. void showDayMon(void) {
  426. dispWDT = DISP_WDT_TIME;
  427. IN15_OFF;
  428. tube4_t buf;
  429. buf.s8.tA = Clock.Day >> 4;
  430. buf.s8.tB = Clock.Day & 0xf;
  431. buf.s8.tD = Clock.Mon >> 4;
  432. buf.s8.tE = Clock.Mon & 0xf;
  433. showDigits(buf);
  434. }
  435. void showYear(void) {
  436. dispWDT = DISP_WDT_TIME;
  437. IN15_OFF;
  438. tube4_t buf;
  439. buf.s8.tA = 2;
  440. buf.s8.tB = 0;
  441. buf.s8.tD = Clock.Year >> 4;
  442. buf.s8.tE = Clock.Year & 0xf;
  443. showDigits(buf);
  444. }
  445. void showHumidity(void) {
  446. dispWDT = DISP_WDT_TIME;
  447. HSV2LED(COLOUR_BLUE, 255, BrightLevel);
  448. //in15Percent();
  449. symToFade = sym_Percent;
  450. IN15_FadeIn();
  451. //PercentFadeIn();
  452. tube4_t buf;
  453. buf.s8.tA = Humidity / 10;
  454. buf.s8.tB = Humidity % 10;
  455. buf.s8.tD = 0xf;
  456. buf.s8.tE = 0xf;
  457. showDigits(buf);
  458. }
  459. void showTemperature(void) {
  460. dispWDT = DISP_WDT_TIME;
  461. HSV2LED(COLOUR_RED, 255, BrightLevel);
  462. //in15Plus();
  463. symToFade = sym_Plus;
  464. IN15_FadeIn();
  465. //PlusFadeIn();
  466. tube4_t buf;
  467. buf.s8.tA = 0xf;
  468. buf.s8.tB = 0xf;
  469. buf.s8.tD = Temperature / 10;
  470. buf.s8.tE = Temperature % 10;
  471. showDigits(buf);
  472. }
  473. void showPressure(void) {
  474. dispWDT = DISP_WDT_TIME;
  475. HSV2LED(COLOUR_GREEN, 255, cie[Lvl_Mdl]); // GREEN
  476. //in15P();
  477. symToFade = sym_Pressure;
  478. IN15_FadeIn();
  479. //PressureFadeIn();
  480. tube4_t buf;
  481. int tmp;
  482. buf.s8.tA = 0xf;
  483. buf.s8.tB = Pressure / 100;
  484. tmp = Pressure % 100;
  485. buf.s8.tD = tmp / 10;
  486. buf.s8.tE = tmp % 10;
  487. showDigits(buf);
  488. }
  489. /* Simple function for cyclic show all sensor data */
  490. void showSensorData(void) {
  491. RTOS_DeleteTask(MinusFadeOut);
  492. showTemperature();
  493. tdelay_ms(3000);
  494. showHumidity();
  495. tdelay_ms(3000);
  496. showPressure();
  497. tdelay_ms(2700);
  498. ES_SetState(stShowTime);
  499. // showTime();
  500. }
  501. void setTimeShow(void) {
  502. dispWDT = DISP_WDT_TIME;
  503. tube4_t buf;
  504. buf.s8.tA = setClock.Hr >> 4;
  505. buf.s8.tB = setClock.Hr & 0xf;
  506. buf.s8.tD = setClock.Min >> 4;
  507. buf.s8.tE = setClock.Min & 0xf;
  508. showDigits(buf);
  509. }
  510. void setTimeBegin(void) {
  511. RTOS_DeleteTask(MinusFadeOut);
  512. in15Minus();
  513. HSV2LED(COLOUR_NIXIE, 255, BrightLevel);
  514. RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
  515. RTC_ReadAll(&setClock);
  516. }
  517. void setHHBegin(void) {
  518. Flag.Blink_1 = 1;
  519. Flag.Blink_2 = 1;
  520. Flag.Blink_4 = 0;
  521. Flag.Blink_5 = 0;
  522. Blink_Start();
  523. setTimeShow();
  524. }
  525. void setHHInc(void) {
  526. valIncrease(&setClock.Hr, 23);
  527. }
  528. void setHHDec(void) {
  529. valDecrease(&setClock.Hr, 23);
  530. }
  531. void setMMBegin(void) {
  532. Flag.Blink_1 = 0;
  533. Flag.Blink_2 = 0;
  534. Flag.Blink_4 = 1;
  535. Flag.Blink_5 = 1;
  536. Blink_Start();
  537. setTimeShow();
  538. }
  539. void setMMInc(void) {
  540. valIncrease(&setClock.Min, 59);
  541. }
  542. void setMMDec(void) {
  543. valDecrease(&setClock.Min, 59);
  544. }
  545. void setTimeEnd(void) {
  546. dispWDT = 0;
  547. RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
  548. setClock.Sec = 0;
  549. RTC_WriteTime(&setClock);
  550. Blink_Stop();
  551. RTC_ReadAll(&Clock);
  552. }
  553. void setDateBegin(void) {
  554. IN15_OFF;
  555. HSV2LED(COLOUR_NIXIE, 255, BrightLevel);
  556. RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
  557. RTC_ReadAll(&setClock);
  558. }
  559. void setDateEnd(void) {
  560. dispWDT = 0;
  561. RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
  562. RTC_WriteCalendar(&setClock);
  563. Blink_Stop();
  564. RTC_ReadAll(&Clock);
  565. }
  566. void setWDBegin(void) {
  567. Flag.Blink_1 = 0;
  568. Flag.Blink_2 = 1;
  569. Flag.Blink_4 = 0;
  570. Flag.Blink_5 = 0;
  571. Blink_Start();
  572. setWDShow();
  573. }
  574. void setWDShow(void) {
  575. dispWDT = DISP_WDT_TIME;
  576. tube4_t buf;
  577. buf.s8.tA = 0xf;
  578. buf.s8.tB = setClock.WD & 0xf;
  579. buf.s8.tD = 0xf;
  580. buf.s8.tE = 0xf;
  581. showDigits(buf);
  582. }
  583. void setDMShow(void) {
  584. dispWDT = DISP_WDT_TIME;
  585. tube4_t buf;
  586. buf.s8.tA = setClock.Day >> 4;
  587. buf.s8.tB = setClock.Day & 0xf;
  588. buf.s8.tD = setClock.Mon >> 4;
  589. buf.s8.tE = setClock.Mon & 0xf;
  590. showDigits(buf);
  591. }
  592. void setYearShow(void) {
  593. dispWDT = DISP_WDT_TIME;
  594. tube4_t buf;
  595. buf.s8.tA = 2;
  596. buf.s8.tB = 0;
  597. buf.s8.tD = setClock.Year >> 4;
  598. buf.s8.tE = setClock.Year & 0xf;
  599. showDigits(buf);
  600. }
  601. void setMDBegin(void) {
  602. Flag.Blink_1 = 1;
  603. Flag.Blink_2 = 1;
  604. Flag.Blink_4 = 0;
  605. Flag.Blink_5 = 0;
  606. Blink_Start();
  607. setDMShow();
  608. }
  609. void setMonthBegin(void) {
  610. Flag.Blink_1 = 0;
  611. Flag.Blink_2 = 0;
  612. Flag.Blink_4 = 1;
  613. Flag.Blink_5 = 1;
  614. Blink_Start();
  615. setDMShow();
  616. }
  617. void setYearBegin(void) {
  618. Flag.Blink_1 = 0;
  619. Flag.Blink_2 = 0;
  620. Flag.Blink_4 = 1;
  621. Flag.Blink_5 = 1;
  622. Blink_Start();
  623. setYearShow();
  624. }
  625. void setIncWDay(void) {
  626. valIncrease(&setClock.WD, 7);
  627. }
  628. void setIncMDay(void) {
  629. valIncrease(&setClock.Day, 31);
  630. }
  631. void setIncMonth(void) {
  632. valIncrease(&setClock.Mon, 12);
  633. }
  634. void setIncYear(void) {
  635. valIncrease(&setClock.Year, 99);
  636. }
  637. void setDecWDay(void) {
  638. valDecrease(&setClock.WD, 7);
  639. }
  640. void setDecMDay(void) {
  641. valDecrease(&setClock.Day, 31);
  642. }
  643. void setDecMonth(void) {
  644. valDecrease(&setClock.Mon, 12);
  645. }
  646. void setDecYear(void) {
  647. valDecrease(&setClock.Year, 99);
  648. }
  649. /**
  650. * @brief Increase BCD value.
  651. * @param : val, max
  652. * @retval : None
  653. */
  654. static void valIncrease(uint8_t * val, uint8_t max) {
  655. uint8_t bin = 10 * (*val >> 4) + (*val & 0x0f);
  656. if (bin < max) {
  657. bin ++;
  658. } else {
  659. bin = 0;
  660. }
  661. *val = ((bin / 10 ) << 4) | (bin % 10);
  662. }
  663. /**
  664. * @brief Decrease BCD value.
  665. * @param : value, max
  666. * @retval : None
  667. */
  668. static void valDecrease(uint8_t * val, uint8_t max) {
  669. uint8_t bin = 10 * (*val >> 4) + (*val & 0x0f);
  670. if (bin > 0) {
  671. bin --;
  672. } else {
  673. bin = max;
  674. }
  675. *val = ((bin / 10 ) << 4) | (bin % 10);
  676. }