Browse Source

Improve HSV2LED. Added View/Edit Day/Night Hour/Bright/Mode/Colour.

Vladimir N. Shilov 2 years ago
parent
commit
b4ed321a29
10 changed files with 524 additions and 165 deletions
  1. 4 0
      Inc/board.h
  2. 25 11
      Inc/clock.h
  3. 33 5
      Inc/event-system.h
  4. 14 1
      Inc/list_event.h
  5. 12 2
      Inc/main.h
  6. 7 5
      Makefile
  7. 10 0
      ReadMe.txt
  8. 12 0
      Src/board.c
  9. 333 124
      Src/clock.c
  10. 74 17
      Src/event-system.c

+ 4 - 0
Inc/board.h

@@ -79,6 +79,7 @@ typedef union {
 #define COLOR_R(x)      TIM1->CCR2 = x
 #define COLOR_R(x)      TIM1->CCR2 = x
 #define COLOR_G(x)      TIM1->CCR3 = x
 #define COLOR_G(x)      TIM1->CCR3 = x
 #define COLOR_B(x)      TIM1->CCR4 = x
 #define COLOR_B(x)      TIM1->CCR4 = x
+#define LEDS_OFF        COLOR_R(0); COLOR_G(0); COLOR_B(0)
 
 
 #define TUBE_A_BRIGHT(x)  TIM1->CCR1 = x
 #define TUBE_A_BRIGHT(x)  TIM1->CCR1 = x
 #define TUBE_B_BRIGHT(x)  TIM3->CCR4 = x
 #define TUBE_B_BRIGHT(x)  TIM3->CCR4 = x
@@ -182,4 +183,7 @@ void tube_PowerOn(tube_pos_t tube);
 void tube_PowerOff(tube_pos_t tube);
 void tube_PowerOff(tube_pos_t tube);
 void tube_BrightLevel(tube_pos_t tube, uint8_t bright);
 void tube_BrightLevel(tube_pos_t tube, uint8_t bright);
 
 
+int Flash_Write(uint64_t * data);
+int Flash_Read(uint64_t * data);
+
 #endif /* _BPARD_H */
 #endif /* _BPARD_H */

+ 25 - 11
Inc/clock.h

@@ -7,16 +7,27 @@
 #include "main.h"
 #include "main.h"
 
 
 /* type defs */
 /* type defs */
+typedef union {
+  uint64_t u64;
+  uint32_t u32[2];
+  uint8_t u8[8];
+  struct {
+    uint8_t DayHour;
+    uint8_t NightHour;
+    uint8_t DayBright;
+    uint8_t NightBright;
+    uint8_t DayMode;
+    uint8_t NightMode;
+    uint8_t DayColour;
+    uint8_t NightColour;
+  } name;
+} flash_data_t;
+
 typedef enum {
 typedef enum {
-  Lvl_Off = 0,
-  Lvl_Min = 1,
-  Lvl_2 = 2,
-  Lvl_3 = 3,
-  Lvl_Mdl = 4,
-  Lvl_5 = 5,
-  Lvl_Max = 6,
-  Lvl_On = 7
-} brigh_level_t;
+  light_Off = 0x0,
+  light_Rainbow = 0x1,
+  light_Colour = 0x2
+} light_mode_t;
 
 
 /* macro */
 /* macro */
 #define FADE_START      0
 #define FADE_START      0
@@ -26,8 +37,11 @@ typedef enum {
 // Day/Night mode
 // Day/Night mode
 #define MORNING_HOUR    0x06
 #define MORNING_HOUR    0x06
 #define EVENING_HOUR    0x22
 #define EVENING_HOUR    0x22
-#define DAY_BR_LVL      (brigh_level_t)5
-#define NIGHT_BR_LVL    (brigh_level_t)2
+#define DAY_BR_LVL      11
+#define NIGHT_BR_LVL    5
+#define MAX_BRIGHT_LVL  15
+#define MAX_LIGHT_MODE  2
+#define MAX_COLOR_VAL   59
 
 
 // Color HUE angle
 // Color HUE angle
 #define COLOUR_RED      0
 #define COLOUR_RED      0

+ 33 - 5
Inc/event-system.h

@@ -72,10 +72,38 @@ void setDecWDay(void);
 void setDecMDay(void);
 void setDecMDay(void);
 void setDecMonth(void);
 void setDecMonth(void);
 void setDecYear(void);
 void setDecYear(void);
-/*
-void showBright(void);
-void incBright(void);
-void decBright(void);
-*/
+
+void showDNhour(void);
+void showDNbright(void);
+void showDNmode(void);
+void showDNcolour(void);
+
+void setDNbegin(void);
+void setDNend(void);
+void setDayHourBegin(void);
+void setNightHourBegin(void);
+void setDayBrightBegin(void);
+void setNightBrightBegin(void);
+void setDayModeBegin(void);
+void setNightModeBegin(void);
+void setDayColourBegin(void);
+void setNightColourBegin(void);
+void setIncDayHour(void);
+void setIncDayBright(void);
+void setIncDayMode(void);
+void setIncDayColour(void);
+void setDecDayHour(void);
+void setDecDayBright(void);
+void setDecDayMode(void);
+void setDecDayColour(void);
+void setIncNightHour(void);
+void setIncNightBright(void);
+void setIncNightMode(void);
+void setIncNightColour(void);
+void setDecNightHour(void);
+void setDecNightBright(void);
+void setDecNightMode(void);
+void setDecNightColour(void);
+void setDNbreak(void);
 
 
 #endif /* EVENT_SYSTEM_H */
 #endif /* EVENT_SYSTEM_H */

+ 14 - 1
Inc/list_event.h

@@ -15,7 +15,8 @@ typedef enum {
   evBTN3Holded,
   evBTN3Holded,
   evBTN4Holded,
   evBTN4Holded,
   evRefreshCal,
   evRefreshCal,
-  evDisplayWDT
+  evDisplayWDT,
+  evLastEvent
 } es_event_t;
 } es_event_t;
 
 
 /* State codes */
 /* State codes */
@@ -37,6 +38,18 @@ typedef enum {
   stSetMD,
   stSetMD,
   stSetMon,
   stSetMon,
   stSetYear,
   stSetYear,
+  stShowDNhours,
+  stShowDNbright,
+  stShowDNmode,
+  stShowDNcolour,
+  stSetDayHour,
+  stSetNightHour,
+  stSetDayBright,
+  stSetNightBright,
+  stSetDayMode,
+  stSetNightMode,
+  stSetDayColour,
+  stSetNightColour,
   /* end */
   /* end */
   stLastState
   stLastState
 } es_state_t;
 } es_state_t;

