Set humidity and temperature with buttons

This commit is contained in:
2026-05-18 22:42:47 +02:00
parent 1f2a0b253e
commit e4308c5e50
3 changed files with 189 additions and 18 deletions

148
Menu.cpp
View File

@@ -2,33 +2,50 @@
Menu::Menu() {
type = MENU_MAIN;
state = STATE_BROWSING;
index = 0;
targetTemperature = 25;
targetHumidity = 40;
}
int Menu::getSize(MenuType type) {
switch (type) {
case MENU_MAIN: return MAIN_SIZE;
case MENU_SETUP: return SETUP_SIZE;
case MENU_MAIN:
return MAIN_SIZE;
case MENU_SETUP:
return SETUP_SIZE;
}
return 0;
}
const char* Menu::getItem(MenuType type, int index) {
switch (type) {
case MENU_MAIN:
switch (index) {
case 0: return "Start";
case 1: return "Setup";
case 2: return "Version";
}
break;
case MENU_SETUP:
switch (index) {
case 0: return "Temperature";
case 1: return "Humidity";
case 2: return "Back";
}
break;
}
@@ -36,22 +53,127 @@ const char* Menu::getItem(MenuType type, int index) {
}
void Menu::next() {
// EDIT MODE
if (state == STATE_EDITING) {
// Temperature
if (type == MENU_SETUP && index == 0) {
targetTemperature++;
if (targetTemperature > 60) {
targetTemperature = 60;
}
}
// Humidity
else if (type == MENU_SETUP && index == 1) {
targetHumidity++;
if (targetHumidity > 100) {
targetHumidity = 100;
}
}
return;
}
// BROWSING MODE
index = (index + 1) % getSize(type);
}
void Menu::prev() {
// EDIT MODE
if (state == STATE_EDITING) {
// Temperature
if (type == MENU_SETUP && index == 0) {
targetTemperature--;
if (targetTemperature < 0) {
targetTemperature = 0;
}
}
// Humidity
else if (type == MENU_SETUP && index == 1) {
targetHumidity--;
if (targetHumidity < 0) {
targetHumidity = 0;
}
}
return;
}
// BROWSING MODE
index--;
if (index < 0) index = getSize(type) - 1;
if (index < 0) {
index = getSize(type) - 1;
}
}
void Menu::enter() {
if (type == MENU_MAIN) {
if (index == 1) type = MENU_SETUP;
// EXIT EDIT MODE
if (state == STATE_EDITING) {
state = STATE_BROWSING;
return;
}
// MAIN MENU
if (type == MENU_MAIN) {
switch (index) {
case 1:
type = MENU_SETUP;
index = 0;
break;
}
return;
}
// SETUP MENU
if (type == MENU_SETUP) {
switch (index) {
// Temperature
case 0:
state = STATE_EDITING;
break;
// Humidity
case 1:
state = STATE_EDITING;
break;
// Back
case 2:
back();
break;
}
}
index = 0;
}
void Menu::back() {
// EXIT EDIT MODE
if (state == STATE_EDITING) {
state = STATE_BROWSING;
return;
}
// RETURN TO MAIN MENU
if (type != MENU_MAIN) {
type = MENU_MAIN;
index = 0;
@@ -63,10 +185,24 @@ const char* Menu::getCurrent() {
}
const char* Menu::getNext() {
int nextIndex = (index + 1) % getSize(type);
return getItem(type, nextIndex);
}
MenuType Menu::getType() {
return type;
}
bool Menu::isEditing() {
return state == STATE_EDITING;
}
int Menu::getTargetTemperature() {
return targetTemperature;
}
int Menu::getTargetHumidity() {
return targetHumidity;
}

16
Menu.h
View File

@@ -8,11 +8,22 @@ enum MenuType {
MENU_SETUP,
};
enum MenuState {
STATE_BROWSING,
STATE_EDITING
};
class Menu {
private:
MenuType type;
MenuState state;
int index;
// Settings
int targetTemperature;
int targetHumidity;
static const int MAIN_SIZE = 3;
static const int SETUP_SIZE = 3;
@@ -32,6 +43,11 @@ public:
const char* getNext();
MenuType getType();
bool isEditing();
int getTargetTemperature();
int getTargetHumidity();
};
#endif

View File

@@ -3,13 +3,15 @@
#include "Button.h"
#include "Menu.h"
DisplayManager display;
SensorManager sensor(7);
#define FIRMWARE_VERSION "v0.1"
Button downButton(8);
Button upButton(9);
Button okButton(10);
Button backButton(11);
DisplayManager display;
SensorManager sensor(10);
Button downButton(3);
Button upButton(4);
Button okButton(2);
Button backButton(5);
Menu menu;
@@ -34,11 +36,11 @@ void loop() {
sensor.update(now);
if (upButton.pressed()) {
menu.prev();
menu.next();
}
if (downButton.pressed()) {
menu.next();
menu.prev();
}
if (okButton.pressed()) {
@@ -50,9 +52,26 @@ void loop() {
}
if (display.ready(now)) {
display.showText(
String("> ") + menu.getCurrent(),
String(" ") + menu.getNext()
);
if (menu.isEditing() && strcmp(menu.getCurrent(), "Temperature") == 0) {
display.showText(
"Set Temp",
String(menu.getTargetTemperature()) + " C"
);
}
else if (menu.isEditing() && strcmp(menu.getCurrent(), "Humidity") == 0) {
display.showText(
"Set Humidity",
String(menu.getTargetHumidity()) + " %"
);
}
else {
display.showText(
String("") + sensor.getTemp() + "C " + sensor.getHumidity() + "%",
String("[") + menu.getCurrent() + String("] ")
);
}
}
}