Code:
// I2C
#include <Wire.h> // Incl I2C comm, but needed for not getting compile error
#define ESPSDA D2 // defaults
#define ESPSCL D1 //
// TFT
//#include <Adafruit_SSD1306.h>
#include <ESP_SSD1306.h> // Modification of Adafruit_SSD1306 for ESP8266 compatibility
#include <Adafruit_GFX.h> // Needs a little change in original Adafruit library (See README.txt file)
#include <Fonts/FreeSans12pt7b.h> //
#include <Fonts/FreeSansBold12pt7b.h> //
#include <Fonts/FreeSans9pt7b.h> //
#include <Fonts/FreeMono12pt7b.h> //
#include <Fonts/FreeMono9pt7b.h>
// Pin definitions
//----------------------------------------------------------------------------
// display driver
//----------------------------------------------------------------------------
#define OLED_RESET 10 // GPIO10=D12 Pin RESET signal (virtual)
//Adafruit_SSD1306 display(OLED_RESET);
//Adafruit_SSD1306 display(128, 64, &Wire, OLED_RESET);
ESP_SSD1306 display(OLED_RESET);
//----------------------------------------------------------------------------
// ButtonClass buttons
//----------------------------------------------------------------------------
#include <ButtonClass.h>
// 3 buttons for menu control
tButton btn0;
tButton btn1;
tButton btnfx;
//----------------------------------------------------------------------------
// OLED menu
//----------------------------------------------------------------------------
class tMenu {
protected:
int16_t MENULEN, LINELEN, VISLNUM, FONTHI;
char buf[20];
int8_t firstvln, lastvln, displn;
public:
int16_t ID, pre_ID;
int8_t act, pre_act;
char **list;
int test;
tMenu (int16_t menulen, int16_t linelen, int16_t id) // constructor
{
firstvln=0; lastvln=0; displn=0;
pre_ID=0;
act=0;
pre_act=0;
MENULEN = menulen; // number of available menu options
LINELEN = linelen; // line length of menu options
ID = id;
list = new char*[MENULEN];
for(int i = 0; i < MENULEN; i++)
{
list[i] = new char[LINELEN+1];
}
}
void init(int16_t vislnum=5, byte fonthi=13 ) { // ()=defaults=(5,13)
for(int i=0; i<MENULEN; i++) {
sprintf(buf,"%d .......|", i);
strncpy(list[i], buf, LINELEN);
VISLNUM = vislnum; // number of visible menu options
FONTHI = fonthi;
}
}
void mdisplay() {
if(act>VISLNUM-1) firstvln=_min(act-1, MENULEN-VISLNUM);
else firstvln=0;
lastvln=firstvln+VISLNUM-1;
display.clearDisplay();
for(byte i=firstvln; i<=lastvln; i++) {
displn=(FONTHI-3) + (i-firstvln)*FONTHI;
if(i==act) {
display.setCursor(0, displn); display.print('>');
Serial.print('>');
}
else Serial.print(' ');
Serial.println(list[i]);
display.setCursor(11, displn); display.print(list[i]);
}
display.display();
Serial.println();
}
int32_t checkbtn(int8_t btop, int8_t bbtm, int8_t bfunc) {
if(btop==1){ // dec
if(act>0) act--;
//Serial.print ("^"); Serial.println(act);
mdisplay();
return 11 ;
}
if(btop==3){ // min
act=0;
//Serial.print ("^"); Serial.println(act);
mdisplay();
return 13 ;
}
if(bbtm==1){ // inc
if(act<MENULEN-1) act++;
//Serial.print ("v"); Serial.println(act);
mdisplay();
return 21;
}
if(bbtm==3){ // max
act=MENULEN-1;
//Serial.print ("v"); Serial.println(act);
mdisplay();
return 23;
}
if(bfunc==1){
sprintf(buf,"Menu %d-%d ToDo=%d", ID,act,bfunc);
Serial.println(buf);
return 1;
}
if(bfunc==2){
sprintf(buf,"Menu %d-%d ToDo=%d", ID,act,bfunc);
Serial.println(buf);
}
if(bfunc==3){
sprintf(buf,"Menu %d-%d ToDo=%d", ID,act,bfunc);
Serial.println(buf);
return 3;
}
return 0;
}
~tMenu() { // destructor
// Dynamically delete the array
delete[] list ;
}
};
tMenu menu0(10,20, 0); // numEntries, lineLength, menuID;
tMenu menu02(10,20, 2);
tMenu & actMenu = menu0;
tMenu & preMenu = menu0;
//----------------------------------------------------------------------------
// OLED dashboard
//----------------------------------------------------------------------------
void dashboard(byte mode) {
display.clearDisplay();
display.setFont();
if (mode == 0) {
display.setFont(); // h=8.0 pt
display.setCursor( 0, 0); display.print(" 0 Hello World 1");
}
display.display();
display.setFont();
}
void setup(void)
{
// Start Serial
//pinMode(D3, OUTPUT); // + LED_BUILTIN
btn0.init(D6, INPUT_PULLUP, 50);
btn1.init(D3, INPUT_PULLUP, 50);
btnfx.init(D4, INPUT_PULLUP, 50);
//pinMode(D7, OUTPUT);
//pinMode(D8, OUTPUT);
Serial.begin(115200);
delay(2000); // wait for Serial()
Serial.println("Serial started");
// Start Wire (SDA, SCL)
//Wire.begin(ESPSDA,ESPSCL); // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Wire.begin();
// SSD1306 Init
display.begin(SSD1306_SWITCHCAPVCC); // Switch OLED
//display.begin(SSD1306_SWITCHCAPVCC, 0x3D, true, false);
display.setRotation(2);
display.clearDisplay(); // Clear the buffer.
// text display tests
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("Hello, world!");
display.display();
//--------------------------------------
// test + debug
Serial.println();
for(byte i=0; i<10; i++) {
Serial.print("-");
Serial.println(menu0.list[i]);
}
menu0.init(); // optional numVisibleLines, fontHeight; defaults = (5,13)
display.clearDisplay();
display.display();
display.setFont(&FreeMono9pt7b); // h=13.10pt
menu0.mdisplay();
}
void loop() {
int32_t result;
result = menu0.checkbtn(btn0.click(), btn1.click(), btnfx.click() );
&actMenu = menu02; // test
}
Lesezeichen