esp-clock/include/display.h

134 lines
3.3 KiB
C++

#pragma once
#include <Adafruit_LEDBackpack.h> // Support for the Backpack FeatherWing
#include <Adafruit_GFX.h> // Adafruit's graphics library
#include <Adafruit_I2CDevice.h>
#include <SPI.h>
#include "ntp_time.h"
#include "mqtt.h"
#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 DISPLAY_TIME 2000
#define DISPLAY_TEMP_TIME 5000
namespace Mqtt {
void publishBrightness();
bool connected();
}
Task tPublishBrightness(TASK_IMMEDIATE, TASK_ONCE, Mqtt::publishBrightness, &ts, false, Mqtt::connected);
namespace Display {
void displayColon();
Task tDisplay(500, TASK_FOREVER, displayColon, &ts, true);
uint8_t brightness = BRIGHTNESS_NIGHT;
int currentHour = -1;
int currentMin = -1;
bool hourFormat24 = false;
// Create display object
Adafruit_7segment clockDisplay = Adafruit_7segment();
void drawTime() {
int displayHour = hourFormat24 ? hour() : hourFormat12();
int displayMinute = minute();
int displayValue = displayHour * 100 + displayMinute;
// Print the time on the display
clockDisplay.print(displayValue, DEC);
// Add zero padding when in 24 hour mode and it's midnight.
// In this case the print function above won't have leading 0's
// which can look confusing. Go in and explicitly add these zeros.
if (displayHour == 0) {
clockDisplay.writeDigitNum(1, 0);
if (displayMinute < 10) {
clockDisplay.writeDigitNum(3, 0);
}
}
}
void setBrightness(uint8_t value) {
brightness = value % (BRIGHTNESS_MAX+1);
clockDisplay.setBrightness(brightness);
tPublishBrightness.restart();
}
void changeBrightness(bool increase) {
increase ? setBrightness(brightness + BRIGHTNESS_STEP) : setBrightness(brightness - BRIGHTNESS_STEP);
}
void updateBrightness() {
if (currentHour > 8 && currentHour < 17) {
setBrightness(BRIGHTNESS_DAY);
} else {
setBrightness(BRIGHTNESS_NIGHT);
}
}
void drawColon(bool colonOn) {
if (colonOn) {
if (currentHour != hour()) {
currentHour = hour();
updateBrightness();
if (currentHour == 8) Wifi::reconnect();
}
if (currentMin != minute()) {
currentMin = minute();
drawTime();
}
}
clockDisplay.drawColon(colonOn);
}
void displayColon() {
static bool colonOn = false;
drawColon(colonOn);
clockDisplay.writeDisplay();
colonOn = !colonOn;
}
void displayTemp(float value) {
tDisplay.disable();
drawColon(false);
clockDisplay.printFloat(value, 1);
clockDisplay.writeDisplay();
drawTime();
tDisplay.enableDelayed(DISPLAY_TEMP_TIME);
}
void displayValue(uint8_t value) {
tDisplay.disable();
drawColon(false);
clockDisplay.print(value, HEX);
clockDisplay.writeDisplay();
drawTime();
tDisplay.enableDelayed(DISPLAY_TIME);
}
void displayText(const char c[]) {
tDisplay.disable();
drawColon(false);
clockDisplay.println(c);
clockDisplay.writeDisplay();
drawTime();
tDisplay.enableDelayed(DISPLAY_TIME);
}
void setup() {
clockDisplay.begin(DISPLAY_ADDRESS);
clockDisplay.setBrightness(brightness);
drawTime();
displayColon();
}
}