clock.c 18 KB

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