I'm a first time c++ explorer having a hard time to figure out how to tell the compiler to *do* what I want...
For my arduino programs I use a kind of menu over serial line.
It might help to understand what I want to do to have a quick look at an example: a simple hardware menu interface over serial line as a kind of arduino software breadboard.https://github.com/reppr/softboard
After cat&pasting the menu code too many times to too many sketches I decided to implement the menu functionality as a library to make reuse even easier. I had done similar things in other languages like c and FORTH but decided to take advantage of the situation and try out c++, not so sure any more if that was a good idea
I started coding on a Linux computer as I did not want to load all my early versions on an arduino. So I wrote and tested basic menu functionality on Linux. On an Arduino the menu would get input from one of it's hw interfaces like Serial and use the same (or another) interface for output. On the pc the I/O was on stdin/stdout.
So I experimented with passing function pointers to the Menu constructor or with defining menu I/O by #ifdef s.
I thought I could use that later to switch different Arduino interfaces, not being restricted to Serial.
I found different working approaches on Linux, but when I tried to use Serial I/O on the Arduino the compiler confused me a lot by not letting me do that and throwing me in a jungle of constructors/destructors, thinking that I want to redefine this-or-that. After a lot of reading, learning and trying different approaches I'm still not there yet.
Let me explain:
The menu takes input (if any) from one of the interfaces like serial, buffers it and returns immediately unless it just received an end token. So the menu must know how to test for input (nonblocking) and to read next byte. After an end token the menu interprets the input in a way defined by the user program and some internal stuff.
On the other hand the menu must have a way to inform the user about active keys, selected menu page, errors like unknown input, a missing number and the like.
So far so good, but now the arduino sketch defines it's menu pages, saying things like:
if on menu page "TEST" react on receiving menu token 'l' or 'r' by informing the user about the pwm value driving left/right motor and let him set a new value to test how much your two-wheel robot needs to start moving... and on pressing '!' store that values to the EEPROM...
So the sketch often wants to use the same I/O functions as the menu class does. This lead to all sorts of errors when trying that with Serial I/O and c++ (If I remember right I had an early c version that worked sort of...).
I thought I could do that along the lines
/* pseudo code */
Menu MENU(buffsize, submenus, &men_getchar [, other I/O function pointers]);
/* the sketch would then call MENU.out() family or use things like Serial.print() directly
to define functionality of the menu pages */
Works well on linux, but not when I try to use it with Arduino Serial, one of the most common errors being that the compiler thinks I want to redefine Serial I/O functions. How do I tell that to the compiler and where would I put Serial.begin()?
I have tried a couple of other implementations, like to derive the Menu class from Serial which had the side effect of making me feel very stupid
I think the key point is that I want to be able to use all I/O interfaces (which might be coded as a class) in *both* the sketch *and* the menu class. Multiple menus on different I/O interfaces should also remain possible.
So before trying next dead end street let me ask:
What is the right approach to do that in c++?