Browse Source

mainmenu

main
Hendrik Langer 7 years ago
parent
commit
1d067b23c9
  1. 13
      src/main.cpp
  2. 1
      src/rotary.cpp
  3. 54
      src/screen.cpp
  4. 38
      src/screen.h

13
src/main.cpp

@ -68,6 +68,9 @@ MP3 mp3;
Rotary rotary; Rotary rotary;
Screen* screen; Screen* screen;
menuType menuChange = eNone;
uint32_t lastButtonPress = 0;
//Variables for the sensor and the battery //Variables for the sensor and the battery
static const int buttonPin = 0; static const int buttonPin = 0;
static const int ResetPin = 17; static const int ResetPin = 17;
@ -269,7 +272,10 @@ void suspendESP(uint16_t packetId) {
void rotation(int i, int direction, int buttonPressed) { void rotation(int i, int direction, int buttonPressed) {
if(millis() - lastButtonPress >= 300) {
lastButtonPress = millis();
if (buttonPressed == 1) screen->select(); if (buttonPressed == 1) screen->select();
}
if (direction == 1) screen->next(); if (direction == 1) screen->next();
else if (direction == -1) screen->previous(); else if (direction == -1) screen->previous();
} }
@ -280,6 +286,13 @@ void loop()
timeClient.update(); timeClient.update();
timeClient.getFormattedTime().toCharArray(timeStr, 50); 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(); screen->draw();
delay(200); delay(200);

1
src/rotary.cpp

@ -50,7 +50,6 @@ void Rotary::task(void *pvParameters) {
delay(2); // wait until bounce settled delay(2); // wait until bounce settled
if (digitalRead(pinButton) == HIGH) { if (digitalRead(pinButton) == HIGH) {
Serial.println("button");
buttonPressed = true; buttonPressed = true;
if (callback) callback(value, 0, 1); if (callback) callback(value, 0, 1);
continue; continue;

54
src/screen.cpp

@ -23,7 +23,8 @@ void SelectionList::draw() {
/* draw menu items */ /* draw menu items */
u8g2.setFont(u8g2_font_9x18B_tr); u8g2.setFont(u8g2_font_9x18B_tr);
for (int i=0; i<visible; i++) { int length = std::min(visible, (uint8_t)string_list.size());
for (int i=0; i<length; i++) {
u8g2.drawUTF8(item_x_offset,header_height+2+(i+1)*item_height,string_list[top_item+i].c_str()); u8g2.drawUTF8(item_x_offset,header_height+2+(i+1)*item_height,string_list[top_item+i].c_str());
} }
@ -33,7 +34,8 @@ void SelectionList::draw() {
void SelectionList::next() { void SelectionList::next() {
if (current_pos < string_list.size()-1) current_pos++; if (current_pos < string_list.size()-1) current_pos++;
if (current_pos > 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() { void SelectionList::previous() {
@ -42,6 +44,44 @@ void SelectionList::previous() {
} }
uint8_t SelectionList::select() { 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; return current_pos;
} }
@ -135,15 +175,7 @@ void MainScreen::draw() {
} }
uint8_t MainScreen::select() { uint8_t MainScreen::select() {
if(millis() - lastButtonPress >= 1000) { menuChange = eMainMenu;
Serial.println("Button pressed");
lastButtonPress = millis();
if (mp3.playing) {
mp3.stop();
} else {
mp3.start();
}
}
} }
void MainScreen::next() { void MainScreen::next() {

38
src/screen.h

@ -5,6 +5,9 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include <Basecamp.hpp>
#include <Configuration.hpp>
#include <U8g2lib.h> #include <U8g2lib.h>
#ifdef U8X8_HAVE_HW_SPI #ifdef U8X8_HAVE_HW_SPI
#include <SPI.h> #include <SPI.h>
@ -17,10 +20,15 @@
#include "BME280.h" #include "BME280.h"
#include "image.h" #include "image.h"
enum menuType { eNone=0, eMainScreen, eMainMenu };
extern U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2; extern U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2;
extern char timeStr[]; extern char timeStr[];
extern BME280 bme280; extern BME280 bme280;
extern MP3 mp3; extern MP3 mp3;
extern menuType menuChange;
extern Basecamp iot;
class Screen { class Screen {
public: public:
@ -42,23 +50,30 @@ class SelectionList : public Screen {
void previous(void) override; void previous(void) override;
uint8_t select(void) override; uint8_t select(void) override;
const char* title = "Cloud Types"; const char* title = "Cloud Types";
private: protected:
uint8_t current_pos = 0;
std::vector<std::string> string_list = { std::vector<std::string> string_list = {
"Altocumulus\n", "Altocumulus",
"Altostratus\n", "Altostratus",
"Cirrocumulus\n", "Cirrocumulus",
"Cirrostratus\n", "Cirrostratus",
"Cirrus\n", "Cirrus",
"Cumulonimbus\n", "Cumulonimbus",
"Cumulus\n", "Cumulus",
"Nimbostratus\n", "Nimbostratus",
"Stratocumulus\n", "Stratocumulus",
"Stratus" }; "Stratus" };
uint8_t current_pos = 0;
uint8_t top_item = 0; uint8_t top_item = 0;
uint8_t visible = 4; uint8_t visible = 4;
}; };
class MainMenu : public SelectionList {
public:
MainMenu();
uint8_t select(void) override;
private:
};
class MainScreen : public Screen { class MainScreen : public Screen {
public: public:
void draw(void) override; void draw(void) override;
@ -67,7 +82,6 @@ class MainScreen : public Screen {
uint8_t select(void) override; uint8_t select(void) override;
const char* title = "Main Screen"; const char* title = "Main Screen";
private: private:
uint32_t lastButtonPress = 0;
uint32_t lastVolumeChange = 0; uint32_t lastVolumeChange = 0;
u8g2_uint_t titleStr_offset = 0; u8g2_uint_t titleStr_offset = 0;
uint8_t volume = 20; uint8_t volume = 20;

Loading…
Cancel
Save