Passing a function object reference on a 3rd part function

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;
}

I don't use the library but this seems like a generic question

assume you have 2 instances of a class

LcdGfxMenu menu1(...);
LcdGfxMenu menu2(...);

and you want to write a function that will work on one such instance.

you could decide to use the passing by reference

void goUP(LcdGfxMenu & aMenu) {
  aMenu.up();
}

The & in the parameters means that the function gets a reference to a LcdGfxMenu instance, and you would just call the function with the object, for example with menu1 goUP(menu1);

if you want to use a pointer, then the function will be

void goUP_ptr(LcdGfxMenu * aMenuPtr) {
  aMenuPtr->up();
}

The * says the parameter is a pointer to a LcdGfxMenu object, and you would call the function with the object address, for example with menu2goUP_ptr(&menu2); // the & is for 'address of'

does this help?

Ahhhh that did it, thank you! I used the 1st half of your suggestion and passed it by reference. To make sure I understand it....

You have to add LcdGfxMenu to the function:

void goUP(LcdGfxMenu & aMenu) {
  aMenu.up();
}

Because you're saying, "Take the address-of & aMenu and use it with class LcdGfxMenu on function .up()". Do I have that correct?

Also, would:

void goUP(LcdGfxMenu & aMenu) {
  aMenu.up();
}

and

void goUP(LcdGfxMenu &aMenu) {[color=#222222][/color]
  aMenu.up();[color=#222222][/color]
}

Be the same? Thanks so much again!

The color stuff in the code makes it difficult to know what you ask for

When a function takes a parameter, you need to tell the compiler what’s the type of the parameter. For example invoid calcuate(int x) {…}i am saying the function calculate takes one parameter of type int and I can call it with

int val=3;
calculate(val);
// or
calculate(32);

When you want the parameter to be a reference to some complex type you add a & next to the variable, hence in your case the type is LcdGfxMenu so if the parameter is passed by reference you write void goUP(LcdGfxMenu & aMenu) {...}in that case the & does not mean address of, because you are in function parameter definition it means this parameter will be passed by reference.
There can be a space before or after the &, it does not change anything. The three versions below are exactly the same, the compiler does not care.

void goUP(LcdGfxMenu& aMenu) {…
void goUP(LcdGfxMenu &aMenu)  {…
void goUP(LcdGfxMenu & aMenu)  {…

Got it, thank you! No idea where those color tags came from. :confused:

have fun!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.