Humidistat
Arduino firmware for a humidistat (humidity controller)
Loading...
Searching...
No Matches
ControllerUI.cpp
Go to the documentation of this file.
1#include "ControllerUI.h"
2
3ControllerUI::ControllerUI(Print *display, const ButtonReader *buttonReader, etl::span<const ThermistorReader, 4> trs)
4 : display(*display), buttonReader(*buttonReader), trs(trs) {}
5
7 // Show splash screen and info (draw it once) for a short time after boot
8 if (millis() < splashDuration) {
9 if (!splashDrawn) {
10 drawSplash();
11 splashDrawn = true;
12 }
13 return;
14 }
15 if (millis() - splashDuration < infoDuration) {
16 if (!infoDrawn) {
17 drawInfo();
18 infoDrawn = true;
19 }
20 return;
21 }
22 // Clear screen once after splash and info are shown
23 if (!screenCleared) {
24 clear();
25 screenCleared = true;
26 }
27
29 uint32_t pressedFor = buttonReader.getPressedFor();
30 // Debouncing: only call the input handler if it has been at least inputInterval since the last action, and if
31 // the keypress has been stable for at least buttonDebounceInterval
32 if (millis() - lastPressed >= inputInterval && pressedFor > buttonDebounceInterval) {
33 if (handleInput(state, pressedFor/1000)) {
34 lastPressed = millis();
35 draw();
36 }
37 }
38 if (millis() - lastRefreshed >= refreshInterval) {
39 draw();
40 }
41}
42
43void ControllerUI::blink(uint8_t col, uint8_t row, const char *buf) {
44 setCursor(col, row);
45 if (millis() % (2 * blinkInterval) >= blinkInterval) {
46 display.print(buf);
47 } else {
48 // Create char array of spaces with same length as buf
49 size_t len = strlen(buf);
50 char clrBuf[len + 1];
51 memset(clrBuf, ' ', len);
52 clrBuf[len] = '\0';
53
54 display.print(clrBuf);
55 }
56}
57
58void ControllerUI::printNTC(uint8_t col, uint8_t row, uint8_t i) {
59 double temp = trs[i].readTemp();
60 if (isnan(temp)) {
61 printf(col, row, "%2u", 0);
62 } else {
63 printf(col, row, "%2u", static_cast<uint8_t>(temp));
64 }
65}
66
67void ControllerUI::adjustValue(double delta, double &value, uint8_t min, uint8_t max) {
68 // Clip value to [min, max] before uint overflow happens
69 if(value + delta < min) {
70 value = min;
71 return;
72 }
73 if(value + delta > max) {
74 value = max;
75 return;
76 }
77 value += delta;
78}
Buttons
Possible button values.
Definition Buttons.h:5
Read button state from a voltage ladder-style keypad.
uint32_t getPressedFor() const
Get the duration the key has been pressed. This can be used for debouncing using a stable interval.
Buttons isPressed() const
Get the button state.
const uint16_t refreshInterval
Print & display
static void adjustValue(double delta, double &value, uint8_t min, uint8_t max)
In-/de-crement a variable, while clipping it to [min, max].
etl::span< const ThermistorReader, 4 > trs
unsigned long lastRefreshed
Last time display was updated (in millis)
const uint16_t splashDuration
void update()
Update the display and handle input: set Humidistat's setpoint.
const uint16_t inputInterval
virtual void drawSplash()=0
Draw splash screen.
virtual void setCursor(uint8_t col, uint8_t row)=0
Set cursor to coordinates.
const uint16_t infoDuration
void printf(uint8_t col, uint8_t row, const char *fmt, T... args)
Print formatted data to display, at (col, row). Calculates lengths and creates appropriate buffer int...
const uint16_t blinkInterval
virtual bool handleInput(Buttons state, uint16_t pressedFor)=0
Handle input.
const uint16_t buttonDebounceInterval
ControllerUI(Print *display, const ButtonReader *buttonReader, etl::span< const ThermistorReader, 4 > trs)
Constructor.
const ButtonReader & buttonReader
void printNTC(uint8_t col, uint8_t row, uint8_t i)
Print temperature read from thermistors. Handles NaN values as 0.
virtual void drawInfo()=0
Draw info screen.
virtual void draw()=0
Draw main interface (main loop).
unsigned long lastPressed
Last time a keypress event occurred (in millis)
virtual void clear()=0
Clear screen.
void blink(uint8_t col, uint8_t row, const char *buf)
Print blinking text.
ButtonReader buttonReader(config::PIN_BTN, &voltLadder)
ThermistorReader trs[]
Definition main.cpp:25