refactor display
This commit is contained in:
parent
167606a19e
commit
9e118cc1b9
@ -1,3 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <Adafruit_LEDBackpack.h> // Support for the Backpack FeatherWing
|
||||
#include <Adafruit_GFX.h> // Adafruit's graphics library
|
||||
#include <Adafruit_I2CDevice.h>
|
||||
@ -5,10 +7,14 @@
|
||||
#include "ntp_time.h"
|
||||
|
||||
#define DISPLAY_ADDRESS 0x70
|
||||
#define BRIGHTNESS 1
|
||||
#define BRIGHTNESS 0
|
||||
#define BRIGHTNESS_STEP 1
|
||||
|
||||
namespace Display {
|
||||
|
||||
void displayColon();
|
||||
Task tDisplay(500, TASK_FOREVER, Display::displayColon, &ts, true);
|
||||
|
||||
uint8_t brightness = BRIGHTNESS;
|
||||
int currentHour = -1;
|
||||
int currentMin = -1;
|
||||
@ -16,7 +22,7 @@ namespace Display {
|
||||
// Create display object
|
||||
Adafruit_7segment clockDisplay = Adafruit_7segment();
|
||||
|
||||
void displayTime() {
|
||||
void adjustTime() {
|
||||
int displayHour = hour();
|
||||
int displayMinute = minute();
|
||||
int displayValue = displayHour * 100 + displayMinute;
|
||||
@ -36,7 +42,7 @@ namespace Display {
|
||||
}
|
||||
|
||||
void changeBrightness(bool increase) {
|
||||
increase ? brightness = (brightness + 2) % 15 : brightness = (brightness - 2) % 15;
|
||||
increase ? brightness = (brightness + BRIGHTNESS_STEP) % 15 : brightness = (brightness - BRIGHTNESS_STEP) % 15;
|
||||
clockDisplay.setBrightness(brightness);
|
||||
}
|
||||
|
||||
@ -44,7 +50,7 @@ namespace Display {
|
||||
if (currentHour > 8 && currentHour < 17) {
|
||||
brightness = 11;
|
||||
} else {
|
||||
brightness = 1;
|
||||
brightness = BRIGHTNESS;
|
||||
}
|
||||
clockDisplay.setBrightness(brightness);
|
||||
}
|
||||
@ -61,7 +67,7 @@ namespace Display {
|
||||
}
|
||||
if (currentMin != minute()) {
|
||||
currentMin = minute();
|
||||
displayTime();
|
||||
adjustTime();
|
||||
}
|
||||
}
|
||||
clockDisplay.drawColon(colonOn);
|
||||
@ -72,12 +78,13 @@ namespace Display {
|
||||
|
||||
void displayValue(int value) {
|
||||
clockDisplay.print(value, HEX);
|
||||
clockDisplay.writeDisplay();
|
||||
}
|
||||
|
||||
void setup() {
|
||||
clockDisplay.begin(DISPLAY_ADDRESS);
|
||||
clockDisplay.setBrightness(brightness);
|
||||
displayTime();
|
||||
adjustTime();
|
||||
displayColon();
|
||||
}
|
||||
}
|
||||
|
||||
17
include/ir.h
17
include/ir.h
@ -1,9 +1,9 @@
|
||||
#if IR
|
||||
|
||||
#include <queue>
|
||||
#include <AsyncMqttClient.h>
|
||||
#include <IRremoteESP8266.h>
|
||||
#include <IRrecv.h>
|
||||
#include "display.h"
|
||||
|
||||
#define IR_INPUT_PIN D6
|
||||
|
||||
@ -21,7 +21,7 @@ namespace Ir {
|
||||
bool readCommand() {
|
||||
bool newCommand = false;
|
||||
if (irrecv.decode(&results)) {
|
||||
if (results.decode_type == NEC) {
|
||||
if (results.decode_type == NEC && results.command != 0) {
|
||||
Serial.print(F(" C=0x"));
|
||||
Serial.print(results.command, HEX);
|
||||
Serial.println();
|
||||
@ -34,13 +34,20 @@ namespace Ir {
|
||||
return newCommand;
|
||||
}
|
||||
|
||||
void displayValue() {
|
||||
Display::displayValue(lastCommand);
|
||||
Display::tDisplay.setCallback(Display::displayColon);
|
||||
Display::adjustTime();
|
||||
}
|
||||
|
||||
void setup() {
|
||||
irrecv.enableIRIn(); // Start the receiver
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (readCommand()) {
|
||||
Display::displayValue(lastCommand);
|
||||
Display::tDisplay.setCallback(displayValue);
|
||||
Display::tDisplay.forceNextIteration();
|
||||
switch (lastCommand)
|
||||
{
|
||||
case 0x9F:
|
||||
@ -56,11 +63,11 @@ namespace Ir {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!avrOn && Mqtt::getCurrentCommand() == 0xC7) {
|
||||
if (!avrOn && Mqtt::commands.getCurrent() == 0xC7) {
|
||||
Display::changeBrightness(true);
|
||||
Mqtt::commands.pop();
|
||||
}
|
||||
if (!avrOn && Mqtt::getCurrentCommand() == 0xC8) {
|
||||
if (!avrOn && Mqtt::commands.getCurrent() == 0xC8) {
|
||||
Display::changeBrightness(false);
|
||||
Mqtt::commands.pop();
|
||||
}
|
||||
|
||||
@ -11,30 +11,44 @@ namespace Mqtt {
|
||||
|
||||
AsyncMqttClient client;
|
||||
|
||||
std::queue<uint8_t> commands;
|
||||
struct {
|
||||
const char* topic = "esp_clock/sensor/ir/value";
|
||||
std::queue<uint8_t> queue;
|
||||
uint8_t getCurrent() {
|
||||
return queue.empty() ? 0 : queue.front();
|
||||
}
|
||||
void push(uint8_t el) {
|
||||
queue.push(el);
|
||||
}
|
||||
void pop() {
|
||||
queue.pop();
|
||||
}
|
||||
uint8_t front() {
|
||||
return queue.front();
|
||||
}
|
||||
} commands;
|
||||
|
||||
void publishCommand() {
|
||||
if (!commands.empty() && client.connected()) {
|
||||
if (!commands.queue.empty() && client.connected()) {
|
||||
char message[32];
|
||||
sprintf(message, "%X", commands.front());
|
||||
if (client.publish("esp_clock/sensor/ir/value", 0, true, message) != 0) {
|
||||
Serial.print(commands.front(), HEX);
|
||||
uint8_t cmd = commands.queue.front();
|
||||
sprintf(message, "%X", cmd);
|
||||
if (client.publish(commands.topic, 0, true, message) != 0) {
|
||||
Serial.print(cmd, HEX);
|
||||
Serial.println();
|
||||
commands.pop();
|
||||
commands.queue.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t getCurrentCommand() {
|
||||
return commands.empty() ? 0 : commands.front();
|
||||
}
|
||||
|
||||
void setup() {
|
||||
client.onConnect([](bool sessionPresent) {
|
||||
tPublish.enableDelayed();
|
||||
Serial.println("Connected to MQTT");
|
||||
});
|
||||
client.onDisconnect([](AsyncMqttClientDisconnectReason reason) {
|
||||
tPublish.disable();
|
||||
Serial.println("Disconnected from MQTT");
|
||||
});
|
||||
client.setServer(MQTT_HOST, MQTT_PORT);
|
||||
Serial.println("Connecting to MQTT...");
|
||||
|
||||
@ -21,14 +21,10 @@ lib_deps =
|
||||
ottowinter/AsyncMqttClient-esphome@^0.8.5
|
||||
crankyoldgit/IRremoteESP8266@^2.7.18
|
||||
arkhipenko/TaskScheduler@^3.4.0
|
||||
|
||||
[env:dev_mode]
|
||||
build_flags = -D IR=1
|
||||
|
||||
[env:laptop_home]
|
||||
build_flags = -D IR=0
|
||||
|
||||
[env:ota_home]
|
||||
build_flags = -D IR=0
|
||||
upload_port = 192.168.5.191
|
||||
upload_protocol = espota
|
||||
|
||||
@ -7,11 +7,11 @@ StatusRequest hourChanged;
|
||||
StatusRequest dayChanged;
|
||||
StatusRequest wifiConnected;
|
||||
|
||||
#include "display.h"
|
||||
#include "wifi.h"
|
||||
#include "ntp_time.h"
|
||||
#include "mqtt.h"
|
||||
#include "ota.h"
|
||||
#include "display.h"
|
||||
#include "ir.h"
|
||||
|
||||
#define STAY_CONNECTED_AFTER_BOOT 5*60
|
||||
@ -19,7 +19,6 @@ StatusRequest wifiConnected;
|
||||
void wifiConnectedCallback();
|
||||
void otaCallback();
|
||||
|
||||
Task tBlinkColon(500, TASK_FOREVER, Display::displayColon, &ts, true);
|
||||
Task tOta(TASK_IMMEDIATE, TASK_FOREVER, otaCallback, &ts);
|
||||
Task tWifiReconnect(Wifi::reconnect, &ts);
|
||||
Task tWifiConnected(wifiConnectedCallback, &ts);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user