diff --git a/MenuItem.cpp b/MenuItem.cpp new file mode 100644 index 0000000..7a06a5d --- /dev/null +++ b/MenuItem.cpp @@ -0,0 +1,69 @@ +#include "MenuItem.h" +#include "Arduino.h" + +MenuItem::MenuItem() { + +} + +MenuItem::MenuItem(const char* name) { + MenuItem::name = name; + MenuItem::parent = NULL; + MenuItem::next = NULL; +} + +MenuItem* MenuItem::goBack() { + return MenuItem::parent; +} + +MenuItem* MenuItem::goDown() { + return MenuItem::next; +} + +MenuItem* MenuItem::goRight() { + return NULL; +} + +String MenuItem::display() { + return MenuItem::name; +} + + +BranchMenuItem::BranchMenuItem(const char* name, const MenuItem* child) { + BranchMenuItem::name = name; + BranchMenuItem::child = child; +} + +MenuItem* BranchMenuItem::goRight() { + return BranchMenuItem::child; +} + +String BranchMenuItem::display() { + return String(BranchMenuItem::name) + " ->"; +} + +IntValueItem::IntValueItem(char const* name, int initial) { + IntValueItem::name = name; + IntValueItem::value = initial; +} + +MenuItem* IntValueItem::goRight() { // increment + IntValueItem::value++; +} + +String IntValueItem::display() { + return String(IntValueItem::name) + ": " + String(IntValueItem::value); +} + +BoolValueItem::BoolValueItem(char const* name, bool initial) { + BoolValueItem::name = name; + BoolValueItem::value = initial; +} + +MenuItem* BoolValueItem::goRight() { // toggle + BoolValueItem::value = !BoolValueItem::value; + return NULL; +} + +String BoolValueItem::display() { + return String(BoolValueItem::name) + ": " + (BoolValueItem::value ? "true" : "false"); +} \ No newline at end of file diff --git a/MenuItem.h b/MenuItem.h new file mode 100644 index 0000000..11dc8b1 --- /dev/null +++ b/MenuItem.h @@ -0,0 +1,48 @@ +#ifndef MenuItem_h +#define MenuItem_h + +#include "Arduino.h" + +class MenuItem { + public: + MenuItem(); + MenuItem(const char* name); + const char* name; + MenuItem* parent; + MenuItem* next; + MenuItem* goBack(); + MenuItem* goDown(); + MenuItem* goRight(); + String display(); +// void addChild(MenuItem* c) { +// if (!child) child = c; +// c->parent = this; +// } +}; + +class BranchMenuItem: public MenuItem { + public: + using MenuItem::MenuItem; + BranchMenuItem(const char* name, const MenuItem* child); + MenuItem* child; + MenuItem* goRight(); + String display(); +}; + +class IntValueItem: public MenuItem { + public: + IntValueItem(const char* name, const int initialValue); + int value; + MenuItem* IntValueItem::goRight(); + String IntValueItem::display(); +}; + +class BoolValueItem: public MenuItem { + public: + BoolValueItem(const char* name, const bool initialValue); + bool value; + MenuItem* BoolValueItem::goRight(); + String BoolValueItem::display(); +}; + +#endif \ No newline at end of file diff --git a/watering_system_mini_project/MenuItem.cpp b/watering_system_mini_project/MenuItem.cpp new file mode 100644 index 0000000..7a06a5d --- /dev/null +++ b/watering_system_mini_project/MenuItem.cpp @@ -0,0 +1,69 @@ +#include "MenuItem.h" +#include "Arduino.h" + +MenuItem::MenuItem() { + +} + +MenuItem::MenuItem(const char* name) { + MenuItem::name = name; + MenuItem::parent = NULL; + MenuItem::next = NULL; +} + +MenuItem* MenuItem::goBack() { + return MenuItem::parent; +} + +MenuItem* MenuItem::goDown() { + return MenuItem::next; +} + +MenuItem* MenuItem::goRight() { + return NULL; +} + +String MenuItem::display() { + return MenuItem::name; +} + + +BranchMenuItem::BranchMenuItem(const char* name, const MenuItem* child) { + BranchMenuItem::name = name; + BranchMenuItem::child = child; +} + +MenuItem* BranchMenuItem::goRight() { + return BranchMenuItem::child; +} + +String BranchMenuItem::display() { + return String(BranchMenuItem::name) + " ->"; +} + +IntValueItem::IntValueItem(char const* name, int initial) { + IntValueItem::name = name; + IntValueItem::value = initial; +} + +MenuItem* IntValueItem::goRight() { // increment + IntValueItem::value++; +} + +String IntValueItem::display() { + return String(IntValueItem::name) + ": " + String(IntValueItem::value); +} + +BoolValueItem::BoolValueItem(char const* name, bool initial) { + BoolValueItem::name = name; + BoolValueItem::value = initial; +} + +MenuItem* BoolValueItem::goRight() { // toggle + BoolValueItem::value = !BoolValueItem::value; + return NULL; +} + +String BoolValueItem::display() { + return String(BoolValueItem::name) + ": " + (BoolValueItem::value ? "true" : "false"); +} \ No newline at end of file diff --git a/watering_system_mini_project/MenuItem.h b/watering_system_mini_project/MenuItem.h new file mode 100644 index 0000000..11dc8b1 --- /dev/null +++ b/watering_system_mini_project/MenuItem.h @@ -0,0 +1,48 @@ +#ifndef MenuItem_h +#define MenuItem_h + +#include "Arduino.h" + +class MenuItem { + public: + MenuItem(); + MenuItem(const char* name); + const char* name; + MenuItem* parent; + MenuItem* next; + MenuItem* goBack(); + MenuItem* goDown(); + MenuItem* goRight(); + String display(); +// void addChild(MenuItem* c) { +// if (!child) child = c; +// c->parent = this; +// } +}; + +class BranchMenuItem: public MenuItem { + public: + using MenuItem::MenuItem; + BranchMenuItem(const char* name, const MenuItem* child); + MenuItem* child; + MenuItem* goRight(); + String display(); +}; + +class IntValueItem: public MenuItem { + public: + IntValueItem(const char* name, const int initialValue); + int value; + MenuItem* IntValueItem::goRight(); + String IntValueItem::display(); +}; + +class BoolValueItem: public MenuItem { + public: + BoolValueItem(const char* name, const bool initialValue); + bool value; + MenuItem* BoolValueItem::goRight(); + String BoolValueItem::display(); +}; + +#endif \ No newline at end of file diff --git a/watering_system_mini_project/watering_system_mini_project.ino b/watering_system_mini_project/watering_system_mini_project.ino new file mode 100644 index 0000000..4d4baa9 --- /dev/null +++ b/watering_system_mini_project/watering_system_mini_project.ino @@ -0,0 +1,44 @@ + + MenuItem top = MenuItem("Main menu"); + MenuItem* current = ⊤ + MenuItem toggle = BoolValueItem("A switch", false); + MenuItem sub = BranchMenuItem("Sub menu", &toggle); + MenuItem next = IntValueItem("Power", 1); + +void displayMenu() { + Serial.println("---- MENU ----"); + Serial.println(current->display()); + Serial.println("--------------"); +} + +void setup() { + Serial.begin(9600); + + top.next = ⊂ + sub.nex t = &main; + + displayMenu(); +} + +void loop() { + if (Serial.available()) { + int button = Serial.parseInt(); + MenuItem* new_menu = NULL; + if (button == 1) { + new_menu = current->goBack(); + Serial.println(1); + } else if (button == 2) { + new_menu = current->goDown(); + Serial.println(2); + } else if (button == 3) { + new_menu = current->goRight(); + Serial.println(3); + } + if (new_menu != NULL) { + current = new_menu; + } + displayMenu(); + + Serial.flush(); + } +} \ No newline at end of file