clock.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. #include "clock.h"
  2. /* type defs */
  3. typedef struct {
  4. uint8_t time;
  5. es_event_t pressed;
  6. es_event_t holded;
  7. uint32_t pin;
  8. //(GPIO_TypeDef *) GPIOA; // ?->IDR
  9. } btn_t;
  10. /* variables */
  11. rtc_t Clock;
  12. static rtc_t setClock;
  13. static btn_t Button[BTN_NUM] = {
  14. {0, evBTN1Pressed, evBTN1Holded, BTN1_PIN},
  15. {0, evBTN2Pressed, evBTN2Pressed, BTN2_PIN},
  16. {0, evBTN3Pressed, evBTN3Pressed, BTN3_PIN},
  17. {0, evBTN4Pressed, evBTN4Holded, BTN4_PIN}
  18. };
  19. static volatile uint8_t dispWDT = 0;
  20. /* function prototypes */
  21. //static void Color_RGB(const uint8_t r, const uint8_t g, const uint8_t b);
  22. static void MinusFadeIn(void);
  23. static void MinusFadeOut(void);
  24. static void PlusFadeIn(void);
  25. static void PercentFadeIn(void);
  26. static void PressureFadeIn(void);
  27. //static void IN15_FadeIn(in15_pin_t pin);
  28. //static void IN15_FadeOut(in15_pin_t pin);
  29. static void valIncrease(uint8_t * val, uint8_t max);
  30. static void valDecrease(uint8_t * val, uint8_t max);
  31. /* funcions */
  32. /**
  33. * @brief Вывод HEX значений цвета в таймер.
  34. * @param : RGB value in range 0x00-0xFF
  35. * @retval : None
  36. */
  37. static void Color_RGB(uint8_t r, uint8_t g, uint8_t b) {
  38. COLOR_R(r);
  39. COLOR_G(g);
  40. COLOR_B(b);
  41. }
  42. /**
  43. * @brief Обработка кнопок.
  44. * @param : None
  45. * @retval : None
  46. */
  47. void btnProcess(void) {
  48. /* get pin state */
  49. uint32_t pins = BTNS_STATE;
  50. int i;
  51. for (i=0; i<BTN_NUM; i++) {
  52. if ((pins & Button[i].pin) == 0) {
  53. /* button pressed */
  54. Button[i].time ++;
  55. if (Button[i].time >= (BTN_TIME_HOLDED/BTN_SCAN_PERIOD)) {
  56. Button[i].time -= (BTN_TIME_REPEATED/BTN_SCAN_PERIOD);
  57. if (Button[i].holded == Button[i].pressed) {
  58. /* if pressed and holded - same function, then button pressed auto repeat */
  59. ES_PlaceEvent(Button[i].pressed);
  60. }
  61. }
  62. } else if (Button[i].time != 0) {
  63. /* button released */
  64. if (Button[i].time >= ((BTN_TIME_HOLDED - BTN_TIME_REPEATED)/BTN_SCAN_PERIOD)) {
  65. /* process long press */
  66. ES_PlaceEvent(Button[i].holded);
  67. } else if (Button[i].time >= (BTN_TIME_PRESSED/BTN_SCAN_PERIOD)) {
  68. /* process short press */
  69. ES_PlaceEvent(Button[i].pressed);
  70. }
  71. Button[i].time = 0;
  72. RTOS_SetTask(btnProcess, BTN_SCAN_PAUSE, BTN_SCAN_PERIOD);
  73. }
  74. } /* end FOR */
  75. }
  76. void new_Second(void) {
  77. // Blink_Start(); // !!! TODO
  78. RTC_ReadAll(&Clock);
  79. if (dispWDT != 0) {
  80. dispWDT--;
  81. if (dispWDT == 0) {
  82. ES_PlaceEvent(evDisplayWDT);
  83. }
  84. }
  85. }
  86. /**
  87. * On/off symbols on IN-15 tube.
  88. */
  89. void in15Off(void) {
  90. IN15_OFF;
  91. TUBE_C_OFF;
  92. }
  93. void in15Minus(void) {
  94. IN15_OFF;
  95. IN15_Minus;
  96. TUBE_C_ON;
  97. }
  98. void in15Plus(void) {
  99. IN15_OFF;
  100. IN15_Plus;
  101. TUBE_C_ON;
  102. }
  103. void in15Percent(void) {
  104. IN15_OFF;
  105. IN15_Percent;
  106. TUBE_C_ON;
  107. }
  108. void in15P(void) {
  109. IN15_OFF;
  110. IN15_P;
  111. TUBE_C_ON;
  112. }
  113. /** 'Faded' funcions */
  114. static void MinusFadeIn(void) {
  115. static uint8_t on = FADE_START;
  116. static uint8_t off = FADE_STOP;
  117. static uint8_t st = 0;
  118. if (st == 0) {
  119. st = 1;
  120. IN15_Minus;
  121. on += FADE_STEP;
  122. if (on < FADE_STOP) {
  123. RTOS_SetTask(MinusFadeIn, on, 0);
  124. } else {
  125. on = FADE_START; off = FADE_STOP; st = 0;
  126. }
  127. } else {
  128. st = 0;
  129. IN15_OFF;
  130. off -= FADE_STEP;
  131. RTOS_SetTask(MinusFadeIn, off, 0);
  132. }
  133. }
  134. static void MinusFadeOut(void) {
  135. static uint8_t off = FADE_START;
  136. static uint8_t on = FADE_STOP;
  137. static uint8_t st = 0;
  138. if (st == 0) {
  139. st = 1;
  140. IN15_OFF;
  141. off += FADE_STEP;
  142. if (off < FADE_STOP) {
  143. RTOS_SetTask(MinusFadeOut, off, 0);
  144. } else {
  145. off = FADE_START; on = FADE_STOP; st = 0;
  146. }
  147. } else {
  148. st = 0;
  149. IN15_Minus;
  150. on -= FADE_STEP;
  151. RTOS_SetTask(MinusFadeOut, on, 0);
  152. }
  153. }
  154. static void PlusFadeIn(void) {
  155. static uint8_t on = FADE_START;
  156. static uint8_t off = FADE_STOP;
  157. static uint8_t st = 0;
  158. if (st == 0) {
  159. st = 1;
  160. IN15_Plus;
  161. on += FADE_STEP;
  162. if (on < FADE_STOP) {
  163. RTOS_SetTask(PlusFadeIn, on, 0);
  164. } else {
  165. on = FADE_START; off = FADE_STOP; st = 0;
  166. }
  167. } else {
  168. st = 0;
  169. IN15_OFF;
  170. off -= FADE_STEP;
  171. RTOS_SetTask(PlusFadeIn, off, 0);
  172. }
  173. }
  174. static void PercentFadeIn(void) {
  175. static uint8_t on = FADE_START;
  176. static uint8_t off = FADE_STOP;
  177. static uint8_t st = 0;
  178. if (st == 0) {
  179. st = 1;
  180. IN15_Percent;
  181. on += FADE_STEP;
  182. if (on < FADE_STOP) {
  183. RTOS_SetTask(PercentFadeIn, on, 0);
  184. } else {
  185. on = FADE_START; off = FADE_STOP; st = 0;
  186. }
  187. } else {
  188. st = 0;
  189. IN15_OFF;
  190. off -= FADE_STEP;
  191. RTOS_SetTask(PercentFadeIn, off, 0);
  192. }
  193. }
  194. static void PressureFadeIn(void) {
  195. static uint8_t on = FADE_START;
  196. static uint8_t off = FADE_STOP;
  197. static uint8_t st = 0;
  198. if (st == 0) {
  199. st = 1;
  200. IN15_P;
  201. on += FADE_STEP;
  202. if (on < FADE_STOP) {
  203. RTOS_SetTask(PressureFadeIn, on, 0);
  204. } else {
  205. on = FADE_START; off = FADE_STOP; st = 0;
  206. }
  207. } else {
  208. st = 0;
  209. IN15_OFF;
  210. off -= FADE_STEP;
  211. RTOS_SetTask(PressureFadeIn, off, 0);
  212. }
  213. }
  214. #ifdef NEW_SHED
  215. static void IN15_FadeIn(in15_pin_t pin) {
  216. static uint8_t on = FADE_START;
  217. static uint8_t off = FADE_STOP;
  218. static uint8_t st = 0;
  219. if (st == 0) {
  220. st = 1;
  221. GPIOA->BSRR = pin;
  222. on += FADE_STEP;
  223. if (on < FADE_STOP) {
  224. RTOS_SetTask(IN15_FadeIn, on, 0);
  225. } else {
  226. on = FADE_START; off = FADE_STOP; st = 0;
  227. }
  228. } else {
  229. st = 0;
  230. IN15_OFF;
  231. off -= FADE_STEP;
  232. RTOS_SetTask(IN15_FadeIn, off, 0);
  233. }
  234. }
  235. static void IN15_FadeOut(in15_pin_t pin) {
  236. static uint8_t off = FADE_START;
  237. static uint8_t on = FADE_STOP;
  238. static uint8_t st = 0;
  239. if (st == 0) {
  240. st = 1;
  241. IN15_OFF;
  242. off += FADE_STEP;
  243. if (off < FADE_STOP) {
  244. RTOS_SetTask(IN15_FadeOut, off, 0);
  245. } else {
  246. off = FADE_START; on = FADE_STOP; st = 0;
  247. }
  248. } else {
  249. st = 0;
  250. GPIOA->BSRR = pin;
  251. on -= FADE_STEP;
  252. RTOS_SetTask(IN15_FadeOut, on, 0);
  253. }
  254. }
  255. #endif /* NewShed */
  256. /**
  257. * Show info on tubes.
  258. */
  259. void showTime(void) {
  260. MinusFadeIn();
  261. RTOS_SetTask(MinusFadeOut, 500, 0);
  262. uint8_t hue = bcd2bin(Clock.Sec);
  263. HSV2LED(hue, 255, cie[Lvl_Mdl]);
  264. tube4_t buf;
  265. buf.s8.tA = Clock.Hr >> 4;
  266. buf.s8.tB = Clock.Hr & 0xf;
  267. buf.s8.tD = Clock.Min >> 4;
  268. buf.s8.tE = Clock.Min & 0xf;
  269. showDigits(buf);
  270. }
  271. void showMMSS(void) {
  272. RTOS_DeleteTask(MinusFadeOut);
  273. IN15_Minus;
  274. uint8_t hue = bcd2bin(Clock.Sec);
  275. HSV2LED(hue, 255, cie[Lvl_Mdl]);
  276. tube4_t buf;
  277. buf.s8.tA = Clock.Min >> 4;
  278. buf.s8.tB = Clock.Min & 0xf;
  279. buf.s8.tD = Clock.Sec >> 4;
  280. buf.s8.tE = Clock.Sec & 0xf;
  281. showDigits(buf);
  282. }
  283. void showWD(void) {
  284. dispWDT = DISP_WDT_TIME;
  285. IN15_OFF;
  286. tube4_t buf;
  287. buf.s8.tA = 0xf;
  288. buf.s8.tB = Clock.WD & 0xf;
  289. buf.s8.tD = 0xf;
  290. buf.s8.tE = 0xf;
  291. showDigits(buf);
  292. }
  293. void showDay(void) {
  294. dispWDT = DISP_WDT_TIME;
  295. IN15_OFF;
  296. tube4_t buf;
  297. buf.s8.tA = Clock.Day >> 4;
  298. buf.s8.tB = Clock.Day & 0xf;
  299. buf.s8.tD = 0xf;
  300. buf.s8.tE = 0xf;
  301. showDigits(buf);
  302. }
  303. void showMonth(void) {
  304. dispWDT = DISP_WDT_TIME;
  305. IN15_OFF;
  306. tube4_t buf;
  307. buf.s8.tA = 0xf;
  308. buf.s8.tB = 0xf;
  309. buf.s8.tD = Clock.Mon >> 4;
  310. buf.s8.tE = Clock.Mon & 0xf;
  311. showDigits(buf);
  312. }
  313. void showDayMon(void) {
  314. dispWDT = DISP_WDT_TIME;
  315. IN15_OFF;
  316. tube4_t buf;
  317. buf.s8.tA = Clock.Day >> 4;
  318. buf.s8.tB = Clock.Day & 0xf;
  319. buf.s8.tD = Clock.Mon >> 4;
  320. buf.s8.tE = Clock.Mon & 0xf;
  321. showDigits(buf);
  322. }
  323. void showYear(void) {
  324. dispWDT = DISP_WDT_TIME;
  325. IN15_OFF;
  326. tube4_t buf;
  327. buf.s8.tA = 2;
  328. buf.s8.tB = 0;
  329. buf.s8.tD = Clock.Year >> 4;
  330. buf.s8.tE = Clock.Year & 0xf;
  331. showDigits(buf);
  332. }
  333. void showHumidity(void) {
  334. dispWDT = DISP_WDT_TIME;
  335. //in15Percent();
  336. //IN15_FadeIn(sym_Percent);
  337. PercentFadeIn();
  338. tube4_t buf;
  339. buf.s8.tA = Humidity / 10;
  340. buf.s8.tB = Humidity % 10;
  341. buf.s8.tD = 0xf;
  342. buf.s8.tE = 0xf;
  343. showDigits(buf);
  344. }
  345. void showTemperature(void) {
  346. dispWDT = DISP_WDT_TIME;
  347. //in15Plus();
  348. //IN15_FadeIn(sym_Plus);
  349. PlusFadeIn();
  350. tube4_t buf;
  351. buf.s8.tA = 0xf;
  352. buf.s8.tB = 0xf;
  353. buf.s8.tD = Temperature / 10;
  354. buf.s8.tE = Temperature % 10;
  355. showDigits(buf);
  356. }
  357. void showPressure(void) {
  358. dispWDT = DISP_WDT_TIME;
  359. //in15P();
  360. //IN15_FadeIn(sym_Pressure);
  361. PressureFadeIn();
  362. tube4_t buf;
  363. int tmp;
  364. buf.s8.tA = 0xf;
  365. buf.s8.tB = Pressure / 100;
  366. tmp = Pressure % 100;
  367. buf.s8.tD = tmp / 10;
  368. buf.s8.tE = tmp % 10;
  369. showDigits(buf);
  370. }
  371. /* Simple function for cyclic show all sensor data */
  372. void showSensorData(void) {
  373. RTOS_DeleteTask(MinusFadeOut);
  374. //HSV2LED(1, 255, cie[Lvl_Mdl]); // Nixie color
  375. HSV2LED(0, 255, cie[Lvl_Mdl]); // RED
  376. showTemperature();
  377. tdelay_ms(3000);
  378. HSV2LED(40, 255, cie[Lvl_Mdl]); // BLUE
  379. showHumidity();
  380. tdelay_ms(3000);
  381. HSV2LED(20, 255, cie[Lvl_Mdl]); // GREEN
  382. showPressure();
  383. tdelay_ms(2700);
  384. ES_SetState(stShowTime);
  385. // showTime();
  386. }
  387. void setTimeShow(void) {
  388. dispWDT = DISP_WDT_TIME;
  389. in15Minus();
  390. HSV2LED(1, 255, cie[Lvl_2]);
  391. tube4_t buf;
  392. buf.s8.tA = setClock.Hr >> 4;
  393. buf.s8.tB = setClock.Hr & 0xf;
  394. buf.s8.tD = setClock.Min >> 4;
  395. buf.s8.tE = setClock.Min & 0xf;
  396. showDigits(buf);
  397. }
  398. void setTimeBegin(void) {
  399. dispWDT = DISP_WDT_TIME;
  400. RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
  401. RTC_ReadAll(&setClock);
  402. }
  403. void setHHBegin(void) {
  404. dispWDT = DISP_WDT_TIME;
  405. Flag.Blink_1 = 1;
  406. Flag.Blink_2 = 1;
  407. Flag.Blink_4 = 0;
  408. Flag.Blink_5 = 0;
  409. Blink_Start();
  410. setTimeShow();
  411. }
  412. void setHHInc(void) {
  413. dispWDT = DISP_WDT_TIME;
  414. valIncrease(&setClock.Hr, 23);
  415. }
  416. void setHHDec(void) {
  417. dispWDT = DISP_WDT_TIME;
  418. valDecrease(&setClock.Hr, 23);
  419. }
  420. void setMMBegin(void) {
  421. dispWDT = DISP_WDT_TIME;
  422. Flag.Blink_1 = 0;
  423. Flag.Blink_2 = 0;
  424. Flag.Blink_4 = 1;
  425. Flag.Blink_5 = 1;
  426. Blink_Start();
  427. setTimeShow();
  428. }
  429. void setMMInc(void) {
  430. dispWDT = DISP_WDT_TIME;
  431. valIncrease(&setClock.Min, 59);
  432. }
  433. void setMMDec(void) {
  434. dispWDT = DISP_WDT_TIME;
  435. valDecrease(&setClock.Min, 59);
  436. }
  437. void setTimeEnd(void) {
  438. RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
  439. setClock.Sec = 0;
  440. RTC_WriteTime(&setClock);
  441. Flag.Blink_1 = 0;
  442. Flag.Blink_2 = 0;
  443. Flag.Blink_4 = 0;
  444. Flag.Blink_5 = 0;
  445. Blink_Stop();
  446. RTC_ReadAll(&Clock);
  447. }
  448. /**
  449. * @brief Increase BCD value.
  450. * @param : val, max
  451. * @retval : None
  452. */
  453. static void valIncrease(uint8_t * val, uint8_t max) {
  454. uint8_t bin = 10 * (*val >> 4) + (*val & 0x0f);
  455. if (bin < max) {
  456. bin ++;
  457. } else {
  458. bin = 0;
  459. }
  460. *val = ((bin / 10 ) << 4) | (bin % 10);
  461. }
  462. /**
  463. * @brief Decrease BCD value.
  464. * @param : value, max
  465. * @retval : None
  466. */
  467. static void valDecrease(uint8_t * val, uint8_t max) {
  468. uint8_t bin = 10 * (*val >> 4) + (*val & 0x0f);
  469. if (bin > 0) {
  470. bin --;
  471. } else {
  472. bin = max;
  473. }
  474. *val = ((bin / 10 ) << 4) | (bin % 10);
  475. }