Added a TimeEffect to show a countdown to a time given via MQTT.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Fabian Schlenz 2019-12-21 14:21:31 +01:00
parent 994f4894dd
commit e2a56d7c29
8 changed files with 67 additions and 0 deletions

View File

@ -36,6 +36,7 @@
#define MQTT_TOPIC "pitrix/" // MQTT topic to listen to. Must not start with a slash, but must end with one. #define MQTT_TOPIC "pitrix/" // MQTT topic to listen to. Must not start with a slash, but must end with one.
#define MQTT_REPORT_METRICS // Whether to report metrics via MQTT. Disable if unwanted. #define MQTT_REPORT_METRICS // Whether to report metrics via MQTT. Disable if unwanted.
#define MQTT_TOPIC_WEATHER "accuweather/pitrix/" // MQTT topic to listen for weather data. Must not start with a slash, but must end with one. #define MQTT_TOPIC_WEATHER "accuweather/pitrix/" // MQTT topic to listen for weather data. Must not start with a slash, but must end with one.
#define MQTT_TOPIC_TIMER "alexa/timer"
#define HOSTNAME "pitrix-%08X" // Hostname of the ESP to use for OTA and MQTT client id. %08X will be replaced by the chip id. #define HOSTNAME "pitrix-%08X" // Hostname of the ESP to use for OTA and MQTT client id. %08X will be replaced by the chip id.
#define OTA_STARTUP_DELAY 10 // How many seconds to wait at startup. This is useful to prevent being unable to flash OTA by a bug in the code. Set to 0 to disable. #define OTA_STARTUP_DELAY 10 // How many seconds to wait at startup. This is useful to prevent being unable to flash OTA by a bug in the code. Set to 0 to disable.

16
include/effect_timer.h Normal file
View File

@ -0,0 +1,16 @@
#pragma once
#include "Effect.h"
#include "prototypes.h"
#include "my_fastled.h"
#include "Window.h"
class TimerEffect : public Effect {
protected:
Window* window = new Window(0, 0, LED_WIDTH, 6);
public:
~TimerEffect();
void loop(uint16_t ms);
String get_name() override { return "timer"; }
};

View File

@ -2,6 +2,7 @@
#include "Effect.h" #include "Effect.h"
#include "effect_clock.h" #include "effect_clock.h"
#include "effect_timer.h"
struct EffectEntry { struct EffectEntry {
const char* name; const char* name;
@ -13,6 +14,7 @@ extern const uint8_t effects_size;
extern Effect* current_effect; extern Effect* current_effect;
extern ClockEffect effect_clock; extern ClockEffect effect_clock;
extern TimerEffect effect_timer;
Effect* select_effect(char* name); Effect* select_effect(char* name);
Effect* select_effect(uint8_t id); Effect* select_effect(uint8_t id);

View File

@ -5,6 +5,7 @@
extern uint8_t baseHue; extern uint8_t baseHue;
extern char hostname[30]; extern char hostname[30];
extern uint16_t frame; extern uint16_t frame;
extern unsigned long timer;
typedef struct { typedef struct {
uint8_t width; uint8_t width;

37
src/effect_timer.cpp Normal file
View File

@ -0,0 +1,37 @@
#include "effect_timer.h"
#include <FastLED.h>
#include "functions.h"
#include "fonts.h"
#include "ntp.h"
void TimerEffect::loop(uint16_t ms) {
if (timer==0) return;
CRGB bg_color(0x000000);
CRGB fg_color(0xCCCCCC);
unsigned long now = ntpClient.getEpochTime() - NTP_OFFSET;
long diff = timer - now;
window->clear(&bg_color);
if (diff < 0 && (now & 1)==0) return;
if (diff < 0) diff = 0;
int hours = diff / 3600;
int minutes = diff / 60;
int seconds = diff % 60;
if (minutes > 99) {
seconds = minutes % 60;
minutes = hours;
}
//void drawChar(Font f, uint8_t x, uint8_t y, const char c, CRGB* color, bool mask=false);
window->drawChar(&font_numbers3x5, 0<<8, 0<<8, '0' + (minutes / 10), &fg_color);
window->drawChar(&font_numbers3x5, 4<<8, 0<<8, '0' + (minutes % 10), &fg_color);
window->drawChar(&font_numbers3x5, 9<<8, 0<<8, '0' + (seconds / 10), &fg_color);
window->drawChar(&font_numbers3x5, 13<<8, 0<<8, '0' + (seconds % 10), &fg_color);
if (now & 1) {
window->setPixel(7, 1, &fg_color);
window->setPixel(7, 3, &fg_color);
}
}
TimerEffect::~TimerEffect() {
delete window;
}

View File

@ -28,6 +28,7 @@
Effect* current_effect; Effect* current_effect;
ClockEffect effect_clock; ClockEffect effect_clock;
TimerEffect effect_timer;
// We're using 0 instead of false to get a better visual difference between true and false. // We're using 0 instead of false to get a better visual difference between true and false.
const EffectEntry effects[] = { const EffectEntry effects[] = {

View File

@ -50,6 +50,10 @@ void mqtt_callback(char* original_topic, byte* pl, unsigned int length) {
LOGln("Set weather_temperatures[%d] to value %d", id, val); LOGln("Set weather_temperatures[%d] to value %d", id, val);
} }
return; return;
} else if (topic.equals(MQTT_TOPIC_TIMER)) {
timer = payload.toInt();
LOGln("Set timer to %lu.", timer);
return;
} }
topic.remove(0, strlen(MQTT_TOPIC)); // Strip MQTT_TOPIC from the beginning topic.remove(0, strlen(MQTT_TOPIC)); // Strip MQTT_TOPIC from the beginning
@ -102,6 +106,7 @@ boolean mqtt_connect() {
mqtt_client.publish(MQTT_TOPIC "status", buffer, true); mqtt_client.publish(MQTT_TOPIC "status", buffer, true);
mqtt_client.subscribe(MQTT_TOPIC "+"); mqtt_client.subscribe(MQTT_TOPIC "+");
mqtt_client.subscribe(MQTT_TOPIC_WEATHER "#"); mqtt_client.subscribe(MQTT_TOPIC_WEATHER "#");
mqtt_client.subscribe(MQTT_TOPIC_TIMER);
} }
return mqtt_client.connected(); return mqtt_client.connected();
} }

View File

@ -19,6 +19,7 @@ uint8_t baseHue = 0; // defined as extern in prototypes.h
char hostname[30]; // defined as extern in prototypes.h char hostname[30]; // defined as extern in prototypes.h
uint16_t frame = 0; // defined as extern in prototypes.h uint16_t frame = 0; // defined as extern in prototypes.h
unsigned long _last_effect_loop_finished_at = 0; unsigned long _last_effect_loop_finished_at = 0;
unsigned long timer = 0;
#ifdef RECORDER_ENABLE #ifdef RECORDER_ENABLE
Recorder* recorder; Recorder* recorder;
#endif #endif
@ -113,6 +114,9 @@ void loop() {
if (current_effect->can_be_shown_with_clock()) { if (current_effect->can_be_shown_with_clock()) {
effect_clock.loop_with_invert(current_effect->clock_as_mask()); effect_clock.loop_with_invert(current_effect->clock_as_mask());
} }
effect_timer.loop(last_loop_ago);
FastLED.show(); FastLED.show();
http_server_send_framedata(); http_server_send_framedata();