I'm a neophyte programmer and I've always gotten confused by pointers and references, so I'm not sure how to address the problem I'm having.
I'm using the lcdgfx library to drive a Zio OLED module. I'm making use of the library's menu function to create and navigate via menus. I'm using a sparkfun twist I2C encoder to navigate through the menus.
Per my understanding of the library, the syntax is to use an object reference (not sure if this is the correct term) with various commands to manipulate the menu. I.E. menu.up() scrolls up on the item named "menu" and submenu.up() scrolls up on the item named "submenu".
I want to write a function such as:
void menu_control(nameofmenu)
{
menu_count = twist.getDiff(); //Get encoder values
if (menu_count > 0){
nameofmenu.up();
menu_count = 0;
}
else if (menu_count < 0){
nameofmenu.down();
menu_count = 0;
}
}
In order to re-use the same menu control code in the main menus as well as the submenus.
Based on my reading, I think I need to pass a reference (?) from the function declaration and then use that in lieu of menu.command. Based on all my reading you define that as:
void menu_control([i]SomeVariable [/i]&nameofmenu);
{
code goes here
}
My problem is I don't understand what goes in for "SomeVariable"? Every example I've read is an example where you define both functions and in this case I didn't write the library. I've looked at their documentation (LCDGFX LCD display driver: LcdGfxMenu Class Reference) and I don't understand what type to use. How can I go about understanding this better?
Full code below:
/*
MIT License
Copyright (c) 2019-2020, Alexey Dynda
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/**
* Nano/Atmega328 PINS: connect LCD to D5 (D/C), D4 (CS), D3 (RES), D11(DIN), D13(CLK)
* Attiny SPI PINS: connect LCD to D4 (D/C), GND (CS), D3 (RES), D1(DIN), D2(CLK)
* ESP8266: connect LCD to D1(D/C), D2(CS), RX(RES), D7(DIN), D5(CLK)
*/
/* !!! THIS DEMO RUNS in SSD1306 COMPATIBLE MODE */
#include "lcdgfx.h"
#include "lcdgfx_gui.h"
#include "SparkFun_Qwiic_Twist_Arduino_Library.h"
//#include "sova.h"
DisplaySSD1327_128x128_I2C display(-1); // This line is suitable for most platforms by default
TWIST twist; //Create instance of this object
int menu_count = 0;
int menu_selec = -1;
const char *menuItems[] =
{
"Set Power",
"Set Duration",
"Start",
};
const char *SubmenuItems[] =
{
"25%",
"50%",
"75%",
"100%",
"Back",
};
LcdGfxMenu menu( menuItems, sizeof(menuItems) / sizeof(char *));
LcdGfxMenu submenu( SubmenuItems, sizeof(SubmenuItems) / sizeof(char *));
void setup()
{
display.begin();
display.setFixedFont(ssd1306xled_font8x16);
display.clear( );
display.setColor(GRAY_COLOR4(255));
//Serial.begin(9600);
//Serial.println("Qwiic Twist Example");
if(twist.begin() == false)
{
Serial.println("Twist does not appear to be connected. Please check wiring. Freezing...");
while(1);
}
}
static void selec1()
{
display.clear();
do{
submenu.show( display );
menu_control(submenu);
} while (1);
}
static void selec2()
{
display.clear();
display.setColor(GRAY_COLOR4(255));
display.printFixed(0, 8, "Selection2", STYLE_NORMAL);
delay(3000);
}
static void selec3()
{
display.clear();
display.setColor(GRAY_COLOR4(255));
display.printFixed(0, 8, "Selection3", STYLE_NORMAL);
delay(3000);
}
void menu_control(char &themenu)
{
menu_count = twist.getDiff();
if (menu_count > 0){
themenu.up();
menu_count = 0;
}
else if (menu_count < 0){
themenu.down();
menu_count = 0;
}
}
void loop()
{
menu.show( display );
menu_control(menu);
if (twist.isPressed()){
menu_selec = menu.selection();
}
switch ( menu_selec )
{
case 0:
selec1();
break;
case 1:
selec2();
break;
case 2:
selec3();
break;
default:
break;
}
menu_selec = -1;
}