From 6de645216c6ec638fb77eb9b1c02657917fdc7a3 Mon Sep 17 00:00:00 2001 From: Gregg Date: Tue, 5 Oct 2021 21:10:24 -0500 Subject: [PATCH] CHANGED default settings for StatusPin and ControlPin Re-worked code to allow for NO Status LED Pin and NO Control Pin. If Control Pin is not set explicitly with homeSpan.setControlPin(), there will be no Control Pin. There is no longer a default since there are too many board variations with S2 and C3 chips now supported. Same for Status Pin - it will not be defined unless set explicitly with homeSpan.setStatusPin(), with the exception that if LED_BUILTIN is defined (i.e. there is a built-in LED), then the Status LED Pin will default to LED_BUILTIN if not explicitly defined. MUST UPDATE DOCUMENTATION - THIS CHANGES DEFAULT BEHAVIOR OF HOMESPAN AND MAY REQUIRE UPDATES TO EXISTING SKETCHES --- src/HomeSpan.cpp | 12 +++++++++--- src/HomeSpan.h | 6 +++--- src/Settings.h | 16 ++++++++++------ src/Utils.cpp | 37 +++++++++++++++++++++++++++++++++++-- src/Utils.h | 6 +++--- 5 files changed, 60 insertions(+), 17 deletions(-) diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 54d96c3..8e53c99 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -95,14 +95,20 @@ void Span::begin(Category catID, const char *displayName, const char *hostNameBa Serial.print("Message Logs: Level "); Serial.print(logLevel); Serial.print("\nStatus LED: Pin "); - Serial.print(statusPin); + if(statusPin>=0) + Serial.print(statusPin); + else + Serial.print("- *** WARNING: Status LED Pin is UNDEFINED"); Serial.print("\nDevice Control: Pin "); - Serial.print(controlPin); + if(controlPin>=0) + Serial.print(controlPin); + else + Serial.print("- *** WARNING: Device Control Pin is UNDEFINED"); Serial.print("\nSketch Version: "); Serial.print(getSketchVersion()); Serial.print("\nHomeSpan Version: "); Serial.print(HOMESPAN_VERSION); - Serial.print("\nArduino-ESP Ver: "); + Serial.print("\nArduino-ESP Ver.: "); Serial.print(ARDUINO_ESP_VERSION); Serial.printf("\nESP-IDF Version: %d.%d.%d",ESP_IDF_VERSION_MAJOR,ESP_IDF_VERSION_MINOR,ESP_IDF_VERSION_PATCH); Serial.printf("\nESP32 Chip: %s Rev %d %s-core %dMB Flash", ESP.getChipModel(),ESP.getChipRevision(), diff --git a/src/HomeSpan.h b/src/HomeSpan.h index c7192cd..daafb93 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -118,8 +118,8 @@ struct Span{ unsigned long alarmConnect=0; // time after which WiFi connection attempt should be tried again const char *defaultSetupCode=DEFAULT_SETUP_CODE; // Setup Code used for pairing - uint8_t statusPin=DEFAULT_STATUS_PIN; // pin for status LED - uint8_t controlPin=DEFAULT_CONTROL_PIN; // pin for Control Pushbutton + int statusPin=DEFAULT_STATUS_PIN; // pin for status LED + int controlPin=DEFAULT_CONTROL_PIN; // pin for Control Pushbutton uint8_t logLevel=DEFAULT_LOG_LEVEL; // level for writing out log messages to serial monitor uint8_t maxConnections=DEFAULT_MAX_CONNECTIONS; // number of simultaneous HAP connections unsigned long comModeLife=DEFAULT_COMMAND_TIMEOUT*1000; // length of time (in milliseconds) to keep Command Mode alive before resuming normal operations @@ -171,7 +171,7 @@ struct Span{ void setControlPin(uint8_t pin){controlPin=pin;} // sets Control Pin void setStatusPin(uint8_t pin){statusPin=pin;} // sets Status Pin - int getStatusPin(){return(statusPin);} // gets Status Pin + int getStatusPin(){return(statusPin);} // get Status Pin 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) diff --git a/src/Settings.h b/src/Settings.h index ec240a1..bcf1178 100644 --- a/src/Settings.h +++ b/src/Settings.h @@ -67,12 +67,17 @@ #define DEFAULT_QR_ID "HSPN" // change with homeSpan.setQRID(qrID); -#define DEFAULT_CONTROL_PIN 21 // change with homeSpan.setControlPin(pin) -#define DEFAULT_STATUS_PIN 13 // change with homeSpan.setStatusPin(pin) +#define DEFAULT_CONTROL_PIN -1 // change with homeSpan.setControlPin(pin) -#define DEFAULT_AP_SSID "HomeSpan-Setup" // change with homeSpan.setApSSID(ssid) -#define DEFAULT_AP_PASSWORD "homespan" // change with homeSpan.setApPassword(pwd) -#define DEFAULT_OTA_PASSWORD "homespan-ota" // change with 'O' command +#ifdef LED_BUILTIN +#define DEFAULT_STATUS_PIN LED_BUILTIN // change with homeSpan.setStatusPin(pin) +#else +#define DEFAULT_STATUS_PIN -1 // change with homeSpan.setStatusPin(pin) +#endif + +#define DEFAULT_AP_SSID "HomeSpan-Setup" // change with homeSpan.setApSSID(ssid) +#define DEFAULT_AP_PASSWORD "homespan" // change with homeSpan.setApPassword(pwd) +#define DEFAULT_OTA_PASSWORD "homespan-ota" // change with 'O' command #define DEFAULT_AP_TIMEOUT 300 // change with homeSpan.setApTimeout(nSeconds) #define DEFAULT_COMMAND_TIMEOUT 120 // change with homeSpan.setCommandTimeout(nSeconds) @@ -82,7 +87,6 @@ #define DEFAULT_MAX_CONNECTIONS 8 // change with homeSpan.setMaxConnections(num); #define DEFAULT_TCP_PORT 80 // change with homeSpan.setPort(port); - ///////////////////////////////////////////////////// // STATUS LED SETTINGS // diff --git a/src/Utils.cpp b/src/Utils.cpp index dd97b63..ea34718 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -89,13 +89,17 @@ PushButton::PushButton(){} ////////////////////////////////////// -PushButton::PushButton(uint8_t pin){ +PushButton::PushButton(int pin){ init(pin); } ////////////////////////////////////// -void PushButton::init(uint8_t pin){ +void PushButton::init(int pin){ + + if(pin<0) + return; + status=0; doubleCheck=false; this->pin=pin; @@ -106,6 +110,9 @@ void PushButton::init(uint8_t pin){ boolean PushButton::triggered(uint16_t singleTime, uint16_t longTime, uint16_t doubleTime){ + if(pin<0) + return(false); + unsigned long cTime=millis(); switch(status){ @@ -182,6 +189,9 @@ boolean PushButton::triggered(uint16_t singleTime, uint16_t longTime, uint16_t d ////////////////////////////////////// boolean PushButton::primed(){ + + if(pin<0) + return(false); if(millis()>singleAlarm && status==1){ status=2; @@ -200,6 +210,10 @@ int PushButton::type(){ ////////////////////////////////////// void PushButton::wait(){ + + if(pin<0) + return; + while(!digitalRead(pin)); } @@ -225,7 +239,11 @@ Blinker::Blinker(int pin, int timerNum){ ////////////////////////////////////// void Blinker::init(int pin, int timerNum){ + this->pin=pin; + if(pin<0) + return; + pinMode(pin,OUTPUT); digitalWrite(pin,0); @@ -322,6 +340,9 @@ void Blinker::start(int period, float dutyCycle){ void Blinker::start(int period, float dutyCycle, int nBlinks, int delayTime){ + if(pin<0) + return; + gpio_set_direction((gpio_num_t)pin, GPIO_MODE_INPUT_OUTPUT); // needed to ensure digitalRead() functions correctly on ESP32-C3 period*=10; @@ -338,12 +359,20 @@ void Blinker::start(int period, float dutyCycle, int nBlinks, int delayTime){ ////////////////////////////////////// void Blinker::stop(){ + + if(pin<0) + return; + timer_pause(group,idx); } ////////////////////////////////////// void Blinker::on(){ + + if(pin<0) + return; + stop(); digitalWrite(pin,1); } @@ -351,6 +380,10 @@ void Blinker::on(){ ////////////////////////////////////// void Blinker::off(){ + + if(pin<0) + return; + stop(); digitalWrite(pin,0); } diff --git a/src/Utils.h b/src/Utils.h index c90a78d..c928054 100644 --- a/src/Utils.h +++ b/src/Utils.h @@ -74,7 +74,7 @@ struct TempBuffer { class PushButton{ int status; - uint8_t pin; + int pin; boolean doubleCheck; uint32_t singleAlarm; uint32_t doubleAlarm; @@ -90,7 +90,7 @@ class PushButton{ }; PushButton(); - PushButton(uint8_t pin); + PushButton(int pin); // Creates generic pushbutton functionality on specified pin // that is wired to connect to ground when the button is pressed. @@ -104,7 +104,7 @@ class PushButton{ // // pin: Pin mumber to which pushbutton connects to ground when pressed - void init(uint8_t pin); + void init(int pin); // Initializes PushButton, if not configured during instantiation. //