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