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