Selaa lähdekoodia

переделал управление яркостью.

Vladimir Shilov 6 vuotta sitten
vanhempi
commit
0e855eb949
2 muutettua tiedostoa jossa 39 lisäystä ja 34 poistoa
  1. 0 5
      inc/main.h
  2. 39 29
      src/main.c

+ 0 - 5
inc/main.h

@@ -2,8 +2,6 @@
 #ifndef _MAIN_H
 #define _MAIN_H
 
-#include "event-system.h"
-
 //#define F_CPU          16000000UL
 
 /**
@@ -63,9 +61,6 @@
 #define ANOD4           _BV(PD7)
 #define ANOD_PINS       (ANOD1 | ANOD2 | ANOD3 | ANOD4)
 
-/* Display timeout, sec */
-#define DISP_WDT_TIME   10
-
 
 typedef struct {
   uint8_t     time;

+ 39 - 29
src/main.c

@@ -3,7 +3,7 @@
  * Vladimir N. Shilov <shilow@ukr.net>
  * 2019.01.18
  */
-
+/* Compiler libs */
 #include <stdbool.h>
 #include <stdint.h>
 #include <stdlib.h>
@@ -11,22 +11,30 @@
 #include <avr/interrupt.h>
 #include <avr/sleep.h>
 #include <avr/eeprom.h>
+#include <avr/pgmspace.h>
 
-#include "main.h"
+/* Project libs */
 #include "i2c.h"
 #include "ds3231.h"
 #include "rtos.h"
 #include "event-system.h"
+#include "main.h"
 
 /* Defines */
 #define TIMER2_HZ         252
 #define TIMER2_PRESCALER  1024
 #define TIMER2_CS         (1<<CS22 | 1<<CS21 | 1<<CS20)
 #define TIMER2_CNT        (0x100 - (F_CPU / TIMER2_PRESCALER / TIMER2_HZ))
-#define OCR2_MAX          0xFF
+
+/* Lamp brightness */
+#define BRIGHT_IDX_MAX    9
 #define FULL_BRIGHT_ON    0x06
 #define FULL_BRIGHT_OFF   0x22
 
+/* Display timeout, sec */
+#define DISP_WDT_TIME   10
+
+
 /* Variables */
 static volatile uint8_t Digit[4] = {1, 2, 3, 4};
 static rtc_t RTC, setRTC;
@@ -46,11 +54,15 @@ static btn_t Button[BTN_NUM] = {
   {0, evBTN3Pressed, evBTN3Pressed, BUTTON3_PIN}
 };
 static volatile uint8_t DISP_WDT = 0;
-static EEMEM uint8_t EEP_Bright;
-static uint8_t Bright;
-static EEMEM uint8_t EEP_SunTime;
+static EEMEM uint8_t EEP_BrightIdx;
+static uint8_t brightIdx;
+static EEMEM uint8_t EEP_SummerTime;
 
 /* Constants */
+static const uint8_t PROGMEM brightConv[10] = {
+  195, 196, 197, 199, 203, 208, 216, 226, 239, 255
+};
+
 /* Function prototypes */
 static void Board_Init(void);
 static void dotOn(void);
@@ -58,7 +70,7 @@ static void btnProcess(void);
 static void valIncrease(uint8_t * val, uint8_t max);
 static void valDecrease(uint8_t * val, uint8_t max);
 static void blink(void);
-static void setSunWinterTime(void);
+static void setSummerWinterTime(void);
 
 void main(void) {
   /**
@@ -74,7 +86,7 @@ void main(void) {
   Flag.blinkC = 0;
   Flag.saveEEP = 0;
 
-  Bright = eeprom_read_byte(&EEP_Bright);
+  brightIdx = eeprom_read_byte(&EEP_BrightIdx);
 
   /**
    * Инициализация, настройка...
@@ -110,12 +122,12 @@ void main(void) {
           ES_PlaceEvent(evRefreshCal);
         }
         if (RTC.Hr >= FULL_BRIGHT_ON && RTC.Hr < FULL_BRIGHT_OFF) {
-          OCR2 = OCR2_MAX;
+          OCR2 = pgm_read_byte(&brightConv[BRIGHT_IDX_MAX]);
         } else {
-          OCR2 = Bright;
+          OCR2 = pgm_read_byte(&brightConv[brightIdx]);
         }
 
-        setSunWinterTime();
+        setSummerWinterTime();
       } // begin new hour
 
       if (DISP_WDT != 0) {
@@ -130,7 +142,7 @@ void main(void) {
 
           if (Flag.saveEEP != 0) {
             Flag.saveEEP = 0;
-            eeprom_update_byte(&EEP_Bright, Bright);
+            eeprom_update_byte(&EEP_BrightIdx, brightIdx);
           }
         }
       }
@@ -171,7 +183,7 @@ static void Board_Init(void) {
   TCCR2 = TIMER2_CS;
   TCNT2 = TIMER2_CNT;
   TIMSK = _BV(TOIE2) | _BV(OCIE2);
-  OCR2 = OCR2_MAX;
+  OCR2 = pgm_read_byte(&brightConv[BRIGHT_IDX_MAX]);
 
   /* Interrupt from RTC */
   MCUCR = _BV(ISC11); // falling edge
@@ -184,8 +196,8 @@ static void Board_Init(void) {
 /**
  * @brief Correct current time for Sun or Winter
  */
-static void setSunWinterTime(void) {
-  uint8_t sunTime = eeprom_read_byte(&EEP_SunTime);
+static void setSummerWinterTime(void) {
+  uint8_t sunTime = eeprom_read_byte(&EEP_SummerTime);
 
   /* Переход на летнее время */
   if ((RTC.Mon == 3) && (RTC.WD == 7) && (RTC.Hr == 3) && (sunTime != 0)) {
@@ -205,7 +217,7 @@ static void setSunWinterTime(void) {
     }
   }
 
-  eeprom_update_byte(&EEP_SunTime, sunTime);
+  eeprom_update_byte(&EEP_SummerTime, sunTime);
 }
 
 static void dotOn(void) {
@@ -308,13 +320,11 @@ void showYear(void) {
 }
 
 void showBright(void) {
-  uint8_t a = Bright / 100;
-  uint8_t b = Bright - (a * 100);
   DISP_WDT = DISP_WDT_TIME;
   Digit[0] = DIGIT_BLANK;
-  Digit[1] = a;
-  Digit[2] = b / 10;
-  Digit[3] = b % 10;
+  Digit[1] = DIGIT_BLANK;
+  Digit[2] = brightIdx;
+  Digit[3] = DIGIT_BLANK;
 }
 
 void incWDay(void) {
@@ -372,22 +382,22 @@ void decYear(void) {
 }
 
 void incBright(void) {
-  if (Bright < 0xFF) {
-    Bright ++;
+  if (brightIdx < BRIGHT_IDX_MAX) {
+    brightIdx ++;
   } else {
-    Bright = TIMER2_CNT;
+    brightIdx = 0;
   }
-  OCR2 = Bright;
+  OCR2 = pgm_read_byte(&brightConv[brightIdx]);
   Flag.saveEEP = 1;
 }
 
 void decBright(void) {
-  if (Bright > TIMER2_CNT) {
-    Bright --;
+  if (brightIdx > TIMER2_CNT) {
+    brightIdx --;
   } else {
-    Bright = 0xFF;
+    brightIdx = 0xFF;
   }
-  OCR2 = Bright;
+  OCR2 = pgm_read_byte(&brightConv[brightIdx]);
   Flag.saveEEP = 1;
 }