// Adds a "command" and a handler function to the list of available commands.
// This is used for matching a found token in the buffer, and gives the pointer
// to the handler function to deal with it.
//void(*function)(char* token);
void SerialCommand::addCommand(const char *command, void (*function)())
{
if (numCommand < MAXSERIALCOMMANDS) {
#ifdef SERIALCOMMANDDEBUG
Serial.print(numCommand);
Serial.print("-");
Serial.print("Adding command for ");
Serial.println(command);
#endif
strncpy(CommandList[numCommand].command,command,SERIALCOMMANDBUFFER);
CommandList[numCommand].function = function;
numCommand++;
} else {
// In this case, you tried to push more commands into the buffer than it is compiled to hold.
// Not much we can do since there is no real visible error assertion, we just ignore adding
// the command
#ifdef SERIALCOMMANDDEBUG
Serial.println("Too many handlers - recompile changing MAXSERIALCOMMANDS");
#endif
}
}
class SerialCommand
{
public:
SerialCommand(); // Constructor
#ifndef SERIALCOMMAND_HARDWAREONLY
SerialCommand(SoftwareSerial &SoftSer); // Constructor for using SoftwareSerial objects
#endif
void clearBuffer(); // Sets the command buffer to all '\0' (nulls)
char *next(); // returns pointer to next token found in command buffer (for getting arguments to commands)
void readSerial(); // Main entry point.
void addCommand(const char *, void(*)()); // Add commands to processing dictionary
void addDefaultHandler(void (*function)()); // A handler to call when no valid command received.
private:
char inChar; // A character read from the serial stream
char buffer[SERIALCOMMANDBUFFER]; // Buffer of stored characters while waiting for terminator character
int bufPos; // Current position in the buffer
char delim[MAXDELIMETER]; // null-terminated list of character to be used as delimeters for tokenizing (default " ")
char term; // Character that signals end of command (default '\r')
char *token; // Returned token from the command buffer as returned by strtok_r
char *last; // State variable used by strtok_r during processing
typedef struct _callback {
char command[SERIALCOMMANDBUFFER];
void (*function)(char* token); // added the variable to be passed to the function
} SerialCommandCallback; // Data structure to hold Command/Handler function key-value pairs
int numCommand;
SerialCommandCallback CommandList[MAXSERIALCOMMANDS]; // Actual definition for command/handler array
void (*defaultHandler)(); // Pointer to the default handler function
int usingSoftwareSerial; // Used as boolean to see if we're using SoftwareSerial object or not
#ifndef SERIALCOMMAND_HARDWAREONLY
SoftwareSerial *SoftSerial; // Pointer to a user-created SoftwareSerial object
#endif
};
this is the part that is affected
typedef struct _callback {
char command[SERIALCOMMANDBUFFER];
void (*function)(char* token); // added the variable to be passed to the function
} SerialCommandCallback; // Data structure to hold Command/Handler function key-value pairs
int numCommand;
SerialCommandCallback CommandList[MAXSERIALCOMMANDS]; // Actual definition for command/handler array
I am compiling for Teensy 3.6. Dont know if that is relevant?
/*************************************************************************
Name: TestTrack
Created: 09/04/2018
Author: Marius D. Liebenberg
Description: controller to move a target at a given speed past a sensor
in order to test and validate operation parameters.
The puck will move from one side to the other at a given speed not
exceeding 10m/s.
Two selectable speeds are provided as well as a start button for left and right.
The system will home the [puck prioir tovery move.
Accelaration, speed and distance are provided via configuartion parameters.
**************************************************************************/
/*******************************************************
* INCLUDES
********************************************************/
#include <SerialCommand.h>
#include <StepControl.h>
#include <EEPROM.h>
//#include <Adafruit_ILI9341_AS.h>
//#include "StepControl.h"
/*******************************************************
* DEFINES
********************************************************/
/*******************************************************
* GLOBALS
********************************************************/
/*******************************************************
* VARIABLES
********************************************************/
/*******************************************************
* CLASS DEFINES
********************************************************/
SerialCommand SCmd;
/*******************************************************
************************ PROGRAM ***********************
********************************************************/
Stepper motor(4, 5);
StepControl<> controller;
constexpr int spr = 16 * 200; // 3200 steps per revolution
constexpr int dpr = 200; // mm's per revolution
#define leftLimitSw 6
#define rightLimitSw 7
#define leftGoButton 8
#define rightGoButton 9
boolean leftHomeStatus;
boolean rightHomeStatus;
/***************************************************
homeLeft
Parameters:
int speed
Description: homes the puck to the left axis and
returns the status
Returns: leftHomeStatus
****************************************************/
boolean homeLeft(int speed)
{
return(leftHomeStatus);
}
/***************************************************
rightHome
Parameters:
int speed
Description: homes the puck to the right axis
Returns: rightHomeStatus
****************************************************/
boolean rightHome(int speed)
{
return(rightHomeStatus);
}
/***************************************************
travelRight
Parameters:
void
Description: move the puck to the right axis
Return: None
****************************************************/
void travelRight(void)
{
}
/***************************************************
travelLeft
Parameters:
void
Description: move the puck to left axis
Return: None
****************************************************/
void travelLeft( void)
{
}
void setup()
{
motor
.setMaxSpeed(2000)
.setAcceleration(2500)
.setStepPinPolarity(0)
.setPullInSpeed(1000)
.setPosition(0);
/* add setup code here */
// Setup callbacks for SerialCommand commands
SCmd.addCommand("G", SetGroupId);
//SCmd.addCommand("M", SetMasterId);
//SCmd.addCommand("S", SetSlaveId);
//SCmd.addCommand("C", GetCurrentConfig);
// //SCmd.addCommand("P",process_command); // Converts two arguments to integers and echos them back
//SCmd.addDefaultHandler(unrecognized); // Handler for command that isn't matched (says "What?")
}
void loop()
{
SCmd.readSerial();
/* add main program code here */
}
void SetGroupId()
{
int aNumber;
char *arg;
arg = SCmd.next();
if (arg != NULL)
{
aNumber = atoi(arg); // Converts a char string to an integer
Serial.print(F("Group Id was set to: "));
Serial.println(arg);
Serial.println();
////config.Group = aNumber;
////EEPROM.put(eePromAdd, config);
////PrintConfig();
}
else
{
Serial.println(F("No arguments"));
}
PrintMenu();
}
// This gets set as the default handler, and gets called when no other command matches.
void unrecognized()
{
PrintMenu();
}
void PrintMenu(void)
{
Serial.println("************ Setup Menu **************");
Serial.println();
Serial.print(F("Group Id = ")); Serial.println(F("'G' ID {numeric}"));
Serial.print(F("Mater Id = ")); Serial.println(F("'M' ID {numeric}"));
Serial.print(F("Slave Id = ")); Serial.println(F("'S' ID {numeric}"));
Serial.print(F("Config = ")); Serial.println(F("'C'"));
Serial.println();
Serial.println(F("G or M or S followed by space and a number or C for configuration"));
Serial.print(F("Enter Selection ->> "));
}
You have some kind of disconnect with your libraries. The .h file you attached shows this as the prototype for ‘addCommand()’:
void addCommand(const char *, void(*)());
but later defines the callback structure thusly:
typedef struct _callback {
char command[SERIALCOMMANDBUFFER];
void (*function)(char* token); // added the variable to be passed to the function
} SerialCommandCallback;
In one the callback function does not take an argument, in the other it does.