refactor to use namespaces

This commit is contained in:
Nicu Hodos 2021-12-05 10:54:19 +01:00
parent 584e1ffc4e
commit eb29c2203d
5 changed files with 157 additions and 140 deletions

View File

@ -7,54 +7,59 @@
#define DISPLAY_ADDRESS 0x70 #define DISPLAY_ADDRESS 0x70
#define BRIGHTNESS 1 #define BRIGHTNESS 1
uint8_t brightness = BRIGHTNESS; namespace Display {
// Create display object uint8_t brightness = BRIGHTNESS;
Adafruit_7segment clockDisplay = Adafruit_7segment();
void setupDisplay() { // Create display object
clockDisplay.begin(DISPLAY_ADDRESS); Adafruit_7segment clockDisplay = Adafruit_7segment();
clockDisplay.setBrightness(brightness);
}
void displayTime() { void displayTime() {
int displayHour = hour(); int displayHour = hour();
int displayMinute = minute(); int displayMinute = minute();
int displayValue = displayHour*100 + displayMinute; int displayValue = displayHour * 100 + displayMinute;
// Print the time on the display // Print the time on the display
clockDisplay.print(displayValue, DEC); clockDisplay.print(displayValue, DEC);
// Add zero padding when in 24 hour mode and it's midnight. // Add zero padding when in 24 hour mode and it's midnight.
// In this case the print function above won't have leading 0's // In this case the print function above won't have leading 0's
// which can look confusing. Go in and explicitly add these zeros. // which can look confusing. Go in and explicitly add these zeros.
if (displayHour == 0) { if (displayHour == 0) {
clockDisplay.writeDigitNum(1, 0); clockDisplay.writeDigitNum(1, 0);
if (displayMinute < 10) { if (displayMinute < 10) {
clockDisplay.writeDigitNum(3, 0); clockDisplay.writeDigitNum(3, 0);
}
} }
} }
}
void changeBrightness(bool increase) { void changeBrightness(bool increase) {
increase ? brightness = (brightness+2) % 15 : brightness = (brightness-2) % 15; increase ? brightness = (brightness + 2) % 15 : brightness = (brightness - 2) % 15;
}
void adjustBrightness() {
int currentHour = hour();
if (currentHour > 8 && currentHour < 17) {
brightness = 11;
} else {
brightness = 1;
} }
}
void displayColon(bool on) { void adjustBrightness() {
int currentHour = hour();
if (currentHour > 8 && currentHour < 17) {
brightness = 11;
} else {
brightness = 1;
}
}
void displayColon(bool on) {
clockDisplay.setBrightness(brightness); clockDisplay.setBrightness(brightness);
clockDisplay.drawColon(on); clockDisplay.drawColon(on);
clockDisplay.writeDisplay(); clockDisplay.writeDisplay();
} }
void displayValue(int value) { void displayValue(int value) {
clockDisplay.print(value, HEX); clockDisplay.print(value, HEX);
} }
void setup() {
clockDisplay.begin(DISPLAY_ADDRESS);
clockDisplay.setBrightness(brightness);
displayTime();
displayColon(false);
}
}

View File

