clock.c 21 KB

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