+ 12 - 2
Inc/main.h

@@ -71,6 +71,7 @@ extern volatile flag_t Flag;
 typedef union {
 typedef union {
   uint16_t u16;           // element specifier for accessing whole u16
   uint16_t u16;           // element specifier for accessing whole u16
   int16_t  i16;           // element specifier for accessing whole i16
   int16_t  i16;           // element specifier for accessing whole i16
+  uint8_t  u8[2];
   struct {
   struct {
     #ifdef LITTLE_ENDIAN  // Byte-order is little endian
     #ifdef LITTLE_ENDIAN  // Byte-order is little endian
     uint8_t u8L;          // element specifier for accessing low u8
     uint8_t u8L;          // element specifier for accessing low u8
@@ -83,8 +84,10 @@ typedef union {
 } nt16_t;
 } nt16_t;
 
 
 typedef union {
 typedef union {
-  uint32_t u32;           // element specifier for accessing whole u32
-  int32_t i32;            // element specifier for accessing whole i32
+  uint32_t  u32;          // element specifier for accessing whole u32
+  int32_t   i32;          // element specifier for accessing whole i32
+  uint16_t  u16[2];
+  uint8_t   u8[4];
  struct {
  struct {
     #ifdef LITTLE_ENDIAN  // Byte-order is little endian
     #ifdef LITTLE_ENDIAN  // Byte-order is little endian
     uint16_t u16L;        // element specifier for accessing low u16
     uint16_t u16L;        // element specifier for accessing low u16
@@ -96,6 +99,13 @@ typedef union {
   } s32;                  // element spec. for acc. struct with low or high u16
   } s32;                  // element spec. for acc. struct with low or high u16
 } nt32_t;
 } nt32_t;
 
 
+typedef union {
+  uint64_t  u64;
+  int64_t   i64;
+  uint32_t  u32[2];
+  int32_t   i32[2];
+} nt64_t;
+
 /* Exported constants --------------------------------------------------------*/
 /* Exported constants --------------------------------------------------------*/
 /* Exported macro ------------------------------------------------------------*/
 /* Exported macro ------------------------------------------------------------*/
 /* Exported functions prototypes ---------------------------------------------*/
 /* Exported functions prototypes ---------------------------------------------*/

+ 7 - 5
Makefile

@@ -166,21 +166,23 @@ OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.s=.o)))
 vpath %.s $(sort $(dir $(ASM_SOURCES)))
 vpath %.s $(sort $(dir $(ASM_SOURCES)))
 
 
 $(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR) 
 $(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR) 
-	@echo Compiling $<
+	@echo Compiling: $<
 	@$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@
 	@$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@
 
 
 $(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR)
 $(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR)
-	@echo Compiling $<
+	@echo Compiling: $<
 	@$(AS) -c $(CFLAGS) $< -o $@
 	@$(AS) -c $(CFLAGS) $< -o $@
 
 
 $(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile
 $(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile
-	@echo Linking...
+	@echo 
+	@echo Linking: $@
 	@$(CC) $(OBJECTS) $(LDFLAGS) -o $@
 	@$(CC) $(OBJECTS) $(LDFLAGS) -o $@
-	@echo Size:
+#	@echo ------------------------------------------------------------
 	@$(SZ) $@
 	@$(SZ) $@
 
 
 $(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
 $(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
-	@echo Make HEX...
+	@echo 
+	@echo Makeing HEX: $@
 	@$(HEX) $< $@
 	@$(HEX) $< $@
 	
 	
 #$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
 #$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf | $(BUILD_DIR)

+ 10 - 0
ReadMe.txt

@@ -101,3 +101,13 @@ Segger убрал, в хардфаулт падает только он.
 
 
 Получается, под GCC какая-то лажа с I2C - при инициализации получается 
 Получается, под GCC какая-то лажа с I2C - при инициализации получается 
 частота прерываний 4096 Гц.
 частота прерываний 4096 Гц.
+---
+07.02.2022
+
+Информация, сохраняемая в FLASH:
+час дня, час ночи - 2 байта;
+яркость дня, яркость ночи - 2 байта (1 байт);
+режим подсветки дня, режим подсветвки ночи - 2 байта (1 байт) - выкл/"радуга"/цвет;
+код цвета дня, код цвета ночи - 2 байта (0..59).
+Пока ужиматься не нужно - будет 8 байт, при необходимости можно сократить до 6-ти,
+освободив 2 байта для чего-то другого.

+ 12 - 0
Src/board.c

@@ -745,3 +745,15 @@ static void USART1_UART_Init(void)
   {
   {
   }
   }
 }
 }
+
+int Flash_Write(uint64_t * data) {
+  nt64_t val;
+  val.u64 = * data;
+  return 0;
+}
+
+int Flash_Read(uint64_t * data) {
+  nt64_t val;
+  val.u64 = * data;
+  return 0;
+}

+ 333 - 124
Src/clock.c

@@ -19,13 +19,17 @@ static btn_t Button[BTN_NUM] = {
   {0, evBTN4Pressed, evBTN4Holded,  BTN4_PIN}
   {0, evBTN4Pressed, evBTN4Holded,  BTN4_PIN}
 };
 };
 //convert linear bright level to logariphmic
 //convert linear bright level to logariphmic
-const uint8_t cie[8] = { 0, 5, 14, 33, 64, 109, 172, 255 };
+const uint8_t cie[MAX_BRIGHT_LVL + 1] = {
+  0, 2, 4, 8, 13, 20, 29, 40, 54, 72, 92, 116, 145, 177, 214, 255
+};
 volatile static uint8_t dispWDT = 0;
 volatile static uint8_t dispWDT = 0;
 static in15_pin_t symToFade = 0;
 static in15_pin_t symToFade = 0;
-static brigh_level_t BrightLevel;
+static uint8_t BrightLevel;
+static flash_data_t Lighting, setLighting;
 
 
 /* function prototypes */
 /* function prototypes */
 static void check_DayNight(void);
 static void check_DayNight(void);
+static void HSV2LED(const uint8_t hue, const uint8_t val);
 static void MinusFadeIn(void);
 static void MinusFadeIn(void);
 static void MinusFadeOut(void);
 static void MinusFadeOut(void);
 static void PlusFadeIn(void);
 static void PlusFadeIn(void);
@@ -33,25 +37,35 @@ static void PercentFadeIn(void);
 static void PressureFadeIn(void);
 static void PressureFadeIn(void);
 static void IN15_FadeIn(void);
 static void IN15_FadeIn(void);
 static void IN15_FadeOut(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);
+static void valIncrease(uint8_t * val, const uint8_t max);
+static void valDecrease(uint8_t * val, const uint8_t max);
+static void dvalIncrease(uint8_t * val, const uint8_t max);
+static void dvalDecrease(uint8_t * val, const uint8_t max);
 
 
 /* funcions */
 /* funcions */
 void Clock_Init(void) {
 void Clock_Init(void) {
   RTC_ReadAll(&Clock);
   RTC_ReadAll(&Clock);
   showTime();
   showTime();
 
 
+  //Lighting.u64 = flash_read();
+  Lighting.name.DayHour = MORNING_HOUR;
+  Lighting.name.NightHour = EVENING_HOUR;
+  Lighting.name.DayBright = DAY_BR_LVL;
+  Lighting.name.NightBright = NIGHT_BR_LVL;
+  Lighting.name.DayMode = light_Rainbow;
+  Lighting.name.NightMode = light_Colour;
+  Lighting.name.NightColour = COLOUR_NIXIE;
+
   check_DayNight();
   check_DayNight();
 }
 }
 
 
 static void check_DayNight(void) {
 static void check_DayNight(void) {
-  if ((Clock.Hr >= MORNING_HOUR) && (Clock.Hr < EVENING_HOUR)) {
+  if ((Clock.Hr >= Lighting.name.DayHour) && (Clock.Hr < Lighting.name.NightHour)) {
     Flag.Now_Day = 1;
     Flag.Now_Day = 1;
-    BrightLevel = cie[DAY_BR_LVL];
+    BrightLevel = cie[Lighting.name.DayBright];
   } else {
   } else {
     Flag.Now_Day = 0;
     Flag.Now_Day = 0;
-    BrightLevel = cie[NIGHT_BR_LVL];
+    BrightLevel = cie[Lighting.name.NightBright];
   }
   }
 
 
   tube_BrightLevel(Tube_All, BrightLevel);
   tube_BrightLevel(Tube_All, BrightLevel);
@@ -305,113 +319,43 @@ static void IN15_FadeOut(void) {
 
 
 /**
 /**
  * @brief  HSV to RGB convertion
  * @brief  HSV to RGB convertion
- * @param  hue: 0-59, sat: 0-255, val (lightness): 0-255
+ * @param  hue: 0-59, sat: allways max, val (lightness): 0-255
  * @return none. RGB value output direct to LED.
  * @return none. RGB value output direct to LED.
  */
  */
-#define HUE_DEGREE    60
-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 * HUE_DEGREE);
-  int p = (256 * v - s * v) / 256;
-
-  if (i & 1)
-  {
-    int q = (256 * 60 * HUE_DEGREE * v - h * s * v + 60 * HUE_DEGREE * s * v * i) / (256 * 60 * HUE_DEGREE);
-    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 * HUE_DEGREE * v + h * s * v - 60 * HUE_DEGREE * s * v * (i + 1)) / (256 * 60 * HUE_DEGREE);
-    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;
+static void HSV2LED(const uint8_t hue, const uint8_t val) {
   uint32_t r=0, g=0, b=0;
   uint32_t r=0, g=0, b=0;
 
 
-  if (sat == 0)
-  { // Achromatic color (gray).
+  switch (hue / 10) {
+  case 0:
     r = val;
     r = val;
+      g = (val * hue) / 10;
+      b = 0;
+      break;
+  case 1:
+    r = (val * (10 - (hue % 10))) / 10;
+    g = val;
+    b = 0;
+    break;
+  case 2:
+    r = 0;
     g = val;
     g = val;
+    b = (val * (hue % 10)) / 10;
+    break;
+  case 3:
+    r = 0;
+    g = (val * (10 - (hue % 10))) / 10;
     b = 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;
-    }
+    break;
+  case 4:
+    r = (val * (hue % 10)) / 10;
+    g = 0;
+    b = val;
+    break;
+  case 5:
+    r = val;
+    g = 0;
+    b = (val * (10 - (hue % 10))) / 10;
+    break;
   }
   }
 
 
   COLOR_R((uint8_t)r);
   COLOR_R((uint8_t)r);
@@ -427,12 +371,10 @@ void showTime(void) {
   RTOS_SetTask(MinusFadeOut, 500, 0);
   RTOS_SetTask(MinusFadeOut, 500, 0);
 
 
   if (Flag.Now_Day != 0) {
   if (Flag.Now_Day != 0) {
-    // new hsv2led
-    //uint16_t hue = (uint16_t)(bcd2bin(Clock.Sec) * 256) / 10;
     uint8_t hue = bcd2bin(Clock.Sec);
     uint8_t hue = bcd2bin(Clock.Sec);
-    HSV2LED(hue, 255, BrightLevel);
+    HSV2LED(hue, BrightLevel);
   } else {
   } else {
-    HSV2LED(COLOUR_NIXIE, 255, BrightLevel);
+    HSV2LED(COLOUR_NIXIE, BrightLevel);
   }
   }
 
 
   tube4_t buf;
   tube4_t buf;
@@ -448,7 +390,7 @@ void showMMSS(void) {
   IN15_Minus;
   IN15_Minus;
 
 
   uint8_t hue = bcd2bin(Clock.Sec);
   uint8_t hue = bcd2bin(Clock.Sec);
-  HSV2LED(hue, 255, BrightLevel);
+  HSV2LED(hue, BrightLevel);
 
 
   tube4_t buf;
   tube4_t buf;
   buf.s8.tA = Clock.Min >> 4;
   buf.s8.tA = Clock.Min >> 4;
@@ -496,11 +438,9 @@ void showYear(void) {
 
 
 void showHumidity(void) {
 void showHumidity(void) {
   dispWDT = DISP_WDT_TIME;
   dispWDT = DISP_WDT_TIME;
-  HSV2LED(COLOUR_BLUE, 255, BrightLevel);
-  //in15Percent();
+  HSV2LED(COLOUR_BLUE, BrightLevel);
   symToFade = sym_Percent;
   symToFade = sym_Percent;
   IN15_FadeIn();
   IN15_FadeIn();
-  //PercentFadeIn();
 
 
   tube4_t buf;
   tube4_t buf;
   buf.s8.tA = Humidity / 10;
   buf.s8.tA = Humidity / 10;
@@ -512,11 +452,11 @@ void showHumidity(void) {
 
 
 void showTemperature(void) {
 void showTemperature(void) {
   dispWDT = DISP_WDT_TIME;
   dispWDT = DISP_WDT_TIME;
-  HSV2LED(COLOUR_RED, 255, BrightLevel);
-  //in15Plus();
+  HSV2LED(COLOUR_RED, BrightLevel);
+  RTOS_DeleteTask(MinusFadeIn);
+  RTOS_DeleteTask(MinusFadeOut);
   symToFade = sym_Plus;
   symToFade = sym_Plus;
   IN15_FadeIn();
   IN15_FadeIn();
-  //PlusFadeIn();
 
 
   tube4_t buf;
   tube4_t buf;
   buf.s8.tA = 0xf;
   buf.s8.tA = 0xf;
@@ -528,11 +468,9 @@ void showTemperature(void) {
 
 
 void showPressure(void) {
 void showPressure(void) {
   dispWDT = DISP_WDT_TIME;
   dispWDT = DISP_WDT_TIME;
-  HSV2LED(COLOUR_GREEN, 255, cie[Lvl_Mdl]); // GREEN
-  //in15P();
+  HSV2LED(COLOUR_GREEN, BrightLevel);
   symToFade = sym_Pressure;
   symToFade = sym_Pressure;
   IN15_FadeIn();
   IN15_FadeIn();
-  //PressureFadeIn();
 
 
   tube4_t buf;
   tube4_t buf;
   int tmp;
   int tmp;
@@ -575,7 +513,7 @@ void setTimeShow(void) {
 void setTimeBegin(void) {
 void setTimeBegin(void) {
 	RTOS_DeleteTask(MinusFadeOut);
 	RTOS_DeleteTask(MinusFadeOut);
   in15Minus();
   in15Minus();
-  HSV2LED(COLOUR_NIXIE, 255, BrightLevel);
+  HSV2LED(COLOUR_NIXIE, BrightLevel);
   RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
   RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
   RTC_ReadAll(&setClock);
   RTC_ReadAll(&setClock);
 }
 }
@@ -628,7 +566,7 @@ void setTimeEnd(void) {
 
 
 void setDateBegin(void) {
 void setDateBegin(void) {
   IN15_OFF;
   IN15_OFF;
-  HSV2LED(COLOUR_NIXIE, 255, BrightLevel);
+  HSV2LED(COLOUR_NIXIE, BrightLevel);
   RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
   RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
   RTC_ReadAll(&setClock);
   RTC_ReadAll(&setClock);
 }
 }
@@ -745,6 +683,255 @@ void setDecYear(void) {
   valDecrease(&setClock.Year, 99);
   valDecrease(&setClock.Year, 99);
 }
 }
 
 
+void showDNhour(void) {
+	dispWDT = DISP_WDT_TIME;
+
+  tube4_t buf;
+  es_state_t e_st = ES_GetState();
+
+  if (e_st == stShowDNhours){
+    buf.s8.tA = Lighting.name.DayHour / 10;
+    buf.s8.tB = Lighting.name.DayHour % 10;
+    buf.s8.tD = Lighting.name.NightHour / 10;
+    buf.s8.tE = Lighting.name.NightHour % 10;
+  } else {
+    buf.s8.tA = setLighting.name.DayHour / 10;
+    buf.s8.tB = setLighting.name.DayHour % 10;
+    buf.s8.tD = setLighting.name.NightHour / 10;
+    buf.s8.tE = setLighting.name.NightHour % 10;
+  }
+  showDigits(buf);
+}
+
+void showDNbright(void) {
+	dispWDT = DISP_WDT_TIME;
+
+  tube4_t buf;
+  es_state_t e_st = ES_GetState();
+
+  if (e_st == stShowDNbright){
+    buf.s8.tA = Lighting.name.DayBright / 10;
+    buf.s8.tB = Lighting.name.DayBright % 10;
+    buf.s8.tD = Lighting.name.NightBright / 10;
+    buf.s8.tE = Lighting.name.NightBright % 10;
+  } else {
+    buf.s8.tA = setLighting.name.DayBright / 10;
+    buf.s8.tB = setLighting.name.DayBright % 10;
+    buf.s8.tD = setLighting.name.NightBright / 10;
+    buf.s8.tE = setLighting.name.NightBright % 10;
+  }
+  showDigits(buf);
+}
+
+void showDNmode(void) {
+	dispWDT = DISP_WDT_TIME;
+
+  tube4_t buf;
+  es_state_t e_st = ES_GetState();
+
+  if (e_st == stShowDNmode){
+    buf.s8.tA = Lighting.name.DayMode / 10;
+    buf.s8.tB = Lighting.name.DayMode % 10;
+    buf.s8.tD = Lighting.name.NightMode / 10;
+    buf.s8.tE = Lighting.name.NightMode % 10;
+  } else {
+    buf.s8.tA = setLighting.name.DayMode / 10;
+    buf.s8.tB = setLighting.name.DayMode % 10;
+    buf.s8.tD = setLighting.name.NightMode / 10;
+    buf.s8.tE = setLighting.name.NightMode % 10;
+  }
+  showDigits(buf);
+}
+
+void showDNcolour(void) {
+	dispWDT = DISP_WDT_TIME;
+
+  tube4_t buf;
+  es_state_t e_st = ES_GetState();
+
+  if (e_st == stShowDNcolour){
+    buf.s8.tA = Lighting.name.DayColour / 10;
+    buf.s8.tB = Lighting.name.DayColour % 10;
+    buf.s8.tD = Lighting.name.NightColour / 10;
+    buf.s8.tE = Lighting.name.NightColour % 10;
+  } else {
+    buf.s8.tA = setLighting.name.DayColour / 10;
+    buf.s8.tB = setLighting.name.DayColour % 10;
+    buf.s8.tD = setLighting.name.NightColour / 10;
+    buf.s8.tE = setLighting.name.NightColour % 10;
+  }
+  showDigits(buf);
+}
+
+void setDNbegin(void) {
+  IN15_OFF;
+  RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
+  setLighting.u64 = Lighting.u64;
+  //Flash_Read(&setLighting.u64);
+}
+
+void setDNend(void) {
+	dispWDT =  0;
+  RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
+  Blink_Stop();
+  LEDS_OFF;
+  Flash_Write(&setLighting.u64);
+  Lighting.u64 = setLighting.u64;
+  check_DayNight();
+}
+
+void setDayHourBegin(void) {
+  LEDS_OFF;
+  Flag.Blink_1 = 1;
+  Flag.Blink_2 = 1;
+  Flag.Blink_4 = 0;
+  Flag.Blink_5 = 0;
+  Blink_Start();
+  showDNhour();
+}
+
+void setNightHourBegin(void) {
+  Flag.Blink_1 = 0;
+  Flag.Blink_2 = 0;
+  Flag.Blink_4 = 1;
+  Flag.Blink_5 = 1;
+  Blink_Start();
+  showDNhour();
+}
+
+void setDayBrightBegin(void) {
+  Flag.Blink_1 = 1;
+  Flag.Blink_2 = 1;
+  Flag.Blink_4 = 0;
+  Flag.Blink_5 = 0;
+  Blink_Start();
+  showDNbright();
+}
+
+void setNightBrightBegin(void) {
+  Flag.Blink_1 = 0;
+  Flag.Blink_2 = 0;
+  Flag.Blink_4 = 1;
+  Flag.Blink_5 = 1;
+  Blink_Start();
+  showDNbright();
+}
+
+void setDayModeBegin(void) {
+  Flag.Blink_1 = 1;
+  Flag.Blink_2 = 1;
+  Flag.Blink_4 = 0;
+  Flag.Blink_5 = 0;
+  Blink_Start();
+  showDNmode();
+}
+
+void setNightModeBegin(void) {
+  Flag.Blink_1 = 0;
+  Flag.Blink_2 = 0;
+  Flag.Blink_4 = 1;
+  Flag.Blink_5 = 1;
+  Blink_Start();
+  showDNmode();
+}
+
+void setDayColourBegin(void) {
+  Flag.Blink_1 = 1;
+  Flag.Blink_2 = 1;
+  Flag.Blink_4 = 0;
+  Flag.Blink_5 = 0;
+  Blink_Start();
+  HSV2LED(setLighting.name.DayColour, BrightLevel);
+  showDNcolour();
+}
+
+void setNightColourBegin(void) {
+  Flag.Blink_1 = 0;
+  Flag.Blink_2 = 0;
+  Flag.Blink_4 = 1;
+  Flag.Blink_5 = 1;
+  Blink_Start();
+  HSV2LED(setLighting.name.NightColour, BrightLevel);
+  showDNcolour();
+}
+
+void setIncDayHour(void) {
+  dvalIncrease(&setLighting.name.DayHour, 23);
+}
+
+void setIncDayBright(void) {
+  dvalIncrease(&setLighting.name.DayBright, MAX_BRIGHT_LVL);
+  tube_BrightLevel(Tube_All, setLighting.name.DayBright);
+}
+
+void setIncDayMode(void) {
+  dvalIncrease(&setLighting.name.DayMode, MAX_LIGHT_MODE);
+}
+
+void setIncDayColour(void) {
+  dvalIncrease(&setLighting.name.DayColour, MAX_COLOR_VAL);
+  HSV2LED(setLighting.name.DayColour, BrightLevel);
+}
+
+void setDecDayHour(void) {
+  dvalDecrease(&setLighting.name.DayHour, 23);
+}
+
+void setDecDayBright(void) {
+  dvalDecrease(&setLighting.name.DayBright, MAX_BRIGHT_LVL);
+  tube_BrightLevel(Tube_All, setLighting.name.DayBright);
+}
+
+void setDecDayMode(void) {
+  dvalDecrease(&setLighting.name.DayMode, MAX_LIGHT_MODE);
+}
+
+void setDecDayColour(void) {
+  dvalDecrease(&setLighting.name.DayColour, MAX_COLOR_VAL);
+  HSV2LED(setLighting.name.DayColour, BrightLevel);
+}
+
+void setIncNightHour(void) {
+  dvalIncrease(&setLighting.name.NightHour, 23);
+}
+
+void setIncNightBright(void) {
+  dvalIncrease(&setLighting.name.NightBright, MAX_BRIGHT_LVL);
+  tube_BrightLevel(Tube_All, setLighting.name.NightBright);
+}
+
+void setIncNightMode(void) {
+  dvalIncrease(&setLighting.name.NightMode, MAX_LIGHT_MODE);
+}
+
+void setIncNightColour(void) {
+  dvalIncrease(&setLighting.name.NightColour, MAX_COLOR_VAL);
+  HSV2LED(setLighting.name.NightColour, BrightLevel);
+}
+
+void setDecNightHour(void) {
+  dvalDecrease(&setLighting.name.NightHour, 23);
+}
+
+void setDecNightBright(void) {
+  dvalDecrease(&setLighting.name.NightBright, MAX_BRIGHT_LVL);
+  tube_BrightLevel(Tube_All, setLighting.name.NightBright);
+}
+
+void setDecNightMode(void) {
+  dvalDecrease(&setLighting.name.NightMode, MAX_LIGHT_MODE);
+}
+
+void setDecNightColour(void) {
+  dvalDecrease(&setLighting.name.NightColour, MAX_COLOR_VAL);
+  HSV2LED(setLighting.name.NightColour, BrightLevel);
+}
+
+void setDNbreak(void) {
+  // restore led and tube bright level
+  check_DayNight();
+}
+
 /**
 /**
   * @brief  Increase BCD value.
   * @brief  Increase BCD value.
   * @param  : val, max
   * @param  : val, max
@@ -774,3 +961,25 @@ static void valDecrease(uint8_t * val, uint8_t max) {
   }
   }
   *val = ((bin / 10 ) << 4) | (bin % 10);
   *val = ((bin / 10 ) << 4) | (bin % 10);
 }
 }
+
+/**
+  * @brief Increase/Decrease decimal value.
+  * @param val
+  * @param max 
+  * @retval : None, change value
+  */
+static void dvalIncrease(uint8_t * val, uint8_t max) {
+  if (*val < max) {
+    *val += 1;
+  } else {
+    *val = 0;
+  }
+}
+
+static void dvalDecrease(uint8_t * val, uint8_t max) {
+  if (*val > 0) {
+    *val -= 1;
+  } else {
+    *val = max;
+  }
+}

+ 74 - 17
Src/event-system.c

@@ -31,40 +31,49 @@ typedef struct {
   */
   */
 const table_state_t stateTable[] = {
 const table_state_t stateTable[] = {
   /* STATE    EVENT   NEXT STATE  STATE_FUNC1 STATE_FUNC2 */
   /* STATE    EVENT   NEXT STATE  STATE_FUNC1 STATE_FUNC2 */
-  /* show date, time */
-  {stShowTime, evBTN1Pressed, stShowWDay, showWD,     NULL},
-  {stShowWDay, evBTN1Pressed, stShowDM,   showDayMon, NULL},
-  {stShowDM,   evBTN1Pressed, stShowYear, showYear,   NULL},
-  {stShowYear, evBTN1Pressed, stShowTime, showTime,   NULL},
+  /* show date, time, ... */
+  // 'Select' Button pressed
+  {stShowTime,     evBTN1Pressed, stShowWDay,     showWD,       NULL},
+  {stShowWDay,     evBTN1Pressed, stShowDM,       showDayMon,   NULL},
+  {stShowDM,       evBTN1Pressed, stShowYear,     showYear,     NULL},
+  {stShowYear,     evBTN1Pressed, stShowDNhours,  showDNhour,   NULL},
+  {stShowDNhours,  evBTN1Pressed, stShowDNbright, showDNbright, NULL},
+  {stShowDNbright, evBTN1Pressed, stShowDNmode,   showDNmode,   NULL},
+  {stShowDNmode,   evBTN1Pressed, stShowDNcolour, showDNcolour, NULL},
+  {stShowDNcolour, evBTN1Pressed, stShowTime,     showTime,     NULL},
+  // '+' Button pressed
   {stShowTime, evBTN2Pressed, stShowMMSS, showMMSS,   NULL},
   {stShowTime, evBTN2Pressed, stShowMMSS, showMMSS,   NULL},
   {stShowMMSS, evBTN2Pressed, stShowTime, showTime,   NULL},
   {stShowMMSS, evBTN2Pressed, stShowTime, showTime,   NULL},
-  {stShowTime, evBTN4Pressed, stShowSensorData, showSensorData, NULL},
-  {stShowSensorData, evBTN4Pressed, stShowTime, showTime, NULL},
+  // Top Buton pressed
+  {stShowTime,        evBTN4Pressed, stShowTemperature, showTemperature, NULL},
+  {stShowTemperature, evBTN4Pressed, stShowHumidity,    showHumidity,    NULL},
+  {stShowHumidity,    evBTN4Pressed, stShowPressure,    showPressure,    NULL},
+  {stShowPressure,    evBTN4Pressed, stShowTime,        showTime,        NULL},
 
 
-  /* display wdt */
+  /* display wdt events */
   {stShowWDay,        evDisplayWDT, stShowTime, showTime, NULL},
   {stShowWDay,        evDisplayWDT, stShowTime, showTime, NULL},
   {stShowDM,          evDisplayWDT, stShowTime, showTime, NULL},
   {stShowDM,          evDisplayWDT, stShowTime, showTime, NULL},
   {stShowYear,        evDisplayWDT, stShowTime, showTime, NULL},
   {stShowYear,        evDisplayWDT, stShowTime, showTime, NULL},
   {stShowHumidity,    evDisplayWDT, stShowTime, showTime, NULL},
   {stShowHumidity,    evDisplayWDT, stShowTime, showTime, NULL},
   {stShowTemperature, evDisplayWDT, stShowTime, showTime, NULL},
   {stShowTemperature, evDisplayWDT, stShowTime, showTime, NULL},
   {stShowPressure,    evDisplayWDT, stShowTime, showTime, NULL},
   {stShowPressure,    evDisplayWDT, stShowTime, showTime, NULL},
-  //{stShowBright,      evDisplayWDT, stShowTime, showTime, NULL},
+  {stShowDNhours,     evDisplayWDT, stShowTime, showTime, NULL},
+  {stShowDNbright,    evDisplayWDT, stShowTime, showTime, NULL},
+  {stShowDNmode,      evDisplayWDT, stShowTime, showTime, NULL},
+  {stShowDNcolour,    evDisplayWDT, stShowTime, showTime, NULL},
 
 
   /* refresh output */
   /* refresh output */
   {stShowTime, evNewSecond,  stNoChange, showTime,   NULL},
   {stShowTime, evNewSecond,  stNoChange, showTime,   NULL},
   {stShowMMSS, evNewSecond,  stNoChange, showMMSS,   NULL},
   {stShowMMSS, evNewSecond,  stNoChange, showMMSS,   NULL},
-//  {stShowWDay, evRefreshCal, stNoChange, showWD,     NULL},
-//  {stShowDM,   evRefreshCal, stNoChange, showDayMon, NULL},
-//  {stShowYear, evRefreshCal, stNoChange, showYear,   NULL},
-//  {stShowYear,   evBTN1Pressed, stShowBright, showBright, NULL},
-//  {stShowBright, evBTN1Pressed, stShowTime,   showTime,   NULL},
 
 
   /* set time */
   /* set time */
   {stShowTime, evBTN1Holded,  stSetHH,    setTimeBegin, setHHBegin},
   {stShowTime, evBTN1Holded,  stSetHH,    setTimeBegin, setHHBegin},
   {stSetHH,    evBTN1Holded,  stShowTime, setTimeEnd,   showTime},
   {stSetHH,    evBTN1Holded,  stShowTime, setTimeEnd,   showTime},
   {stSetMM,    evBTN1Holded,  stShowTime, setTimeEnd,   showTime},
   {stSetMM,    evBTN1Holded,  stShowTime, setTimeEnd,   showTime},
+
   {stSetHH,    evBTN1Pressed, stSetMM,    setMMBegin,   NULL},
   {stSetHH,    evBTN1Pressed, stSetMM,    setMMBegin,   NULL},
   {stSetMM,    evBTN1Pressed, stSetHH,    setHHBegin,   NULL},
   {stSetMM,    evBTN1Pressed, stSetHH,    setHHBegin,   NULL},
+
   {stSetHH,    evBTN2Pressed, stNoChange, setHHInc,     setTimeShow},
   {stSetHH,    evBTN2Pressed, stNoChange, setHHInc,     setTimeShow},
   {stSetHH,    evBTN3Pressed, stNoChange, setHHDec,     setTimeShow},
   {stSetHH,    evBTN3Pressed, stNoChange, setHHDec,     setTimeShow},
   {stSetMM,    evBTN2Pressed, stNoChange, setMMInc,     setTimeShow},
   {stSetMM,    evBTN2Pressed, stNoChange, setMMInc,     setTimeShow},
@@ -103,9 +112,57 @@ const table_state_t stateTable[] = {
   {stSetMon,  evDisplayWDT, stShowTime, showTime, NULL},
   {stSetMon,  evDisplayWDT, stShowTime, showTime, NULL},
   {stSetYear, evDisplayWDT, stShowTime, showTime, NULL},
   {stSetYear, evDisplayWDT, stShowTime, showTime, NULL},
 
 
-  /* set bright */
-//  {stShowBright, evBTN3Pressed, stNoChange, incBright, showBright},
-//  {stShowBright, evBTN2Pressed, stNoChange, decBright, showBright},
+  /* set lighting */
+  // begin
+  {stShowDNhours,  evBTN1Holded, stSetDayHour,   setDNbegin, setDayHourBegin},
+  {stShowDNbright, evBTN1Holded, stSetDayBright, setDNbegin, setDayBrightBegin},
+  {stShowDNmode,   evBTN1Holded, stSetDayMode,   setDNbegin, setDayModeBegin},
+  {stShowDNcolour, evBTN1Holded, stSetDayColour, setDNbegin, setDayColourBegin},
+  // end
+  {stSetDayHour,     evBTN1Holded, stShowDNhours,  setDNend, showDNhour},
+  {stSetNightHour,   evBTN1Holded, stShowDNhours,  setDNend, showDNhour},
+  {stSetDayBright,   evBTN1Holded, stShowDNbright, setDNend, showDNbright},
+  {stSetNightBright, evBTN1Holded, stShowDNbright, setDNend, showDNbright},
+  {stSetDayMode,     evBTN1Holded, stShowDNmode,   setDNend, showDNmode},
+  {stSetNightMode,   evBTN1Holded, stShowDNmode,   setDNend, showDNmode},
+  {stSetDayColour,   evBTN1Holded, stShowDNcolour, setDNend, showDNcolour},
+  {stSetNightColour, evBTN1Holded, stShowDNcolour, setDNend, showDNcolour},
+  // next
+  {stSetDayHour,     evBTN1Pressed, stSetNightHour,   setNightHourBegin,   NULL},
+  {stSetNightHour,   evBTN1Pressed, stSetDayBright,   setDayBrightBegin,   NULL},
+  {stSetDayBright,   evBTN1Pressed, stSetNightBright, setNightBrightBegin, NULL},
+  {stSetNightBright, evBTN1Pressed, stSetDayMode,     setDayModeBegin,     NULL},
+  {stSetDayMode,     evBTN1Pressed, stSetNightMode,   setNightModeBegin,   NULL},
+  {stSetNightMode,   evBTN1Pressed, stSetDayColour,   setDayColourBegin,   NULL},
+  {stSetDayColour,   evBTN1Pressed, stSetNightColour, setNightColourBegin, NULL},
+  {stSetNightColour, evBTN1Pressed, stSetDayHour,     setDayHourBegin,     NULL},
+  // change
+  {stSetDayHour,     evBTN2Pressed, stNoChange, setIncDayHour,     showDNhour},
+  {stSetDayHour,     evBTN3Pressed, stNoChange, setDecDayHour,     showDNhour},
+  {stSetNightHour,   evBTN2Pressed, stNoChange, setIncNightHour,   showDNhour},
+  {stSetNightHour,   evBTN3Pressed, stNoChange, setDecNightHour,   showDNhour},
+  {stSetDayBright,   evBTN2Pressed, stNoChange, setIncDayBright,   showDNbright},
+  {stSetDayBright,   evBTN3Pressed, stNoChange, setDecDayBright,   showDNbright},
+  {stSetNightBright, evBTN2Pressed, stNoChange, setIncNightBright, showDNbright},
+  {stSetNightBright, evBTN3Pressed, stNoChange, setDecNightBright, showDNbright},
+  {stSetDayMode,     evBTN2Pressed, stNoChange, setIncDayMode,     showDNmode},
+  {stSetDayMode,     evBTN3Pressed, stNoChange, setDecDayMode,     showDNmode},
+  {stSetNightMode,   evBTN2Pressed, stNoChange, setIncNightMode,   showDNmode},
+  {stSetNightMode,   evBTN3Pressed, stNoChange, setDecNightMode,   showDNmode},
+  {stSetDayColour,   evBTN2Pressed, stNoChange, setIncDayColour,   showDNcolour},
+  {stSetDayColour,   evBTN3Pressed, stNoChange, setDecDayColour,   showDNcolour},
+  {stSetNightColour, evBTN2Pressed, stNoChange, setIncNightColour, showDNcolour},
+  {stSetNightColour, evBTN3Pressed, stNoChange, setDecNightColour, showDNcolour},
+  // WDT
+  {stSetDayHour,     evDisplayWDT, stShowTime, setDNbreak, showTime},
+  {stSetNightHour,   evDisplayWDT, stShowTime, setDNbreak, showTime},
+  {stSetDayBright,   evDisplayWDT, stShowTime, setDNbreak, showTime},
+  {stSetNightBright, evDisplayWDT, stShowTime, setDNbreak, showTime},
+  {stSetDayMode,     evDisplayWDT, stShowTime, setDNbreak, showTime},
+  {stSetNightMode,   evDisplayWDT, stShowTime, setDNbreak, showTime},
+  {stSetDayColour,   evDisplayWDT, stShowTime, setDNbreak, showTime},
+  {stSetNightColour, evDisplayWDT, stShowTime, setDNbreak, showTime},
+
   /* mandatory empty table row */
   /* mandatory empty table row */
   {stNoChange, eventNull, stNoChange, EmptyFunc, EmptyFunc}
   {stNoChange, eventNull, stNoChange, EmptyFunc, EmptyFunc}
 };
 };