@ -10,52 +10,55 @@
#define MQTT_HOST IPAddress(192, 168, 5, 138) #define MQTT_HOST IPAddress(192, 168, 5, 138)
#define MQTT_PORT 1883 #define MQTT_PORT 1883
IRrecv irrecv(IR_INPUT_PIN); namespace Ir {
decode_results results;
AsyncMqttClient mqttClient; IRrecv irrecv(IR_INPUT_PIN);
decode_results results;
std::queue<uint8_t> irCommands; AsyncMqttClient mqttClient;
uint8_t lastIrCommand = 0x9F;
void setupIr() { std::queue<uint8_t> commands;
mqttClient.setServer(MQTT_HOST, MQTT_PORT); uint8_t lastCommand = 0x9F;
Serial.println("Connecting to MQTT...");
mqttClient.connect();
irrecv.enableIRIn(); // Start the receiver void setup() {
} mqttClient.setServer(MQTT_HOST, MQTT_PORT);
Serial.println("Connecting to MQTT...");
mqttClient.connect();
void sendCommand() { irrecv.enableIRIn(); // Start the receiver
if (!irCommands.empty() && mqttClient.connected()) { }
char message[32];
sprintf(message, "%X", irCommands.front()); void publishCommand() {
if (mqttClient.publish("esp_clock/sensor/ir/value", 0, true, message) != 0) { if (!commands.empty() && mqttClient.connected()) {
Serial.print(irCommands.front(), HEX); char message[32];
Serial.println(); sprintf(message, "%X", commands.front());
irCommands.pop(); if (mqttClient.publish("esp_clock/sensor/ir/value", 0, true, message) != 0) {
Serial.print(commands.front(), HEX);
Serial.println();
commands.pop();
}
} }
} }
}
bool readIrCommand() { bool readCommand() {
bool newCommand = false; bool newCommand = false;
if (irrecv.decode(&results)) { if (irrecv.decode(&results)) {
if (results.decode_type == NEC) { if (results.decode_type == NEC) {
Serial.print(F(" C=0x")); Serial.print(F(" C=0x"));
Serial.print(results.command, HEX); Serial.print(results.command, HEX);
Serial.println(); Serial.println();
lastIrCommand = results.command; lastCommand = results.command;
irCommands.push(results.command); commands.push(results.command);
newCommand = true; newCommand = true;
}
irrecv.resume(); // Receive the next value
} }
irrecv.resume(); // Receive the next value return newCommand;
} }
return newCommand;
}
uint8_t getCurrentCommand() { uint8_t getCurrentCommand() {
return irCommands.empty() ? 0 : irCommands.front(); return commands.empty() ? 0 : commands.front();
}
} }
#endif #endif

View File

@ -4,24 +4,29 @@
#include <WiFiUdp.h> #include <WiFiUdp.h>
#include <Timezone.h> #include <Timezone.h>
WiFiUDP ntpUDP; namespace Ntp {
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org");
// Central European Time (Frankfurt, Paris) WiFiUDP ntpUDP;
TimeChangeRule CEST = {"CEST", Last, Sun, Mar, 2, 120}; // Central European Summer Time NTPClient timeClient(ntpUDP, "europe.pool.ntp.org");
TimeChangeRule CET = {"CET ", Last, Sun, Oct, 3, 60}; // Central European Standard Time time_t timeAtStartup;
Timezone CE(CEST, CET);
void setupTime() { // Central European Time (Frankfurt, Paris)
timeClient.begin(); TimeChangeRule CEST = { "CEST", Last, Sun, Mar, 2, 120 }; // Central European Summer Time
} TimeChangeRule CET = { "CET ", Last, Sun, Oct, 3, 60 }; // Central European Standard Time
Timezone CE(CEST, CET);
time_t updateTime() { time_t updateTime() {
if (timeClient.forceUpdate()) { if (timeClient.forceUpdate()) {
time_t newTime = CE.toLocal(timeClient.getEpochTime()); time_t newTime = CE.toLocal(timeClient.getEpochTime());
setTime(newTime); setTime(newTime);
return newTime; return newTime;
} else { } else {
return 0; return 0;
}
} }
}
void setup() {
timeClient.begin();
timeAtStartup = updateTime();
}
}

View File

@ -1,22 +1,30 @@
#include <ArduinoOTA.h> #include <ArduinoOTA.h>
void setupOTA() { namespace Ota
ArduinoOTA.onStart([]() { {
Serial.println("Start");
}); void setup() {
ArduinoOTA.onEnd([]() { ArduinoOTA.onStart([]() {
Serial.println("\nEnd"); Serial.println("Start");
}); });
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { ArduinoOTA.onEnd([]() {
Serial.printf("Progress: %u%%\r", (progress / (total / 100))); Serial.println("\nEnd");
}); });
ArduinoOTA.onError([](ota_error_t error) { ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Error[%u]: ", error); Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); });
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); ArduinoOTA.onError([](ota_error_t error) {
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); Serial.printf("Error[%u]: ", error);
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed"); else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
}); else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
ArduinoOTA.begin(); else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
}
void loop() {
ArduinoOTA.handle();
}
} }

