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