diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 0435c93..5ecda27 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -1387,6 +1387,24 @@ void Span::checkRanges(){ homeSpan.configLog+="\n\n"; } +/////////////////////////////// + +void Span::protectGPIOs(boolean suspend){ + + auto gp=ProtectedGPIOs.begin(); + while(gp!=ProtectedGPIOs.end()){ + if(suspend){ + Serial.printf("PIN: %d Status: %d : ",gp->first,GPIO.pin[gp->first].int_type); + gp->second=GPIO.pin[gp->first].int_type; + GPIO.pin[gp->first].int_type=0; + Serial.printf("PIN: %d Status: %d\n",gp->first,GPIO.pin[gp->first].int_type); + } else { + GPIO.pin[gp->first].int_type=gp->second; + } + gp++; + } +} + /////////////////////////////// // SpanAccessory // /////////////////////////////// diff --git a/src/HomeSpan.h b/src/HomeSpan.h index 9acd360..8822342 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -145,6 +145,7 @@ 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 ProtectedGPIOs; // map of pins to protect when running an ISR during NVS operations unordered_map UserCommands; // map of pointers to all UserCommands @@ -159,6 +160,7 @@ struct Span{ void commandMode(); // allows user to control and reset HomeSpan settings with the control button void processSerialCommand(const char *c); // process command 'c' (typically from readSerial, though can be called with any 'c') void checkRanges(); // checks values of all Characteristics to ensure they are each within range + void protectGPIOs(boolean suspend); // true - disable ISRs on protectedGPIOs; false - re-enable ISRs on protectedGPIOs int sprintfAttributes(char *cBuf); // prints Attributes JSON database into buf, unless buf=NULL; return number of characters printed, excluding null terminator, even if buf=NULL void prettyPrint(char *buf, int nsp=2); // print arbitrary JSON from buf to serial monitor, formatted with indentions of 'nsp' spaces @@ -190,6 +192,7 @@ struct Span{ const char *getSketchVersion(){return sketchVersion;} // get sketch version number void setWifiCallback(void (*f)()){wifiCallback=f;} // sets an optional user-defined function to call once WiFi connectivity is established void setApFunction(void (*f)()){apFunction=f;} // sets an optional user-defined function to call when activating the WiFi Access Point + void protectPinISR(uint8_t pin){ProtectedGPIOs[pin]=0;} // protects ISR on pin from NVS operations void setPairingCode(const char *s){sprintf(pairingCodeCommand,"S %9s",s);} // sets the Pairing Code - use is NOT recommended. Use 'S' from CLI instead @@ -409,6 +412,7 @@ struct SpanCharacteristic{ uvSet(value,val); if(nvsStore){ + homeSpan.protectGPIOs(true); nvsKey=(char *)malloc(16); uint16_t t; sscanf(type,"%x",&t); @@ -438,6 +442,7 @@ struct SpanCharacteristic{ nvsFlag=1; } } + homeSpan.protectGPIOs(false); } uvSet(newValue,value); @@ -534,8 +539,10 @@ struct SpanCharacteristic{ homeSpan.Notifications.push_back(sb); // store SpanBuf in Notifications vector if(nvsKey){ + homeSpan.protectGPIOs(true); nvs_set_str(homeSpan.charNVS,nvsKey,value.STRING); // store data nvs_commit(homeSpan.charNVS); + homeSpan.protectGPIOs(false); } } // setString() @@ -566,8 +573,10 @@ struct SpanCharacteristic{ homeSpan.Notifications.push_back(sb); // store SpanBuf in Notifications vector if(nvsKey){ + homeSpan.protectGPIOs(true); nvs_set_blob(homeSpan.charNVS,nvsKey,&value,sizeof(UVal)); // store data nvs_commit(homeSpan.charNVS); + homeSpan.protectGPIOs(false); } }