esp-clock/include/display.h
2021-12-09 22:49:27 +01:00

116 lines
2.8 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"
#define DISPLAY_ADDRESS 0x70
#define BRIGHTNESS 0
#define BRIGHTNESS_STEP 1
namespace Display {
void displayColon();
Task tDisplay(500, TASK_FOREVER, displayColon, &ts, true);
uint8_t brightness = BRIGHTNESS;
int currentHour = -1;
int currentMin = -1;
// Create display object
Adafruit_7segment clockDisplay = Adafruit_7segment();
void drawTime() {
int displayHour = hour();
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 changeBrightness(bool increase) {
increase ? brightness = (brightness + BRIGHTNESS_STEP) % 15 : brightness = (brightness - BRIGHTNESS_STEP) % 15;
clockDisplay.setBrightness(brightness);
}
void adjustBrightness() {
if (currentHour > 8 && currentHour < 17) {
brightness = 11;
} else {
brightness = BRIGHTNESS;
}
clockDisplay.setBrightness(brightness);
}
void drawColon(bool colonOn) {
if (colonOn) {
if (currentHour != hour()) {
currentHour = hour();
Display::adjustBrightness();
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);
clockDisplay.writeDisplay();
drawTime();
tDisplay.enableDelayed(1000);
}
void displayValue(uint8_t value) {
tDisplay.disable();
drawColon(false);
clockDisplay.print(value, HEX);
clockDisplay.writeDisplay();
drawTime();
tDisplay.enableDelayed(1000);
}
void displayText(const char c[]) {
tDisplay.disable();
drawColon(false);
clockDisplay.println(c);
clockDisplay.writeDisplay();
drawTime();
tDisplay.enableDelayed(1000);
}
void setup() {
clockDisplay.begin(DISPLAY_ADDRESS);
clockDisplay.setBrightness(brightness);
drawTime();
displayColon();
}
}