123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773 |
- #include "clock.h"
- /* type defs */
- typedef struct t_btn {
- uint8_t time;
- es_event_t pressed;
- es_event_t holded;
- uint32_t pin;
- //(GPIO_TypeDef *) GPIOA; // ?->IDR
- } btn_t;
- /* variables */
- rtc_t Clock;
- static rtc_t setClock;
- static btn_t Button[BTN_NUM] = {
- {0, evBTN1Pressed, evBTN1Holded, BTN1_PIN},
- {0, evBTN2Pressed, evBTN2Pressed, BTN2_PIN},
- {0, evBTN3Pressed, evBTN3Pressed, BTN3_PIN},
- {0, evBTN4Pressed, evBTN4Holded, BTN4_PIN}
- };
- //convert linear bright level to logariphmic
- const uint8_t cie[8] = { 0, 5, 14, 33, 64, 109, 172, 255 };
- volatile static uint8_t dispWDT = 0;
- static in15_pin_t symToFade = 0;
- static brigh_level_t BrightLevel;
- /* function prototypes */
- static void check_DayNight(void);
- static void MinusFadeIn(void);
- static void MinusFadeOut(void);
- static void PlusFadeIn(void);
- static void PercentFadeIn(void);
- static void PressureFadeIn(void);
- static void IN15_FadeIn(void);
- static void IN15_FadeOut(void);
- static void valIncrease(uint8_t * val, uint8_t max);
- static void valDecrease(uint8_t * val, uint8_t max);
- static void HSV2LED(const int hue, const uint8_t sat, const uint8_t val);
- /* funcions */
- void Clock_Init(void) {
- RTC_ReadAll(&Clock);
- showTime();
- check_DayNight();
- }
- static void check_DayNight(void) {
- if ((Clock.Hr >= MORNING_HOUR) && (Clock.Hr < EVENING_HOUR)) {
- Flag.Now_Day = 1;
- BrightLevel = cie[DAY_BR_LVL];
- } else {
- Flag.Now_Day = 0;
- BrightLevel = cie[NIGHT_BR_LVL];
- }
- tube_BrightLevel(Tube_All, BrightLevel);
- //TUBES_BRIGHT(BrightLevel);
- }
- /**
- * @brief Обработка кнопок.
- * @param : None
- * @retval : None
- */
- void btnProcess(void) {
- /* get pin state */
- uint32_t pins = BTNS_STATE;
- int i;
- for (i=0; i<BTN_NUM; i++) {
- if ((pins & Button[i].pin) == 0) {
- /* button pressed */
- Button[i].time ++;
- if (Button[i].time >= (BTN_TIME_HOLDED/BTN_SCAN_PERIOD)) {
- Button[i].time -= (BTN_TIME_REPEATED/BTN_SCAN_PERIOD);
- if (Button[i].holded == Button[i].pressed) {
- /* if pressed and holded - same function, then button pressed auto repeat */
- ES_PlaceEvent(Button[i].pressed);
- }
- }
- } else if (Button[i].time != 0) {
- /* button released */
- if (Button[i].time >= ((BTN_TIME_HOLDED - BTN_TIME_REPEATED)/BTN_SCAN_PERIOD)) {
- /* process long press */
- ES_PlaceEvent(Button[i].holded);
- } else if (Button[i].time >= (BTN_TIME_PRESSED/BTN_SCAN_PERIOD)) {
- /* process short press */
- ES_PlaceEvent(Button[i].pressed);
- }
- Button[i].time = 0;
- RTOS_SetTask(btnProcess, BTN_SCAN_PAUSE, BTN_SCAN_PERIOD);
- }
- } /* end FOR */
- }
- void new_Second(void) {
- RTC_ReadAll(&Clock);
- // new hour
- if (Clock.Min == 0 && Clock.Sec == 0) {
- check_DayNight();
- }
- // check display watch dog timer
- if (dispWDT != 0) {
- dispWDT--;
- if (dispWDT == 0) {
- Blink_Stop();
- ES_PlaceEvent(evDisplayWDT);
- }
- }
- }
- /**
- * On/off symbols on IN-15 tube.
- */
- void in15Off(void) {
- IN15_OFF;
- TUBE_C_OFF;
- }
- void in15Minus(void) {
- IN15_OFF;
- IN15_Minus;
- TUBE_C_ON;
- }
- void in15Plus(void) {
- IN15_OFF;
- IN15_Plus;
- TUBE_C_ON;
- }
- void in15Percent(void) {
- IN15_OFF;
- IN15_Percent;
- TUBE_C_ON;
- }
- void in15P(void) {
- IN15_OFF;
- IN15_P;
- TUBE_C_ON;
- }
- /** 'Faded' funcions */
- static void MinusFadeIn(void) {
- static uint8_t on = FADE_START;
- static uint8_t off = FADE_STOP;
- static uint8_t st = 0;
- if (st == 0) {
- st = 1;
- IN15_Minus;
- on += FADE_STEP;
- if (on < FADE_STOP) {
- RTOS_SetTask(MinusFadeIn, on, 0);
- } else {
- on = FADE_START; off = FADE_STOP; st = 0;
- }
- } else {
- st = 0;
- IN15_OFF;
- off -= FADE_STEP;
- RTOS_SetTask(MinusFadeIn, off, 0);
- }
- }
- static void MinusFadeOut(void) {
- static uint8_t off = FADE_START;
- static uint8_t on = FADE_STOP;
- static uint8_t st = 0;
- if (st == 0) {
- st = 1;
- IN15_OFF;
- off += FADE_STEP;
- if (off < FADE_STOP) {
- RTOS_SetTask(MinusFadeOut, off, 0);
- } else {
- off = FADE_START; on = FADE_STOP; st = 0;
- }
- } else {
- st = 0;
- IN15_Minus;
- on -= FADE_STEP;
- RTOS_SetTask(MinusFadeOut, on, 0);
- }
- }
- static void PlusFadeIn(void) {
- static uint8_t on = FADE_START;
- static uint8_t off = FADE_STOP;
- static uint8_t st = 0;
- if (st == 0) {
- st = 1;
- IN15_Plus;
- on += FADE_STEP;
- if (on < FADE_STOP) {
- RTOS_SetTask(PlusFadeIn, on, 0);
- } else {
- on = FADE_START; off = FADE_STOP; st = 0;
- }
- } else {
- st = 0;
- IN15_OFF;
- off -= FADE_STEP;
- RTOS_SetTask(PlusFadeIn, off, 0);
- }
- }
- static void PercentFadeIn(void) {
- static uint8_t on = FADE_START;
- static uint8_t off = FADE_STOP;
- static uint8_t st = 0;
- if (st == 0) {
- st = 1;
- IN15_Percent;
- on += FADE_STEP;
- if (on < FADE_STOP) {
- RTOS_SetTask(PercentFadeIn, on, 0);
- } else {
- on = FADE_START; off = FADE_STOP; st = 0;
- }
- } else {
- st = 0;
- IN15_OFF;
- off -= FADE_STEP;
- RTOS_SetTask(PercentFadeIn, off, 0);
- }
- }
- static void PressureFadeIn(void) {
- static uint8_t on = FADE_START;
- static uint8_t off = FADE_STOP;
- static uint8_t st = 0;
- if (st == 0) {
- st = 1;
- IN15_P;
- on += FADE_STEP;
- if (on < FADE_STOP) {
- RTOS_SetTask(PressureFadeIn, on, 0);
- } else {
- on = FADE_START; off = FADE_STOP; st = 0;
- }
- } else {
- st = 0;
- IN15_OFF;
- off -= FADE_STEP;
- RTOS_SetTask(PressureFadeIn, off, 0);
- }
- }
- static void IN15_FadeIn(void) {
- static uint8_t on = FADE_START;
- static uint8_t off = FADE_STOP;
- static uint8_t st = 0;
- if (symToFade != 0) {
- if (st == 0) {
- st = 1;
- GPIOA->BSRR = symToFade;
- on += FADE_STEP;
- if (on < FADE_STOP) {
- RTOS_SetTask(IN15_FadeIn, on, 0);
- } else {
- on = FADE_START; off = FADE_STOP; st = 0; symToFade = 0;
- }
- } else {
- st = 0;
- IN15_OFF;
- off -= FADE_STEP;
- RTOS_SetTask(IN15_FadeIn, off, 0);
- }
- }
- }
- static void IN15_FadeOut(void) {
- static uint8_t off = FADE_START;
- static uint8_t on = FADE_STOP;
- static uint8_t st = 0;
- if (symToFade != 0) {
- if (st == 0) {
- st = 1;
- IN15_OFF;
- off += FADE_STEP;
- if (off < FADE_STOP) {
- RTOS_SetTask(IN15_FadeOut, off, 0);
- } else {
- off = FADE_START; on = FADE_STOP; st = 0; symToFade = 0;
- }
- } else {
- st = 0;
- GPIOA->BSRR = symToFade;
- on -= FADE_STEP;
- RTOS_SetTask(IN15_FadeOut, on, 0);
- }
- }
- }
- /**
- * @brief HSV to RGB convertion
- * @param hue: 0-59, sat: 0-255, val (lightness): 0-255
- * @return none. RGB value output direct to LED.
- */
- static void HSV2LED(const int hue, const uint8_t sat, const uint8_t val) {
- uint8_t r, g, b;
- int h = hue;
- int s = sat;
- int v = val;
- int i = h / 60;
- int p = (256 * v - s * v) / 256;
- if (i & 1)
- {
- int q = (256 * 60 * v - h * s * v + 60 * s * v * i) / (256 * 60);
- switch (i)
- {
- case 1:
- r = q;
- g = v;
- b = p;
- break;
- case 3:
- r = p;
- g = q;
- b = v;
- break;
- case 5:
- r = v;
- g = p;
- b = q;
- break;
- }
- }
- else
- {
- int t = (256 * 60 * v + h * s * v - 60 * s * v * (i + 1)) / (256 * 60);
- switch (i)
- {
- case 0:
- r = v;
- g = t;
- b = p;
- break;
- case 2:
- r = p;
- g = v;
- b = t;
- break;
- case 4:
- r = t;
- g = p;
- b = v;
- break;
- }
- }
- COLOR_R(r);
- COLOR_G(g);
- COLOR_B(b);
- }
- static void old_HSV2LED(const uint8_t hue, const uint8_t sat, const uint8_t val) {
- int base;
- uint32_t r=0, g=0, b=0;
- if (sat == 0)
- { // Achromatic color (gray).
- r = val;
- g = val;
- b = val;
- } else {
- base = ((255 - sat) * val) >> 8;
- switch (hue / 10) {
- case 0:
- r = val;
- g = (((val - base) * hue) / 10) + base;
- b = base;
- break;
- case 1:
- r = (((val - base) * (10 - (hue % 10))) / 10) + base;
- g = val;
- b = base;
- break;
- case 2:
- r = base;
- g = val;
- b = (((val - base) * (hue % 10)) / 10) + base;
- break;
- case 3:
- r = base;
- g = (((val - base) * (10 - (hue % 10))) / 10) + base;
- b = val;
- break;
- case 4:
- r = (((val - base) * (hue % 10)) / 10) + base;
- g = base;
- b = val;
- break;
- case 5:
- r = val;
- g = base;
- b = (((val - base) * (10 - (hue % 10))) / 10) + base;
- break;
- }
- }
- COLOR_R((uint8_t)r);
- COLOR_G((uint8_t)g);
- COLOR_B((uint8_t)b);
- }
- /**
- * Show info on tubes.
- */
- void showTime(void) {
- MinusFadeIn();
- RTOS_SetTask(MinusFadeOut, 500, 0);
- if (Flag.Now_Day != 0) {
- int hue = 6 * bcd2bin(Clock.Sec);
- HSV2LED(hue, 255, BrightLevel);
- } else {
- HSV2LED(COLOUR_NIXIE, 255, BrightLevel);
- }
- tube4_t buf;
- buf.s8.tA = Clock.Hr >> 4;
- buf.s8.tB = Clock.Hr & 0xf;
- buf.s8.tD = Clock.Min >> 4;
- buf.s8.tE = Clock.Min & 0xf;
- showDigits(buf);
- }
- void showMMSS(void) {
- RTOS_DeleteTask(MinusFadeOut);
- IN15_Minus;
- int hue = 6 * bcd2bin(Clock.Sec);
- HSV2LED(hue, 255, BrightLevel);
- tube4_t buf;
- buf.s8.tA = Clock.Min >> 4;
- buf.s8.tB = Clock.Min & 0xf;
- buf.s8.tD = Clock.Sec >> 4;
- buf.s8.tE = Clock.Sec & 0xf;
- showDigits(buf);
- }
- void showWD(void) {
- dispWDT = DISP_WDT_TIME;
- IN15_OFF;
- tube4_t buf;
- buf.s8.tA = 0xf;
- buf.s8.tB = Clock.WD & 0xf;
- buf.s8.tD = 0xf;
- buf.s8.tE = 0xf;
- showDigits(buf);
- }
- void showDayMon(void) {
- dispWDT = DISP_WDT_TIME;
- IN15_OFF;
- tube4_t buf;
- buf.s8.tA = Clock.Day >> 4;
- buf.s8.tB = Clock.Day & 0xf;
- buf.s8.tD = Clock.Mon >> 4;
- buf.s8.tE = Clock.Mon & 0xf;
- showDigits(buf);
- }
- void showYear(void) {
- dispWDT = DISP_WDT_TIME;
- IN15_OFF;
- tube4_t buf;
- buf.s8.tA = 2;
- buf.s8.tB = 0;
- buf.s8.tD = Clock.Year >> 4;
- buf.s8.tE = Clock.Year & 0xf;
- showDigits(buf);
- }
- void showHumidity(void) {
- dispWDT = DISP_WDT_TIME;
- HSV2LED(COLOUR_BLUE, 255, BrightLevel);
- //in15Percent();
- symToFade = sym_Percent;
- IN15_FadeIn();
- //PercentFadeIn();
- tube4_t buf;
- buf.s8.tA = Humidity / 10;
- buf.s8.tB = Humidity % 10;
- buf.s8.tD = 0xf;
- buf.s8.tE = 0xf;
- showDigits(buf);
- }
- void showTemperature(void) {
- dispWDT = DISP_WDT_TIME;
- HSV2LED(COLOUR_RED, 255, BrightLevel);
- //in15Plus();
- symToFade = sym_Plus;
- IN15_FadeIn();
- //PlusFadeIn();
- tube4_t buf;
- buf.s8.tA = 0xf;
- buf.s8.tB = 0xf;
- buf.s8.tD = Temperature / 10;
- buf.s8.tE = Temperature % 10;
- showDigits(buf);
- }
- void showPressure(void) {
- dispWDT = DISP_WDT_TIME;
- HSV2LED(COLOUR_GREEN, 255, cie[Lvl_Mdl]); // GREEN
- //in15P();
- symToFade = sym_Pressure;
- IN15_FadeIn();
- //PressureFadeIn();
- tube4_t buf;
- int tmp;
- buf.s8.tA = 0xf;
- buf.s8.tB = Pressure / 100;
- tmp = Pressure % 100;
- buf.s8.tD = tmp / 10;
- buf.s8.tE = tmp % 10;
- showDigits(buf);
- }
- /* Simple function for cyclic show all sensor data */
- void showSensorData(void) {
- RTOS_DeleteTask(MinusFadeOut);
- showTemperature();
- tdelay_ms(3000);
- showHumidity();
- tdelay_ms(3000);
- showPressure();
- tdelay_ms(2700);
- ES_SetState(stShowTime);
- // showTime();
- }
- void setTimeShow(void) {
- dispWDT = DISP_WDT_TIME;
- tube4_t buf;
- buf.s8.tA = setClock.Hr >> 4;
- buf.s8.tB = setClock.Hr & 0xf;
- buf.s8.tD = setClock.Min >> 4;
- buf.s8.tE = setClock.Min & 0xf;
- showDigits(buf);
- }
- void setTimeBegin(void) {
- RTOS_DeleteTask(MinusFadeOut);
- in15Minus();
- HSV2LED(COLOUR_NIXIE, 255, BrightLevel);
- RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
- RTC_ReadAll(&setClock);
- }
- void setHHBegin(void) {
- Flag.Blink_1 = 1;
- Flag.Blink_2 = 1;
- Flag.Blink_4 = 0;
- Flag.Blink_5 = 0;
- Blink_Start();
- setTimeShow();
- }
- void setHHInc(void) {
- valIncrease(&setClock.Hr, 23);
- }
- void setHHDec(void) {
- valDecrease(&setClock.Hr, 23);
- }
- void setMMBegin(void) {
- Flag.Blink_1 = 0;
- Flag.Blink_2 = 0;
- Flag.Blink_4 = 1;
- Flag.Blink_5 = 1;
- Blink_Start();
- setTimeShow();
- }
- void setMMInc(void) {
- valIncrease(&setClock.Min, 59);
- }
- void setMMDec(void) {
- valDecrease(&setClock.Min, 59);
- }
- void setTimeEnd(void) {
- dispWDT = 0;
- RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
- setClock.Sec = 0;
- RTC_WriteTime(&setClock);
- Blink_Stop();
- RTC_ReadAll(&Clock);
- }
- void setDateBegin(void) {
- IN15_OFF;
- HSV2LED(COLOUR_NIXIE, 255, BrightLevel);
- RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
- RTC_ReadAll(&setClock);
- }
- void setDateEnd(void) {
- dispWDT = 0;
- RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
-
- RTC_WriteCalendar(&setClock);
- Blink_Stop();
- RTC_ReadAll(&Clock);
- }
- void setWDBegin(void) {
- Flag.Blink_1 = 0;
- Flag.Blink_2 = 1;
- Flag.Blink_4 = 0;
- Flag.Blink_5 = 0;
- Blink_Start();
- setWDShow();
- }
- void setWDShow(void) {
- dispWDT = DISP_WDT_TIME;
- tube4_t buf;
- buf.s8.tA = 0xf;
- buf.s8.tB = setClock.WD & 0xf;
- buf.s8.tD = 0xf;
- buf.s8.tE = 0xf;
- showDigits(buf);
- }
- void setDMShow(void) {
- dispWDT = DISP_WDT_TIME;
- tube4_t buf;
- buf.s8.tA = setClock.Day >> 4;
- buf.s8.tB = setClock.Day & 0xf;
- buf.s8.tD = setClock.Mon >> 4;
- buf.s8.tE = setClock.Mon & 0xf;
- showDigits(buf);
- }
- void setYearShow(void) {
- dispWDT = DISP_WDT_TIME;
- tube4_t buf;
- buf.s8.tA = 2;
- buf.s8.tB = 0;
- buf.s8.tD = setClock.Year >> 4;
- buf.s8.tE = setClock.Year & 0xf;
- showDigits(buf);
- }
- void setMDBegin(void) {
- Flag.Blink_1 = 1;
- Flag.Blink_2 = 1;
- Flag.Blink_4 = 0;
- Flag.Blink_5 = 0;
- Blink_Start();
- setDMShow();
- }
- void setMonthBegin(void) {
- Flag.Blink_1 = 0;
- Flag.Blink_2 = 0;
- Flag.Blink_4 = 1;
- Flag.Blink_5 = 1;
- Blink_Start();
- setDMShow();
- }
- void setYearBegin(void) {
- Flag.Blink_1 = 0;
- Flag.Blink_2 = 0;
- Flag.Blink_4 = 1;
- Flag.Blink_5 = 1;
- Blink_Start();
- setYearShow();
- }
- void setIncWDay(void) {
- valIncrease(&setClock.WD, 7);
- }
- void setIncMDay(void) {
- valIncrease(&setClock.Day, 31);
- }
- void setIncMonth(void) {
- valIncrease(&setClock.Mon, 12);
- }
- void setIncYear(void) {
- valIncrease(&setClock.Year, 99);
- }
- void setDecWDay(void) {
- valDecrease(&setClock.WD, 7);
- }
- void setDecMDay(void) {
- valDecrease(&setClock.Day, 31);
- }
- void setDecMonth(void) {
- valDecrease(&setClock.Mon, 12);
- }
- void setDecYear(void) {
- valDecrease(&setClock.Year, 99);
- }
- /**
- * @brief Increase BCD value.
- * @param : val, max
- * @retval : None
- */
- static void valIncrease(uint8_t * val, uint8_t max) {
- uint8_t bin = 10 * (*val >> 4) + (*val & 0x0f);
- if (bin < max) {
- bin ++;
- } else {
- bin = 0;
- }
- *val = ((bin / 10 ) << 4) | (bin % 10);
- }
- /**
- * @brief Decrease BCD value.
- * @param : value, max
- * @retval : None
- */
- static void valDecrease(uint8_t * val, uint8_t max) {
- uint8_t bin = 10 * (*val >> 4) + (*val & 0x0f);
- if (bin > 0) {
- bin --;
- } else {
- bin = max;
- }
- *val = ((bin / 10 ) << 4) | (bin % 10);
- }
|