You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
435 lines
12 KiB
435 lines
12 KiB
#include "esp32-hal-gpio.h"
|
|
#include "esp32-hal.h"
|
|
#include "menuBase.h"
|
|
#include <Arduino.h>
|
|
|
|
/********#include "esp32-hal-gpio.h"
|
|
************
|
|
Arduino generic menu system
|
|
U8G2 menu exampl#include "esp32-hal-gpio.h"
|
|
e
|
|
U8G2: https://github.com/olikraus/u8g2
|
|
|
|
Oct. 2016 Stephen Denne https://github.com/datacute
|
|
Based on example from Rui Azevedo - ruihfazevedo(@rrob@)gmail.com
|
|
Original from: https://github.com/christophepersoz
|
|
|
|
menu on U8G2 device
|
|
output: Wemos D1 mini OLED Shield (SSD1306 64x48 I2C) + Serial
|
|
input: Serial + encoder
|
|
mcu: nano328p
|
|
|
|
*/
|
|
|
|
#include <cstddef>
|
|
#include <menu.h>
|
|
#include <menuIO/u8g2Out.h>
|
|
// #include <menuIO/encoderIn.h>
|
|
#include <menuIO/altKeyIn.h>
|
|
#include <menuIO/chainStream.h>
|
|
#include <menuIO/serialIn.h>
|
|
#include <menuIO/serialOut.h>
|
|
|
|
using namespace Menu;
|
|
|
|
#define LEDPIN 2
|
|
#define LOCKPIN LEDPIN
|
|
#define BTN_SEL 33
|
|
#define WAKE_UP_GPIO GPIO_NUM_33
|
|
#define BTN_UP 32
|
|
#define BTN_DOWN 25
|
|
|
|
#define LOCK_HOLD_DURATION 2000 // ms
|
|
#define UNLOCK_HOLD_DURATION 2000 // ms
|
|
// #define LOCK_ICON_DURATION 1000 // ms
|
|
#define SAFETY_HOLD_DURATION 5000 // ms
|
|
|
|
// #define USE_PCD8544
|
|
#define USE_SSD1306
|
|
|
|
#if defined(USE_PCD8544)
|
|
// rotary encoder pins
|
|
// #define encA 2
|
|
// #define encB 3
|
|
// #define encBtn 4
|
|
|
|
#include <SPI.h>
|
|
#define USE_HWSPI
|
|
#define U8_DC 9
|
|
#define U8_CS 8
|
|
#define U8_RST 7
|
|
#define fontName u8g2_font_5x7_tf
|
|
#define fontX 5
|
|
#define fontY 9
|
|
#define offsetX 0
|
|
#define offsetY 0
|
|
#define U8_Width 84
|
|
#define U8_Height 48
|
|
U8G2_PCD8544_84X48_1_4W_HW_SPI u8g2(U8G2_R0, U8_CS, U8_DC, U8_RST);
|
|
#elif defined(USE_SSD1306)
|
|
// rotary encoder pins
|
|
// #define encA 5
|
|
// #define encB 6
|
|
// #define encBtn 7
|
|
|
|
#include <Wire.h>
|
|
#define fontName u8g2_font_7x13_mf
|
|
#define fontX 7
|
|
#define fontY 16
|
|
#define offsetX 0
|
|
#define offsetY 3
|
|
#define U8_Width 128
|
|
#define U8_Height 64
|
|
#define USE_HWI2C
|
|
// U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0);//, SCL, SDA);
|
|
// U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R2, U8X8_PIN_NONE, 4, 5);
|
|
// U8G2_SSD1306_128X64_VCOMH0_F_HW_I2C u8g2(U8G2_R2, U8X8_PIN_NONE, 4, 5);
|
|
U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE, 4, 5);
|
|
#else
|
|
#error DEFINE YOUR OUTPUT HERE.
|
|
#endif
|
|
|
|
// define menu colors --------------------------------------------------------
|
|
// each color is in the format:
|
|
// {{disabled normal,disabled selected},{enabled normal,enabled selected,
|
|
// enabled editing}}
|
|
// this is a monochromatic color table
|
|
const colorDef<uint8_t> colors[6] MEMMODE = {
|
|
{{0, 0}, {0, 1, 1}}, // bgColor
|
|
{{1, 1}, {1, 0, 0}}, // fgColor
|
|
{{1, 1}, {1, 0, 0}}, // valColor
|
|
{{1, 1}, {1, 0, 0}}, // unitColor
|
|
{{0, 1}, {0, 0, 1}}, // cursorColor
|
|
{{1, 1}, {1, 0, 0}}, // titleColor
|
|
};
|
|
|
|
result doAlert(eventMask e, prompt &item);
|
|
result doLockScreen(menuOut &o, idleEvent e);
|
|
result activateLock();
|
|
result deactivateLock();
|
|
void decrement_timer();
|
|
result setTimeout();
|
|
|
|
int battery = 55;
|
|
int ledCtrl = LOW;
|
|
int timeout_s = 6;
|
|
|
|
result myLedOn() {
|
|
ledCtrl = HIGH;
|
|
return proceed;
|
|
}
|
|
result myLedOff() {
|
|
ledCtrl = LOW;
|
|
return proceed;
|
|
}
|
|
|
|
TOGGLE(ledCtrl, setLed, "Led: ", doNothing, noEvent,
|
|
noStyle //,doExit,enterEvent,noStyle
|
|
,
|
|
VALUE("On", HIGH, doNothing, noEvent),
|
|
VALUE("Off", LOW, doNothing, noEvent));
|
|
|
|
enum theme { PROGRESS_BAR_THEME, TIMER_THEME, NOTHING_THEME };
|
|
enum theme selectedTheme = PROGRESS_BAR_THEME;
|
|
CHOOSE(selectedTheme, selTheme, "Theme:", doNothing, noEvent, noStyle,
|
|
VALUE("Progress Bar", PROGRESS_BAR_THEME, doNothing, noEvent),
|
|
VALUE("Timer", TIMER_THEME, doNothing, noEvent),
|
|
VALUE("Nothing...", NOTHING_THEME, doNothing, noEvent));
|
|
|
|
// int chooseTest = -1;
|
|
// CHOOSE(chooseTest, chooseMenu, "Choose", doNothing, noEvent, noStyle,
|
|
// VALUE("First", 1, doNothing, noEvent),
|
|
// VALUE("Second", 2, doNothing, noEvent),
|
|
// VALUE("Third", 3, doNothing, noEvent),
|
|
// VALUE("Last", -1, doNothing, noEvent));
|
|
|
|
// //customizing a prompt look!
|
|
// //by extending the prompt class
|
|
// class altPrompt:public prompt {
|
|
// public:
|
|
// altPrompt(constMEM promptShadow& p):prompt(p) {}
|
|
// Used printTo(navRoot &root,bool sel,menuOut& out, idx_t idx,idx_t len,idx_t
|
|
// panelNr) override {
|
|
// return out.printRaw(F("special prompt!"),len);;
|
|
// }
|
|
// };
|
|
|
|
// char *constMEM hexDigit MEMMODE = "0123456789ABCDEF";
|
|
// char *constMEM hexNr[] MEMMODE = {"0", "x", hexDigit, hexDigit};
|
|
// char buf1[] = "0x11";
|
|
|
|
// char lockTimeBuf[] = "HH:MM:SS";
|
|
|
|
MENU(lockMenu, "Lock Now!", doNothing, noEvent, noStyle,
|
|
EXIT("Cancel"), //
|
|
OP("Confirm", doLockScreen, enterEvent));
|
|
|
|
MENU(subMenu, "Settings", doNothing, noEvent, noStyle,
|
|
// altOP(altPrompt, "", doNothing, noEvent) SUBMENU(setLed), //
|
|
// OP("LED On", myLedOn, enterEvent), //
|
|
// OP("LED Off", myLedOff, enterEvent), //
|
|
FIELD(battery, "Battery", "%", 0, 100, 0, 0, doNothing, noEvent, noStyle),
|
|
FIELD(timeout_s, "Timeout", "s", 0, 3600, 10, 1, setTimeout, updateEvent,
|
|
noStyle),
|
|
SUBMENU(setLed), //
|
|
SUBMENU(selTheme), //
|
|
OP("Alert test", doAlert, enterEvent), //
|
|
OP("Lock test", activateLock, enterEvent), //
|
|
OP("Unlock test", deactivateLock, enterEvent), //
|
|
// EDIT("Hex", buf1, hexNr, doNothing, noEvent, noStyle), //
|
|
EXIT("<Back"));
|
|
|
|
uint16_t hrs = 0;
|
|
uint16_t mins = 0;
|
|
uint16_t secs = 10;
|
|
|
|
// define a pad style menu (single line menu)
|
|
// here with a set of fields to enter a date in YYYY/MM/DD format
|
|
altMENU(menu, timeMenu, "Time", doNothing, noEvent, noStyle,
|
|
(systemStyles)(_asPad | Menu::_menuData | Menu::_canNav | _parentDraw),
|
|
FIELD(hrs, "", ":", 0, 11, 1, 0, doNothing, noEvent, noStyle),
|
|
FIELD(mins, "", ":", 0, 59, 10, 1, doNothing, noEvent, wrapStyle),
|
|
FIELD(secs, "", "", 0, 59, 10, 1, doNothing, noEvent, wrapStyle));
|
|
|
|
MENU(mainMenu, "Main menu", doNothing, noEvent, wrapStyle,
|
|
SUBMENU(lockMenu), //
|
|
//,FIELD(test,"Test","%",0,100,10,1,doNothing,noEvent,wrapStyle)
|
|
SUBMENU(timeMenu), //
|
|
SUBMENU(subMenu), //
|
|
EXIT("<Exit"));
|
|
|
|
#define MAX_DEPTH 3
|
|
|
|
// encoderIn<encA,encB> encoder;//simple quad encoder driver
|
|
// encoderInStream<encA,encB> encStream(encoder,4);// simple quad encoder fake
|
|
// Stream
|
|
|
|
keyMap joystickBtn_map[] = {
|
|
{BTN_SEL, defaultNavCodes[enterCmd].ch, INPUT_PULLUP},
|
|
{BTN_UP, defaultNavCodes[upCmd].ch, INPUT_PULLUP},
|
|
{BTN_DOWN, defaultNavCodes[downCmd].ch, INPUT_PULLUP}}; // negative pin
|
|
keyIn<3> joystickBtns(joystickBtn_map); // the input driver
|
|
serialIn serial(Serial);
|
|
|
|
// numbers use internal pull-up, this is on when low keyIn<1>
|
|
// encButton(encBtn_map); // 1 is the number of keys
|
|
|
|
// menuIn *inputsList[] = {&joystickBtns, &serial};
|
|
// chainStream<2> in(inputsList); // 1 is the number of inputs
|
|
|
|
// MENU_INPUTS(in, &serial);
|
|
MENU_INPUTS(in, &joystickBtns, &serial);
|
|
|
|
MENU_OUTPUTS(out, MAX_DEPTH,
|
|
U8G2_OUT(u8g2, colors, fontX, fontY, offsetX, offsetY,
|
|
{0, 0, U8_Width / fontX, U8_Height / fontY}),
|
|
SERIAL_OUT(Serial));
|
|
|
|
NAVROOT(nav, mainMenu, MAX_DEPTH, in, out);
|
|
|
|
result alert(menuOut &o, idleEvent e) {
|
|
if (e == idling) {
|
|
o.setCursor(0, 0);
|
|
o.print("alert test");
|
|
o.setCursor(0, 1);
|
|
o.print("press [select]");
|
|
o.setCursor(0, 2);
|
|
o.print("to continue...");
|
|
}
|
|
return proceed;
|
|
}
|
|
|
|
result setTimeout() {
|
|
nav.timeOut = timeout_s;
|
|
return proceed;
|
|
}
|
|
|
|
unsigned long lock_timer_s = 0; // this decrements
|
|
unsigned long lock_time_s = 0; // this doesn't
|
|
|
|
///////////////////////
|
|
// TIME STRING LOGIC //
|
|
///////////////////////
|
|
char time_string[20];
|
|
unsigned get_seconds(unsigned long time) { return time % 60; }
|
|
unsigned get_minutes(unsigned long time) { return (time % 3600) / 60; }
|
|
unsigned get_hrs(unsigned long time) { return (time / 3600); }
|
|
|
|
void set_time_string() {
|
|
sprintf(time_string, "%02u:%02u:%02u", get_hrs(lock_timer_s),
|
|
get_minutes(lock_timer_s), get_seconds(lock_timer_s));
|
|
}
|
|
|
|
////////////////////
|
|
// LOCKING LOGIC! //
|
|
////////////////////
|
|
unsigned long lock_hold_start;
|
|
|
|
result activateLock() {
|
|
ledCtrl = HIGH; // for demo
|
|
digitalWrite(LOCKPIN, HIGH);
|
|
return proceed;
|
|
}
|
|
result deactivateLock() {
|
|
ledCtrl = LOW; // for demo
|
|
digitalWrite(LOCKPIN, LOW);
|
|
return proceed;
|
|
}
|
|
|
|
result lockScreen(menuOut &o, idleEvent e) {
|
|
Serial.println("lockScreen()");
|
|
activateLock();
|
|
int progress;
|
|
|
|
while (lock_timer_s > 0) {
|
|
set_time_string();
|
|
u8g2.firstPage();
|
|
do {
|
|
if (millis() - lock_hold_start <=
|
|
/* LOCK_ICON_DURATION + */ LOCK_HOLD_DURATION) {
|
|
// u8g.drawBox(0, 0, 15, 15);
|
|
u8g2.drawStr(25, 50, "LOCKING ...");
|
|
// u8g2.drawXBMP((128 - 26) / 2, 1, 26, 38, epd_bitmap_lock);
|
|
} else {
|
|
switch (selectedTheme) {
|
|
case PROGRESS_BAR_THEME:
|
|
progress = map(lock_timer_s, 0, lock_time_s, 0, 108);
|
|
u8g2.drawBox(10, 15, progress, 10);
|
|
// print progress bar
|
|
u8g2.drawFrame(0, 10, 128, 20);
|
|
u8g2.drawStr(30, 50, time_string);
|
|
break;
|
|
case TIMER_THEME:
|
|
u8g2.drawStr(30, 50, time_string);
|
|
break;
|
|
case NOTHING_THEME:
|
|
break;
|
|
}
|
|
decrement_timer();
|
|
}
|
|
|
|
} while (u8g2.nextPage());
|
|
}
|
|
return proceed;
|
|
}
|
|
|
|
result doLockScreen(menuOut &o, idleEvent e) {
|
|
Serial.println("doLockScreen()");
|
|
lock_timer_s = secs + (mins * 60) + (hrs * 3600);
|
|
lock_hold_start = millis();
|
|
lock_time_s = lock_timer_s;
|
|
nav.idleOn(lockScreen);
|
|
nav.idleOff();
|
|
deactivateLock();
|
|
nav.exit();
|
|
return proceed;
|
|
}
|
|
|
|
result doAlert(eventMask e, prompt &item) {
|
|
Serial.println("doAlert()");
|
|
nav.idleOn(alert);
|
|
return proceed;
|
|
}
|
|
|
|
// when menu is suspended
|
|
result idle(menuOut &o, idleEvent e) {
|
|
Serial.println("idle()");
|
|
o.clear();
|
|
switch (e) {
|
|
case idleStart:
|
|
o.println("suspending menu!");
|
|
break;
|
|
case idling:
|
|
o.println("suspended...");
|
|
break;
|
|
case idleEnd:
|
|
o.println("resuming menu.");
|
|
break;
|
|
}
|
|
return proceed;
|
|
}
|
|
|
|
void setup() {
|
|
joystickBtns.begin();
|
|
pinMode(BTN_UP, INPUT_PULLUP);
|
|
pinMode(BTN_DOWN, INPUT_PULLUP);
|
|
pinMode(BTN_SEL, INPUT_PULLUP);
|
|
|
|
Serial.begin(115200);
|
|
while (!Serial)
|
|
;
|
|
Serial.println("menu 4.x test");
|
|
Serial.flush();
|
|
// encButton.begin();
|
|
// encoder.begin();
|
|
pinMode(LEDPIN, OUTPUT); // cant use pin 13 when using hw spi
|
|
// and on esp12 i2c can be on pin 2, and that is also led pin
|
|
// so check first if this is adequate for your board
|
|
#if defined(USE_HWSPI)
|
|
SPI.begin();
|
|
u8g2.begin();
|
|
#elif defined(USE_HWI2C)
|
|
Wire.begin();
|
|
u8g2.begin();
|
|
#else
|
|
#error "please choose your interface (I2c,SPI)"
|
|
#endif
|
|
u8g2.setFont(fontName);
|
|
// u8g2.setBitmapMode(0);
|
|
|
|
// disable second option
|
|
// mainMenu[1].enabled = disabledStatus;
|
|
nav.idleTask = idle; // point a function to be used when menu is suspended
|
|
setTimeout();
|
|
options->invertFieldKeys = true;
|
|
Serial.println("setup done.");
|
|
Serial.flush();
|
|
|
|
esp_sleep_enable_ext0_wakeup(WAKE_UP_GPIO, 0);
|
|
}
|
|
|
|
int lastSleep;
|
|
int thisSleep;
|
|
void loop() {
|
|
lastSleep = nav.sleepTask == NULL ? 0 : 1;
|
|
nav.doInput();
|
|
thisSleep = nav.sleepTask == NULL ? 0 : 1;
|
|
digitalWrite(LEDPIN, ledCtrl);
|
|
if (nav.sleepTask) {
|
|
u8g2.firstPage();
|
|
do {
|
|
u8g2.setCursor(0, 15);
|
|
u8g2.setPowerSave(1);
|
|
esp_light_sleep_start();
|
|
} while (u8g2.nextPage());
|
|
} else if (lastSleep != thisSleep) {
|
|
Serial.println("Waking Up!");
|
|
u8g2.setPowerSave(0);
|
|
} else if (nav.changed(0)) { // only draw if menu changed for gfx device
|
|
// change checking leaves more time for other tasks
|
|
u8g2.firstPage();
|
|
do
|
|
nav.doOutput();
|
|
while (u8g2.nextPage());
|
|
}
|
|
// delay(100); // simulate other tasks delay
|
|
}
|
|
|
|
void decrement_timer() {
|
|
unsigned long timer_interval = 1000; // decrement rate
|
|
static unsigned long timer_previousMillis =
|
|
0; // Variable to store the last time the decrement occurred
|
|
unsigned long currentMillis =
|
|
millis(); // Get the current time in milliseconds
|
|
|
|
// Check if it's time to decrement
|
|
if (currentMillis - timer_previousMillis >= timer_interval) {
|
|
|
|
// Update the previous time
|
|
timer_previousMillis = currentMillis;
|
|
|
|
// Decrement the variable
|
|
if (lock_timer_s > 0)
|
|
lock_timer_s--;
|
|
}
|
|
}
|
|
|