Browse Source

mainmenu

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

15
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);

1
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;

54
src/screen.cpp

@ -23,7 +23,8 @@ void SelectionList::draw() {
/* draw menu items */
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());
}
@ -33,7 +34,8 @@ void SelectionList::draw() {
void SelectionList::next() {
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() {
@ -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() {

38
src/screen.h

@ -5,6 +5,9 @@
#include <vector>
#include <string>
#include <Basecamp.hpp>
#include <Configuration.hpp>
#include <U8g2lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
@ -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<std::string> 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;

Loading…
Cancel
Save