diff --git a/include/display.h b/include/display.h index 322e4c1..19f287e 100644 --- a/include/display.h +++ b/include/display.h @@ -7,54 +7,59 @@ #define DISPLAY_ADDRESS 0x70 #define BRIGHTNESS 1 -uint8_t brightness = BRIGHTNESS; +namespace Display { -// Create display object -Adafruit_7segment clockDisplay = Adafruit_7segment(); + uint8_t brightness = BRIGHTNESS; -void setupDisplay() { - clockDisplay.begin(DISPLAY_ADDRESS); - clockDisplay.setBrightness(brightness); -} + // Create display object + Adafruit_7segment clockDisplay = Adafruit_7segment(); -void displayTime() { - int displayHour = hour(); - int displayMinute = minute(); - int displayValue = displayHour*100 + displayMinute; + void displayTime() { + int displayHour = hour(); + int displayMinute = minute(); + int displayValue = displayHour * 100 + displayMinute; - // Print the time on the display - clockDisplay.print(displayValue, DEC); + // Print the time on the display + clockDisplay.print(displayValue, DEC); - // 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 - // which can look confusing. Go in and explicitly add these zeros. - if (displayHour == 0) { - clockDisplay.writeDigitNum(1, 0); - if (displayMinute < 10) { - clockDisplay.writeDigitNum(3, 0); + // 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 + // which can look confusing. Go in and explicitly add these zeros. + if (displayHour == 0) { + clockDisplay.writeDigitNum(1, 0); + if (displayMinute < 10) { + clockDisplay.writeDigitNum(3, 0); + } } } -} -void changeBrightness(bool increase) { - 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 changeBrightness(bool increase) { + increase ? brightness = (brightness + 2) % 15 : brightness = (brightness - 2) % 15; } -} -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.drawColon(on); - clockDisplay.writeDisplay(); -} + clockDisplay.drawColon(on); + clockDisplay.writeDisplay(); + } -void displayValue(int value) { - clockDisplay.print(value, HEX); -} + void displayValue(int value) { + clockDisplay.print(value, HEX); + } + + void setup() { + clockDisplay.begin(DISPLAY_ADDRESS); + clockDisplay.setBrightness(brightness); + displayTime(); + displayColon(false); + } +} \ No newline at end of file diff --git a/include/ir.h b/include/ir.h index 999a89d..97a6056 100644 --- a/include/ir.h +++ b/include/ir.h @@ -10,52 +10,55 @@ #define MQTT_HOST IPAddress(192, 168, 5, 138) #define MQTT_PORT 1883 -IRrecv irrecv(IR_INPUT_PIN); -decode_results results; +namespace Ir { -AsyncMqttClient mqttClient; + IRrecv irrecv(IR_INPUT_PIN); + decode_results results; -std::queue irCommands; -uint8_t lastIrCommand = 0x9F; + AsyncMqttClient mqttClient; -void setupIr() { - mqttClient.setServer(MQTT_HOST, MQTT_PORT); - Serial.println("Connecting to MQTT..."); - mqttClient.connect(); + std::queue commands; + uint8_t lastCommand = 0x9F; - irrecv.enableIRIn(); // Start the receiver -} + void setup() { + mqttClient.setServer(MQTT_HOST, MQTT_PORT); + Serial.println("Connecting to MQTT..."); + mqttClient.connect(); -void sendCommand() { - if (!irCommands.empty() && mqttClient.connected()) { - char message[32]; - sprintf(message, "%X", irCommands.front()); - if (mqttClient.publish("esp_clock/sensor/ir/value", 0, true, message) != 0) { - Serial.print(irCommands.front(), HEX); - Serial.println(); - irCommands.pop(); + irrecv.enableIRIn(); // Start the receiver + } + + void publishCommand() { + if (!commands.empty() && mqttClient.connected()) { + char message[32]; + sprintf(message, "%X", commands.front()); + if (mqttClient.publish("esp_clock/sensor/ir/value", 0, true, message) != 0) { + Serial.print(commands.front(), HEX); + Serial.println(); + commands.pop(); + } } } -} -bool readIrCommand() { - 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); - newCommand = true; + 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(); + lastCommand = results.command; + commands.push(results.command); + newCommand = true; + } + irrecv.resume(); // Receive the next value } - irrecv.resume(); // Receive the next value + return newCommand; } - return newCommand; -} -uint8_t getCurrentCommand() { - return irCommands.empty() ? 0 : irCommands.front(); + uint8_t getCurrentCommand() { + return commands.empty() ? 0 : commands.front(); + } } #endif \ No newline at end of file diff --git a/include/ntp_time.h b/include/ntp_time.h index 6c85dad..3380cb1 100644 --- a/include/ntp_time.h +++ b/include/ntp_time.h @@ -4,24 +4,29 @@ #include #include -WiFiUDP ntpUDP; -NTPClient timeClient(ntpUDP, "europe.pool.ntp.org"); +namespace Ntp { -// 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); + WiFiUDP ntpUDP; + NTPClient timeClient(ntpUDP, "europe.pool.ntp.org"); + time_t timeAtStartup; -void setupTime() { - timeClient.begin(); -} + // 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); -time_t updateTime() { - if (timeClient.forceUpdate()) { - time_t newTime = CE.toLocal(timeClient.getEpochTime()); - setTime(newTime); - return newTime; - } else { - return 0; + time_t updateTime() { + if (timeClient.forceUpdate()) { + time_t newTime = CE.toLocal(timeClient.getEpochTime()); + setTime(newTime); + return newTime; + } else { + return 0; + } } -} + + void setup() { + timeClient.begin(); + timeAtStartup = updateTime(); + } +} \ No newline at end of file diff --git a/include/ota.h b/include/ota.h index 835b80d..69b736f 100644 --- a/include/ota.h +++ b/include/ota.h @@ -1,22 +1,30 @@ #include -void setupOTA() { - ArduinoOTA.onStart([]() { - Serial.println("Start"); - }); - ArduinoOTA.onEnd([]() { - Serial.println("\nEnd"); - }); - ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { - Serial.printf("Progress: %u%%\r", (progress / (total / 100))); - }); - ArduinoOTA.onError([](ota_error_t error) { - Serial.printf("Error[%u]: ", error); - if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); - else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); - else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); - else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); - else if (error == OTA_END_ERROR) Serial.println("End Failed"); - }); - ArduinoOTA.begin(); +namespace Ota +{ + + void setup() { + ArduinoOTA.onStart([]() { + Serial.println("Start"); + }); + ArduinoOTA.onEnd([]() { + Serial.println("\nEnd"); + }); + ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { + Serial.printf("Progress: %u%%\r", (progress / (total / 100))); + }); + ArduinoOTA.onError([](ota_error_t error) { + Serial.printf("Error[%u]: ", error); + if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); + else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); + else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); + 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(); + } } diff --git a/src/esp_clock.cpp b/src/esp_clock.cpp index 4a7e965..b282f88 100644 --- a/src/esp_clock.cpp +++ b/src/esp_clock.cpp @@ -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); }