From df2f62f78405a80ea07fb4cbf73d670766545de4 Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Fri, 2 Feb 2018 17:34:42 +0100 Subject: [PATCH] menu --- src/main.cpp | 9 ++++++--- src/main.h | 2 +- src/rotary.cpp | 14 +++++++++----- src/rotary.h | 4 ++-- src/selectionlist.cpp | 37 ++++++++++++++++++++++++++++++++++++- src/selectionlist.h | 41 +++++++++++++++++++++++++++++------------ 6 files changed, 83 insertions(+), 24 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index c19eca0..39e1e06 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -272,9 +272,11 @@ void suspendESP(uint16_t packetId) { } -void rotation(int i) { +void rotation(int i, int direction) { Serial.println(i); mp3.setVolume(i); + if (direction == 1) menu.next(); + else if (direction == -1) menu.previous(); } @@ -294,7 +296,8 @@ void loop() } } } - +menu.draw(); +/* u8g2.clearBuffer(); // clear the internal memory if (digitalRead(buttonPin) == HIGH) { @@ -320,6 +323,6 @@ void loop() } u8g2.sendBuffer(); // bme280.printValues(); - +*/ delay(200); } diff --git a/src/main.h b/src/main.h index e6302d2..cad7638 100644 --- a/src/main.h +++ b/src/main.h @@ -7,4 +7,4 @@ void transmitStatus(); void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total); void suspendESP(uint16_t packetId); void loop(); -void rotation(int i); +void rotation(int i, int direction); diff --git a/src/rotary.cpp b/src/rotary.cpp index 3c98ac7..42ea5e2 100644 --- a/src/rotary.cpp +++ b/src/rotary.cpp @@ -65,18 +65,22 @@ void Rotary::task(void *pvParameters) { debouncePulses++; if (debouncePulses > 3) { // update every 4 pulses debouncePulses = 0; - if (encoderPos > encoderPosOld+1) value++; // if the value has at least changed for 2 - else if (encoderPos < encoderPosOld-1) value--; - else continue; // otherwise skip + if (encoderPos > encoderPosOld+1) { + value++; // if the value has at least changed for 2 + if (callback) callback(value, 1); + } else if (encoderPos < encoderPosOld-1) { + value--; + if (callback) callback(value, -1); + } + // otherwise skip encoderPosOld = encoderPos; - if (callback) callback(value); } } vTaskDelete(NULL); } -bool Rotary::registerCallback(std::function callback) { +bool Rotary::registerCallback(std::function callback) { this->callback = callback; return true; } diff --git a/src/rotary.h b/src/rotary.h index 60a4325..f14fc25 100644 --- a/src/rotary.h +++ b/src/rotary.h @@ -16,7 +16,7 @@ class Rotary { public: Rotary(); bool begin(uint8_t pinA, uint8_t pinB, uint8_t pinButton); - bool registerCallback(std::function callback); + bool registerCallback(std::function callback); static void IRAM_ATTR doEncoder(void); static Rotary* instance; volatile int encoderPos = 0; @@ -31,7 +31,7 @@ class Rotary { uint8_t pinA; uint8_t pinB; uint8_t pinButton; - std::function callback = nullptr; + std::function callback = nullptr; TaskHandle_t taskHandle; void task(void*); static void cTaskWrapper(void*); diff --git a/src/selectionlist.cpp b/src/selectionlist.cpp index bf235a9..9085fc4 100644 --- a/src/selectionlist.cpp +++ b/src/selectionlist.cpp @@ -4,4 +4,39 @@ SelectionList::SelectionList() {} SelectionList::SelectionList(const char* title, uint8_t start_pos, const char* string_list) {} -void SelectionList::draw() {} +void SelectionList::draw() { + uint8_t item_x_offset = 15; + uint8_t header_height = 12; + uint8_t item_height = 12; + uint8_t num_active_item = current_pos - top_item; + + u8g2.clearBuffer(); + + /* draw Title */ + u8g2.setFont(u8g2_font_guildenstern_nbp_tr); + u8g2.drawUTF8(2,header_height-2,title); + u8g2.drawLine(0,header_height,u8g2.getDisplayWidth(),header_height); + + /* draw menu items */ + u8g2.setFont(u8g2_font_9x18B_tr); + for (int i=0; i top_item+visible-1) top_item++; +} + +void SelectionList::previous() { + if (current_pos > 0 ) current_pos--; + if (current_pos < top_item) top_item--; +} + +uint8_t SelectionList::select() { + return current_pos; +} diff --git a/src/selectionlist.h b/src/selectionlist.h index 982ab85..de158df 100644 --- a/src/selectionlist.h +++ b/src/selectionlist.h @@ -2,26 +2,43 @@ #define _SELECTIONLIST_H #include +#include +#include + +#include +#ifdef U8X8_HAVE_HW_SPI +#include +#endif +#ifdef U8X8_HAVE_HW_I2C +#include +#endif + +extern U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2; class SelectionList { public: SelectionList(); SelectionList(const char*, uint8_t, const char*); void draw(void); + void next(void); + void previous(void); + uint8_t select(void); private: const char* title = "Cloud Types"; - const char *string_list = - "Altocumulus\n" - "Altostratus\n" - "Cirrocumulus\n" - "Cirrostratus\n" - "Cirrus\n" - "Cumulonimbus\n" - "Cumulus\n" - "Nimbostratus\n" - "Stratocumulus\n" - "Stratus"; - uint8_t current_selection = 1; + std::vector string_list = { + "Altocumulus\n", + "Altostratus\n", + "Cirrocumulus\n", + "Cirrostratus\n", + "Cirrus\n", + "Cumulonimbus\n", + "Cumulus\n", + "Nimbostratus\n", + "Stratocumulus\n", + "Stratus" }; + uint8_t current_pos = 0; + uint8_t top_item = 0; + uint8_t visible = 4; }; #endif /* _SELECTIONLIST_H */