Merge branch 'display-properties-refactor'
This commit is contained in:
commit
e2aea2a428
@ -37,13 +37,13 @@ namespace Devices {
|
|||||||
|
|
||||||
auto brightnessMqtt = Builder<Number>::instance(new Number{ "Brightness", "brightness",
|
auto brightnessMqtt = Builder<Number>::instance(new Number{ "Brightness", "brightness",
|
||||||
[](const char* msg) {
|
[](const char* msg) {
|
||||||
Display::Brightness::set(String{ msg }.toInt());
|
Display::brightness = String{ msg }.toInt();
|
||||||
}
|
}
|
||||||
}).withMin(BRIGHTNESS_MIN).withMax(BRIGHTNESS_MAX).withStep(BRIGHTNESS_STEP).restoreStateFromCommand().build();
|
}).withMin(Display::Brightness::MIN).withMax(Display::Brightness::MAX).withStep(1).restoreStateFromCommand().build();
|
||||||
|
|
||||||
auto hourFormatMqtt = Builder<Switch>::instance(new Switch{ "Format 24h", "format_24h",
|
auto hourFormatMqtt = Builder<Switch>::instance(new Switch{ "Format 24h", "format_24h",
|
||||||
[](const char* msg) {
|
[](const char* msg) {
|
||||||
strcmp("ON", msg) == 0 ? Display::changeHourFormat24(true) : Display::changeHourFormat24(false);
|
Display::hourFormat24 = (strcmp("ON", msg) == 0);
|
||||||
}
|
}
|
||||||
}).restoreStateFromCommand().build();
|
}).restoreStateFromCommand().build();
|
||||||
Number* displayTimerMqtt = Builder<Number>::instance(new Number{ "Timer duration", "timer_duration",
|
Number* displayTimerMqtt = Builder<Number>::instance(new Number{ "Timer duration", "timer_duration",
|
||||||
@ -131,14 +131,14 @@ namespace Devices {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Display::hourFormatChangedCallback = []{
|
Display::hourFormat24.registerCallback([]{
|
||||||
hourFormatMqtt->updateState(Display::hourFormat24);
|
hourFormatMqtt->updateState(Display::hourFormat24);
|
||||||
};
|
});
|
||||||
Display::Brightness::brightnessChangedCallback = []{
|
Display::brightness.registerCallback([]{
|
||||||
brightnessMqtt->updateState(Display::Brightness::current);
|
brightnessMqtt->updateState(Display::brightness);
|
||||||
};
|
});
|
||||||
Display::Timer::remainingCallback = [](int8 current){
|
Display::Timer::timer.registerCallback([](int8 current){
|
||||||
timerRemainingMqtt->updateState(to_string(current).c_str());
|
timerRemainingMqtt->updateState(to_string(current).c_str());
|
||||||
};
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -8,17 +8,23 @@
|
|||||||
#include "bme.h"
|
#include "bme.h"
|
||||||
|
|
||||||
#define DISPLAY_ADDRESS 0x70
|
#define DISPLAY_ADDRESS 0x70
|
||||||
#define BRIGHTNESS_MIN 0
|
|
||||||
#define BRIGHTNESS_MAX 15
|
|
||||||
#define BRIGHTNESS_STEP 1
|
|
||||||
#define BRIGHTNESS_NIGHT BRIGHTNESS_MIN
|
|
||||||
#define BRIGHTNESS_DAY 11
|
|
||||||
#define MILLISECONDS(value) value*TASK_MILLISECOND
|
#define MILLISECONDS(value) value*TASK_MILLISECOND
|
||||||
#define SECONDS(value) value*TASK_SECOND
|
#define SECONDS(value) value*TASK_SECOND
|
||||||
#define MINUTES(value) value*TASK_MINUTE
|
#define MINUTES(value) value*TASK_MINUTE
|
||||||
#define DISPLAY_SENSOR_ITERATIONS 2+1
|
#define DISPLAY_SENSOR_ITERATIONS 2+1
|
||||||
#define DISPLAY_DELAY (SECONDS(2))
|
#define DISPLAY_DELAY (SECONDS(2))
|
||||||
|
|
||||||
|
typedef void (*callback_t)();
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct CallbackAware {
|
||||||
|
void registerCallback(T f) {
|
||||||
|
callback = f;
|
||||||
|
}
|
||||||
|
protected:
|
||||||
|
T callback;
|
||||||
|
};
|
||||||
|
|
||||||
namespace Display {
|
namespace Display {
|
||||||
|
|
||||||
void displayTime();
|
void displayTime();
|
||||||
@ -56,35 +62,77 @@ namespace Display {
|
|||||||
tDisplayTime.enable();
|
tDisplayTime.enable();
|
||||||
});
|
});
|
||||||
|
|
||||||
bool hourFormat24 = false;
|
struct HourFormat : public CallbackAware<callback_t> {
|
||||||
void (*hourFormatChangedCallback)();
|
|
||||||
|
operator bool() {
|
||||||
|
return format24;
|
||||||
|
}
|
||||||
|
|
||||||
|
void operator=(bool value) {
|
||||||
|
format24 = value;
|
||||||
|
tDisplayTime.restart();
|
||||||
|
if (callback) callback();
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
bool format24 = false;
|
||||||
|
} hourFormat24;
|
||||||
|
|
||||||
// Create display object
|
// Create display object
|
||||||
Adafruit_7segment clockDisplay = Adafruit_7segment();
|
Adafruit_7segment clockDisplay = Adafruit_7segment();
|
||||||
|
|
||||||
namespace Brightness {
|
struct Brightness : public CallbackAware<callback_t> {
|
||||||
uint8_t current = BRIGHTNESS_NIGHT;
|
static constexpr uint8 MIN = 0;
|
||||||
void (*brightnessChangedCallback)();
|
static constexpr uint8 MAX = 15;
|
||||||
|
static constexpr uint8 DAY = 11;
|
||||||
|
static constexpr uint8 NIGHT = MIN;
|
||||||
|
|
||||||
void set(uint8_t value) {
|
void operator=(uint8 value) {
|
||||||
current = value % (BRIGHTNESS_MAX + 1);
|
current = value % (MAX + 1);
|
||||||
clockDisplay.setBrightness(current);
|
clockDisplay.setBrightness(current);
|
||||||
if (brightnessChangedCallback) brightnessChangedCallback();
|
if (callback) callback();
|
||||||
}
|
}
|
||||||
|
|
||||||
void change(bool increase) {
|
operator uint8() {
|
||||||
increase ? set(current + BRIGHTNESS_STEP) : set(current - BRIGHTNESS_STEP);
|
return current;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
private:
|
||||||
|
uint8 current = NIGHT;
|
||||||
|
} brightness;
|
||||||
|
|
||||||
namespace Timer {
|
namespace Timer {
|
||||||
int8 timer = 0, current = 0;
|
typedef void (*remaining_callback_t)(int8);
|
||||||
void (*remainingCallback)(int8);
|
struct : public CallbackAware<remaining_callback_t> {
|
||||||
|
|
||||||
|
void operator=(int8 value) {
|
||||||
|
initial = remaining = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
operator int8() {
|
||||||
|
return remaining;
|
||||||
|
}
|
||||||
|
|
||||||
|
void start() {
|
||||||
|
remaining = initial + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void decrease() {
|
||||||
|
remaining--;
|
||||||
|
if (callback) callback(remaining);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool atBeginning() {
|
||||||
|
return initial == remaining;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
int8 initial = 0, remaining = 0;
|
||||||
|
} timer;
|
||||||
|
|
||||||
Task tDisplayTimer(SECONDS(10), TASK_ONCE + 1,
|
Task tDisplayTimer(SECONDS(10), TASK_ONCE + 1,
|
||||||
[]{
|
[]{
|
||||||
if (current >= 0) {
|
if (timer >= 0) {
|
||||||
clockDisplay.print(current, DEC);
|
clockDisplay.print(timer, DEC);
|
||||||
clockDisplay.writeDisplay();
|
clockDisplay.writeDisplay();
|
||||||
}
|
}
|
||||||
}, &ts, false,
|
}, &ts, false,
|
||||||
@ -100,18 +148,17 @@ namespace Display {
|
|||||||
Task tTimer(MINUTES(1), TASK_FOREVER,
|
Task tTimer(MINUTES(1), TASK_FOREVER,
|
||||||
[]{
|
[]{
|
||||||
static constexpr uint8 threshold = 16;
|
static constexpr uint8 threshold = 16;
|
||||||
current--;
|
timer.decrease();
|
||||||
if (current == timer) {
|
if (timer.atBeginning()) {
|
||||||
if (timer <= threshold) tDisplayTimer.setIterations(TASK_FOREVER);
|
if (timer <= threshold) tDisplayTimer.setIterations(TASK_FOREVER);
|
||||||
tDisplayTimer.restart();
|
tDisplayTimer.restart();
|
||||||
} else if (current == threshold) {
|
} else if (timer == threshold) {
|
||||||
tDisplayTimer.setIterations(TASK_FOREVER);
|
tDisplayTimer.setIterations(TASK_FOREVER);
|
||||||
tDisplayTimer.restart();
|
tDisplayTimer.restart();
|
||||||
}
|
}
|
||||||
if (remainingCallback) remainingCallback(current);
|
|
||||||
}, &ts, false,
|
}, &ts, false,
|
||||||
[]{
|
[]{
|
||||||
current = timer+1;
|
timer.start();
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
[]{
|
[]{
|
||||||
@ -155,11 +202,11 @@ namespace Display {
|
|||||||
Ntp::tUpdateTime.restart();
|
Ntp::tUpdateTime.restart();
|
||||||
}
|
}
|
||||||
if (currentHour == 8) {
|
if (currentHour == 8) {
|
||||||
Brightness::set(BRIGHTNESS_DAY);
|
brightness = Brightness::DAY;
|
||||||
Wifi::tConnect.enable();
|
Wifi::tConnect.enable();
|
||||||
}
|
}
|
||||||
if (currentHour == 17) {
|
if (currentHour == 17) {
|
||||||
Brightness::set(BRIGHTNESS_NIGHT);
|
brightness = Brightness::NIGHT;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
static int currentMin = -1;
|
static int currentMin = -1;
|
||||||
@ -194,7 +241,7 @@ namespace Display {
|
|||||||
tDisplaySensor.setCallback(&displayTemp);
|
tDisplaySensor.setCallback(&displayTemp);
|
||||||
}
|
}
|
||||||
|
|
||||||
void displayValue(uint8_t value) {
|
void displayValue(uint8 value) {
|
||||||
tDisplayTime.disable();
|
tDisplayTime.disable();
|
||||||
clockDisplay.print(value, HEX);
|
clockDisplay.print(value, HEX);
|
||||||
clockDisplay.writeDisplay();
|
clockDisplay.writeDisplay();
|
||||||
@ -224,15 +271,9 @@ namespace Display {
|
|||||||
clockDisplay.writeDisplay();
|
clockDisplay.writeDisplay();
|
||||||
}
|
}
|
||||||
|
|
||||||
void changeHourFormat24(bool format24) {
|
|
||||||
hourFormat24 = format24;
|
|
||||||
tDisplayTime.restart();
|
|
||||||
if (hourFormatChangedCallback) hourFormatChangedCallback();
|
|
||||||
}
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
clockDisplay.begin(DISPLAY_ADDRESS);
|
clockDisplay.begin(DISPLAY_ADDRESS);
|
||||||
clockDisplay.setBrightness(Brightness::current);
|
clockDisplay.setBrightness(brightness);
|
||||||
tDisplayTime.enable();
|
tDisplayTime.enable();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user