clock.c 18 KB

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