How do I move the function_map and call_function into my HardwareClass.h and HardwareClass.cpp? I've tried quite a few things and couldn't get it working.
This kind of layout below should work, ( untested ) and function_map is not available outside of the Cpp, if it needs to be usable in the sketch file, you'll need a variable marked extern.
Edit: or you will have to make processStatusLine visible to the cpp, by placing its declaration in the header ( can leave the definition in the sketch )
static function_map _function_map; is invalid, function_map is declared as an array of 'anonymous structs'. Don't worry about typedefs on structs, they are not necessary for this usage in C++.
What I had does work, here is a working example you can modify.
Is it possible for the processStatusLine, function_map_struct, and call_function to all be within the scope of a HwClassSerial class instance instead of in the global scope? The reason for this is I have several pieces of gear all controlled by serial so I'm trying to setup a base class for each to extend. Each child class would define it's own function_map_type and there could be several child classes instantiated simultaneously.
Is it possible for the processStatusLine, function_map_struct, and call_function to all be within the scope of a HwClassSerial class instance instead of in the global scope?
Yes, using the correct working code I provided, you should be able to put them in. However not for the way you want.
To keep with ease of use, processStatusLine and other functions to be called via a pointer need to be non-member functions( static or friends ).
But really, you are mixing two different systems when one will do. call_function is a lookup method, whereas using an inherited/base type should imply that the derived type provides the missing information, which completes the base type.
The problem I'm trying to solve is that I will have several device classes. For code re-usability I want to have a base class that has a few private methods.
// sends a command to a serial device and sends each received byte from the output to _processIncomingByte
void _processCommand(const char * commandName, const int longestLine, int linesToRead);
// this takes each received byte and constructs character arrays to be processed. The method to process the
// character array is determined by the mapping of the commandName
void _processIncomingByte(const byte c, char * buffer, const char * commandName, const int lineLength);
So each class that extends the base class will have a different set of commands to run and methods to process those commands. Once class could have:
If you want the idea encapsulated in a class, and only one serial device is communicating at once, then things are easy using classes.
Imagine your base class is something like this.
struct BaseType{
/*
Pure virtual as you will not be creating instances of this class, only instances of
classes that derive this base class.
*/
virtual void RunCommand( const char *c_Data ) = 0;
};
DerivedA objA;
DerivedB objB;
BaseType *currentObj = &objA;
void loop(){
char c_Buffer[ x ];
//... fill c_buffer with command.
//Run command based on current object.
currentObj->RunCommand( c_Buffer );
}