clock.c 22 KB

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