Browse Source

Added Day/Nigh mode. Small projec reorgonize.

Vladimir N. Shilov 2 years ago
parent
commit
96b3adf102
7 changed files with 171 additions and 99 deletions
  1. 10 13
      Inc/board.h
  2. 25 1
      Inc/clock.h
  3. 3 2
      Inc/main.h
  4. 4 2
      Makefile
  5. 34 58
      Src/board.c
  6. 92 18
      Src/clock.c
  7. 3 5
      Src/main.c

+ 10 - 13
Inc/board.h

@@ -9,23 +9,14 @@
 
 /* Type Defs */
 typedef enum {
+  Tube_All = 0xf,
+  Tube_C = 4,
   Tube_A = 3,
   Tube_B = 2,
   Tube_D = 1,
   Tube_E = 0
 } tube_pos_t;
 
-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;
-
 typedef enum {
   sym_Pressure  = 0x1,
   sym_Plus      = 0x2,
@@ -89,6 +80,13 @@ typedef union {
 #define COLOR_G(x)      TIM1->CCR3 = x
 #define COLOR_B(x)      TIM1->CCR4 = x
 
+#define TUBE_A_BRIGHT(x)  TIM1->CCR1 = x
+#define TUBE_B_BRIGHT(x)  TIM3->CCR4 = x
+#define TUBE_C_BRIGHT(x)  TIM3->CCR3 = x
+#define TUBE_D_BRIGHT(x)  TIM3->CCR2 = x
+#define TUBE_E_BRIGHT(x)  TIM3->CCR1 = x
+#define TUBES_BRIGHT(x)   TUBE_A_BRIGHT(x); TUBE_B_BRIGHT(x); TUBE_C_BRIGHT(x); TUBE_D_BRIGHT(x); TUBE_E_BRIGHT(x)
+
 /* Constants */
 /* PWM Timers for 250 Hz */
 #define TIM1_PSC            (375 - 1)
@@ -173,7 +171,6 @@ typedef union {
 #define BTNS_STATE          (BTNS1_STATE | BTNS2_STATE)
 
 /* Variables */
-extern const uint8_t cie[];
 
 /* Exported funcions */
 void SystemClock_Config(void);
@@ -181,8 +178,8 @@ void Board_Init(void);
 void Blink_Start(void);
 void Blink_Stop(void);
 void showDigits(tube4_t dig);
-void HSV2LED(const uint8_t hue, const uint8_t sat, const uint8_t val);
 void tube_PowerOn(tube_pos_t tube);
 void tube_PowerOff(tube_pos_t tube);
+void tube_BrightLevel(tube_pos_t tube, uint8_t bright);
 
 #endif /* _BPARD_H */

+ 25 - 1
Inc/clock.h

@@ -6,11 +6,35 @@
 /* Includes */
 #include "main.h"
 
+/* type defs */
+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;
+
 /* macro */
 #define FADE_START      0
 #define FADE_STOP       15
 #define FADE_STEP       1
 
+// Day/Night mode
+#define MORNING_HOUR    0x06
+#define EVENING_HOUR    0x22
+#define DAY_BR_LVL      (brigh_level_t)2
+#define NIGHT_BR_LVL    (brigh_level_t)5
+
+// Color HUE angle
+#define COLOUR_RED      0
+#define COLOUR_GREEN    20
+#define COLOUR_BLUE     40
+#define COLOUR_NIXIE    1
+
 /* time constant in ms */
 #define BTN_SCAN_PERIOD     10
 #define BTN_SCAN_PAUSE      200
@@ -25,7 +49,7 @@
 extern rtc_t Clock;
 
 /* function prototypes */
-//void Clock_Init(void);
+void Clock_Init(void);
 void btnProcess(void);
 void new_Second(void);
 

+ 3 - 2
Inc/main.h

@@ -50,7 +50,7 @@ typedef enum {
   ON = 1
 } onoff_t;
 
-typedef struct {
+typedef struct t_flag {
   uint32_t RTC_IRQ:     1;
   uint32_t SPI_TX_End:  1;
   uint32_t I2C_TX_End:  1;
@@ -63,7 +63,8 @@ typedef struct {
   uint32_t Blink_3:     1;
   uint32_t Blink_4:     1;
   uint32_t Blink_5:     1;
-  uint32_t _reserv:    20;
+  uint32_t Now_Day:     1;
+  uint32_t _reserv:    19;
 } flag_t;
 extern volatile flag_t Flag;
 

+ 4 - 2
Makefile

@@ -151,7 +151,8 @@ LIBDIR =
 LDFLAGS = $(MCU) -specs=nano.specs -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections
 
 # default action: build all
-all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin
+all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex
+# $(BUILD_DIR)/$(TARGET).bin
 
 
 #######################################
@@ -175,7 +176,8 @@ $(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR)
 $(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile
 	@echo Linking...
 	@$(CC) $(OBJECTS) $(LDFLAGS) -o $@
-	$(SZ) $@
+	@echo Size:
+	@$(SZ) $@
 
 $(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
 	@echo Make HEX...

+ 34 - 58
Src/board.c

@@ -26,8 +26,6 @@ static const uint16_t nixieCathodeMap[4][10] = {
 };
 //static const uint8_t nixieCathodeMask[4][2] = {{0x00, 0x3f}, {0xc0, 0x0f}, {0xf0, 0x03}, {0xc0, 0x00}};
 static uint8_t tubesBuffer[SPI_BUFFER_SIZE] = {0};
-//convert linear bright level to logariphmic
-const uint8_t cie[8] = { 0, 5, 14, 33, 64, 109, 172, 255 };
 
 /* private typedef */
 
@@ -177,62 +175,6 @@ static void _show_digits(const uint32_t digits)
   }
 }
 
-/**
- * @brief  HSV to RGB convertion
- * @param  hue: 0-59, sat: 0-255, val (lightness): 0-255
- * @return none. RGB value out direct to LED.
- */
-void 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);
-}
-
 void tube_PowerOn(tube_pos_t tube)
 {
   switch (tube) {
@@ -248,6 +190,9 @@ void tube_PowerOn(tube_pos_t tube)
   case Tube_E:
     TUBE_E_ON;
     break;
+  case Tube_All:
+    TUBE_ALL_ON;
+    break;
   
   default:
     break;
@@ -269,10 +214,41 @@ void tube_PowerOff(tube_pos_t tube)
   case Tube_E:
     TUBE_E_OFF;
     break;
+  case Tube_All:
+    TUBE_ALL_OFF;
+    break;
+  
+  default:
+    break;
+  }
+}
+
+void tube_BrightLevel(tube_pos_t tube, uint8_t bright)
+{
+  switch (tube) {
+  case Tube_A:
+    TUBE_A_BRIGHT(bright);
+    break;
+  case Tube_B:
+    TUBE_B_BRIGHT(bright);
+    break;
+  case Tube_C:
+    TUBE_C_BRIGHT(bright);
+    break;
+  case Tube_D:
+    TUBE_D_BRIGHT(bright);
+    break;
+  case Tube_E:
+    TUBE_E_BRIGHT(bright);
+    break;
+  case Tube_All:
+    TUBES_BRIGHT(bright);
+    break;
   
   default:
     break;
   }
+
 }
 
 /**

+ 92 - 18
Src/clock.c

@@ -18,11 +18,14 @@ static btn_t Button[BTN_NUM] = {
   {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 Color_RGB(const uint8_t r, const uint8_t g, const uint8_t b);
+static void check_DayNight(void);
 static void MinusFadeIn(void);
 static void MinusFadeOut(void);
 static void PlusFadeIn(void);
@@ -32,17 +35,27 @@ 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 uint8_t hue, const uint8_t sat, const uint8_t val);
 
 /* funcions */
-/**
-  * @brief  Вывод HEX значений цвета в таймер.
-  * @param  : RGB value in range 0x00-0xFF
-  * @retval : None
-  */
-static void Color_RGB(uint8_t r, uint8_t g, uint8_t b) {
-  COLOR_R(r);
-  COLOR_G(g);
-  COLOR_B(b);
+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);
 }
 
 /**
@@ -82,9 +95,14 @@ void btnProcess(void) {
 }
 
 void new_Second(void) {
-//  Blink_Start(); // !!! TODO
   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) {
@@ -92,6 +110,7 @@ void new_Second(void) {
     }
   }
 }
+
 /**
   * On/off symbols on IN-15 tube.
   */
@@ -279,6 +298,62 @@ static void IN15_FadeOut(void) {
   }
 }
 
+/**
+ * @brief  HSV to RGB convertion
+ * @param  hue: 0-59, sat: 0-255, val (lightness): 0-255
+ * @return none. RGB value out direct to LED.
+ */
+static void 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.
   */
@@ -287,7 +362,7 @@ void showTime(void) {
   RTOS_SetTask(MinusFadeOut, 500, 0);
 
   uint8_t hue = bcd2bin(Clock.Sec);
-  HSV2LED(hue, 255, cie[Lvl_Mdl]);
+  HSV2LED(hue, 255, BrightLevel);
 
   tube4_t buf;
   buf.s8.tA = Clock.Hr >> 4;
@@ -302,7 +377,7 @@ void showMMSS(void) {
   IN15_Minus;
 
   uint8_t hue = bcd2bin(Clock.Sec);
-  HSV2LED(hue, 255, cie[Lvl_Mdl]);
+  HSV2LED(hue, 255, BrightLevel);
 
   tube4_t buf;
   buf.s8.tA = Clock.Min >> 4;
@@ -350,6 +425,7 @@ void showYear(void) {
 
 void showHumidity(void) {
   dispWDT = DISP_WDT_TIME;
+  HSV2LED(COLOUR_BLUE, 255, BrightLevel);
   //in15Percent();
   symToFade = sym_Percent;
   IN15_FadeIn();
@@ -365,6 +441,7 @@ void showHumidity(void) {
 
 void showTemperature(void) {
   dispWDT = DISP_WDT_TIME;
+  HSV2LED(COLOUR_RED, 255, BrightLevel);
   //in15Plus();
   symToFade = sym_Plus;
   IN15_FadeIn();
@@ -380,7 +457,7 @@ void showTemperature(void) {
 
 void showPressure(void) {
   dispWDT = DISP_WDT_TIME;
-  HSV2LED(20, 255, cie[Lvl_Mdl]); // GREEN
+  HSV2LED(COLOUR_GREEN, 255, cie[Lvl_Mdl]); // GREEN
   //in15P();
   symToFade = sym_Pressure;
   IN15_FadeIn();
@@ -399,13 +476,10 @@ void showPressure(void) {
 /* Simple function for cyclic show all sensor data */
 void showSensorData(void) {
   RTOS_DeleteTask(MinusFadeOut);
-  //HSV2LED(1, 255, cie[Lvl_Mdl]); // Nixie color
 
-  HSV2LED(0, 255, cie[Lvl_Mdl]); // RED
   showTemperature();
   tdelay_ms(3000);
 
-  HSV2LED(40, 255, cie[Lvl_Mdl]); // BLUE
   showHumidity();
   tdelay_ms(3000);
 
@@ -430,7 +504,7 @@ void setTimeShow(void) {
 void setTimeBegin(void) {
 	dispWDT = DISP_WDT_TIME;
   in15Minus();
-  HSV2LED(1, 255, cie[Lvl_2]);
+  HSV2LED(COLOUR_NIXIE, 255, BrightLevel);
   RTOS_SetTask(btnProcess, BTN_TIME_HOLDED, BTN_SCAN_PERIOD);
   RTC_ReadAll(&setClock);
 }

+ 3 - 5
Src/main.c

@@ -49,10 +49,8 @@ int main(void)
   RTC_Init();
   sensor_Init();
 
-  es_event_t event = eventNull;
-
-  RTC_ReadAll(&Clock);
-  showTime();
+  /* Init Clock module */
+  Clock_Init();
 
   /** Set tasks for Sheduler */
   RTOS_SetTask(btnProcess, 1, BTN_SCAN_PERIOD);
@@ -66,7 +64,7 @@ int main(void)
       new_Second();
     } /* end of New second */
 
-    event = ES_GetEvent();
+    es_event_t event = ES_GetEvent();
     if (event) {
       ES_Dispatch(event);
     }