There isn't really a great way to do it, but there are a few ways to get it to work. Most of the problem here is that there is actually a different LCD.print
function for every data type. You aren't really calling the same function, your calling different functions that share the same name.
You could use templates. They are a powerful system built into C plus plus for generating functions and avr-gcc supports them well. With them, you can make the compiler generate all the necessary versions of your function for you. Your function would then look like this:
template<typename T>
void lcdPositionPrint(int x, int y, T content) throw() {
lcdPosition(row, col);
LCD.print(content);
}
That part about throw()
isn't doing anything except keeping the ".ino" preprocessor from messing the template function up. You can call the templated function like normal, or with an explicit type
lcdPositionPrint(0,0,"hello");
lcdPositionPrint<float>(0,0,32.0f);
You could also use the preprocessor. Your function is very simple, so you could just as easily make it a preprocessor macro that just places you code you want in the file whenever your "function call" appears. It looks like this:
#define lcdPositionPrint2(row,col,content) \
do { lcdPosition(row, col); LCD.print(content); } while (0)
The \
there tells the preprocessor that the next line is also part of the macro, and you will need one on every line but the last of your function. Wrapping it in a do
loop that immediately ends makes it nicely substitute into more situations. Micros don't scale up well, but they are a bit simpler for quick debugging situations. The code is duplicated in each call location instead of being bundled into functions though.