clock.c 14 KB

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