clock.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  1. #include "clock.h"
  2. #include "utils.h"
  3. /* type defs */
  4. typedef struct t_btn {
  5. uint8_t time;
  6. es_event_t pressed;
  7. es_event_t holded;
  8. uint32_t pin;
  9. //(GPIO_TypeDef *) GPIOA; // ?->IDR
  10. } btn_t;
  11. /* variables */
  12. rtc_t Clock;
  13. static rtc_t setClock;
  14. static btn_t Button[BTN_NUM] = {
  15. {0, evBTN1Pressed, evBTN1Holded, BTN1_PIN},
  16. {0, evBTN2Pressed, evBTN2Pressed, BTN2_PIN},
  17. {0, evBTN3Pressed, evBTN3Pressed, BTN3_PIN},
  18. {0, evBTN4Pressed, evBTN4Holded, BTN4_PIN}
  19. };
  20. //convert linear bright level to logariphmic
  21. const uint8_t cie[MAX_BRIGHT_LVL + 1] = {
  22. 0, 2, 4, 8, 13, 20, 29, 40, 54, 72, 92, 116, 145, 177, 214, 255
  23. };
  24. volatile static uint8_t dispWDT = 0;
  25. static in15_pin_t symToFade = 0;
  26. static uint8_t BrightLevel;
  27. static flash_data_t Lighting, setLighting;
  28. /* function prototypes */
  29. static void check_DayNight(void);
  30. static void HSV2LED(const uint8_t hue, const uint8_t val);
  31. static void MinusFadeIn(void);
  32. static void MinusFadeOut(void);
  33. static void PlusFadeIn(void);
  34. static void PercentFadeIn(void);
  35. static void PressureFadeIn(void);
  36. static void IN15_FadeIn(void);
  37. static void IN15_FadeOut(void);
  38. static void valIncrease(uint8_t * val, const uint8_t max);
  39. static void valDecrease(uint8_t * val, const uint8_t max);
  40. static void dvalIncrease(uint8_t * val, const uint8_t max);
  41. static void dvalDecrease(uint8_t * val, const uint8_t max);
  42. /* funcions */
  43. void Clock_Init(void) {
  44. RTC_ReadAll(&Clock);
  45. showTime();
  46. flash_result_t flash_res = Flash_Read(&Lighting.u64);
  47. if ( flash_res != Flash_Ok) {
  48. Lighting.name.DayHour = MORNING_HOUR;
  49. Lighting.name.NightHour = EVENING_HOUR;
  50. Lighting.name.DayBright = DAY_BR_LVL;
  51. Lighting.name.NightBright = NIGHT_BR_LVL;
  52. Lighting.name.DayMode = light_Rainbow;
  53. Lighting.name.NightMode = light_Colour;
  54. Lighting.name.NightColour = COLOUR_NIXIE;
  55. } else {
  56. if (Lighting.name.DayHour == 0xff) {
  57. /* launch set parameters
  58. ES_SetState(stSetDayHour);
  59. setDNbegin();
  60. setDayHourBegin();
  61. white (ES_GetState() != ???); */
  62. }
  63. }
  64. check_DayNight();
  65. }
  66. static void check_DayNight(void) {
  67. if ((Clock.Hr >= Lighting.name.DayHour) && (Clock.Hr < Lighting.name.NightHour)) {
  68. Flag.Now_Day = 1;
  69. BrightLevel = cie[Lighting.name.DayBright];
  70. } else {
  71. Flag.Now_Day = 0;
  72. BrightLevel = cie[Lighting.name.NightBright];
  73. }
  74. tube_BrightLevel(Tube_All, BrightLevel);
  75. //TUBES_BRIGHT(BrightLevel);
  76. }
  77. /**
  78. * @brief Обработка кнопок.
  79. * @param : None
  80. * @retval : None
  81. */
  82. void btnProcess(void) {
  83. /* get pin state */
  84. uint32_t pins = BTNS_STATE;
  85. int i;
  86. for (i=0; i<BTN_NUM; i++) {
  87. if ((pins & Button[i].pin) == 0) {
  88. /* button pressed */
  89. Button[i].time ++;
  90. if (Button[i].time >= (BTN_TIME_HOLDED/BTN_SCAN_PERIOD)) {
  91. Button[i].time -= (BTN_TIME_REPEATED/BTN_SCAN_PERIOD);
  92. if (Button[i].holded == Button[i].pressed) {
  93. /* if pressed and holded - same function, then button pressed auto repeat */
  94. ES_PlaceEvent(Button[i].pressed);
  95. }
  96. }
  97. } else if (Button[i].time != 0) {
  98. /* button released */
  99. if (Button[i].time >= ((BTN_TIME_HOLDED - BTN_TIME_REPEATED)/BTN_SCAN_PERIOD)) {
  100. /* process long press */
  101. ES_PlaceEvent(Button[i].holded);
  102. } else if (Button[i].time >= (BTN_TIME_PRESSED/BTN_SCAN_PERIOD)) {
  103. /* process short press */
  104. ES_PlaceEvent(Button[i].pressed);
  105. }
  106. Button[i].time = 0;
  107. RTOS_SetTask(btnProcess, BTN_SCAN_PAUSE, BTN_SCAN_PERIOD);
  108. }
  109. } /* end FOR */
  110. }
  111. void new_Second(void) {
  112. RTC_ReadAll(&Clock);
  113. // new hour
  114. if (Clock.Min == 0 && Clock.Sec == 0) {
  115. check_DayNight();
  116. }
  117. // check display watch dog timer
  118. if (dispWDT != 0) {
  119. dispWDT--;
  120. if (dispWDT == 0) {
  121. Blink_Stop();
  122. ES_PlaceEvent(evDisplayWDT);
  123. }
  124. }
  125. }
  126. /**
  127. * On/off symbols on IN-15 tube.
  128. */
  129. void in15Off(void) {
  130. IN15_OFF;
  131. TUBE_C_OFF;
  132. }
  133. void in15Minus(void) {
  134. IN15_OFF;
  135. IN15_Minus;
  136. TUBE_C_ON;
  137. }
  138. void in15Plus(void) {
  139. IN15_OFF;
  140. IN15_Plus;
  141. TUBE_C_ON;
  142. }
  143. void in15Percent(void) {
  144. IN15_OFF;
  145. IN15_Percent;
  146. TUBE_C_ON;
  147. }
  148. void in15P(void) {
  149. IN15_OFF;
  150. IN15_P;
  151. TUBE_C_ON;
  152. }
  153. /** 'Faded' funcions */
  154. static void MinusFadeIn(void) {
  155. static uint8_t on = FADE_START;
  156. static uint8_t off = FADE_STOP;
  157. static uint8_t st = 0;
  158. if (st == 0) {
  159. st = 1;
  160. IN15_Minus;
  161. on += FADE_STEP;
  162. if (on < FADE_STOP) {
  163. RTOS_SetTask(MinusFadeIn, on, 0);
  164. } else {
  165. on = FADE_START; off = FADE_STOP; st = 0;
  166. }
  167. } else {
  168. st = 0;
  169. IN15_OFF;
  170. off -= FADE_STEP;
  171. RTOS_SetTask(MinusFadeIn, off, 0);
  172. }
  173. }
  174. static void MinusFadeOut(void) {
  175. static uint8_t off = FADE_START;
  176. static uint8_t on = FADE_STOP;
  177. static uint8_t st = 0;
  178. if (st == 0) {
  179. st = 1;
  180. IN15_OFF;
  181. off += FADE_STEP;
  182. if (off < FADE_STOP) {
  183. RTOS_SetTask(MinusFadeOut, off, 0);
  184. } else {
  185. off = FADE_START; on = FADE_STOP; st = 0;
  186. }
  187. } else {
  188. st = 0;
  189. IN15_Minus;
  190. on -= FADE_STEP;
  191. RTOS_SetTask(MinusFadeOut, on, 0);
  192. }
  193. }
  194. static void PlusFadeIn(void) {
  195. static uint8_t on = FADE_START;
  196. static uint8_t off = FADE_STOP;
  197. static uint8_t st = 0;
  198. if (st == 0) {
  199. st = 1;
  200. IN15_Plus;
  201. on += FADE_STEP;
  202. if (on < FADE_STOP) {
  203. RTOS_SetTask(PlusFadeIn, on, 0);
  204. } else {
  205. on = FADE_START; off = FADE_STOP; st = 0;
  206. }
  207. } else {
  208. st = 0;
  209. IN15_OFF;
  210. off -= FADE_STEP;
  211. RTOS_SetTask(PlusFadeIn, off, 0);
  212. }
  213. }
  214. static void PercentFadeIn(void) {
  215. static uint8_t on = FADE_START;
  216. static uint8_t off = FADE_STOP;
  217. static uint8_t st = 0;
  218. if (st == 0) {
  219. st = 1;
  220. IN15_Percent;
  221. on += FADE_STEP;
  222. if (on < FADE_STOP) {
  223. RTOS_SetTask(PercentFadeIn, on, 0);
  224. } else {
  225. on = FADE_START; off = FADE_STOP; st = 0;
  226. }
  227. } else {
  228. st = 0;
  229. IN15_OFF;
  230. off -= FADE_STEP;
  231. RTOS_SetTask(PercentFadeIn, off, 0);
  232. }
  233. }
  234. static void PressureFadeIn(void) {
  235. static uint8_t on = FADE_START;
  236. static uint8_t off = FADE_STOP;
  237. static uint8_t st = 0;
  238. if (st == 0) {
  239. st = 1;
  240. IN15_P;
  241. on += FADE_STEP;
  242. if (on < FADE_STOP) {
  243. RTOS_SetTask(PressureFadeIn, on, 0);
  244. } else {
  245. on = FADE_START; off = FADE_STOP; st = 0;
  246. }
  247. } else {
  248. st = 0;
  249. IN15_OFF;
  250. off -= FADE_STEP;
  251. RTOS_SetTask(PressureFadeIn, off, 0);
  252. }
  253. }
  254. static void IN15_FadeIn(void) {
  255. static uint8_t on = FADE_START;
  256. static uint8_t off = FADE_STOP;
  257. static uint8_t st = 0;
  258. if (symToFade != 0) {
  259. if (st == 0) {
  260. st = 1;
  261. GPIOA->BSRR = symToFade;
  262. on += FADE_STEP;
  263. if (on < FADE_STOP) {
  264. RTOS_SetTask(IN15_FadeIn, on, 0);
  265. } else {
  266. on = FADE_START; off = FADE_STOP; st = 0; symToFade = 0;
  267. }
  268. } else {
  269. st = 0;
  270. IN15_OFF;
  271. off -= FADE_STEP;
  272. RTOS_SetTask(IN15_FadeIn, off, 0);
  273. }
  274. }
  275. }
  276. static void IN15_FadeOut(void) {
  277. static uint8_t off = FADE_START;
  278. static uint8_t on = FADE_STOP;
  279. static uint8_t st = 0;
  280. if (symToFade != 0) {
  281. if (st == 0) {
  282. st = 1;
  283. IN15_OFF;
  284. off += FADE_STEP;
  285. if (off < FADE_STOP) {
  286. RTOS_SetTask(IN15_FadeOut, off, 0);
  287. } else {
  288. off = FADE_START; on = FADE_STOP; st = 0; symToFade = 0;
  289. }
  290. } else {
  291. st = 0;
  292. GPIOA->BSRR = symToFade;
  293. on -= FADE_STEP;
  294. RTOS_SetTask(IN15_FadeOut, on, 0);
  295. }
  296. }
  297. }
  298. /**
  299. * @brief HSV to RGB convertion
  300. * @param hue: 0-59, sat: allways max, val (lightness): 0-255
  301. * @return none. RGB value output direct to LED.
  302. */
  303. static void HSV2LED(const uint8_t hue, const uint8_t val) {
  304. uint32_t r=0, g=0, b=0;
  305. switch (hue / 10) {
  306. case 0:
  307. r = val;
  308. g = (val * hue) / 10;
  309. b = 0;
  310. break;
  311. case 1:
  312. r = (val * (10 - (hue % 10))) / 10;
  313. g = val;
  314. b = 0;
  315. break;
  316. case 2:
  317. r = 0;
  318. g = val;
  319. b = (val * (hue % 10)) / 10;
  320. break;
  321. case 3:
  322. r = 0;
  323. g = (val * (10 - (hue % 10))) / 10;
  324. b = val;
  325. break;
  326. case 4:
  327. r = (val * (hue % 10)) / 10;
  328. g = 0;
  329. b = val;
  330. break;
  331. case 5:
  332. r = val;
  333. g = 0;
  334. b = (val * (10 - (hue % 10))) / 10;
  335. break;
  336. }
  337. COLOR_R((uint8_t)r);
  338. COLOR_G((uint8_t)g);
  339. COLOR_B((uint8_t)b);
  340. }
  341. /**
  342. * Show info on tubes.
  343. */
  344. void showTime(void) {
  345. MinusFadeIn();
  346. RTOS_SetTask(MinusFadeOut, 500, 0);
  347. if (Flag.Now_Day != 0) {
  348. uint8_t hue = bcd2bin(Clock.Sec);
  349. HSV2LED(hue, BrightLevel);
  350. } else {
  351. HSV2LED(COLOUR_NIXIE, BrightLevel);
  352. }
  353. tube4_t buf;
  354. buf.s8.tA = Clock.Hr >> 4;
  355. buf.s8.tB = Clock.Hr & 0xf;
  356. buf.s8.tD = Clock.Min >> 4;
  357. buf.s8.tE = Clock.Min & 0xf;
  358. showDigits(buf);
  359. }
  360. void showMMSS(void) {
  361. RTOS_DeleteTask(MinusFadeOut);
  362. IN15_Minus;
  363. uint8_t hue = bcd2bin(Clock.Sec);
  364. HSV2LED(hue, BrightLevel);
  365. tube4_t buf;
  366. buf.s8.tA = Clock.Min >> 4;
  367. buf.s8.tB = Clock.Min & 0xf;
  368. buf.s8.tD = Clock.Sec >> 4;
  369. buf.s8.tE = Clock.Sec & 0xf;
  370. showDigits(buf);
  371. }
  372. void showWD(void) {
  373. dispWDT = DISP_WDT_TIME;
  374. IN15_OFF;
  375. tube4_t buf;
  376. buf.s8.tA = 0xf;
  377. buf.s8.tB = Clock.WD & 0xf;
  378. buf.s8.tD = 0xf;
  379. buf.s8.tE = 0xf;
  380. showDigits(buf);
  381. }
  382. void showDayMon(void) {
  383. dispWDT = DISP_WDT_TIME;
  384. IN15_OFF;
  385. tube4_t buf;
  386. buf.s8.tA = Clock.Day >> 4;
  387. buf.s8.tB = Clock.Day & 0xf;
  388. buf.s8.tD = Clock.Mon >> 4;
  389. buf.s8.tE = Clock.Mon & 0xf;
  390. showDigits(buf);
  391. }
  392. void showYear(void) {
  393. dispWDT = DISP_WDT_TIME;
  394. IN15_OFF;
  395. tube4_t buf;
  396. buf.s8.tA = 2;
  397. buf.s8.tB = 0;
  398. buf.s8.tD = Clock.Year >> 4;
  399. buf.s8.tE = Clock.Year & 0xf;
  400. showDigits(buf);
  401. }
  402. void showHumidity(void) {
  403. dispWDT = DISP_WDT_TIME;
  404. HSV2LED(COLOUR_BLUE, BrightLevel);
  405. symToFade = sym_Percent;
  406. IN15_FadeIn();
  407. tube4_t buf;
  408. buf.s8.tA = Humidity / 10;
  409. buf.s8.tB = Humidity % 10;
  410. buf.s8.tD = 0xf;
  411. buf.s8.tE = 0xf;
  412. showDigits(buf);
  413. }
  414. void showTemperature(void) {
  415. dispWDT = DISP_WDT_TIME;
  416. HSV2LED(COLOUR_RED, BrightLevel);
  417. RTOS_DeleteTask(MinusFadeIn);
  418. RTOS_DeleteTask(MinusFadeOut);
  419. symToFade = sym_Plus;
  420. IN15_FadeIn();
  421. tube4_t buf;
  422. buf.s8.tA = 0xf;
  423. buf.s8.tB = 0xf;
  424. buf.s8.tD = Temperature / 10;
  425. buf.s8.tE = Temperature % 10;
  426. showDigits(buf);
  427. }
  428. void showPressure(void) {
  429. dispWDT = DISP_WDT_TIME;
  430. HSV2LED(COLOUR_GREEN, BrightLevel);
  431. symToFade = sym_Pressure;
  432. IN15_FadeIn();
  433. tube4_t buf;
  434. int tmp;
  435. buf.s8.tA = 0xf;
  436. buf.s8.tB = Pressure / 100;
  437. tmp = Pressure % 100;
  438. buf.s8.tD = tmp / 10;
  439. buf.s8.tE = tmp % 10;
  440. showDigits(buf);
  441. }
  442. /* Simple function for cyclic show all sensor data */
  443. void showSensorData(void) {
  444. RTOS_DeleteTask(MinusFadeOut);
  445. showTemperature();
  446. tdelay_ms(3000);
  447. showHumidity();
  448. tdelay_ms(3000);
  449. showPressure();
  450. tdelay_ms(2700);
  451. ES_SetState(stShowTime);
  452. // showTime();
  453. }
  454. void setTimeShow(void) {
  455. dispWDT = DISP_WDT_TIME;
  456. tube4_t buf;
  457. buf.s8.tA = setClock.Hr >> 4;
  458. buf.s8.tB = setClock.Hr & 0xf;
  459. buf.s8.tD = setClock.Min >> 4;
  460. buf.s8.tE = setClock.Min & 0xf;
  461. showDigits(buf);
  462. }
  463. void setTimeBegin(void) {
  464. RTOS_DeleteTask(MinusFadeOut);
  465. in15Minus();
  466. HSV2LED(COLOUR_NIXIE, BrightLevel);
  467. RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
  468. RTC_ReadAll(&setClock);
  469. }
  470. void setHHBegin(void) {
  471. Flag.Blink_1 = 1;
  472. Flag.Blink_2 = 1;
  473. Flag.Blink_4 = 0;
  474. Flag.Blink_5 = 0;
  475. Blink_Start();
  476. setTimeShow();
  477. }
  478. void setHHInc(void) {
  479. valIncrease(&setClock.Hr, 23);
  480. }
  481. void setHHDec(void) {
  482. valDecrease(&setClock.Hr, 23);
  483. }
  484. void setMMBegin(void) {
  485. Flag.Blink_1 = 0;
  486. Flag.Blink_2 = 0;
  487. Flag.Blink_4 = 1;
  488. Flag.Blink_5 = 1;
  489. Blink_Start();
  490. setTimeShow();
  491. }
  492. void setMMInc(void) {
  493. valIncrease(&setClock.Min, 59);
  494. }
  495. void setMMDec(void) {
  496. valDecrease(&setClock.Min, 59);
  497. }
  498. void setTimeEnd(void) {
  499. dispWDT = 0;
  500. RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
  501. setClock.Sec = 0;
  502. RTC_WriteTime(&setClock);
  503. Blink_Stop();
  504. RTC_ReadAll(&Clock);
  505. }
  506. void setDateBegin(void) {
  507. IN15_OFF;
  508. HSV2LED(COLOUR_NIXIE, BrightLevel);
  509. RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
  510. RTC_ReadAll(&setClock);
  511. }
  512. void setDateEnd(void) {
  513. dispWDT = 0;
  514. RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
  515. RTC_WriteCalendar(&setClock);
  516. Blink_Stop();
  517. RTC_ReadAll(&Clock);
  518. }
  519. void setWDBegin(void) {
  520. Flag.Blink_1 = 0;
  521. Flag.Blink_2 = 1;
  522. Flag.Blink_4 = 0;
  523. Flag.Blink_5 = 0;
  524. Blink_Start();
  525. setWDShow();
  526. }
  527. void setWDShow(void) {
  528. dispWDT = DISP_WDT_TIME;
  529. tube4_t buf;
  530. buf.s8.tA = 0xf;
  531. buf.s8.tB = setClock.WD & 0xf;
  532. buf.s8.tD = 0xf;
  533. buf.s8.tE = 0xf;
  534. showDigits(buf);
  535. }
  536. void setDMShow(void) {
  537. dispWDT = DISP_WDT_TIME;
  538. tube4_t buf;
  539. buf.s8.tA = setClock.Day >> 4;
  540. buf.s8.tB = setClock.Day & 0xf;
  541. buf.s8.tD = setClock.Mon >> 4;
  542. buf.s8.tE = setClock.Mon & 0xf;
  543. showDigits(buf);
  544. }
  545. void setYearShow(void) {
  546. dispWDT = DISP_WDT_TIME;
  547. tube4_t buf;
  548. buf.s8.tA = 2;
  549. buf.s8.tB = 0;
  550. buf.s8.tD = setClock.Year >> 4;
  551. buf.s8.tE = setClock.Year & 0xf;
  552. showDigits(buf);
  553. }
  554. void setMDBegin(void) {
  555. Flag.Blink_1 = 1;
  556. Flag.Blink_2 = 1;
  557. Flag.Blink_4 = 0;
  558. Flag.Blink_5 = 0;
  559. Blink_Start();
  560. setDMShow();
  561. }
  562. void setMonthBegin(void) {
  563. Flag.Blink_1 = 0;
  564. Flag.Blink_2 = 0;
  565. Flag.Blink_4 = 1;
  566. Flag.Blink_5 = 1;
  567. Blink_Start();
  568. setDMShow();
  569. }
  570. void setYearBegin(void) {
  571. Flag.Blink_1 = 0;
  572. Flag.Blink_2 = 0;
  573. Flag.Blink_4 = 1;
  574. Flag.Blink_5 = 1;
  575. Blink_Start();
  576. setYearShow();
  577. }
  578. void setIncWDay(void) {
  579. valIncrease(&setClock.WD, 7);
  580. }
  581. void setIncMDay(void) {
  582. valIncrease(&setClock.Day, 31);
  583. }
  584. void setIncMonth(void) {
  585. valIncrease(&setClock.Mon, 12);
  586. }
  587. void setIncYear(void) {
  588. valIncrease(&setClock.Year, 99);
  589. }
  590. void setDecWDay(void) {
  591. valDecrease(&setClock.WD, 7);
  592. }
  593. void setDecMDay(void) {
  594. valDecrease(&setClock.Day, 31);
  595. }
  596. void setDecMonth(void) {
  597. valDecrease(&setClock.Mon, 12);
  598. }
  599. void setDecYear(void) {
  600. valDecrease(&setClock.Year, 99);
  601. }
  602. void showDNhour(void) {
  603. dispWDT = DISP_WDT_TIME;
  604. tube4_t buf;
  605. es_state_t e_st = ES_GetState();
  606. if (e_st == stShowDNhours){
  607. buf.s8.tA = Lighting.name.DayHour >> 4;
  608. buf.s8.tB = Lighting.name.DayHour & 0xf;
  609. buf.s8.tD = Lighting.name.NightHour >> 4;
  610. buf.s8.tE = Lighting.name.NightHour & 0xf;
  611. } else {
  612. buf.s8.tA = setLighting.name.DayHour >> 4;
  613. buf.s8.tB = setLighting.name.DayHour & 0xf;
  614. buf.s8.tD = setLighting.name.NightHour >> 4;
  615. buf.s8.tE = setLighting.name.NightHour & 0xf;
  616. }
  617. showDigits(buf);
  618. }
  619. void showDNbright(void) {
  620. dispWDT = DISP_WDT_TIME;
  621. tube4_t buf;
  622. es_state_t e_st = ES_GetState();
  623. if (e_st == stShowDNbright){
  624. buf.s8.tA = Lighting.name.DayBright / 10;
  625. buf.s8.tB = Lighting.name.DayBright % 10;
  626. buf.s8.tD = Lighting.name.NightBright / 10;
  627. buf.s8.tE = Lighting.name.NightBright % 10;
  628. } else {
  629. buf.s8.tA = setLighting.name.DayBright / 10;
  630. buf.s8.tB = setLighting.name.DayBright % 10;
  631. buf.s8.tD = setLighting.name.NightBright / 10;
  632. buf.s8.tE = setLighting.name.NightBright % 10;
  633. }
  634. showDigits(buf);
  635. }
  636. void showDNmode(void) {
  637. dispWDT = DISP_WDT_TIME;
  638. tube4_t buf;
  639. es_state_t e_st = ES_GetState();
  640. if (e_st == stShowDNmode){
  641. buf.s8.tA = Lighting.name.DayMode / 10;
  642. buf.s8.tB = Lighting.name.DayMode % 10;
  643. buf.s8.tD = Lighting.name.NightMode / 10;
  644. buf.s8.tE = Lighting.name.NightMode % 10;
  645. } else {
  646. buf.s8.tA = setLighting.name.DayMode / 10;
  647. buf.s8.tB = setLighting.name.DayMode % 10;
  648. buf.s8.tD = setLighting.name.NightMode / 10;
  649. buf.s8.tE = setLighting.name.NightMode % 10;
  650. }
  651. showDigits(buf);
  652. }
  653. void showDNcolour(void) {
  654. dispWDT = DISP_WDT_TIME;
  655. tube4_t buf;
  656. es_state_t e_st = ES_GetState();
  657. if (e_st == stShowDNcolour){
  658. buf.s8.tA = Lighting.name.DayColour / 10;
  659. buf.s8.tB = Lighting.name.DayColour % 10;
  660. buf.s8.tD = Lighting.name.NightColour / 10;
  661. buf.s8.tE = Lighting.name.NightColour % 10;
  662. } else {
  663. buf.s8.tA = setLighting.name.DayColour / 10;
  664. buf.s8.tB = setLighting.name.DayColour % 10;
  665. buf.s8.tD = setLighting.name.NightColour / 10;
  666. buf.s8.tE = setLighting.name.NightColour % 10;
  667. }
  668. showDigits(buf);
  669. }
  670. void setDNbegin(void) {
  671. IN15_OFF;
  672. RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
  673. setLighting.u64 = Lighting.u64;
  674. //Flash_Read(&setLighting.u64);
  675. }
  676. void setDNend(void) {
  677. dispWDT = 0;
  678. RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
  679. Blink_Stop();
  680. LEDS_OFF;
  681. Flash_Write(&setLighting.u64);
  682. Lighting.u64 = setLighting.u64;
  683. check_DayNight();
  684. }
  685. void setDayHourBegin(void) {
  686. LEDS_OFF;
  687. Flag.Blink_1 = 1;
  688. Flag.Blink_2 = 1;
  689. Flag.Blink_4 = 0;
  690. Flag.Blink_5 = 0;
  691. Blink_Start();
  692. showDNhour();
  693. }
  694. void setNightHourBegin(void) {
  695. Flag.Blink_1 = 0;
  696. Flag.Blink_2 = 0;
  697. Flag.Blink_4 = 1;
  698. Flag.Blink_5 = 1;
  699. Blink_Start();
  700. showDNhour();
  701. }
  702. void setDayBrightBegin(void) {
  703. Flag.Blink_1 = 1;
  704. Flag.Blink_2 = 1;
  705. Flag.Blink_4 = 0;
  706. Flag.Blink_5 = 0;
  707. Blink_Start();
  708. showDNbright();
  709. }
  710. void setNightBrightBegin(void) {
  711. Flag.Blink_1 = 0;
  712. Flag.Blink_2 = 0;
  713. Flag.Blink_4 = 1;
  714. Flag.Blink_5 = 1;
  715. Blink_Start();
  716. showDNbright();
  717. }
  718. void setDayModeBegin(void) {
  719. Flag.Blink_1 = 1;
  720. Flag.Blink_2 = 1;
  721. Flag.Blink_4 = 0;
  722. Flag.Blink_5 = 0;
  723. Blink_Start();
  724. showDNmode();
  725. }
  726. void setNightModeBegin(void) {
  727. Flag.Blink_1 = 0;
  728. Flag.Blink_2 = 0;
  729. Flag.Blink_4 = 1;
  730. Flag.Blink_5 = 1;
  731. Blink_Start();
  732. showDNmode();
  733. }
  734. void setDayColourBegin(void) {
  735. Flag.Blink_1 = 1;
  736. Flag.Blink_2 = 1;
  737. Flag.Blink_4 = 0;
  738. Flag.Blink_5 = 0;
  739. Blink_Start();
  740. HSV2LED(setLighting.name.DayColour, BrightLevel);
  741. showDNcolour();
  742. }
  743. void setNightColourBegin(void) {
  744. Flag.Blink_1 = 0;
  745. Flag.Blink_2 = 0;
  746. Flag.Blink_4 = 1;
  747. Flag.Blink_5 = 1;
  748. Blink_Start();
  749. HSV2LED(setLighting.name.NightColour, BrightLevel);
  750. showDNcolour();
  751. }
  752. void setIncDayHour(void) {
  753. valIncrease(&setLighting.name.DayHour, 23);
  754. }
  755. void setIncDayBright(void) {
  756. dvalIncrease(&setLighting.name.DayBright, MAX_BRIGHT_LVL);
  757. tube_BrightLevel(Tube_All, setLighting.name.DayBright);
  758. }
  759. void setIncDayMode(void) {
  760. dvalIncrease(&setLighting.name.DayMode, MAX_LIGHT_MODE);
  761. }
  762. void setIncDayColour(void) {
  763. dvalIncrease(&setLighting.name.DayColour, MAX_COLOR_VAL);
  764. HSV2LED(setLighting.name.DayColour, BrightLevel);
  765. }
  766. void setDecDayHour(void) {
  767. valDecrease(&setLighting.name.DayHour, 23);
  768. }
  769. void setDecDayBright(void) {
  770. dvalDecrease(&setLighting.name.DayBright, MAX_BRIGHT_LVL);
  771. tube_BrightLevel(Tube_All, setLighting.name.DayBright);
  772. }
  773. void setDecDayMode(void) {
  774. dvalDecrease(&setLighting.name.DayMode, MAX_LIGHT_MODE);
  775. }
  776. void setDecDayColour(void) {
  777. dvalDecrease(&setLighting.name.DayColour, MAX_COLOR_VAL);
  778. HSV2LED(setLighting.name.DayColour, BrightLevel);
  779. }
  780. void setIncNightHour(void) {
  781. valIncrease(&setLighting.name.NightHour, 23);
  782. }
  783. void setIncNightBright(void) {
  784. dvalIncrease(&setLighting.name.NightBright, MAX_BRIGHT_LVL);
  785. tube_BrightLevel(Tube_All, setLighting.name.NightBright);
  786. }
  787. void setIncNightMode(void) {
  788. dvalIncrease(&setLighting.name.NightMode, MAX_LIGHT_MODE);
  789. }
  790. void setIncNightColour(void) {
  791. dvalIncrease(&setLighting.name.NightColour, MAX_COLOR_VAL);
  792. HSV2LED(setLighting.name.NightColour, BrightLevel);
  793. }
  794. void setDecNightHour(void) {
  795. valDecrease(&setLighting.name.NightHour, 23);
  796. }
  797. void setDecNightBright(void) {
  798. dvalDecrease(&setLighting.name.NightBright, MAX_BRIGHT_LVL);
  799. tube_BrightLevel(Tube_All, setLighting.name.NightBright);
  800. }
  801. void setDecNightMode(void) {
  802. dvalDecrease(&setLighting.name.NightMode, MAX_LIGHT_MODE);
  803. }
  804. void setDecNightColour(void) {
  805. dvalDecrease(&setLighting.name.NightColour, MAX_COLOR_VAL);
  806. HSV2LED(setLighting.name.NightColour, BrightLevel);
  807. }
  808. void setDNbreak(void) {
  809. // restore led and tube bright level
  810. check_DayNight();
  811. }
  812. /**
  813. * @brief Increase BCD value.
  814. * @param : val, max
  815. * @retval : None
  816. */
  817. static void valIncrease(uint8_t * val, uint8_t max) {
  818. uint8_t bin = 10 * (*val >> 4) + (*val & 0x0f);
  819. if (bin < max) {
  820. bin ++;
  821. } else {
  822. bin = 0;
  823. }
  824. *val = ((bin / 10 ) << 4) | (bin % 10);
  825. }
  826. /**
  827. * @brief Decrease BCD value.
  828. * @param : value, max
  829. * @retval : None
  830. */
  831. static void valDecrease(uint8_t * val, uint8_t max) {
  832. uint8_t bin = 10 * (*val >> 4) + (*val & 0x0f);
  833. if (bin > 0) {
  834. bin --;
  835. } else {
  836. bin = max;
  837. }
  838. *val = ((bin / 10 ) << 4) | (bin % 10);
  839. }
  840. /**
  841. * @brief Increase/Decrease decimal value.
  842. * @param val
  843. * @param max
  844. * @retval : None, change value
  845. */
  846. static void dvalIncrease(uint8_t * val, uint8_t max) {
  847. if (*val < max) {
  848. *val += 1;
  849. } else {
  850. *val = 0;
  851. }
  852. }
  853. static void dvalDecrease(uint8_t * val, uint8_t max) {
  854. if (*val > 0) {
  855. *val -= 1;
  856. } else {
  857. *val = max;
  858. }
  859. }