clock.c 20 KB

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