clock.c 21 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  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. uint8_t hue;
  345. MinusFadeIn();
  346. RTOS_SetTask(MinusFadeOut, 500, 0);
  347. switch (LightingMode) {
  348. case light_Rainbow:
  349. hue = bcd2bin(Clock.Sec);
  350. HSV2LED(hue, LightingBright);
  351. break;
  352. case light_Colour:
  353. HSV2LED(LightingColour, LightingBright);
  354. break;
  355. default:
  356. LEDS_OFF;
  357. break;
  358. }
  359. tube4_t buf;
  360. buf.s8.tA = Clock.Hr >> 4;
  361. buf.s8.tB = Clock.Hr & 0xf;
  362. buf.s8.tD = Clock.Min >> 4;
  363. buf.s8.tE = Clock.Min & 0xf;
  364. showDigits(buf);
  365. }
  366. void showMMSS(void) {
  367. RTOS_DeleteTask(MinusFadeOut);
  368. IN15_Minus;
  369. uint8_t hue = bcd2bin(Clock.Sec);
  370. HSV2LED(hue, LightingBright);
  371. tube4_t buf;
  372. buf.s8.tA = Clock.Min >> 4;
  373. buf.s8.tB = Clock.Min & 0xf;
  374. buf.s8.tD = Clock.Sec >> 4;
  375. buf.s8.tE = Clock.Sec & 0xf;
  376. showDigits(buf);
  377. }
  378. void showWD(void) {
  379. dispWDT = DISP_WDT_TIME;
  380. IN15_OFF;
  381. tube4_t buf;
  382. buf.s8.tA = TUBE_BLANK;
  383. buf.s8.tB = Clock.WD & 0xf;
  384. buf.s8.tD = TUBE_BLANK;
  385. buf.s8.tE = TUBE_BLANK;
  386. showDigits(buf);
  387. }
  388. void showDayMon(void) {
  389. dispWDT = DISP_WDT_TIME;
  390. IN15_OFF;
  391. tube4_t buf;
  392. buf.s8.tA = Clock.Day >> 4;
  393. buf.s8.tB = Clock.Day & 0xf;
  394. buf.s8.tD = Clock.Mon >> 4;
  395. buf.s8.tE = Clock.Mon & 0xf;
  396. showDigits(buf);
  397. }
  398. void showYear(void) {
  399. dispWDT = DISP_WDT_TIME;
  400. IN15_OFF;
  401. tube4_t buf;
  402. buf.s8.tA = 2;
  403. buf.s8.tB = 0;
  404. buf.s8.tD = Clock.Year >> 4;
  405. buf.s8.tE = Clock.Year & 0xf;
  406. showDigits(buf);
  407. }
  408. void showHumidity(void) {
  409. dispWDT = DISP_WDT_TIME/2;
  410. HSV2LED(COLOUR_BLUE, LightingBright);
  411. symToFade = sym_Percent;
  412. IN15_FadeIn();
  413. tube4_t buf;
  414. buf.s8.tA = Humidity / 10;
  415. buf.s8.tB = Humidity % 10;
  416. buf.s8.tD = TUBE_BLANK;
  417. buf.s8.tE = TUBE_BLANK;
  418. showDigits(buf);
  419. }
  420. void showTemperature(void) {
  421. dispWDT = DISP_WDT_TIME/2;
  422. HSV2LED(COLOUR_RED, LightingBright);
  423. RTOS_DeleteTask(MinusFadeIn);
  424. RTOS_DeleteTask(MinusFadeOut);
  425. symToFade = sym_Plus;
  426. IN15_FadeIn();
  427. tube4_t buf;
  428. buf.s8.tA = TUBE_BLANK;
  429. buf.s8.tB = TUBE_BLANK;
  430. buf.s8.tD = Temperature / 10;
  431. buf.s8.tE = Temperature % 10;
  432. showDigits(buf);
  433. }
  434. void showPressure(void) {
  435. dispWDT = DISP_WDT_TIME/2;
  436. HSV2LED(COLOUR_GREEN, LightingBright);
  437. symToFade = sym_Pressure;
  438. IN15_FadeIn();
  439. tube4_t buf;
  440. int tmp;
  441. buf.s8.tA = TUBE_BLANK;
  442. buf.s8.tB = Pressure / 100;
  443. tmp = Pressure % 100;
  444. buf.s8.tD = tmp / 10;
  445. buf.s8.tE = tmp % 10;
  446. showDigits(buf);
  447. }
  448. /* Simple function for cyclic show all sensor data */
  449. void showSensorData(void) {
  450. RTOS_DeleteTask(MinusFadeOut);
  451. showTemperature();
  452. tdelay_ms(3000);
  453. showHumidity();
  454. tdelay_ms(3000);
  455. showPressure();
  456. tdelay_ms(2700);
  457. ES_SetState(stShowTime);
  458. // showTime();
  459. }
  460. void setTimeShow(void) {
  461. dispWDT = DISP_WDT_TIME;
  462. tube4_t buf;
  463. buf.s8.tA = setClock.Hr >> 4;
  464. buf.s8.tB = setClock.Hr & 0xf;
  465. buf.s8.tD = setClock.Min >> 4;
  466. buf.s8.tE = setClock.Min & 0xf;
  467. showDigits(buf);
  468. }
  469. void setTimeBegin(void) {
  470. RTOS_DeleteTask(MinusFadeOut);
  471. in15Minus();
  472. HSV2LED(COLOUR_NIXIE, LightingBright);
  473. RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
  474. RTC_ReadAll(&setClock);
  475. }
  476. void setHHBegin(void) {
  477. Flag.Blink_1 = 1;
  478. Flag.Blink_2 = 1;
  479. Flag.Blink_4 = 0;
  480. Flag.Blink_5 = 0;
  481. Blink_Start();
  482. setTimeShow();
  483. }
  484. void setHHInc(void) {
  485. valIncrease(&setClock.Hr, 23);
  486. }
  487. void setHHDec(void) {
  488. valDecrease(&setClock.Hr, 23);
  489. }
  490. void setMMBegin(void) {
  491. Flag.Blink_1 = 0;
  492. Flag.Blink_2 = 0;
  493. Flag.Blink_4 = 1;
  494. Flag.Blink_5 = 1;
  495. Blink_Start();
  496. setTimeShow();
  497. }
  498. void setMMInc(void) {
  499. valIncrease(&setClock.Min, 59);
  500. }
  501. void setMMDec(void) {
  502. valDecrease(&setClock.Min, 59);
  503. }
  504. void setTimeEnd(void) {
  505. dispWDT = 0;
  506. RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
  507. setClock.Sec = 0;
  508. RTC_WriteTime(&setClock);
  509. Blink_Stop();
  510. RTC_ReadAll(&Clock);
  511. }
  512. void setDateBegin(void) {
  513. IN15_OFF;
  514. HSV2LED(COLOUR_NIXIE, LightingBright);
  515. RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
  516. RTC_ReadAll(&setClock);
  517. }
  518. void setDateEnd(void) {
  519. dispWDT = 0;
  520. RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
  521. RTC_WriteCalendar(&setClock);
  522. Blink_Stop();
  523. RTC_ReadAll(&Clock);
  524. }
  525. void setWDBegin(void) {
  526. Flag.Blink_1 = 0;
  527. Flag.Blink_2 = 1;
  528. Flag.Blink_4 = 0;
  529. Flag.Blink_5 = 0;
  530. Blink_Start();
  531. setWDShow();
  532. }
  533. void setWDShow(void) {
  534. dispWDT = DISP_WDT_TIME;
  535. tube4_t buf;
  536. buf.s8.tA = TUBE_BLANK;
  537. buf.s8.tB = setClock.WD & 0xf;
  538. buf.s8.tD = TUBE_BLANK;
  539. buf.s8.tE = TUBE_BLANK;
  540. showDigits(buf);
  541. }
  542. void setDMShow(void) {
  543. dispWDT = DISP_WDT_TIME;
  544. tube4_t buf;
  545. buf.s8.tA = setClock.Day >> 4;
  546. buf.s8.tB = setClock.Day & 0xf;
  547. buf.s8.tD = setClock.Mon >> 4;
  548. buf.s8.tE = setClock.Mon & 0xf;
  549. showDigits(buf);
  550. }
  551. void setYearShow(void) {
  552. dispWDT = DISP_WDT_TIME;
  553. tube4_t buf;
  554. buf.s8.tA = 2;
  555. buf.s8.tB = 0;
  556. buf.s8.tD = setClock.Year >> 4;
  557. buf.s8.tE = setClock.Year & 0xf;
  558. showDigits(buf);
  559. }
  560. void setMDBegin(void) {
  561. Flag.Blink_1 = 1;
  562. Flag.Blink_2 = 1;
  563. Flag.Blink_4 = 0;
  564. Flag.Blink_5 = 0;
  565. Blink_Start();
  566. setDMShow();
  567. }
  568. void setMonthBegin(void) {
  569. Flag.Blink_1 = 0;
  570. Flag.Blink_2 = 0;
  571. Flag.Blink_4 = 1;
  572. Flag.Blink_5 = 1;
  573. Blink_Start();
  574. setDMShow();
  575. }
  576. void setYearBegin(void) {
  577. Flag.Blink_1 = 0;
  578. Flag.Blink_2 = 0;
  579. Flag.Blink_4 = 1;
  580. Flag.Blink_5 = 1;
  581. Blink_Start();
  582. setYearShow();
  583. }
  584. void setIncWDay(void) {
  585. valIncrease(&setClock.WD, 7);
  586. }
  587. void setIncMDay(void) {
  588. valIncrease(&setClock.Day, 31);
  589. }
  590. void setIncMonth(void) {
  591. valIncrease(&setClock.Mon, 12);
  592. }
  593. void setIncYear(void) {
  594. valIncrease(&setClock.Year, 99);
  595. }
  596. void setDecWDay(void) {
  597. valDecrease(&setClock.WD, 7);
  598. }
  599. void setDecMDay(void) {
  600. valDecrease(&setClock.Day, 31);
  601. }
  602. void setDecMonth(void) {
  603. valDecrease(&setClock.Mon, 12);
  604. }
  605. void setDecYear(void) {
  606. valDecrease(&setClock.Year, 99);
  607. }
  608. void showDNhour(void) {
  609. dispWDT = DISP_WDT_TIME;
  610. tube4_t buf;
  611. es_state_t e_st = ES_GetState();
  612. if (e_st == stShowDNhours){
  613. buf.s8.tA = Lighting.name.DayHour >> 4;
  614. buf.s8.tB = Lighting.name.DayHour & 0xf;
  615. buf.s8.tD = Lighting.name.NightHour >> 4;
  616. buf.s8.tE = Lighting.name.NightHour & 0xf;
  617. } else {
  618. buf.s8.tA = setLighting.name.DayHour >> 4;
  619. buf.s8.tB = setLighting.name.DayHour & 0xf;
  620. buf.s8.tD = setLighting.name.NightHour >> 4;
  621. buf.s8.tE = setLighting.name.NightHour & 0xf;
  622. }
  623. showDigits(buf);
  624. }
  625. void showDNbright(void) {
  626. dispWDT = DISP_WDT_TIME;
  627. tube4_t buf;
  628. es_state_t e_st = ES_GetState();
  629. if (e_st == stShowDNbright){
  630. buf.s8.tA = Lighting.name.DayBright / 10;
  631. buf.s8.tB = Lighting.name.DayBright % 10;
  632. buf.s8.tD = Lighting.name.NightBright / 10;
  633. buf.s8.tE = Lighting.name.NightBright % 10;
  634. } else {
  635. buf.s8.tA = setLighting.name.DayBright / 10;
  636. buf.s8.tB = setLighting.name.DayBright % 10;
  637. buf.s8.tD = setLighting.name.NightBright / 10;
  638. buf.s8.tE = setLighting.name.NightBright % 10;
  639. }
  640. showDigits(buf);
  641. }
  642. void showDNmode(void) {
  643. dispWDT = DISP_WDT_TIME;
  644. uint8_t dm, nm;
  645. tube4_t buf;
  646. es_state_t e_st = ES_GetState();
  647. if (e_st == stShowDNmode){
  648. dm = Lighting.name.DayMode;
  649. nm = Lighting.name.NightMode;
  650. } else {
  651. dm = setLighting.name.DayMode;
  652. nm = setLighting.name.NightMode;
  653. }
  654. buf.s8.tA = TUBE_BLANK;
  655. buf.s8.tB = dm & 0xf;
  656. buf.s8.tD = TUBE_BLANK;
  657. buf.s8.tE = nm & 0xf;
  658. showDigits(buf);
  659. }
  660. void showDNcolour(void) {
  661. dispWDT = DISP_WDT_TIME;
  662. tube4_t buf;
  663. es_state_t e_st = ES_GetState();
  664. if (e_st == stShowDNcolour){
  665. buf.s8.tA = Lighting.name.DayColour / 10;
  666. buf.s8.tB = Lighting.name.DayColour % 10;
  667. buf.s8.tD = Lighting.name.NightColour / 10;
  668. buf.s8.tE = Lighting.name.NightColour % 10;
  669. } else {
  670. buf.s8.tA = setLighting.name.DayColour / 10;
  671. buf.s8.tB = setLighting.name.DayColour % 10;
  672. buf.s8.tD = setLighting.name.NightColour / 10;
  673. buf.s8.tE = setLighting.name.NightColour % 10;
  674. }
  675. showDigits(buf);
  676. }
  677. void setDNbegin(void) {
  678. IN15_OFF;
  679. RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
  680. setLighting.u64 = Lighting.u64;
  681. //Flash_Read(&setLighting.u64);
  682. }
  683. void setDNend(void) {
  684. dispWDT = 0;
  685. RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
  686. Blink_Stop();
  687. LEDS_OFF;
  688. Flash_Write(&setLighting.u64);
  689. Lighting.u64 = setLighting.u64;
  690. check_DayNight();
  691. }
  692. void setDayHourBegin(void) {
  693. LEDS_OFF;
  694. Flag.Blink_1 = 1;
  695. Flag.Blink_2 = 1;
  696. Flag.Blink_4 = 0;
  697. Flag.Blink_5 = 0;
  698. Blink_Start();
  699. showDNhour();
  700. }
  701. void setNightHourBegin(void) {
  702. Flag.Blink_1 = 0;
  703. Flag.Blink_2 = 0;
  704. Flag.Blink_4 = 1;
  705. Flag.Blink_5 = 1;
  706. Blink_Start();
  707. showDNhour();
  708. }
  709. void setDayBrightBegin(void) {
  710. Flag.Blink_1 = 1;
  711. Flag.Blink_2 = 1;
  712. Flag.Blink_4 = 0;
  713. Flag.Blink_5 = 0;
  714. Blink_Start();
  715. showDNbright();
  716. }
  717. void setNightBrightBegin(void) {
  718. Flag.Blink_1 = 0;
  719. Flag.Blink_2 = 0;
  720. Flag.Blink_4 = 1;
  721. Flag.Blink_5 = 1;
  722. Blink_Start();
  723. showDNbright();
  724. }
  725. void setDayModeBegin(void) {
  726. Flag.Blink_1 = 0;
  727. Flag.Blink_2 = 1;
  728. Flag.Blink_4 = 0;
  729. Flag.Blink_5 = 0;
  730. Blink_Start();
  731. showDNmode();
  732. }
  733. void setNightModeBegin(void) {
  734. Flag.Blink_1 = 0;
  735. Flag.Blink_2 = 0;
  736. Flag.Blink_4 = 0;
  737. Flag.Blink_5 = 1;
  738. Blink_Start();
  739. showDNmode();
  740. }
  741. void setDayColourBegin(void) {
  742. Flag.Blink_1 = 1;
  743. Flag.Blink_2 = 1;
  744. Flag.Blink_4 = 0;
  745. Flag.Blink_5 = 0;
  746. Blink_Start();
  747. HSV2LED(setLighting.name.DayColour, LightingBright);
  748. showDNcolour();
  749. }
  750. void setNightColourBegin(void) {
  751. Flag.Blink_1 = 0;
  752. Flag.Blink_2 = 0;
  753. Flag.Blink_4 = 1;
  754. Flag.Blink_5 = 1;
  755. Blink_Start();
  756. HSV2LED(setLighting.name.NightColour, LightingBright);
  757. showDNcolour();
  758. }
  759. void setIncDayHour(void) {
  760. valIncrease(&setLighting.name.DayHour, 23);
  761. }
  762. void setIncDayBright(void) {
  763. dvalIncrease(&setLighting.name.DayBright, MAX_BRIGHT_LVL);
  764. //tube_BrightLevel(Tube_All, setLighting.name.DayBright);
  765. TUBES_BRIGHT(setLighting.name.DayBright);
  766. }
  767. void setIncDayMode(void) {
  768. dvalIncrease(&setLighting.name.DayMode, MAX_LIGHT_MODE);
  769. if (setLighting.name.DayMode == light_Rainbow) {
  770. RTOS_SetTask(showRainbow, 0, 20);
  771. } else {
  772. RTOS_DeleteTask(showRainbow);
  773. }
  774. }
  775. void setIncDayColour(void) {
  776. dvalIncrease(&setLighting.name.DayColour, MAX_COLOR_VAL);
  777. HSV2LED(setLighting.name.DayColour, LightingBright);
  778. }
  779. void setDecDayHour(void) {
  780. valDecrease(&setLighting.name.DayHour, 23);
  781. }
  782. void setDecDayBright(void) {
  783. dvalDecrease(&setLighting.name.DayBright, MAX_BRIGHT_LVL);
  784. //tube_BrightLevel(Tube_All, setLighting.name.DayBright);
  785. TUBES_BRIGHT(setLighting.name.DayBright);
  786. }
  787. void setDecDayMode(void) {
  788. dvalDecrease(&setLighting.name.DayMode, MAX_LIGHT_MODE);
  789. if (setLighting.name.DayMode == light_Rainbow) {
  790. RTOS_SetTask(showRainbow, 0, 20);
  791. } else {
  792. RTOS_DeleteTask(showRainbow);
  793. }
  794. }
  795. void setDecDayColour(void) {
  796. dvalDecrease(&setLighting.name.DayColour, MAX_COLOR_VAL);
  797. HSV2LED(setLighting.name.DayColour, LightingBright);
  798. }
  799. void setIncNightHour(void) {
  800. valIncrease(&setLighting.name.NightHour, 23);
  801. }
  802. void setIncNightBright(void) {
  803. dvalIncrease(&setLighting.name.NightBright, MAX_BRIGHT_LVL);
  804. //tube_BrightLevel(Tube_All, setLighting.name.NightBright);
  805. TUBES_BRIGHT(setLighting.name.NightBright);
  806. }
  807. void setIncNightMode(void) {
  808. dvalIncrease(&setLighting.name.NightMode, MAX_LIGHT_MODE);
  809. if (setLighting.name.NightMode == light_Rainbow) {
  810. RTOS_SetTask(showRainbow, 0, 20);
  811. } else {
  812. RTOS_DeleteTask(showRainbow);
  813. }
  814. }
  815. void setIncNightColour(void) {
  816. dvalIncrease(&setLighting.name.NightColour, MAX_COLOR_VAL);
  817. HSV2LED(setLighting.name.NightColour, LightingBright);
  818. }
  819. void setDecNightHour(void) {
  820. valDecrease(&setLighting.name.NightHour, 23);
  821. }
  822. void setDecNightBright(void) {
  823. dvalDecrease(&setLighting.name.NightBright, MAX_BRIGHT_LVL);
  824. //tube_BrightLevel(Tube_All, setLighting.name.NightBright);
  825. TUBES_BRIGHT(setLighting.name.NightBright);
  826. }
  827. void setDecNightMode(void) {
  828. dvalDecrease(&setLighting.name.NightMode, MAX_LIGHT_MODE);
  829. if (setLighting.name.NightMode == light_Rainbow) {
  830. RTOS_SetTask(showRainbow, 0, 20);
  831. } else {
  832. RTOS_DeleteTask(showRainbow);
  833. }
  834. }
  835. void setDecNightColour(void) {
  836. dvalDecrease(&setLighting.name.NightColour, MAX_COLOR_VAL);
  837. HSV2LED(setLighting.name.NightColour, LightingBright);
  838. }
  839. void setDNbreak(void) {
  840. // restore led and tube bright level
  841. check_DayNight();
  842. }
  843. /**
  844. * @brief Increase BCD value.
  845. * @param : val, max
  846. * @retval : None
  847. */
  848. static void valIncrease(uint8_t * val, uint8_t max) {
  849. uint8_t bin = 10 * (*val >> 4) + (*val & 0x0f);
  850. if (bin < max) {
  851. bin ++;
  852. } else {
  853. bin = 0;
  854. }
  855. *val = ((bin / 10 ) << 4) | (bin % 10);
  856. }
  857. /**
  858. * @brief Decrease BCD value.
  859. * @param : value, max
  860. * @retval : None
  861. */
  862. static void valDecrease(uint8_t * val, uint8_t max) {
  863. uint8_t bin = 10 * (*val >> 4) + (*val & 0x0f);
  864. if (bin > 0) {
  865. bin --;
  866. } else {
  867. bin = max;
  868. }
  869. *val = ((bin / 10 ) << 4) | (bin % 10);
  870. }
  871. /**
  872. * @brief Increase/Decrease decimal value.
  873. * @param val
  874. * @param max
  875. * @retval : None, change value
  876. */
  877. static void dvalIncrease(uint8_t * val, uint8_t max) {
  878. if (*val < max) {
  879. *val += 1;
  880. } else {
  881. *val = 0;
  882. }
  883. }
  884. static void dvalDecrease(uint8_t * val, uint8_t max) {
  885. if (*val > 0) {
  886. *val -= 1;
  887. } else {
  888. *val = max;
  889. }
  890. }
  891. static void showRainbow(void) {
  892. static uint8_t hue = 0;
  893. HSV2LED(hue, LightingBright);
  894. if (hue < 59) {
  895. hue ++;
  896. } else {
  897. hue = 0;
  898. RTOS_DeleteTask(showRainbow);
  899. }
  900. }