Set humidity and temperature with buttons
This commit is contained in:
146
Menu.cpp
146
Menu.cpp
@@ -2,33 +2,50 @@
|
|||||||
|
|
||||||
Menu::Menu() {
|
Menu::Menu() {
|
||||||
type = MENU_MAIN;
|
type = MENU_MAIN;
|
||||||
|
state = STATE_BROWSING;
|
||||||
|
|
||||||
index = 0;
|
index = 0;
|
||||||
|
|
||||||
|
targetTemperature = 25;
|
||||||
|
targetHumidity = 40;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Menu::getSize(MenuType type) {
|
int Menu::getSize(MenuType type) {
|
||||||
|
|
||||||
switch (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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* Menu::getItem(MenuType type, int index) {
|
const char* Menu::getItem(MenuType type, int index) {
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
|
|
||||||
case MENU_MAIN:
|
case MENU_MAIN:
|
||||||
|
|
||||||
switch (index) {
|
switch (index) {
|
||||||
case 0: return "Start";
|
case 0: return "Start";
|
||||||
case 1: return "Setup";
|
case 1: return "Setup";
|
||||||
case 2: return "Version";
|
case 2: return "Version";
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MENU_SETUP:
|
case MENU_SETUP:
|
||||||
|
|
||||||
switch (index) {
|
switch (index) {
|
||||||
case 0: return "Temperature";
|
case 0: return "Temperature";
|
||||||
case 1: return "Humidity";
|
case 1: return "Humidity";
|
||||||
case 2: return "Back";
|
case 2: return "Back";
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,22 +53,127 @@ const char* Menu::getItem(MenuType type, int index) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Menu::next() {
|
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);
|
index = (index + 1) % getSize(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Menu::prev() {
|
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--;
|
index--;
|
||||||
if (index < 0) index = getSize(type) - 1;
|
|
||||||
|
if (index < 0) {
|
||||||
|
index = getSize(type) - 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Menu::enter() {
|
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;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Menu::back() {
|
void Menu::back() {
|
||||||
|
|
||||||
|
// EXIT EDIT MODE
|
||||||
|
if (state == STATE_EDITING) {
|
||||||
|
state = STATE_BROWSING;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// RETURN TO MAIN MENU
|
||||||
if (type != MENU_MAIN) {
|
if (type != MENU_MAIN) {
|
||||||
type = MENU_MAIN;
|
type = MENU_MAIN;
|
||||||
index = 0;
|
index = 0;
|
||||||
@@ -63,10 +185,24 @@ const char* Menu::getCurrent() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const char* Menu::getNext() {
|
const char* Menu::getNext() {
|
||||||
|
|
||||||
int nextIndex = (index + 1) % getSize(type);
|
int nextIndex = (index + 1) % getSize(type);
|
||||||
|
|
||||||
return getItem(type, nextIndex);
|
return getItem(type, nextIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
MenuType Menu::getType() {
|
MenuType Menu::getType() {
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Menu::isEditing() {
|
||||||
|
return state == STATE_EDITING;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Menu::getTargetTemperature() {
|
||||||
|
return targetTemperature;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Menu::getTargetHumidity() {
|
||||||
|
return targetHumidity;
|
||||||
|
}
|
||||||
16
Menu.h
16
Menu.h
@@ -8,11 +8,22 @@ enum MenuType {
|
|||||||
MENU_SETUP,
|
MENU_SETUP,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum MenuState {
|
||||||
|
STATE_BROWSING,
|
||||||
|
STATE_EDITING
|
||||||
|
};
|
||||||
|
|
||||||
class Menu {
|
class Menu {
|
||||||
private:
|
private:
|
||||||
MenuType type;
|
MenuType type;
|
||||||
|
MenuState state;
|
||||||
|
|
||||||
int index;
|
int index;
|
||||||
|
|
||||||
|
// Settings
|
||||||
|
int targetTemperature;
|
||||||
|
int targetHumidity;
|
||||||
|
|
||||||
static const int MAIN_SIZE = 3;
|
static const int MAIN_SIZE = 3;
|
||||||
static const int SETUP_SIZE = 3;
|
static const int SETUP_SIZE = 3;
|
||||||
|
|
||||||
@@ -32,6 +43,11 @@ public:
|
|||||||
const char* getNext();
|
const char* getNext();
|
||||||
|
|
||||||
MenuType getType();
|
MenuType getType();
|
||||||
|
|
||||||
|
bool isEditing();
|
||||||
|
|
||||||
|
int getTargetTemperature();
|
||||||
|
int getTargetHumidity();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
39
drybox.ino
39
drybox.ino
@@ -3,13 +3,15 @@
|
|||||||
#include "Button.h"
|
#include "Button.h"
|
||||||
#include "Menu.h"
|
#include "Menu.h"
|
||||||
|
|
||||||
DisplayManager display;
|
#define FIRMWARE_VERSION "v0.1"
|
||||||
SensorManager sensor(7);
|
|
||||||
|
|
||||||
Button downButton(8);
|
DisplayManager display;
|
||||||
Button upButton(9);
|
SensorManager sensor(10);
|
||||||
Button okButton(10);
|
|
||||||
Button backButton(11);
|
Button downButton(3);
|
||||||
|
Button upButton(4);
|
||||||
|
Button okButton(2);
|
||||||
|
Button backButton(5);
|
||||||
|
|
||||||
Menu menu;
|
Menu menu;
|
||||||
|
|
||||||
@@ -34,11 +36,11 @@ void loop() {
|
|||||||
sensor.update(now);
|
sensor.update(now);
|
||||||
|
|
||||||
if (upButton.pressed()) {
|
if (upButton.pressed()) {
|
||||||
menu.prev();
|
menu.next();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (downButton.pressed()) {
|
if (downButton.pressed()) {
|
||||||
menu.next();
|
menu.prev();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (okButton.pressed()) {
|
if (okButton.pressed()) {
|
||||||
@@ -50,9 +52,26 @@ void loop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (display.ready(now)) {
|
if (display.ready(now)) {
|
||||||
|
if (menu.isEditing() && strcmp(menu.getCurrent(), "Temperature") == 0) {
|
||||||
display.showText(
|
display.showText(
|
||||||
String("> ") + menu.getCurrent(),
|
"Set Temp",
|
||||||
String(" ") + menu.getNext()
|
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("] ")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user