View File

@ -8,42 +8,38 @@
#define STAY_CONNECTED_AFTER_BOOT 5*60 #define STAY_CONNECTED_AFTER_BOOT 5*60
int currentHour = -1; int currentHour = -1;
time_t timeAtStartup;
bool avrOn = false; bool avrOn = false;
Wifi wifi; Wifi wifi;
void setup() { void setup() {
setupDisplay(); Display::setup();
displayTime();
displayColon(false);
Serial.begin(9600); // Start the serial console Serial.begin(9600);
wifi.setup(); wifi.setup();
setupOTA(); Ota::setup();
setupTime(); Ntp::setup();
timeAtStartup = updateTime(); Display::adjustBrightness();
adjustBrightness();
#if IR #if IR
setupIr(); Ir::setup();
#endif #endif
} }
void loop() { void loop() {
#if IR #if IR
if (readIrCommand()) { if (Ir::readCommand()) {
displayValue(lastIrCommand); Display::displayValue(Ir::lastCommand);
displayColon(false); Display::displayColon(false);
delay(1000); delay(1000);
switch (lastIrCommand) switch (Ir::lastCommand)
{ {
case 0x9F: case 0x9F:
avrOn = false; avrOn = false;
timeAtStartup = now(); Ntp::timeAtStartup = now();
break; break;
case 0xC4: case 0xC4:
case 0xD0: case 0xD0:
@ -52,41 +48,41 @@ void loop() {
if (WiFi.status() == WL_DISCONNECTED) { if (WiFi.status() == WL_DISCONNECTED) {
wifi.reconnect(); wifi.reconnect();
// connect on wifi connected // connect on wifi connected
mqttClient.connect(); Ir::mqttClient.connect();
} }
break; break;
default: default:
break; break;
} }
} }
if (!avrOn && getCurrentCommand() == 0xC7) { if (!avrOn && Ir::getCurrentCommand() == 0xC7) {
changeBrightness(true); Display::changeBrightness(true);
irCommands.pop(); Ir::commands.pop();
} }
if (!avrOn && getCurrentCommand() == 0xC8) { if (!avrOn && Ir::getCurrentCommand() == 0xC8) {
changeBrightness(false); Display::changeBrightness(false);
irCommands.pop(); Ir::commands.pop();
} }
sendCommand(); Ir::publishCommand();
#endif #endif
if ((currentHour != hour())) { if ((currentHour != hour())) {
adjustBrightness(); Display::adjustBrightness();
wifi.reconnect(); wifi.reconnect();
wifi.printStatus(); wifi.printStatus();
if (WiFi.status() == WL_CONNECTED) { if (WiFi.status() == WL_CONNECTED) {
if (time_t newTime = updateTime()) Serial.println(asctime(localtime(&newTime))); if (time_t newTime = Ntp::updateTime()) Serial.println(asctime(localtime(&newTime)));
} }
currentHour = hour(); currentHour = hour();
} }
if (WiFi.status() == WL_CONNECTED) { if (WiFi.status() == WL_CONNECTED) {
ArduinoOTA.handle(); Ota::loop();
if ((difftime(now(), timeAtStartup) > STAY_CONNECTED_AFTER_BOOT) && !avrOn) { if ((difftime(now(), Ntp::timeAtStartup) > STAY_CONNECTED_AFTER_BOOT) && !avrOn) {
wifi.disconnect(); wifi.disconnect();
} }
} }
displayTime(); Display::displayTime();
displayColon(true); Display::displayColon(true);
delay(500); delay(500);
displayColon(false); Display::displayColon(false);
delay(500); delay(500);
} }