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,16 +7,13 @@
#define DISPLAY_ADDRESS 0x70
#define BRIGHTNESS 1
namespace Display {
uint8_t brightness = BRIGHTNESS;
// Create display object
Adafruit_7segment clockDisplay = Adafruit_7segment();
void setupDisplay() {
clockDisplay.begin(DISPLAY_ADDRESS);
clockDisplay.setBrightness(brightness);
}
void displayTime() {
int displayHour = hour();
int displayMinute = minute();
@ -58,3 +55,11 @@ void displayColon(bool on) {
void displayValue(int value) {
clockDisplay.print(value, HEX);
}
void setup() {
clockDisplay.begin(DISPLAY_ADDRESS);
clockDisplay.setBrightness(brightness);
displayTime();
displayColon(false);
}
}

View File

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

View File

@ -4,18 +4,17 @@
#include <WiFiUdp.h>
#include <Timezone.h>
namespace Ntp {
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org");
time_t timeAtStartup;
// Central European Time (Frankfurt, Paris)
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);
void setupTime() {
timeClient.begin();
}
time_t updateTime() {
if (timeClient.forceUpdate()) {
time_t newTime = CE.toLocal(timeClient.getEpochTime());
@ -25,3 +24,9 @@ time_t updateTime() {
return 0;
}
}
void setup() {
timeClient.begin();
timeAtStartup = updateTime();
}
}

View File

@ -1,6 +1,9 @@
#include <ArduinoOTA.h>
void setupOTA() {
namespace Ota
{
void setup() {
ArduinoOTA.onStart([]() {
Serial.println("Start");
});
@ -20,3 +23,8 @@ void setupOTA() {
});
ArduinoOTA.begin();
}
void loop() {
ArduinoOTA.handle();
}
}

View File

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