clock.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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. tube4_t buf;
  207. buf.s8.tA = Clock.Hr >> 4;
  208. buf.s8.tB = Clock.Hr & 0xf;
  209. buf.s8.tD = Clock.Min >> 4;
  210. buf.s8.tE = Clock.Min & 0xf;
  211. showDigits(buf);
  212. }
  213. void showMMSS(void) {
  214. in15Minus();
  215. uint8_t hue = bcd2bin(Clock.Sec);
  216. HSV2LED(hue, LightingBright);
  217. tube4_t buf;
  218. buf.s8.tA = Clock.Min >> 4;
  219. buf.s8.tB = Clock.Min & 0xf;
  220. buf.s8.tD = Clock.Sec >> 4;
  221. buf.s8.tE = Clock.Sec & 0xf;
  222. showDigits(buf);
  223. }
  224. void showWD(void) {
  225. dispWDT = DISP_WDT_TIME;
  226. in15Off();
  227. tube4_t buf;
  228. buf.s8.tA = TUBE_BLANK;
  229. buf.s8.tB = Clock.WD & 0xf;
  230. buf.s8.tD = TUBE_BLANK;
  231. buf.s8.tE = TUBE_BLANK;
  232. showDigits(buf);
  233. }
  234. void showDayMon(void) {
  235. dispWDT = DISP_WDT_TIME;
  236. in15Off();
  237. tube4_t buf;
  238. buf.s8.tA = Clock.Day >> 4;
  239. buf.s8.tB = Clock.Day & 0xf;
  240. buf.s8.tD = Clock.Mon >> 4;
  241. buf.s8.tE = Clock.Mon & 0xf;
  242. showDigits(buf);
  243. }
  244. void showYear(void) {
  245. dispWDT = DISP_WDT_TIME;
  246. in15Off();
  247. tube4_t buf;
  248. buf.s8.tA = 2;
  249. buf.s8.tB = 0;
  250. buf.s8.tD = Clock.Year >> 4;
  251. buf.s8.tE = Clock.Year & 0xf;
  252. showDigits(buf);
  253. }
  254. void showHumidity(void) {
  255. dispWDT = DISP_WDT_TIME/2;
  256. HSV2LED(COLOUR_BLUE, LightingBright);
  257. in15Percent();
  258. tube4_t buf;
  259. buf.s8.tA = Humidity / 10;
  260. buf.s8.tB = Humidity % 10;
  261. buf.s8.tD = TUBE_BLANK;
  262. buf.s8.tE = TUBE_BLANK;
  263. showDigits(buf);
  264. }
  265. void showTemperature(void) {
  266. dispWDT = DISP_WDT_TIME/2;
  267. HSV2LED(COLOUR_RED, LightingBright);
  268. in15Plus();
  269. tube4_t buf;
  270. buf.s8.tA = TUBE_BLANK;
  271. buf.s8.tB = TUBE_BLANK;
  272. buf.s8.tD = Temperature / 10;
  273. buf.s8.tE = Temperature % 10;
  274. showDigits(buf);
  275. }
  276. void showPressure(void) {
  277. dispWDT = DISP_WDT_TIME/2;
  278. HSV2LED(COLOUR_GREEN, LightingBright);
  279. in15P();
  280. tube4_t buf;
  281. int tmp;
  282. buf.s8.tA = TUBE_BLANK;
  283. buf.s8.tB = Pressure / 100;
  284. tmp = Pressure % 100;
  285. buf.s8.tD = tmp / 10;
  286. buf.s8.tE = tmp % 10;
  287. showDigits(buf);
  288. }
  289. /* Simple function for cyclic show all sensor data */
  290. void showSensorData(void) {
  291. in15Off();
  292. showTemperature();
  293. tdelay_ms(3000);
  294. showHumidity();
  295. tdelay_ms(3000);
  296. showPressure();
  297. tdelay_ms(2700);
  298. ES_SetState(stShowTime);
  299. }
  300. void setTimeShow(void) {
  301. dispWDT = DISP_WDT_TIME;
  302. tube4_t buf;
  303. buf.s8.tA = setClock.Hr >> 4;
  304. buf.s8.tB = setClock.Hr & 0xf;
  305. buf.s8.tD = setClock.Min >> 4;
  306. buf.s8.tE = setClock.Min & 0xf;
  307. showDigits(buf);
  308. }
  309. void setTimeBegin(void) {
  310. in15Minus();
  311. HSV2LED(COLOUR_NIXIE, LightingBright);
  312. RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
  313. RTC_ReadAll(&setClock);
  314. }
  315. void setHHBegin(void) {
  316. Flag.Blink_1 = 1;
  317. Flag.Blink_2 = 1;
  318. Flag.Blink_4 = 0;
  319. Flag.Blink_5 = 0;
  320. Blink_Start();
  321. setTimeShow();
  322. }
  323. void setHHInc(void) {
  324. valIncrease(&setClock.Hr, 23);
  325. }
  326. void setHHDec(void) {
  327. valDecrease(&setClock.Hr, 23);
  328. }
  329. void setMMBegin(void) {
  330. Flag.Blink_1 = 0;
  331. Flag.Blink_2 = 0;
  332. Flag.Blink_4 = 1;
  333. Flag.Blink_5 = 1;
  334. Blink_Start();
  335. setTimeShow();
  336. }
  337. void setMMInc(void) {
  338. valIncrease(&setClock.Min, 59);
  339. }
  340. void setMMDec(void) {
  341. valDecrease(&setClock.Min, 59);
  342. }
  343. void setTimeEnd(void) {
  344. dispWDT = 0;
  345. RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
  346. setClock.Sec = 0;
  347. RTC_WriteTime(&setClock);
  348. Blink_Stop();
  349. RTC_ReadAll(&Clock);
  350. }
  351. void setDateBegin(void) {
  352. in15Off();
  353. HSV2LED(COLOUR_NIXIE, LightingBright);
  354. RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
  355. RTC_ReadAll(&setClock);
  356. }
  357. void setDateEnd(void) {
  358. dispWDT = 0;
  359. RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
  360. RTC_WriteCalendar(&setClock);
  361. Blink_Stop();
  362. RTC_ReadAll(&Clock);
  363. }
  364. void setWDBegin(void) {
  365. Flag.Blink_1 = 0;
  366. Flag.Blink_2 = 1;
  367. Flag.Blink_4 = 0;
  368. Flag.Blink_5 = 0;
  369. Blink_Start();
  370. setWDShow();
  371. }
  372. void setWDShow(void) {
  373. dispWDT = DISP_WDT_TIME;
  374. tube4_t buf;
  375. buf.s8.tA = TUBE_BLANK;
  376. buf.s8.tB = setClock.WD & 0xf;
  377. buf.s8.tD = TUBE_BLANK;
  378. buf.s8.tE = TUBE_BLANK;
  379. showDigits(buf);
  380. }
  381. void setDMShow(void) {
  382. dispWDT = DISP_WDT_TIME;
  383. tube4_t buf;
  384. buf.s8.tA = setClock.Day >> 4;
  385. buf.s8.tB = setClock.Day & 0xf;
  386. buf.s8.tD = setClock.Mon >> 4;
  387. buf.s8.tE = setClock.Mon & 0xf;
  388. showDigits(buf);
  389. }
  390. void setYearShow(void) {
  391. dispWDT = DISP_WDT_TIME;
  392. tube4_t buf;
  393. buf.s8.tA = 2;
  394. buf.s8.tB = 0;
  395. buf.s8.tD = setClock.Year >> 4;
  396. buf.s8.tE = setClock.Year & 0xf;
  397. showDigits(buf);
  398. }
  399. void setMDBegin(void) {
  400. Flag.Blink_1 = 1;
  401. Flag.Blink_2 = 1;
  402. Flag.Blink_4 = 0;
  403. Flag.Blink_5 = 0;
  404. Blink_Start();
  405. setDMShow();
  406. }
  407. void setMonthBegin(void) {
  408. Flag.Blink_1 = 0;
  409. Flag.Blink_2 = 0;
  410. Flag.Blink_4 = 1;
  411. Flag.Blink_5 = 1;
  412. Blink_Start();
  413. setDMShow();
  414. }
  415. void setYearBegin(void) {
  416. Flag.Blink_1 = 0;
  417. Flag.Blink_2 = 0;
  418. Flag.Blink_4 = 1;
  419. Flag.Blink_5 = 1;
  420. Blink_Start();
  421. setYearShow();
  422. }
  423. void setIncWDay(void) {
  424. valIncrease(&setClock.WD, 7);
  425. }
  426. void setIncMDay(void) {
  427. valIncrease(&setClock.Day, 31);
  428. }
  429. void setIncMonth(void) {
  430. valIncrease(&setClock.Mon, 12);
  431. }
  432. void setIncYear(void) {
  433. valIncrease(&setClock.Year, 99);
  434. }
  435. void setDecWDay(void) {
  436. valDecrease(&setClock.WD, 7);
  437. }
  438. void setDecMDay(void) {
  439. valDecrease(&setClock.Day, 31);
  440. }
  441. void setDecMonth(void) {
  442. valDecrease(&setClock.Mon, 12);
  443. }
  444. void setDecYear(void) {
  445. valDecrease(&setClock.Year, 99);
  446. }
  447. void showDNhour(void) {
  448. dispWDT = DISP_WDT_TIME;
  449. tube4_t buf;
  450. es_state_t e_st = ES_GetState();
  451. if (e_st == stShowDNhours){
  452. buf.s8.tA = Lighting.name.DayHour >> 4;
  453. buf.s8.tB = Lighting.name.DayHour & 0xf;
  454. buf.s8.tD = Lighting.name.NightHour >> 4;
  455. buf.s8.tE = Lighting.name.NightHour & 0xf;
  456. } else {
  457. buf.s8.tA = setLighting.name.DayHour >> 4;
  458. buf.s8.tB = setLighting.name.DayHour & 0xf;
  459. buf.s8.tD = setLighting.name.NightHour >> 4;
  460. buf.s8.tE = setLighting.name.NightHour & 0xf;
  461. }
  462. showDigits(buf);
  463. }
  464. void showDNbright(void) {
  465. dispWDT = DISP_WDT_TIME;
  466. tube4_t buf;
  467. es_state_t e_st = ES_GetState();
  468. if (e_st == stShowDNbright){
  469. buf.s8.tA = Lighting.name.DayBright / 10;
  470. buf.s8.tB = Lighting.name.DayBright % 10;
  471. buf.s8.tD = Lighting.name.NightBright / 10;
  472. buf.s8.tE = Lighting.name.NightBright % 10;
  473. } else {
  474. buf.s8.tA = setLighting.name.DayBright / 10;
  475. buf.s8.tB = setLighting.name.DayBright % 10;
  476. buf.s8.tD = setLighting.name.NightBright / 10;
  477. buf.s8.tE = setLighting.name.NightBright % 10;
  478. }
  479. showDigits(buf);
  480. }
  481. void showDNmode(void) {
  482. dispWDT = DISP_WDT_TIME;
  483. uint8_t dm, nm;
  484. tube4_t buf;
  485. es_state_t e_st = ES_GetState();
  486. if (e_st == stShowDNmode){
  487. dm = Lighting.name.DayMode;
  488. nm = Lighting.name.NightMode;
  489. } else {
  490. dm = setLighting.name.DayMode;
  491. nm = setLighting.name.NightMode;
  492. }
  493. buf.s8.tA = TUBE_BLANK;
  494. buf.s8.tB = dm & 0xf;
  495. buf.s8.tD = TUBE_BLANK;
  496. buf.s8.tE = nm & 0xf;
  497. showDigits(buf);
  498. }
  499. void showDNcolour(void) {
  500. dispWDT = DISP_WDT_TIME;
  501. tube4_t buf;
  502. es_state_t e_st = ES_GetState();
  503. if (e_st == stShowDNcolour){
  504. buf.s8.tA = Lighting.name.DayColour / 10;
  505. buf.s8.tB = Lighting.name.DayColour % 10;
  506. buf.s8.tD = Lighting.name.NightColour / 10;
  507. buf.s8.tE = Lighting.name.NightColour % 10;
  508. } else {
  509. buf.s8.tA = setLighting.name.DayColour / 10;
  510. buf.s8.tB = setLighting.name.DayColour % 10;
  511. buf.s8.tD = setLighting.name.NightColour / 10;
  512. buf.s8.tE = setLighting.name.NightColour % 10;
  513. }
  514. showDigits(buf);
  515. }
  516. void setDNbegin(void) {
  517. in15Off();
  518. RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
  519. setLighting.u64 = Lighting.u64;
  520. //Flash_Read(&setLighting.u64);
  521. }
  522. void setDNend(void) {
  523. dispWDT = 0;
  524. RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
  525. Blink_Stop();
  526. LEDS_OFF;
  527. Flash_Write(&setLighting.u64);
  528. Lighting.u64 = setLighting.u64;
  529. check_DayNight();
  530. }
  531. void setDayHourBegin(void) {
  532. LEDS_OFF;
  533. Flag.Blink_1 = 1;
  534. Flag.Blink_2 = 1;
  535. Flag.Blink_4 = 0;
  536. Flag.Blink_5 = 0;
  537. Blink_Start();
  538. showDNhour();
  539. }
  540. void setNightHourBegin(void) {
  541. Flag.Blink_1 = 0;
  542. Flag.Blink_2 = 0;
  543. Flag.Blink_4 = 1;
  544. Flag.Blink_5 = 1;
  545. Blink_Start();
  546. showDNhour();
  547. }
  548. void setDayBrightBegin(void) {
  549. Flag.Blink_1 = 1;
  550. Flag.Blink_2 = 1;
  551. Flag.Blink_4 = 0;
  552. Flag.Blink_5 = 0;
  553. Blink_Start();
  554. showDNbright();
  555. }
  556. void setNightBrightBegin(void) {
  557. Flag.Blink_1 = 0;
  558. Flag.Blink_2 = 0;
  559. Flag.Blink_4 = 1;
  560. Flag.Blink_5 = 1;
  561. Blink_Start();
  562. showDNbright();
  563. }
  564. void setDayModeBegin(void) {
  565. Flag.Blink_1 = 0;
  566. Flag.Blink_2 = 1;
  567. Flag.Blink_4 = 0;
  568. Flag.Blink_5 = 0;
  569. Blink_Start();
  570. showDNmode();
  571. }
  572. void setNightModeBegin(void) {
  573. Flag.Blink_1 = 0;
  574. Flag.Blink_2 = 0;
  575. Flag.Blink_4 = 0;
  576. Flag.Blink_5 = 1;
  577. Blink_Start();
  578. showDNmode();
  579. }
  580. void setDayColourBegin(void) {
  581. Flag.Blink_1 = 1;
  582. Flag.Blink_2 = 1;
  583. Flag.Blink_4 = 0;
  584. Flag.Blink_5 = 0;
  585. Blink_Start();
  586. HSV2LED(setLighting.name.DayColour, LightingBright);
  587. showDNcolour();
  588. }
  589. void setNightColourBegin(void) {
  590. Flag.Blink_1 = 0;
  591. Flag.Blink_2 = 0;
  592. Flag.Blink_4 = 1;
  593. Flag.Blink_5 = 1;
  594. Blink_Start();
  595. HSV2LED(setLighting.name.NightColour, LightingBright);
  596. showDNcolour();
  597. }
  598. void setIncDayHour(void) {
  599. valIncrease(&setLighting.name.DayHour, 23);
  600. }
  601. void setIncDayBright(void) {
  602. dvalIncrease(&setLighting.name.DayBright, MAX_BRIGHT_LVL);
  603. //tube_BrightLevel(Tube_All, setLighting.name.DayBright);
  604. TUBES_BRIGHT(setLighting.name.DayBright);
  605. }
  606. void setIncDayMode(void) {
  607. dvalIncrease(&setLighting.name.DayMode, MAX_LIGHT_MODE);
  608. if (setLighting.name.DayMode == light_Rainbow) {
  609. RTOS_SetTask(showRainbow, 0, 20);
  610. } else {
  611. RTOS_DeleteTask(showRainbow);
  612. }
  613. }
  614. void setIncDayColour(void) {
  615. dvalIncrease(&setLighting.name.DayColour, MAX_COLOR_VAL);
  616. HSV2LED(setLighting.name.DayColour, LightingBright);
  617. }
  618. void setDecDayHour(void) {
  619. valDecrease(&setLighting.name.DayHour, 23);
  620. }
  621. void setDecDayBright(void) {
  622. dvalDecrease(&setLighting.name.DayBright, MAX_BRIGHT_LVL);
  623. //tube_BrightLevel(Tube_All, setLighting.name.DayBright);
  624. TUBES_BRIGHT(setLighting.name.DayBright);
  625. }
  626. void setDecDayMode(void) {
  627. dvalDecrease(&setLighting.name.DayMode, MAX_LIGHT_MODE);
  628. if (setLighting.name.DayMode == light_Rainbow) {
  629. RTOS_SetTask(showRainbow, 0, 20);
  630. } else {
  631. RTOS_DeleteTask(showRainbow);
  632. }
  633. }
  634. void setDecDayColour(void) {
  635. dvalDecrease(&setLighting.name.DayColour, MAX_COLOR_VAL);
  636. HSV2LED(setLighting.name.DayColour, LightingBright);
  637. }
  638. void setIncNightHour(void) {
  639. valIncrease(&setLighting.name.NightHour, 23);
  640. }
  641. void setIncNightBright(void) {
  642. dvalIncrease(&setLighting.name.NightBright, MAX_BRIGHT_LVL);
  643. //tube_BrightLevel(Tube_All, setLighting.name.NightBright);
  644. TUBES_BRIGHT(setLighting.name.NightBright);
  645. }
  646. void setIncNightMode(void) {
  647. dvalIncrease(&setLighting.name.NightMode, MAX_LIGHT_MODE);
  648. if (setLighting.name.NightMode == light_Rainbow) {
  649. RTOS_SetTask(showRainbow, 0, 20);
  650. } else {
  651. RTOS_DeleteTask(showRainbow);
  652. }
  653. }
  654. void setIncNightColour(void) {
  655. dvalIncrease(&setLighting.name.NightColour, MAX_COLOR_VAL);
  656. HSV2LED(setLighting.name.NightColour, LightingBright);
  657. }
  658. void setDecNightHour(void) {
  659. valDecrease(&setLighting.name.NightHour, 23);
  660. }
  661. void setDecNightBright(void) {
  662. dvalDecrease(&setLighting.name.NightBright, MAX_BRIGHT_LVL);
  663. //tube_BrightLevel(Tube_All, setLighting.name.NightBright);
  664. TUBES_BRIGHT(setLighting.name.NightBright);
  665. }
  666. void setDecNightMode(void) {
  667. dvalDecrease(&setLighting.name.NightMode, MAX_LIGHT_MODE);
  668. if (setLighting.name.NightMode == light_Rainbow) {
  669. RTOS_SetTask(showRainbow, 0, 20);
  670. } else {
  671. RTOS_DeleteTask(showRainbow);
  672. }
  673. }
  674. void setDecNightColour(void) {
  675. dvalDecrease(&setLighting.name.NightColour, MAX_COLOR_VAL);
  676. HSV2LED(setLighting.name.NightColour, LightingBright);
  677. }
  678. void setDNbreak(void) {
  679. // restore led and tube bright level
  680. check_DayNight();
  681. }
  682. /**
  683. * @brief Increase BCD value.
  684. * @param : val, max
  685. * @retval : None
  686. */
  687. static void valIncrease(uint8_t * val, uint8_t max) {
  688. uint8_t bin = 10 * (*val >> 4) + (*val & 0x0f);
  689. if (bin < max) {
  690. bin ++;
  691. } else {
  692. bin = 0;
  693. }
  694. *val = ((bin / 10 ) << 4) | (bin % 10);
  695. }
  696. /**
  697. * @brief Decrease BCD value.
  698. * @param : value, max
  699. * @retval : None
  700. */
  701. static void valDecrease(uint8_t * val, uint8_t max) {
  702. uint8_t bin = 10 * (*val >> 4) + (*val & 0x0f);
  703. if (bin > 0) {
  704. bin --;
  705. } else {
  706. bin = max;
  707. }
  708. *val = ((bin / 10 ) << 4) | (bin % 10);
  709. }
  710. /**
  711. * @brief Increase/Decrease decimal value.
  712. * @param val
  713. * @param max
  714. * @retval : None, change value
  715. */
  716. static void dvalIncrease(uint8_t * val, uint8_t max) {
  717. if (*val < max) {
  718. *val += 1;
  719. } else {
  720. *val = 0;
  721. }
  722. }
  723. static void dvalDecrease(uint8_t * val, uint8_t max) {
  724. if (*val > 0) {
  725. *val -= 1;
  726. } else {
  727. *val = max;
  728. }
  729. }
  730. static void showRainbow(void) {
  731. static uint8_t hue = 0;
  732. HSV2LED(hue, LightingBright);
  733. if (hue < 59) {
  734. hue ++;
  735. } else {
  736. hue = 0;
  737. RTOS_DeleteTask(showRainbow);
  738. }
  739. }