- duration - start/stop switch On start, it will display the remaining time instead of the watch
209 lines
6.0 KiB
C++
209 lines
6.0 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 "bme.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 MILLISECONDS(value) value*TASK_MILLISECOND
|
|
#define SECONDS(value) value*TASK_SECOND
|
|
#define MINUTES(value) value*TASK_MINUTE
|
|
#define DISPLAY_SENSOR_ITERATIONS 2
|
|
#define DISPLAY_DELAY (SECONDS(2))
|
|
|
|
namespace Display {
|
|
|
|
void displayTime();
|
|
void displayDate();
|
|
void displayTemp();
|
|
void displayHumidity();
|
|
void drawTime();
|
|
void drawColon(bool);
|
|
Task tDisplayTime(MILLISECONDS(500), TASK_FOREVER, displayTime, &ts, false,
|
|
[]() {
|
|
drawTime();
|
|
return true;
|
|
},
|
|
[]() {
|
|
drawColon(false);
|
|
});
|
|
Task tDisplaySensor(SECONDS(5), DISPLAY_SENSOR_ITERATIONS, displayTemp, &ts, false,
|
|
[]() {
|
|
tDisplayTime.disable();
|
|
return true;
|
|
},
|
|
[]() {
|
|
tDisplaySensor.setIterations(DISPLAY_SENSOR_ITERATIONS * 2);
|
|
tDisplayTime.enableDelayed(tDisplaySensor.getInterval());
|
|
});
|
|
Task tDisplayDate(TASK_IMMEDIATE, TASK_ONCE, displayDate, &ts, false,
|
|
[]() {
|
|
tDisplayTime.disable();
|
|
tDisplaySensor.disable();
|
|
return true;
|
|
},
|
|
[]() {
|
|
tDisplayTime.enableDelayed(SECONDS(5));
|
|
});
|
|
|
|
bool hourFormat24 = false;
|
|
void (*hourFormatChangedCallback)();
|
|
|
|
// Create display object
|
|
Adafruit_7segment clockDisplay = Adafruit_7segment();
|
|
|
|
namespace Brightness {
|
|
uint8_t current = BRIGHTNESS_NIGHT;
|
|
void (*brightnessChangedCallback)();
|
|
|
|
void set(uint8_t value) {
|
|
current = value % (BRIGHTNESS_MAX + 1);
|
|
clockDisplay.setBrightness(current);
|
|
if (brightnessChangedCallback) brightnessChangedCallback();
|
|
}
|
|
|
|
void change(bool increase) {
|
|
increase ? set(current + BRIGHTNESS_STEP) : set(current - BRIGHTNESS_STEP);
|
|
}
|
|
}
|
|
|
|
namespace Timer {
|
|
int8 timer = 0, currentTimer = 0;
|
|
|
|
void start() {
|
|
currentTimer = timer;
|
|
auto displayTimer = []{
|
|
if (currentTimer >= 0) {
|
|
clockDisplay.print(currentTimer--, DEC);
|
|
clockDisplay.writeDisplay();
|
|
}
|
|
};
|
|
tDisplayTime.setInterval(MINUTES(1));
|
|
tDisplayTime.setCallback(displayTimer);
|
|
tDisplayTime.restart();
|
|
}
|
|
|
|
void stop() {
|
|
tDisplayTime.setInterval(MILLISECONDS(500));
|
|
tDisplayTime.setCallback(displayTime);
|
|
tDisplayTime.restart();
|
|
}
|
|
}
|
|
|
|
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 drawColon(bool colonOn) {
|
|
if (colonOn) {
|
|
static int currentHour = -1;
|
|
if (currentHour != hour()) {
|
|
currentHour = hour();
|
|
if (currentHour == 4) {
|
|
Ntp::tUpdateTime.restart();
|
|
}
|
|
if (currentHour == 8) {
|
|
Brightness::set(BRIGHTNESS_DAY);
|
|
Wifi::tConnect.enable();
|
|
}
|
|
if (currentHour == 17) {
|
|
Brightness::set(BRIGHTNESS_NIGHT);
|
|
}
|
|
}
|
|
static int currentMin = -1;
|
|
if (currentMin != minute()) {
|
|
currentMin = minute();
|
|
drawTime();
|
|
}
|
|
}
|
|
clockDisplay.drawColon(colonOn);
|
|
}
|
|
|
|
void displayTime() {
|
|
static bool colonOn = true;
|
|
|
|
drawColon(colonOn);
|
|
clockDisplay.writeDisplay();
|
|
|
|
colonOn = !colonOn;
|
|
}
|
|
|
|
void displayTemp() {
|
|
clockDisplay.printFloat(Bme::data.temp, 2);
|
|
clockDisplay.writeDigitAscii(4, 'c');
|
|
clockDisplay.writeDisplay();
|
|
tDisplaySensor.setCallback(&displayHumidity);
|
|
}
|
|
|
|
void displayHumidity() {
|
|
clockDisplay.printFloat(Bme::data.humidity, 2);
|
|
clockDisplay.writeDigitAscii(4, '%');
|
|
clockDisplay.writeDisplay();
|
|
tDisplaySensor.setCallback(&displayTemp);
|
|
}
|
|
|
|
void displayValue(uint8_t value) {
|
|
tDisplayTime.disable();
|
|
clockDisplay.print(value, HEX);
|
|
clockDisplay.writeDisplay();
|
|
tDisplayTime.enableDelayed(DISPLAY_DELAY);
|
|
}
|
|
|
|
void displayText(const char c[]) {
|
|
tDisplayTime.disable();
|
|
clockDisplay.println(c);
|
|
clockDisplay.writeDisplay();
|
|
tDisplayTime.enableDelayed(DISPLAY_DELAY);
|
|
}
|
|
|
|
unordered_map<char, const char*> daysOfWeek = {
|
|
{1, "So"},
|
|
{2, "Mo"},
|
|
{3, "Di"},
|
|
{4, "Mi"},
|
|
{5, "Do"},
|
|
{6, "Fr"},
|
|
{7, "Sa"},
|
|
};
|
|
void displayDate() {
|
|
char date[5];
|
|
sprintf(date, "%2s%2u", daysOfWeek[weekday()], day());
|
|
clockDisplay.println(date);
|
|
clockDisplay.writeDisplay();
|
|
}
|
|
|
|
void changeHourFormat24(bool format24) {
|
|
hourFormat24 = format24;
|
|
drawTime();
|
|
if (hourFormatChangedCallback) hourFormatChangedCallback();
|
|
}
|
|
|
|
void setup() {
|
|
clockDisplay.begin(DISPLAY_ADDRESS);
|
|
clockDisplay.setBrightness(Brightness::current);
|
|
}
|
|
}
|