clock.c 21 KB

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