Display function for OLED accepting different argument type

Hello

I am trying to write a function that allows me to display on a OLED I2C screen using a Teensy 4.1

I would like the function to be able to work with String or Float or Int so I can easily print text and numbers etc.

Here is the function :

void displayOnScreen(byte indexScreen,char *MyMsgBanner, char *MyMsg){

     TCA9548A(indexScreen); 
     display.clearDisplay();
     display.setCursor(0,0);
     display.setTextSize(2);
     display.println(MyMsgBanner);
     DEBUG_MSG.print(MyMsgBanner);
  	 DEBUG_MSG.print("Length: ");
  	 int length = sizeof(MyMsgBanner)/sizeof(char);
  	 DEBUG_MSG.println(length);
  	 display.setCursor(20,30);
  	 display.setCursor((SCREEN_WIDTH-length)/2,30);
  	 display.setTextSize(3);
  	 display.println(MyMsg);
  	 display.display();

Now when I try to use it to display a number, I use the following

byte currentPage = 1;
displayOnScreen(1,"Page",(char) currentPage);

However I get the following warning.

Invalid arguments '

Candidates are:

void displayOnScreen(unsigned char, const char *, const char *)

'

What's the proper way to format the byte (or int or float) to const char*?
Is there a way to create a function that works with any type so I could either pass a number or a string ?

Thank you

Try this(not advised to use String excessively).

displayOnScreen(1,"Page", String(currentPage).c_str());

This may work..

Another approach you could take is to use the power of C++. When you define a function, part of the "signature" are the type and number of parameters. That is how the compiler determines which function to call. This allows you to declare multiple versions of the same function, much like Serial.print().

void dispayOnScreen(byte indexScreen,char *MyMsgBanner, char *MyMsg)
{
...
}

void displayOnScreen(byte indexScreen,char *MyMsgBanner, int Number)
{
  displayOnScreen(1,"Page", String(Number).c_str());
}

void displayOnScreen(byte indexScreen,char *MyMsgBanner, long Number)
{
  displayOnScreen(1,"Page", String(Number).c_str());
}

void displayOnScreen(byte indexScreen,char *MyMsgBanner, float Number)
{
  displayOnScreen(1,"Page", String(Number,2).c_str());
}

Then, your code does not have to do any of the casting

displayOnScreen(1,"Page","Message");
displayOnScreen(1,"Page", 1);
displayOnScreen(1,"Page", 3.14);
1 Like

I was going to suggest this, but decided I wasn't up to it. :wink:

So well done for not being lazy. :joy: :grin:

1 Like

seems there are many commands necessary to display things on such displays. i've tried capturing them all in a single sub-function that is passed one or more formatted strings.

the calling functions can used sprintf() to format the strings in a variety of ways and types of values. nulls can be passed for unused lines on the display

the strings can also be dumped on the serial monitor for debugging

Thanks a lot for your help

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