Code:
// TFT Menu
// (C) 2018 by HaWe
// This example code is in the public domain for private use.
// Use for professional or business purpose: only
// by personal written permission by the author.
// default TFT: OLED 128x64, compatible to Adafruit (R) Libs
// in this example: using an ESP8266 NodeMCU 1.0 board
// using ButtonClass for button action (up, down, enter, single/double/long press)
// history:
// 0.0.1 tMenu new list
// simple menu example
// ver 0.0.1
// I2C
#include <Wire.h> // Incl I2C comm, but needed for not getting compile error
//-----------------------------------------------------------------------
// display driver
//-----------------------------------------------------------------------
#include <Adafruit_GFX.h> // https://github.com/adafruit/Adafruit-GFX-Library
// Adafruit Arduino OLED driver
#include <Adafruit_SSD1306.h> // https://github.com/esp8266/arduino
#include <Fonts/FreeSans9pt7b.h> // optional
#include <Fonts/FreeMono12pt7b.h> // optional
#include <Fonts/FreeMono9pt7b.h> // used here
// Pin definitions
#define OLED_RESET 10 // GPIO10=D12 Pin RESET signal (virtual)
//Adafruit_SSD1306 display(OLED_RESET); // old Adafruit lib (tested OK)
Adafruit_SSD1306 display(128, 64, &Wire, OLED_RESET); // new Adafruit lib
//-----------------------------------------------------------------------
// OLED menu
//-----------------------------------------------------------------------
#include <MenuClass.h>
char * mlist1[7] = {"Menu1 L0","L1","L2","LED ON","LED OFF", "L5", "L6"};
tMenu menu1(7,11, (char**)mlist1, &menu1, 1);
// numEntries, lineLength, menu_list, preMenu, menu-ID
tMenu * actMenu = &menu1;
//-----------------------------------------------------------------------
// ButtonClass buttons
//-----------------------------------------------------------------------
#include <ButtonClass.h>
// 3 buttons for menu control
tButton btnUp;
tButton btnDown;
tButton btnEnter;
//-----------------------------------------------------------------------
// setup
//-----------------------------------------------------------------------
void setup(void)
{
// Start Serial
Serial.begin(115200);
delay(3000); // wait for Serial()
Serial.println("Serial started");
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, !LOW); // inverted LED_BUILTIN signal logic
btnUp.init(D6, INPUT_PULLUP);
btnDown.init(D3, INPUT_PULLUP);
btnEnter.init(D4, INPUT_PULLUP);
// Start Wire (SDA, SCL)
//Wire.begin(ESPSDA,ESPSCL); // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Wire.begin();
// SSD1306 Init
//display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // old Adafruit lib
display.begin(SSD1306_SWITCHCAPVCC, 0x3C, true, false); // new Adafruit lib
display.setRotation(2);
display.clearDisplay(); // Clear the buffer.
// text display tests
display.setTextSize(1);
display.setFont();
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("Hello, world!");
display.display();
delay(500);
//--------------------------------------
// test + debug
display.setFont(&FreeMono9pt7b);
display.clearDisplay();
display.display();
Serial.println("menu init:");
actMenu = &menu1; //
actMenu->mdisplay();
Serial.println("\n\n");
}
//-----------------------------------------------------------------------
// loop
//-----------------------------------------------------------------------
void loop() {
int32_t ID;
int16_t ln;
char buf[20];
ln = actMenu->checkbtn(btnUp.click(), btnDown.click(), btnEnter.click() );
if(ln!=-1) { // double click btnEnter: do if selected
sprintf(buf, "select: line=%d ID=%d contents=%s", ln, ID, actMenu->list[ln]);
Serial.println(buf);
if(ln==0) { // menu ID 1, line 0
// do something
}
else
if(ln==1) { // menu ID 1, line 1
// do something
}
else
if(ln==2) { // menu ID 1, line 2: > next menu
// do something
}
else
if(ln==3) { // menu ID 1, line 3
digitalWrite(LED_BUILTIN, !HIGH); // inverted LED_BUILTIN signal logic
}
else
if(ln==4) { // menu ID 1, line 4
digitalWrite(LED_BUILTIN, !LOW); // inverted LED_BUILTIN signal logic
}
// etc.
}
}
// end of file
Lesezeichen