clock.c 22 KB

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