Created SpanUserCommand()

Allows the user to add a command function to the Command Line Interface.  All User Commands are defined with a '@' prefix.

To Do:  Document this new feature.
This commit is contained in:
Gregg 2021-05-30 13:45:52 -05:00
parent 7ce04a70f4
commit bda90c59ca
3 changed files with 62 additions and 14 deletions

View File

@ -892,17 +892,33 @@ void Span::processSerialCommand(const char *c){
Serial.print(" E - erase ALL stored data and restart\n"); Serial.print(" E - erase ALL stored data and restart\n");
Serial.print("\n"); Serial.print("\n");
Serial.print(" L <level> - change the Log Level setting to <level>\n"); Serial.print(" L <level> - change the Log Level setting to <level>\n");
Serial.print("\n"); Serial.print("\n");
Serial.print(" ? - print this list of commands\n");
Serial.print("\n"); for(auto uCom=homeSpan.UserCommands.begin(); uCom!=homeSpan.UserCommands.end(); uCom++) // loop over all UserCommands using an iterator
Serial.print("\n*** End Commands ***\n\n"); Serial.printf(" @%c %s\n",uCom->first,uCom->second->s);
if(!homeSpan.UserCommands.empty())
Serial.print("\n");
Serial.print(" ? - print this list of commands\n\n");
Serial.print("*** End Commands ***\n\n");
} }
break; break;
case '@':{
auto uCom=UserCommands.find(c[1]);
if(uCom!=UserCommands.end()){
uCom->second->userFunction(c+1);
break;
}
}
default: default:
Serial.print("** Unknown command: '"); Serial.print("*** Unknown command: '");
Serial.print(c); Serial.print(c);
Serial.print("' - type '?' for list of commands.\n"); Serial.print("'. Type '?' for list of commands.\n");
break; break;
} // switch } // switch
@ -1695,4 +1711,14 @@ SpanButton::SpanButton(int pin, uint16_t longTime, uint16_t singleTime, uint16_t
homeSpan.PushButtons.push_back(this); homeSpan.PushButtons.push_back(this);
} }
/////////////////////////////// ///////////////////////////////
// SpanUserCommand //
///////////////////////////////
SpanUserCommand::SpanUserCommand(char c, const char *s, void (*f)(const char *v)){
this->s=s;
userFunction=f;
homeSpan.UserCommands[c]=this;
}

View File

@ -66,6 +66,7 @@ struct SpanCharacteristic;
struct SpanRange; struct SpanRange;
struct SpanBuf; struct SpanBuf;
struct SpanButton; struct SpanButton;
struct SpanUserCommand;
extern Span homeSpan; extern Span homeSpan;
@ -134,6 +135,8 @@ struct Span{
vector<SpanBuf> Notifications; // vector of SpanBuf objects that store info for Characteristics that are updated with setVal() and require a Notification Event vector<SpanBuf> Notifications; // vector of SpanBuf objects that store info for Characteristics that are updated with setVal() and require a Notification Event
vector<SpanButton *> PushButtons; // vector of pointer to all PushButtons vector<SpanButton *> PushButtons; // vector of pointer to all PushButtons
unordered_map<uint64_t, uint32_t> TimedWrites; // map of timed-write PIDs and Alarm Times (based on TTLs) unordered_map<uint64_t, uint32_t> TimedWrites; // map of timed-write PIDs and Alarm Times (based on TTLs)
unordered_map<char, SpanUserCommand *> UserCommands; // map of pointers to all UserCommands
void begin(Category catID=DEFAULT_CATEGORY, void begin(Category catID=DEFAULT_CATEGORY,
const char *displayName=DEFAULT_DISPLAY_NAME, const char *displayName=DEFAULT_DISPLAY_NAME,
@ -474,7 +477,15 @@ struct SpanButton{
SpanButton(int pin, uint16_t longTime=2000, uint16_t singleTime=5, uint16_t doubleTime=200); SpanButton(int pin, uint16_t longTime=2000, uint16_t singleTime=5, uint16_t doubleTime=200);
}; };
///////////////////////////////
struct SpanUserCommand {
const char *s; // description of command
void (*userFunction)(const char *v); // user-defined function to call
SpanUserCommand(char c, const char *s, void (*f)(const char *v));
};
///////////////////////////////////////////////// /////////////////////////////////////////////////
#include "Span.h" #include "Span.h"

View File

@ -18,6 +18,9 @@ void setup() {
homeSpan.setSketchVersion("Test 1.3.1"); homeSpan.setSketchVersion("Test 1.3.1");
homeSpan.setWifiCallback(wifiEstablished); homeSpan.setWifiCallback(wifiEstablished);
new SpanUserCommand('d',"My Description",userCom1);
new SpanUserCommand('f',"My second Description",userCom2);
homeSpan.begin(Category::Lighting,"HomeSpan Lamp Server","homespan"); homeSpan.begin(Category::Lighting,"HomeSpan Lamp Server","homespan");
new SpanAccessory(); // Begin by creating a new Accessory using SpanAccessory(), which takes no arguments new SpanAccessory(); // Begin by creating a new Accessory using SpanAccessory(), which takes no arguments
@ -30,21 +33,17 @@ void setup() {
new Characteristic::FirmwareRevision(HOMESPAN_VERSION); // Firmware of the Accessory (arbitrary text string, and can be the same for every Accessory) new Characteristic::FirmwareRevision(HOMESPAN_VERSION); // Firmware of the Accessory (arbitrary text string, and can be the same for every Accessory)
new Characteristic::Identify(); // Create the required Identify new Characteristic::Identify(); // Create the required Identify
// new Service::HAPProtocolInformation(); // Create the HAP Protcol Information Service new Service::HAPProtocolInformation(); // Create the HAP Protcol Information Service
new Characteristic::Version("1.1.0"); // Set the Version Characteristic to "1.1.0" as required by HAP new Characteristic::Version("1.1.0"); // Set the Version Characteristic to "1.1.0" as required by HAP
new Service::LightBulb(); new Service::LightBulb();
// new Characteristic::On(); new Characteristic::On();
new Characteristic::Brightness(); new Characteristic::Brightness();
new Characteristic::Name("Light 1"); new Characteristic::Name("Light 1");
new Service::LightBulb(); new Service::LightBulb();
new Characteristic::On(2); new Characteristic::On(2);
(new Characteristic::Brightness(150))->setRange(0,140,5); (new Characteristic::Brightness(50))->setRange(10,100,5);
new Characteristic::Name("Light 2"); new Characteristic::Name("Light 2");
(new Service::Switch())->setPrimary();
new Characteristic::On();
new Characteristic::Name("Switch 3");
new SpanButton(17);
} // end of setup() } // end of setup()
@ -61,3 +60,15 @@ void loop(){
void wifiEstablished(){ void wifiEstablished(){
Serial.print("IN CALLBACK FUNCTION\n\n"); Serial.print("IN CALLBACK FUNCTION\n\n");
} }
//////////////////////////////////////
void userCom1(const char *v){
Serial.printf("In User Command 1: '%s'\n\n",v);
}
//////////////////////////////////////
void userCom2(const char *v){
Serial.printf("In User Command 2: '%s'\n\n",v);
}