clock.c 22 KB

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