diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 07dd911..e4fbd8a 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -539,7 +539,7 @@ void Span::checkConnect(){ /////////////////////////////// -void Span::setQRID(const char *id){ +Span& Span::setQRID(const char *id){ char tBuf[5]; sscanf(id,"%4[0-9A-Za-z]",tBuf); @@ -547,7 +547,8 @@ void Span::setQRID(const char *id){ if(strlen(id)==4 && strlen(tBuf)==4){ sprintf(qrID,"%s",id); } - + + return(*this); } // setQRID /////////////////////////////// @@ -833,7 +834,7 @@ void Span::processSerialCommand(const char *c){ case 'm': { multi_heap_info_t heapInfo; heap_caps_get_info(&heapInfo,MALLOC_CAP_INTERNAL); - LOG0("Total Heap=%d ",heapInfo.total_free_bytes); + LOG0("Total Heap=%d (low=%d) ",heapInfo.total_free_bytes,heapInfo.minimum_free_bytes); heap_caps_get_info(&heapInfo,MALLOC_CAP_DEFAULT); LOG0("DRAM-Capable=%d ",heapInfo.total_free_bytes); heap_caps_get_info(&heapInfo,MALLOC_CAP_EXEC); @@ -1197,13 +1198,15 @@ const char* Span::statusString(HS_STATUS s){ /////////////////////////////// -void Span::setWifiCredentials(const char *ssid, const char *pwd){ +Span& Span::setWifiCredentials(const char *ssid, const char *pwd){ sprintf(network.wifiData.ssid,"%.*s",MAX_SSID,ssid); sprintf(network.wifiData.pwd,"%.*s",MAX_PWD,pwd); if(wifiNVS){ // is begin() already called and wifiNVS is open nvs_set_blob(wifiNVS,"WIFIDATA",&network.wifiData,sizeof(network.wifiData)); // update data nvs_commit(wifiNVS); // commit to NVS } + + return(*this); } /////////////////////////////// diff --git a/src/HomeSpan.h b/src/HomeSpan.h index 5a22b59..2334ab2 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -291,49 +291,51 @@ class Span{ boolean updateDatabase(boolean updateMDNS=true); // updates HAP Configuration Number and Loop vector; if updateMDNS=true and config number has changed, re-broadcasts MDNS 'c#' record; returns true if config number changed boolean deleteAccessory(uint32_t aid); // deletes Accessory with matching aid; returns true if found, else returns false - void setControlPin(uint8_t pin){controlButton=new PushButton(pin);} // sets Control Pin - void setStatusPin(uint8_t pin){statusDevice=new GenericLED(pin);} // sets Status Device to a simple LED on specified pin - void setStatusAutoOff(uint16_t duration){autoOffLED=duration;} // sets Status LED auto off (seconds) - int getStatusPin(){return(statusLED->getPin());} // get Status Pin (getPin will return -1 if underlying statusDevice is undefined) - int getControlPin(){return(controlButton?controlButton->getPin():-1);} // get Control Pin (returns -1 if undefined) + Span& setControlPin(uint8_t pin){controlButton=new PushButton(pin);return(*this);} // sets Control Pin + Span& setStatusPin(uint8_t pin){statusDevice=new GenericLED(pin);return(*this);} // sets Status Device to a simple LED on specified pin + Span& setStatusAutoOff(uint16_t duration){autoOffLED=duration;return(*this);} // sets Status LED auto off (seconds) + int getStatusPin(){return(statusLED->getPin());} // get Status Pin (getPin will return -1 if underlying statusDevice is undefined) + int getControlPin(){return(controlButton?controlButton->getPin():-1);} // get Control Pin (returns -1 if undefined) - void setStatusPixel(uint8_t pin,float h=0,float s=100,float v=100){ // sets Status Device to an RGB Pixel on specified pin + Span& setStatusPixel(uint8_t pin,float h=0,float s=100,float v=100){ // sets Status Device to an RGB Pixel on specified pin statusDevice=((new Pixel(pin))->setOnColor(Pixel::HSV(h,s,v))); + return(*this); } - void setStatusDevice(Blinkable *sDev){statusDevice=sDev;} + Span& setStatusDevice(Blinkable *sDev){statusDevice=sDev;return(*this);} void refreshStatusDevice(){if(statusLED)statusLED->refresh();} - void setApSSID(const char *ssid){network.apSSID=ssid;} // sets Access Point SSID - void setApPassword(const char *pwd){network.apPassword=pwd;} // sets Access Point Password - void setApTimeout(uint16_t nSec){network.lifetime=nSec*1000;} // sets Access Point Timeout (seconds) - void setCommandTimeout(uint16_t nSec){comModeLife=nSec*1000;} // sets Command Mode Timeout (seconds) - void setLogLevel(int level){logLevel=level;} // sets Log Level for log messages (0=baseline, 1=intermediate, 2=all, -1=disable all serial input/output) - int getLogLevel(){return(logLevel);} // get Log Level - void setSerialInputDisable(boolean val){serialInputDisabled=val;} // sets whether serial input is disabled (true) or enabled (false) - boolean getSerialInputDisable(){return(serialInputDisabled);} // returns true if serial input is disabled, or false if serial input in enabled - void reserveSocketConnections(uint8_t n){maxConnections-=n;} // reserves n socket connections *not* to be used for HAP - void setHostNameSuffix(const char *suffix){hostNameSuffix=suffix;} // sets the hostName suffix to be used instead of the 6-byte AccessoryID - void setPortNum(uint16_t port){tcpPortNum=port;} // sets the TCP port number to use for communications between HomeKit and HomeSpan - void setQRID(const char *id); // sets the Setup ID for optional pairing with a QR Code - void setSketchVersion(const char *sVer){sketchVersion=sVer;} // set optional sketch version number - 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 setPairCallback(void (*f)(boolean isPaired)){pairCallback=f;} // sets an optional user-defined function to call when Pairing is established (true) or lost (false) - void setApFunction(void (*f)()){apFunction=f;} // sets an optional user-defined function to call when activating the WiFi Access Point - void enableAutoStartAP(){autoStartAPEnabled=true;} // enables auto start-up of Access Point when WiFi Credentials not found - void setWifiCredentials(const char *ssid, const char *pwd); // sets WiFi Credentials - void setStatusCallback(void (*f)(HS_STATUS status)){statusCallback=f;} // sets an optional user-defined function to call when HomeSpan status changes - const char* statusString(HS_STATUS s); // returns char string for HomeSpan status change messages + Span& setApSSID(const char *ssid){network.apSSID=ssid;return(*this);} // sets Access Point SSID + Span& setApPassword(const char *pwd){network.apPassword=pwd;return(*this);} // sets Access Point Password + Span& setApTimeout(uint16_t nSec){network.lifetime=nSec*1000;return(*this);} // sets Access Point Timeout (seconds) + Span& setCommandTimeout(uint16_t nSec){comModeLife=nSec*1000;return(*this);} // sets Command Mode Timeout (seconds) + Span& setLogLevel(int level){logLevel=level;return(*this);} // sets Log Level for log messages (0=baseline, 1=intermediate, 2=all, -1=disable all serial input/output) + int getLogLevel(){return(logLevel);} // get Log Level + Span& setSerialInputDisable(boolean val){serialInputDisabled=val;return(*this);} // sets whether serial input is disabled (true) or enabled (false) + boolean getSerialInputDisable(){return(serialInputDisabled);} // returns true if serial input is disabled, or false if serial input in enabled + Span& reserveSocketConnections(uint8_t n){maxConnections-=n;return(*this);} // reserves n socket connections *not* to be used for HAP + Span& setHostNameSuffix(const char *suffix){hostNameSuffix=suffix;return(*this);} // sets the hostName suffix to be used instead of the 6-byte AccessoryID + Span& setPortNum(uint16_t port){tcpPortNum=port;return(*this);} // sets the TCP port number to use for communications between HomeKit and HomeSpan + Span& setQRID(const char *id); // sets the Setup ID for optional pairing with a QR Code + Span& setSketchVersion(const char *sVer){sketchVersion=sVer;return(*this);} // set optional sketch version number + const char *getSketchVersion(){return sketchVersion;} // get sketch version number + Span& setWifiCallback(void (*f)()){wifiCallback=f;return(*this);} // sets an optional user-defined function to call once WiFi connectivity is established + Span& setPairCallback(void (*f)(boolean isPaired)){pairCallback=f;return(*this);} // sets an optional user-defined function to call when Pairing is established (true) or lost (false) + Span& setApFunction(void (*f)()){apFunction=f;return(*this);} // sets an optional user-defined function to call when activating the WiFi Access Point + Span& enableAutoStartAP(){autoStartAPEnabled=true;return(*this);} // enables auto start-up of Access Point when WiFi Credentials not found + Span& setWifiCredentials(const char *ssid, const char *pwd); // sets WiFi Credentials + Span& setStatusCallback(void (*f)(HS_STATUS status)){statusCallback=f;return(*this);} // sets an optional user-defined function to call when HomeSpan status changes + const char* statusString(HS_STATUS s); // returns char string for HomeSpan status change messages - void setPairingCode(const char *s){sprintf(pairingCodeCommand,"S %9s",s);} // sets the Pairing Code - use is NOT recommended. Use 'S' from CLI instead - void deleteStoredValues(){processSerialCommand("V");} // deletes stored Characteristic values from NVS + Span& setPairingCode(const char *s){sprintf(pairingCodeCommand,"S %9s",s);return(*this);} // sets the Pairing Code - use is NOT recommended. Use 'S' from CLI instead + void deleteStoredValues(){processSerialCommand("V");} // deletes stored Characteristic values from NVS int enableOTA(boolean auth=true, boolean safeLoad=true){return(spanOTA.init(auth, safeLoad, NULL));} // enables Over-the-Air updates, with (auth=true) or without (auth=false) authorization password int enableOTA(const char *pwd, boolean safeLoad=true){return(spanOTA.init(true, safeLoad, pwd));} // enables Over-the-Air updates, with custom authorization password (overrides any password stored with the 'O' command) - void enableWebLog(uint16_t maxEntries=0, const char *serv=NULL, const char *tz="UTC", const char *url=DEFAULT_WEBLOG_URL){ // enable Web Logging + Span& enableWebLog(uint16_t maxEntries=0, const char *serv=NULL, const char *tz="UTC", const char *url=DEFAULT_WEBLOG_URL){ // enable Web Logging webLog.init(maxEntries, serv, tz, url); + return(*this); } void addWebLog(boolean sysMsg, const char *fmt, ...){ // add Web Log entry @@ -343,14 +345,14 @@ class Span{ va_end(ap); } - void setWebLogCSS(const char *css){webLog.css="\n" + String(css) + "\n";} + Span& setWebLogCSS(const char *css){webLog.css="\n" + String(css) + "\n";return(*this);} void autoPoll(uint32_t stackSize=8192, uint32_t priority=1, uint32_t cpu=0){ // start pollTask() xTaskCreateUniversal([](void *parms){for(;;)homeSpan.pollTask();}, "pollTask", stackSize, NULL, priority, &pollTaskHandle, cpu); LOG0("\n*** AutoPolling Task started with priority=%d\n\n",uxTaskPriorityGet(pollTaskHandle)); } - void setTimeServerTimeout(uint32_t tSec){webLog.waitTime=tSec*1000;} // sets wait time (in seconds) for optional web log time server to connect + Span& setTimeServerTimeout(uint32_t tSec){webLog.waitTime=tSec*1000;return(*this);} // sets wait time (in seconds) for optional web log time server to connect [[deprecated("Please use reserveSocketConnections(n) method instead.")]] void setMaxConnections(uint8_t n){requestedMaxCon=n;} // sets maximum number of simultaneous HAP connections diff --git a/src/src.ino b/src/src.ino index d82cf30..26674a7 100644 --- a/src/src.ino +++ b/src/src.ino @@ -45,35 +45,16 @@ struct LED_Service : Service::LightBulb { }; -////////////////////////////////////// - -struct invertedLED : Blinkable { // create a child class derived from Blinkable - - int pin; // variable to store the pin number - - invertedLED(int pin) : pin{pin} { // constructor that initializes the pin parameter - pinMode(pin,OUTPUT); // set the pin to OUTPUT - digitalWrite(pin,HIGH); // set pin HIGH (which is off for an inverted LED) - } - - void on() override { digitalWrite(pin,LOW); } // required function on() - sets pin LOW - void off() override { digitalWrite(pin,HIGH); } // required function off() - sets pin HIGH - int getPin() override { return(pin); } // required function getPin() - returns pin number -}; - - ////////////////////////////////////// void setup() { Serial.begin(115200); -// homeSpan.setLogLevel(-1); + homeSpan.setLogLevel(2) + .setStatusPin(13); // homeSpan.setSerialInputDisable(true); - homeSpan.enableOTA(); - - homeSpan.setStatusDevice(new invertedLED(13)); // set Status LED to be a new Blinkable device attached to pin 13 - homeSpan.setStatusAutoOff(30); +// homeSpan.enableOTA(); homeSpan.begin(Category::Lighting,"HomeSpan LED");