Hendrik Langer 7 years ago
parent
commit
df2f62f784
  1. 9
      src/main.cpp
  2. 2
      src/main.h
  3. 14
      src/rotary.cpp
  4. 4
      src/rotary.h
  5. 37
      src/selectionlist.cpp
  6. 41
      src/selectionlist.h

9
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); Serial.println(i);
mp3.setVolume(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 u8g2.clearBuffer(); // clear the internal memory
if (digitalRead(buttonPin) == HIGH) { if (digitalRead(buttonPin) == HIGH) {
@ -320,6 +323,6 @@ void loop()
} }
u8g2.sendBuffer(); u8g2.sendBuffer();
// bme280.printValues(); // bme280.printValues();
*/
delay(200); delay(200);
} }

2
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 onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total);
void suspendESP(uint16_t packetId); void suspendESP(uint16_t packetId);
void loop(); void loop();
void rotation(int i); void rotation(int i, int direction);

14
src/rotary.cpp

@ -65,18 +65,22 @@ void Rotary::task(void *pvParameters) {
debouncePulses++; debouncePulses++;
if (debouncePulses > 3) { // update every 4 pulses if (debouncePulses > 3) { // update every 4 pulses
debouncePulses = 0; debouncePulses = 0;
if (encoderPos > encoderPosOld+1) value++; // if the value has at least changed for 2 if (encoderPos > encoderPosOld+1) {
else if (encoderPos < encoderPosOld-1) value--; value++; // if the value has at least changed for 2
else continue; // otherwise skip if (callback) callback(value, 1);
} else if (encoderPos < encoderPosOld-1) {
value--;
if (callback) callback(value, -1);
}
// otherwise skip
encoderPosOld = encoderPos; encoderPosOld = encoderPos;
if (callback) callback(value);
} }
} }
vTaskDelete(NULL); vTaskDelete(NULL);
} }
bool Rotary::registerCallback(std::function<void(int)> callback) { bool Rotary::registerCallback(std::function<void(int, int)> callback) {
this->callback = callback; this->callback = callback;
return true; return true;
} }

4
src/rotary.h

@ -16,7 +16,7 @@ class Rotary {
public: public:
Rotary(); Rotary();
bool begin(uint8_t pinA, uint8_t pinB, uint8_t pinButton); bool begin(uint8_t pinA, uint8_t pinB, uint8_t pinButton);
bool registerCallback(std::function<void(int)> callback); bool registerCallback(std::function<void(int, int)> callback);
static void IRAM_ATTR doEncoder(void); static void IRAM_ATTR doEncoder(void);
static Rotary* instance; static Rotary* instance;
volatile int encoderPos = 0; volatile int encoderPos = 0;
@ -31,7 +31,7 @@ class Rotary {
uint8_t pinA; uint8_t pinA;
uint8_t pinB; uint8_t pinB;
uint8_t pinButton; uint8_t pinButton;
std::function<void(int)> callback = nullptr; std::function<void(int, int)> callback = nullptr;
TaskHandle_t taskHandle; TaskHandle_t taskHandle;
void task(void*); void task(void*);
static void cTaskWrapper(void*); static void cTaskWrapper(void*);

37
src/selectionlist.cpp

@ -4,4 +4,39 @@ SelectionList::SelectionList() {}
SelectionList::SelectionList(const char* title, uint8_t start_pos, const char* string_list) {} 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<visible; i++) {
u8g2.drawUTF8(item_x_offset,header_height+2+(i+1)*item_height,string_list[top_item+i].c_str());
}
u8g2.drawFrame(0,header_height+1+num_active_item*item_height,u8g2.getDisplayWidth(),item_height+2 );
u8g2.sendBuffer(); // transfer internal memory to the display
}
void SelectionList::next() {
if (current_pos < string_list.size()-1) current_pos++;
if (current_pos > 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;
}

41
src/selectionlist.h

@ -2,26 +2,43 @@
#define _SELECTIONLIST_H #define _SELECTIONLIST_H
#include <Arduino.h> #include <Arduino.h>
#include <vector>
#include <string>
#include <U8g2lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
extern U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2;
class SelectionList { class SelectionList {
public: public:
SelectionList(); SelectionList();
SelectionList(const char*, uint8_t, const char*); SelectionList(const char*, uint8_t, const char*);
void draw(void); void draw(void);
void next(void);
void previous(void);
uint8_t select(void);
private: private:
const char* title = "Cloud Types"; const char* title = "Cloud Types";
const char *string_list = std::vector<std::string> string_list = {
"Altocumulus\n" "Altocumulus\n",
"Altostratus\n" "Altostratus\n",
"Cirrocumulus\n" "Cirrocumulus\n",
"Cirrostratus\n" "Cirrostratus\n",
"Cirrus\n" "Cirrus\n",
"Cumulonimbus\n" "Cumulonimbus\n",
"Cumulus\n" "Cumulus\n",
"Nimbostratus\n" "Nimbostratus\n",
"Stratocumulus\n" "Stratocumulus\n",
"Stratus"; "Stratus" };
uint8_t current_selection = 1; uint8_t current_pos = 0;
uint8_t top_item = 0;
uint8_t visible = 4;
}; };
#endif /* _SELECTIONLIST_H */ #endif /* _SELECTIONLIST_H */

Loading…
Cancel
Save