diff --git a/examples/16-ProgrammableSwitches/16-ProgrammableSwitches.ino b/examples/16-ProgrammableSwitches/16-ProgrammableSwitches.ino index 02dc5e6..3b9c92e 100644 --- a/examples/16-ProgrammableSwitches/16-ProgrammableSwitches.ino +++ b/examples/16-ProgrammableSwitches/16-ProgrammableSwitches.ino @@ -15,7 +15,6 @@ #include "DEV_Identify.h" void setup() { - Serial.begin(115200); @@ -27,9 +26,10 @@ void setup() { new Characteristic::Version("1.1.0"); new SpanAccessory(); - new DEV_Identify("PushButton Switch","HomeSpan","123-ABC","20mA LED","0.9",0); + new DEV_Identify("PushButton Switches","HomeSpan","123-ABC","Prog Switches","0.9",0); - new DEV_ProgButton(23); + new DEV_ProgButton(23,2); + new DEV_ProgButton(5,7); } // end of setup() diff --git a/examples/16-ProgrammableSwitches/DEV_ProgButton.h b/examples/16-ProgrammableSwitches/DEV_ProgButton.h index 4d4b130..b9210cb 100644 --- a/examples/16-ProgrammableSwitches/DEV_ProgButton.h +++ b/examples/16-ProgrammableSwitches/DEV_ProgButton.h @@ -8,15 +8,18 @@ struct DEV_ProgButton : Service::StatelessProgrammableSwitch { // Stateles int buttonPin; // pin with programmable pushbutton SpanCharacteristic *switchEvent; // reference to the ProgrammableSwitchEvent Characteristic - DEV_ProgButton(int buttonPin) : Service::StatelessProgrammableSwitch(){ + DEV_ProgButton(int buttonPin, int index) : Service::StatelessProgrammableSwitch(){ - switchEvent=new Characteristic::ProgrammableSwitchEvent(); // ProgrammableSwitchEvent Characteristic + switchEvent=new Characteristic::ProgrammableSwitchEvent(); // ProgrammableSwitchEvent Characteristic + new Characteristic::ServiceLabelIndex(index); // set service label index new SpanButton(buttonPin); // create new SpanButton this->buttonPin=buttonPin; // save button pin number - Serial.print("Configuring Programmable Pushbuton: Pin="); // initialization message + Serial.print("Configuring Programmable Pushbutton: Pin="); // initialization message Serial.print(buttonPin); + Serial.print(" Index="); + Serial.print(index); Serial.print("\n"); } // end constructor diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 074dcc8..4d55ec0 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -79,12 +79,9 @@ void Span::poll() { homeSpan.Accessories.back()->validate(); } + + processSerialCommand("i"); // print homeSpan configuration info - Serial.print(configLog); - Serial.print("\nConfigured as Bridge: "); - Serial.print(homeSpan.isBridge?"YES":"NO"); - Serial.print("\n\n*** End Config Log ***\n"); - if(nFatalErrors>0){ Serial.print("\n*** PROGRAM HALTED DUE TO "); Serial.print(nFatalErrors); @@ -617,8 +614,14 @@ void Span::processSerialCommand(char *c){ case 'i':{ - char d[]="------------------------------"; Serial.print("\n*** HomeSpan Info ***\n\n"); + + Serial.print(configLog); + Serial.print("\nConfigured as Bridge: "); + Serial.print(homeSpan.isBridge?"YES":"NO"); + Serial.print("\n\n"); + + char d[]="------------------------------"; char cBuf[256]; sprintf(cBuf,"%-30s %s %s %s %s %s %s\n","Service","Type","AID","IID","Update","Loop","Button"); Serial.print(cBuf); @@ -655,7 +658,7 @@ void Span::processSerialCommand(char *c){ Serial.print("\n"); Serial.print(" R - restart device\n"); Serial.print(" F - factory reset and restart\n"); - Serial.print(" E - delete all stored data and restart\n"); + Serial.print(" E - erase ALL stored data and restart\n"); Serial.print("\n"); Serial.print(" ? - print this list of commands\n"); Serial.print("\n"); @@ -1026,7 +1029,7 @@ SpanAccessory::SpanAccessory(){ homeSpan.Accessories.push_back(this); aid=homeSpan.Accessories.size(); - homeSpan.configLog+="+Accessory " + String(aid) + "\n"; + homeSpan.configLog+="+Accessory-" + String(aid) + "\n"; } /////////////////////////////// @@ -1082,7 +1085,7 @@ int SpanAccessory::sprintfAttributes(char *cBuf){ SpanService::SpanService(const char *type, const char *hapName){ - if(!homeSpan.Accessories.back()->Services.empty()) // this is not the first Service to be defined for this Accessory + if(!homeSpan.Accessories.empty() && !homeSpan.Accessories.back()->Services.empty()) // this is not the first Service to be defined for this Accessory homeSpan.Accessories.back()->Services.back()->validate(); this->type=type; @@ -1096,9 +1099,10 @@ SpanService::SpanService(const char *type, const char *hapName){ return; } - homeSpan.configLog+="\n"; homeSpan.Accessories.back()->Services.push_back(this); iid=++(homeSpan.Accessories.back()->iidCount); + + homeSpan.configLog+=+"-" + String(iid) + String(" (") + String(type) + String(")\n"); } /////////////////////////////// @@ -1175,6 +1179,14 @@ SpanCharacteristic::SpanCharacteristic(char *type, uint8_t perms, char *hapName) return; } + iid=++(homeSpan.Accessories.back()->iidCount); + service=homeSpan.Accessories.back()->Services.back(); + aid=homeSpan.Accessories.back()->aid; + + ev=(boolean *)calloc(homeSpan.maxConnections,sizeof(boolean)); + + homeSpan.configLog+="-" + String(iid) + String(" (") + String(type) + String(") "); + boolean valid=false; for(int i=0; !valid && iServices.back()->req.size(); i++) @@ -1188,23 +1200,20 @@ SpanCharacteristic::SpanCharacteristic(char *type, uint8_t perms, char *hapName) homeSpan.nFatalErrors++; } - valid=true; + boolean repeated=false; - for(int i=0; valid && iServices.back()->Characteristics.size(); i++) - valid=strcmp(type,homeSpan.Accessories.back()->Services.back()->Characteristics[i]->type); + for(int i=0; !repeated && iServices.back()->Characteristics.size(); i++) + repeated=!strcmp(type,homeSpan.Accessories.back()->Services.back()->Characteristics[i]->type); - if(!valid){ + if(valid && repeated){ homeSpan.configLog+=" *** ERROR! Characteristic already defined for this Service. ***"; homeSpan.nFatalErrors++; } - homeSpan.configLog+="\n"; homeSpan.Accessories.back()->Services.back()->Characteristics.push_back(this); - iid=++(homeSpan.Accessories.back()->iidCount); - service=homeSpan.Accessories.back()->Services.back(); - aid=homeSpan.Accessories.back()->aid; - ev=(boolean *)calloc(homeSpan.maxConnections,sizeof(boolean)); + homeSpan.configLog+="\n"; + } /////////////////////////////// @@ -1327,101 +1336,6 @@ int SpanCharacteristic::sprintfAttributes(char *cBuf, int flags){ nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?128:0,",\"minValue\":%d,\"maxValue\":%d,\"minStep\":%d",range->min,range->max,range->step); } -/* - - switch(format){ - - case BOOL: - if(perms&PR){ - if(!(perms&NV)) - nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"value\":%s",value.BOOL?"true":"false"); - else - nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"value\":null"); - } - if(flags&GET_META) - nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"format\":\"bool\""); - break; - - case INT: - if(perms&PR){ - if(!(perms&NV)) - nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"value\":%d",value.INT); - else - nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"value\":null"); - } - if(flags&GET_META) - nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"format\":\"int\""); - break; - - case UINT8: - if(perms&PR){ - if(!(perms&NV)) - nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"value\":%u",value.UINT8); - else - nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"value\":null"); - } - if(flags&GET_META) - nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"format\":\"uint8\""); - break; - - case UINT16: - if(perms&PR){ - if(!(perms&NV)) - nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"value\":%u",value.UINT16); - else - nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"value\":null"); - } - if(flags&GET_META) - nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"format\":\"uint16\""); - break; - - case UINT32: - if(perms&PR){ - if(!(perms&NV)) - nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"value\":%lu",value.UINT32); - else - nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"value\":null"); - } - if(flags&GET_META) - nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"format\":\"uint32\""); - break; - - case UINT64: - if(perms&PR){ - if(!(perms&NV)) - nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"value\":%llu",value.UINT64); - else - nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"value\":null"); - } - if(flags&GET_META) - nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"format\":\"uint64\""); - break; - - case FLOAT: - if(perms&PR){ - if(!(perms&NV)) - nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"value\":%lg",value.FLOAT); - else - nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"value\":null"); - } - if(flags&GET_META) - nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"format\":\"float\""); - break; - - case STRING: - if(perms&PR){ - if(!(perms&NV)) - nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"value\":\"%s\"",value.STRING); - else - nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"value\":null"); - } - if(flags&GET_META) - nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?64:0,",\"format\":\"string\""); - break; - - } // switch -*/ - if(desc && (flags&GET_DESC)){ nBytes+=snprintf(cBuf?(cBuf+nBytes):NULL,cBuf?128:0,",\"description\":\"%s\"",desc); } diff --git a/src/HomeSpan.h b/src/HomeSpan.h index 4b2bcec..2b18d8e 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -56,7 +56,7 @@ struct Span{ unsigned long snapTime; // current time (in millis) snapped before entering Service loops() or updates() boolean isInitialized=false; // flag indicating HomeSpan has been initialized int nFatalErrors=0; // number of fatal errors in user-defined configuration - String configLog="\n*** Config Log ***\n\n"; // log of configuration process, including any errors + String configLog; // log of configuration process, including any errors boolean isBridge=true; // flag indicating whether device is configured as a bridge (i.e. first Accessory contains nothing but AccessoryInformation and HAPProtocolInformation) char *defaultSetupCode=DEFAULT_SETUP_CODE; // Setup Code used for pairing