Hi,
First post on this forum and thanks in advance for any help received. Apologies if I'm using the incorrect terminology. I want to have a crack at writing my own class/ library code for inputting a number on an LCD using a clickable quadrature rotatory switch. To start with I thought I'd just try to get it to work as a standalone function before I converted it into a class.
My plan was to wrap up a bunch of code into a single function, pass in the myLCD.setCursor and myLCD.print function (where myLCD is an instance of an I2C LCD display, or any display for that matter) and couple of other things like position , message, and size of the number, etc. Then it would do its magic and return a long number.
I can get the code to work if I create wrapper functions around the lcd's .setCursor(x,y) and .print(text) functions, but I'd like to avoid having to create wrapper functions if possible. I'd like to be able to pass in the instances of these methods directly without having to wrap them.
Hopefully some code might make this clearer
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
void PrintWrapper(String text);
void setCursorWrapper(uint8_t col, uint8_t row);
long InputANumber(void (*LCDSetCursorFunction)(uint8_t, uint8_t), uint8_t x_cord, uint8_t y_cord, void (*LCDPrintFunction)(String), String text, int value);
LiquidCrystal_I2C myLCD(0x27, 16, 2); //Create an instance of the LCD display
void setup() {
Serial.begin(9600);
myLCD.init();
myLCD.backlight();
myLCD.setCursor(0, 0);
myLCD.print("Testing...");
}
void loop() {
//long answer=InputANumber(myLCD.setCursor, 1, 1, myLCD.print, "A Message", 4); //------------This Doesn't even Compile--------------
long answer = InputANumber(setCursorWrapper, 1, 1, LCDPrintWrapper, "A Message", 4); // ------------This works --------
// Do Something with the answer
}
// Arguments are the SetCursor function, x cord, y cord, LCDPrint function, Message as a String, Number of digits as an int.
long InputANumber(void (*LCDSetCursorFunction)(uint8_t, uint8_t), uint8_t x, uint8_t y, void (*LCDPrintFunction)(String), String message, int numberOfDigits) {
(*LCDSetCursorFunction)(x, y);
delay(1000);
(*LCDPrintFunction)(message);
delay(1000);
// TODO: A bunch of code here that drives a cursor around the LCD screen,
// and allows the input of a number of size numberOfDigits
// using a quadrature clickable rotorary knob
// Will also need to pass in pin numbers for A & B channel and the push to click input
// Just return 9999 to make it compile
return 9999;
}
void setCursorWrapper(uint8_t col, uint8_t row) {
myLCD.setCursor(col, row);
Serial.print("Setting cursor position to ");
Serial.print(col);
Serial.print(",");
Serial.println(row);
}
void LCDPrintWrapper(String text) {
myLCD.print(text);
Serial.print("Just printed: ");
Serial.print(text);
Serial.println(" to the LCD");
}
The code shown above works when the lcd functions are wrapped, but if I uncomment the first line in loop() and comment the second line, it won't compile. It gives an invalid use of non-static member function error.
Is what I'm trying to do possible without creating the wrappers functions?
If so what is the correct syntax for passing in the non static member functions of the myLCD object?
Thanks in advance.
Warren