Go Down

Topic: Success of member function executing depends on return type? [Solved] (Read 1 time) previous topic - next topic

fhtrogu

Background:
I am trying to write a library called Menu that provides a simple UI for the Adafruit 2x16 LCD Shield.  The library employs a class that has a member function that tells the LCD to print when called.

Problem:
The only time the function works is when the return type is "String", else the LCD prints out nothing. There are no compiler errors. My issue is that I don't want to continue writing this library until I understand why this is happening for fear of creating bigger problems later on.

Solution: After I stopped using the String class (as PeterH suggested) and started using C strings, I was able to implement my code as desired, i.e. making the printStatic member function void.

Here is the important part of the code:
header, Menu.h
Code: [Select]
#ifndef Menu_h
#define Menu_h

#include "Arduino.h"

class Adafruit_MCP23017;
class Adafruit_RGBLCDShield;

class Menu
{
public:
    // Constructors
    Menu();
    // Static menu
    Menu(String line0, String line1, Menu menu0, Adafruit_RGBLCDShield lcd);

    String printStatic();
private:
    Adafruit_RGBLCDShield* m_lcd;

    String  m_line0;
    String m_line1;
    Menu* m_menu0;

};

#endif

Menu.cpp
Code: [Select]

#include "Menu.h"

#include "Adafruit_MCP23017.h"
#include "Adafruit_RGBLCDShield.h"

Menu::Menu(){}

Menu::Menu(String line0, String line1, Menu menu0, Adafruit_RGBLCDShield lcd)
{
    m_lcd = &lcd;

    m_line0 = line0;
    m_line1 = line1;

    m_menu0 = &menu0;
}

String Menu::printStatic()
{
Adafruit_RGBLCDShield lcd = *m_lcd;

lcd.clear();
lcd.setCursor(0,0);
lcd.print(m_line0);
lcd.setCursor(0,1);
lcd.print(m_line1);

return "";
}

menuTesting.ino
Code: [Select]
#include <Menu.h>
#include <Wire.h>

#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>

Adafruit_RGBLCDShield lcd;

#define VIOLET 0x5

Menu m0;
Menu m1;

void setup()
{
  lcd.begin(16,2);
  lcd.setBacklight(VIOLET);
  lcd.clear();
  lcd.noCursor();
  lcd.leftToRight();

  m0 = Menu("TOP", "BOTTOM", m1, lcd);
}

boolean runOnce = true;
void loop()
{
  if (runOnce)
  {
    m0.printStatic()
    runOnce = false;
  }
}


This works. But changing the type to "int" or "void" or anything else makes the LCD not print. I need to know why so as to avoid problems later


econjack

The code you posted doesn't have enough details for us to be helpful. Post all of it.

fhtrogu

Apologies, I hit tab then enter which caused it to prematurely be posted  :smiley-eek: I resaved it with the .cpp and .ino.

michinyon

String,  as a return type,  requires dynamic memory allocation,  and on the arduino that is best to be avoided.

In fact,  Strings are probably best avoided altogether.

What unsuccesful outcome do you get,  with some other return type ?

michinyon

The only function I see there,   with a String return type,  is printStatic( ).

You pointlessly return an empty string from this,    and then the function which called printStatic( )  doesn't even collect this returned object or do anything with it .   What is the intended point of doing this ?

fhtrogu

I would gladly make the function void - if it worked. Like i said, the only way the function works (by displaying text on the LCD) is if the function returns the String type. I don't know why - it just does. That's where I'm confused.

fhtrogu

^^ with other return types everything compiles just fine, but no text is displayed.

Hackscribble

Hi fhtrogu

Could the problem be related to the creation of another instance of the lcd object inside the function?

Try changing the function to this, which references the object passed to the constructor.

Code: [Select]
void Menu::printStatic()
{
m_lcd->clear();
m_lcd->setCursor(0,0);
m_lcd->print(m_line0);
m_lcd->setCursor(0,1);
m_lcd->print(m_line1); 
}


Regards

Ray
Hackscribble.  Writing about making things
hackscribble@outlook.com

fhtrogu

This did something strange: the LCD rapidly flashed on and off. It did not print anything. However when the return type was changed to String (while still using the pointer), it worked :/

PeterH


else the LCD prints out nothing. There are no compiler errors.


You're using the String class all over the place and I strongly suggest you stop using it.

Can you show a complete minimal sketch that demonstrates the problem? At the moment I think you have posted the 'working' version (the one with the String return type).

fhtrogu

It appears that the String class was the issue. I'm using C strings now, which works. I'm new to this forum so I'm not sure how much of the solution to post. Do I simply post the code with a small explanation? or do I just explain the fix?

PaulS

Quote
Do I simply post the code with a small explanation? or do I just explain the fix?

I don't think the choices are mutually exclusive.
The art of getting good answers lies in asking good questions.

Go Up