Skip to content

Instantly share code, notes, and snippets.

@Spirik
Last active March 24, 2024 16:54
Show Gist options
  • Save Spirik/1a5fdd54f809035950a655f7daa2d9a8 to your computer and use it in GitHub Desktop.
Save Spirik/1a5fdd54f809035950a655f7daa2d9a8 to your computer and use it in GitHub Desktop.
test_65_gem_u8g2_sh1106_encoder_proxy-menu-page
/*
Modified basic menu example using GEM library. Using rotary encoder as an input source.
Create submenu page that holds proxy variables, which update real variables upon save. Menu can have multiple menu items linked
to the same proxy page.
U8g2lib library is used to draw menu.
KeyDetector library (version 1.2.0 or later) is used to detect rotary encoder operation.
Additional info (including the breadboard view) available on GitHub:
https://github.com/Spirik/GEM
This example code is in the public domain.
*/
#include <GEM_u8g2.h>
#include <KeyDetector.h>
// Define signal identifiers for three outputs of encoder (channel A, channel B and a push-button)
#define KEY_A 1
#define KEY_B 2
#define KEY_C 3
// Pins encoder is connected to
const byte channelA = 2;
const byte channelB = 3;
const byte buttonPin = 4;
// Array of Key objects that will link GEM key identifiers with dedicated pins
// (it is only necessary to detect signal change on a single channel of the encoder, either A or B;
// order of the channel and push-button Key objects in an array is not important)
Key keys[] = {{KEY_A, channelA}, {KEY_C, buttonPin}};
//Key keys[] = {{KEY_C, buttonPin}, {KEY_A, channelA}};
// Create KeyDetector object
// KeyDetector myKeyDetector(keys, sizeof(keys)/sizeof(Key));
// To account for switch bounce effect of the buttons (if occur) you may want to specify debounceDelay
// as the third argument to KeyDetector constructor.
// Make sure to adjust debounce delay to better fit your rotary encoder.
// Also it is possible to enable pull-up mode when buttons wired with pull-up resistors (as in this case).
// Analog threshold is not necessary for this example and is set to default value 16.
KeyDetector myKeyDetector(keys, sizeof(keys)/sizeof(Key), /* debounceDelay= */ 10, /* analogThreshold= */ 16, /* pullup= */ true);
bool secondaryPressed = false; // If encoder rotated while key was being pressed; used to prevent unwanted triggers
bool cancelPressed = false; // Flag indicating that Cancel action was triggered, used to prevent it from triggering multiple times
const int keyPressDelay = 1000; // How long to hold key in pressed state to trigger Cancel action, ms
long keyPressTime = 0; // Variable to hold time of the key press event
long now; // Variable to hold current time taken with millis() function at the beginning of loop()
// Create an instance of the U8g2 library.
// Use constructor that matches your setup (see https://github.com/olikraus/u8g2/wiki/u8g2setupcpp for details).
// This instance is used to call all the subsequent U8g2 functions (internally from GEM library,
// or manually in your sketch if it is required).
// Please update the pin numbers according to your setup. Use U8X8_PIN_NONE if the reset pin is not connected
U8G2_SH1106_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
// Create variables
int number[] = {128, 256, 512};
bool enablePrint[] = {true, false, true};
// Config pages names
const char* configPageName[] = {"Config 0", "Config 1", "Config 2"};
byte currentConfigIndex = 0;
// Create variables that will be editable through the menu and assign them initial values
int numberProxy = 0;
bool enablePrintProxy = false;
// Create variable that will be editable through option select and create associated option select.
// This variable will be passed to menu.invertKeysDuringEdit(), and naturally can be presented as a boolean,
// but is declared as a byte type to be used in an option select rather than checkbox (for demonstration purposes)
byte invert = 1;
SelectOptionByte selectInvertOptions[] = {{"Invert", 1}, {"Normal", 0}};
GEMSelect selectInvert(sizeof(selectInvertOptions)/sizeof(SelectOptionByte), selectInvertOptions);
// Create menu item for option select with applyInvert() callback function
void applyInvert(); // Forward declaration
GEMItem menuItemInvert("Chars order:", invert, selectInvert, applyInvert);
// Create two menu item objects of class GEMItem, linked to numberProxy and enablePrintProxy variables
void numberProxySaveCallback();
GEMItem menuItemInt("Number:", numberProxy, numberProxySaveCallback);
void enablePrintProxySaveCallback();
GEMItem menuItemBool("Enable print:", enablePrintProxy, enablePrintProxySaveCallback);
// Create menu button that will trigger printData() function. It will print value of our number variable
// to Serial monitor if enablePrint is true. We will write (define) this function later. However, we should
// forward-declare it in order to pass to GEMItem constructor
void printData(); // Forward declaration
GEMItem menuItemButton("Print Number 0", printData);
// Create menu page object of class GEMPage. Menu page holds menu items (GEMItem) and represents menu level.
// Menu can have multiple menu pages (linked to each other) with multiple menu items each
GEMPage menuPageMain("Main Menu"); // Main page
GEMPage menuPageSettings("Settings", menuPageMain); // Settings submenu
GEMPage menuPageConfig("Config", menuPageMain); // Config submenu
// Create menu item linked to Settings menu page
GEMItem menuItemMainSettings("Settings", menuPageSettings);
// Create menu item linked to Config menu page
GEMItem menuItemMainConfig[] {
{configPageName[0], menuPageConfig},
{configPageName[1], menuPageConfig},
{configPageName[2], menuPageConfig}
};
// Create menu object of class GEM_u8g2. Supply its constructor with reference to u8g2 object we created earlier
GEM_u8g2 menu(u8g2);
void setup() {
// Pin modes
pinMode(channelA, INPUT_PULLUP);
pinMode(channelB, INPUT_PULLUP);
pinMode(buttonPin, INPUT_PULLUP);
// Serial communication setup
Serial.begin(115200);
// U8g2 library init.
u8g2.begin();
// Turn inverted order of characters during edit mode on (feels more natural when using encoder)
menu
.invertKeysDuringEdit(invert)
// Menu init, setup and draw
.init();
setupMenu();
menu.drawMenu();
Serial.println("Initialized");
}
void setupMenu() {
// Add menu items to menu page
menuPageMain
.addMenuItem(menuItemMainSettings)
.addMenuItem(menuItemMainConfig[0])
.addMenuItem(menuItemMainConfig[1])
.addMenuItem(menuItemMainConfig[2])
.addMenuItem(menuItemButton);
// Add menu items to Settings menu page
menuPageSettings
.addMenuItem(menuItemInvert);
// Add menu items to Config menu page
menuPageConfig
.addMenuItem(menuItemInt)
.addMenuItem(menuItemBool);
// Add menu page to menu and set it as current
menu.setMenuPageCurrent(menuPageMain);
}
void loop() {
// Get current time to use later on
now = millis();
// If menu is ready to accept button press...
if (menu.readyForKey()) {
// ...detect key press using KeyDetector library
// and pass pressed button to menu
myKeyDetector.detect();
switch (myKeyDetector.trigger) {
case KEY_C:
// Button was pressed
// Serial.println("Button pressed");
// Save current time as a time of the key press event
keyPressTime = now;
const char* title = menu.getCurrentMenuPage()->getCurrentMenuItem()->getTitle();
for (byte i = 0; i < 3; i++) {
if (strcmp(title, configPageName[i]) == 0) {
currentConfigIndex = i;
}
}
menuPageConfig.setTitle(menuItemMainConfig[currentConfigIndex].getTitle());
passValuesToProxy();
break;
}
/* Detecting rotation of the encoder on release rather than push
(i.e. myKeyDetector.triggerRelease rather myKeyDetector.trigger)
may lead to more stable readings (without excessive signal ripple) */
switch (myKeyDetector.triggerRelease) {
case KEY_A:
// Signal from Channel A of encoder was detected
if (digitalRead(channelB) == LOW) {
// If channel B is low then the knob was rotated CCW
if (myKeyDetector.current == KEY_C) {
// If push-button was pressed at that time, then treat this action as GEM_KEY_LEFT,...
// Serial.println("Rotation CCW with button pressed (release)");
menu.registerKeyPress(GEM_KEY_LEFT);
// Button was in a pressed state during rotation of the knob, acting as a modifier to rotation action
secondaryPressed = true;
} else {
// ...or GEM_KEY_UP otherwise
// Serial.println("Rotation CCW (release)");
menu.registerKeyPress(GEM_KEY_UP);
}
} else {
// If channel B is high then the knob was rotated CW
if (myKeyDetector.current == KEY_C) {
// If push-button was pressed at that time, then treat this action as GEM_KEY_RIGHT,...
// Serial.println("Rotation CW with button pressed (release)");
menu.registerKeyPress(GEM_KEY_RIGHT);
// Button was in a pressed state during rotation of the knob, acting as a modifier to rotation action
secondaryPressed = true;
} else {
// ...or GEM_KEY_DOWN otherwise
// Serial.println("Rotation CW (release)");
menu.registerKeyPress(GEM_KEY_DOWN);
}
}
break;
case KEY_C:
// Button was released
// Serial.println("Button released");
if (!secondaryPressed) {
// If button was not used as a modifier to rotation action...
if (now <= keyPressTime + keyPressDelay) {
// ...and if not enough time passed since keyPressTime,
// treat key that was pressed as Ok button
menu.registerKeyPress(GEM_KEY_OK);
}
}
secondaryPressed = false;
cancelPressed = false;
break;
}
// After keyPressDelay passed since keyPressTime
if (now > keyPressTime + keyPressDelay) {
switch (myKeyDetector.current) {
case KEY_C:
if (!secondaryPressed && !cancelPressed) {
// If button was not used as a modifier to rotation action, and Cancel action was not triggered yet
// Serial.println("Button remained pressed");
// Treat key that was pressed as Cancel button
menu.registerKeyPress(GEM_KEY_CANCEL);
cancelPressed = true;
}
break;
}
}
}
}
void printData() {
// If enablePrint flag is set to true (checkbox on screen is checked)...
if (enablePrint[0]) {
// ...print the number to Serial
Serial.print("Number is: ");
Serial.println(number[0]);
} else {
Serial.println("Printing is disabled, sorry:(");
}
}
void applyInvert() {
menu.invertKeysDuringEdit(invert);
// Print invert variable to Serial
Serial.print("Invert: ");
Serial.println(invert);
}
void passValuesToProxy() {
numberProxy = number[currentConfigIndex];
enablePrintProxy = enablePrint[currentConfigIndex];
}
void numberProxySaveCallback() {
number[currentConfigIndex] = numberProxy;
}
void enablePrintProxySaveCallback() {
enablePrint[currentConfigIndex] = enablePrintProxy;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment