ilyagin
17
#include <EEPROMex.h>
#define OLED
#define EMULATION
#define OPTRON
#ifdef OLED
#include <Adafruit_SSD1306.h>
const uint8_t oled_width = 128;
const uint8_t oled_height = 32;
const uint8_t oled_address = 0x3C;
const int8_t oled_rst = -1;
const int16_t x_cursor_line1 = 0;
const int16_t y_cursor_line1 = 0;
const int16_t x_cursor_line2 = 0;
const int16_t y_cursor_line2 = 16;
Adafruit_SSD1306 display(oled_width, oled_height, &Wire, oled_rst);
#endif
const int password_value = 37;
const int8_t event_pin = 7;
const int8_t rst_pin = 4;
const int eeprom_address = 0;
const unsigned long debounce_delay = 100;
const unsigned long btn_press_interval = 200;
#ifdef EMULATION
const unsigned long event_min_duration = 1300;
#else
const unsigned long event_min_duration = 300000;//Минимальная длительность учитываемого события 5 min(ms)
#endif
const unsigned long long_press_duration = 10000;
const unsigned long master_mode_timeout = 2000;
uint32_t events_count = 0;
int last_switch_state = LOW;
int last_rst_state = LOW;
int last_master_state = LOW;
#ifdef OPTRON
const int event_state = LOW;
#else
const int event_state = HIGH;
#endif
void setup() {
pinMode (event_pin, INPUT_PULLUP);
pinMode (rst_pin, INPUT_PULLUP);
display.begin(SSD1306_SWITCHCAPVCC, oled_address);
user_mode();
}
void loop() {
rst_btn_listener();
event_listener();
}
void event_listener() {
int switch_state = digitalRead(event_pin);
if (switch_state != last_switch_state) {
unsigned long switch_change_started = millis();
while (digitalRead(event_pin) == event_state) {
if ((millis() - switch_change_started) > event_min_duration) {
increment();
#ifdef EMULATION
user_mode(); //выведение на дисплей после каждого события
#endif
break;
}
}
}
delay(debounce_delay);
last_switch_state = switch_state;
}
void rst_btn_listener() {
int rst_state = digitalRead(rst_pin);
if (rst_state != last_rst_state) {
unsigned long press_btn_started = millis();
while (digitalRead(rst_pin) == LOW) {
if ((millis() - press_btn_started) > long_press_duration) {
master_mode();
break;
}
}
}
delay(debounce_delay);
last_rst_state = rst_state;
}
void user_mode() {
events_count = read();
// display_text("USER MODE");
display_num(events_count);
display.display();
}
void display_text(String text) {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(x_cursor_line1, y_cursor_line1);
display.print(text);
}
void display_num(uint32_t num) {
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(x_cursor_line2, y_cursor_line2);
display.print(num);
}
void rst_mode(byte btn_pressed_counter) {
display_text("MASTER MODE");
display_num(btn_pressed_counter);
display.display();
delay(btn_press_interval);
}
void master_mode() {
int btn_pressed_counter = 0;
unsigned long last_btn_pressed_time = millis();
rst_mode(btn_pressed_counter);
while (btn_pressed_counter != password_value) {
if ((millis() - last_btn_pressed_time) > master_mode_timeout)
break;
if (rst_btn_toggled()) {
last_btn_pressed_time = millis();
btn_pressed_counter++;
rst_mode(btn_pressed_counter);
}
}
if (btn_pressed_counter == password_value) {
display_text("MEMORY RESET");
display.display();
delay(master_mode_timeout);
clear();
}
delay(master_mode_timeout);
user_mode();
}
bool rst_btn_toggled() {
bool toggled = false;
int rst_state = digitalRead(rst_pin);
if (rst_state != last_master_state) {
unsigned long press_btn_started = millis();
if (rst_state == LOW)
toggled = true;
}
last_master_state = rst_state;
return toggled;
}
void increment() {
events_count = read();
events_count += 1;
write(events_count);
}
void write(uint32_t data) {
EEPROM.writeLong(eeprom_address, data);
}
uint32_t read() {
return EEPROM.readLong(eeprom_address);
}
void clear() {
EEPROM.writeLong(eeprom_address, 0);
}