diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index c6faa3c..1c16941 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -892,17 +892,33 @@ void Span::processSerialCommand(const char *c){ Serial.print(" E - erase ALL stored data and restart\n"); Serial.print("\n"); Serial.print(" L - change the Log Level setting to \n"); - Serial.print("\n"); - Serial.print(" ? - print this list of commands\n"); - Serial.print("\n"); - Serial.print("\n*** End Commands ***\n\n"); + Serial.print("\n"); + + for(auto uCom=homeSpan.UserCommands.begin(); uCom!=homeSpan.UserCommands.end(); uCom++) // loop over all UserCommands using an iterator + 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; + case '@':{ + + auto uCom=UserCommands.find(c[1]); + + if(uCom!=UserCommands.end()){ + uCom->second->userFunction(c+1); + break; + } + } + default: - Serial.print("** Unknown command: '"); + Serial.print("*** Unknown command: '"); Serial.print(c); - Serial.print("' - type '?' for list of commands.\n"); + Serial.print("'. Type '?' for list of commands.\n"); break; } // switch @@ -1695,4 +1711,14 @@ SpanButton::SpanButton(int pin, uint16_t longTime, uint16_t singleTime, uint16_t 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; +} diff --git a/src/HomeSpan.h b/src/HomeSpan.h index fbf2cde..149d7c4 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -66,6 +66,7 @@ struct SpanCharacteristic; struct SpanRange; struct SpanBuf; struct SpanButton; +struct SpanUserCommand; extern Span homeSpan; @@ -134,6 +135,8 @@ struct Span{ vector Notifications; // vector of SpanBuf objects that store info for Characteristics that are updated with setVal() and require a Notification Event vector PushButtons; // vector of pointer to all PushButtons unordered_map TimedWrites; // map of timed-write PIDs and Alarm Times (based on TTLs) + + unordered_map UserCommands; // map of pointers to all UserCommands void begin(Category catID=DEFAULT_CATEGORY, 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); }; +/////////////////////////////// + +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" diff --git a/src/src.ino b/src/src.ino index 3becb91..ad9f749 100644 --- a/src/src.ino +++ b/src/src.ino @@ -18,6 +18,9 @@ void setup() { homeSpan.setSketchVersion("Test 1.3.1"); homeSpan.setWifiCallback(wifiEstablished); + new SpanUserCommand('d',"My Description",userCom1); + new SpanUserCommand('f',"My second Description",userCom2); + homeSpan.begin(Category::Lighting,"HomeSpan Lamp Server","homespan"); 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::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 Service::LightBulb(); -// new Characteristic::On(); + new Characteristic::On(); new Characteristic::Brightness(); new Characteristic::Name("Light 1"); new Service::LightBulb(); 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 Service::Switch())->setPrimary(); - new Characteristic::On(); - new Characteristic::Name("Switch 3"); - new SpanButton(17); } // end of setup() @@ -61,3 +60,15 @@ void loop(){ void wifiEstablished(){ 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); +}