refactor to use namespaces
This commit is contained in:
parent
584e1ffc4e
commit
eb29c2203d
@ -7,20 +7,17 @@
|
|||||||
#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);
|
||||||
@ -34,27 +31,35 @@ void displayTime() {
|
|||||||
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() {
|
void adjustBrightness() {
|
||||||
int currentHour = hour();
|
int currentHour = hour();
|
||||||
if (currentHour > 8 && currentHour < 17) {
|
if (currentHour > 8 && currentHour < 17) {
|
||||||
brightness = 11;
|
brightness = 11;
|
||||||
} else {
|
} else {
|
||||||
brightness = 1;
|
brightness = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void displayColon(bool on) {
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
41
include/ir.h
41
include/ir.h
@ -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;
|
||||||
|
uint8_t lastCommand = 0x9F;
|
||||||
|
|
||||||
|
void setup() {
|
||||||
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
|
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
|
||||||
Serial.println("Connecting to MQTT...");
|
Serial.println("Connecting to MQTT...");
|
||||||
mqttClient.connect();
|
mqttClient.connect();
|
||||||
|
|
||||||
irrecv.enableIRIn(); // Start the receiver
|
irrecv.enableIRIn(); // Start the receiver
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendCommand() {
|
void publishCommand() {
|
||||||
if (!irCommands.empty() && mqttClient.connected()) {
|
if (!commands.empty() && mqttClient.connected()) {
|
||||||
char message[32];
|
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) {
|
if (mqttClient.publish("esp_clock/sensor/ir/value", 0, true, message) != 0) {
|
||||||
Serial.print(irCommands.front(), HEX);
|
Serial.print(commands.front(), HEX);
|
||||||
Serial.println();
|
Serial.println();
|
||||||
irCommands.pop();
|
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
|
||||||
@ -4,19 +4,18 @@
|
|||||||
#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);
|
||||||
@ -24,4 +23,10 @@ time_t updateTime() {
|
|||||||
} else {
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
timeClient.begin();
|
||||||
|
timeAtStartup = updateTime();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -1,6 +1,9 @@
|
|||||||
#include <ArduinoOTA.h>
|
#include <ArduinoOTA.h>
|
||||||
|
|
||||||
void setupOTA() {
|
namespace Ota
|
||||||
|
{
|
||||||
|
|
||||||
|
void setup() {
|
||||||
ArduinoOTA.onStart([]() {
|
ArduinoOTA.onStart([]() {
|
||||||
Serial.println("Start");
|
Serial.println("Start");
|
||||||
});
|
});
|
||||||
@ -19,4 +22,9 @@ void setupOTA() {
|
|||||||
else if (error == OTA_END_ERROR) Serial.println("End Failed");
|
else if (error == OTA_END_ERROR) Serial.println("End Failed");
|
||||||
});
|
});
|
||||||
ArduinoOTA.begin();
|
ArduinoOTA.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
ArduinoOTA.handle();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user