diff --git a/src/main.cpp b/src/main.cpp index 177f307..3b1a52d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -68,6 +68,9 @@ MP3 mp3; Rotary rotary; Screen* screen; +menuType menuChange = eNone; +uint32_t lastButtonPress = 0; + //Variables for the sensor and the battery static const int buttonPin = 0; static const int ResetPin = 17; @@ -269,7 +272,10 @@ void suspendESP(uint16_t packetId) { void rotation(int i, int direction, int buttonPressed) { - if (buttonPressed == 1) screen->select(); + if(millis() - lastButtonPress >= 300) { + lastButtonPress = millis(); + if (buttonPressed == 1) screen->select(); + } if (direction == 1) screen->next(); else if (direction == -1) screen->previous(); } @@ -280,6 +286,13 @@ void loop() timeClient.update(); timeClient.getFormattedTime().toCharArray(timeStr, 50); + if (menuChange != eNone) { + delete screen; + if (menuChange == eMainScreen) screen = new MainScreen(); + if (menuChange == eMainMenu) screen = new MainMenu(); + menuChange = eNone; + } + screen->draw(); delay(200); diff --git a/src/rotary.cpp b/src/rotary.cpp index d3c0409..8cb51d5 100644 --- a/src/rotary.cpp +++ b/src/rotary.cpp @@ -50,7 +50,6 @@ void Rotary::task(void *pvParameters) { delay(2); // wait until bounce settled if (digitalRead(pinButton) == HIGH) { - Serial.println("button"); buttonPressed = true; if (callback) callback(value, 0, 1); continue; diff --git a/src/screen.cpp b/src/screen.cpp index 74074c7..f0ca85d 100644 --- a/src/screen.cpp +++ b/src/screen.cpp @@ -23,7 +23,8 @@ void SelectionList::draw() { /* draw menu items */ u8g2.setFont(u8g2_font_9x18B_tr); - for (int i=0; i top_item+visible-1) top_item++; + int length = std::min(visible, (uint8_t)string_list.size()); + if (current_pos > top_item+length-1) top_item++; } void SelectionList::previous() { @@ -42,6 +44,44 @@ void SelectionList::previous() { } uint8_t SelectionList::select() { + switch (current_pos) { +// case 0: +// break; + default: + Serial.println("unknown entry selected"); + break; + } + menuChange = eMainScreen; + return current_pos; +} + +MainMenu::MainMenu() { + string_list = { + "MP3", + "test", + "test2", + "test3"}; +} + +uint8_t MainMenu::select() { + String topic = "esp32-node/stat/" + iot.hostname + "/blabla"; + switch (current_pos) { + case 0: + if (mp3.playing) { + mp3.stop(); + } else { + mp3.start(); + } + break; + case 1: + iot.mqtt.publish(topic.c_str(), 1, true, "test" ); + case 2: + iot.mqtt.publish(topic.c_str(), 1, true, "foo" ); + default: + Serial.println("unknown entry selected"); + break; + } + menuChange = eMainScreen; return current_pos; } @@ -135,15 +175,7 @@ void MainScreen::draw() { } uint8_t MainScreen::select() { - if(millis() - lastButtonPress >= 1000) { - Serial.println("Button pressed"); - lastButtonPress = millis(); - if (mp3.playing) { - mp3.stop(); - } else { - mp3.start(); - } - } + menuChange = eMainMenu; } void MainScreen::next() { diff --git a/src/screen.h b/src/screen.h index a0b0bdb..8271cbe 100644 --- a/src/screen.h +++ b/src/screen.h @@ -5,6 +5,9 @@ #include #include +#include +#include + #include #ifdef U8X8_HAVE_HW_SPI #include @@ -17,10 +20,15 @@ #include "BME280.h" #include "image.h" +enum menuType { eNone=0, eMainScreen, eMainMenu }; + extern U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2; extern char timeStr[]; extern BME280 bme280; extern MP3 mp3; +extern menuType menuChange; +extern Basecamp iot; + class Screen { public: @@ -42,23 +50,30 @@ class SelectionList : public Screen { void previous(void) override; uint8_t select(void) override; const char* title = "Cloud Types"; - private: + protected: + uint8_t current_pos = 0; std::vector string_list = { - "Altocumulus\n", - "Altostratus\n", - "Cirrocumulus\n", - "Cirrostratus\n", - "Cirrus\n", - "Cumulonimbus\n", - "Cumulus\n", - "Nimbostratus\n", - "Stratocumulus\n", + "Altocumulus", + "Altostratus", + "Cirrocumulus", + "Cirrostratus", + "Cirrus", + "Cumulonimbus", + "Cumulus", + "Nimbostratus", + "Stratocumulus", "Stratus" }; - uint8_t current_pos = 0; uint8_t top_item = 0; uint8_t visible = 4; }; +class MainMenu : public SelectionList { + public: + MainMenu(); + uint8_t select(void) override; + private: +}; + class MainScreen : public Screen { public: void draw(void) override; @@ -67,7 +82,6 @@ class MainScreen : public Screen { uint8_t select(void) override; const char* title = "Main Screen"; private: - uint32_t lastButtonPress = 0; uint32_t lastVolumeChange = 0; u8g2_uint_t titleStr_offset = 0; uint8_t volume = 20;