diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index fa22b5f..0b48a7e 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -23,6 +23,9 @@ void Span::begin(Category catID, char *displayName, char *hostNameBase, char *mo this->hostNameBase=hostNameBase; this->modelName=modelName; sprintf(this->category,"%d",catID); + + controlButton.init(controlPin); + statusLED.init(statusPin); delay(2000); @@ -32,8 +35,10 @@ void Span::begin(Category catID, char *displayName, char *hostNameBase, char *mo "************************************************************\n\n" "** Please ensure serial monitor is set to transmit \n\n"); - Serial.print("Device Control: Pin "); - Serial.print(CONTROL_PIN); + Serial.print("Status LED: Pin "); + Serial.print(statusPin); + Serial.print("\nDevice Control: Pin "); + Serial.print(controlPin); Serial.print("\nHomeSpan Version: "); Serial.print(HOMESPAN_VERSION); Serial.print("\nESP-IDF Version: "); @@ -44,7 +49,7 @@ void Span::begin(Category catID, char *displayName, char *hostNameBase, char *mo Serial.print(__TIME__); Serial.print("\n\n"); - if(!digitalRead(CONTROL_PIN)){ // factory reset pin is low upon start-up + if(!digitalRead(controlPin)){ // factory reset pin is low upon start-up nvs_flash_erase(); // erase NVS storage Serial.print("** CONTROL BUTTON PRESSED DURING STARTUP! ALL STORED DATA ERASED **\n\n"); statusLED.start(100); @@ -283,7 +288,7 @@ void Span::initWifi(){ if(WiFi.begin(network.wifiData.ssid,network.wifiData.pwd)!=WL_CONNECTED){ int delayTime=nTries%6?5000:60000; char buf[8]=""; - Serial.print("Can't connect --- Re-trying in "); + Serial.print("Can't connect. Re-trying in "); Serial.print(delayTime/1000); Serial.print(" seconds. Type 'W ' or press Control Button for 3 seconds to reset WiFi data...\n"); long sTime=millis(); diff --git a/src/HomeSpan.h b/src/HomeSpan.h index 9887650..c8f1d13 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -49,10 +49,12 @@ struct Span{ char category[3]=""; // category ID of primary accessory - broadcast as Bonjour field "ci" (HAP Section 13) unsigned long snapTime; // current time (in millis) snapped before entering Service loops() or updates() - char *defaultSetupCode=(char *)DEFAULT_SETUP_CODE; // default Setup Code upon factory reset; user will change to desired code when configuring network + char *defaultSetupCode=DEFAULT_SETUP_CODE; // default configuration parameters + uint8_t statusPin=DEFAULT_STATUS_PIN; + uint8_t controlPin=DEFAULT_CONTROL_PIN; - Blinker statusLED{LED_BUILTIN}; // indicates HomeSpan status - PushButton controlButton{CONTROL_PIN}; // controls HomeSpan configuration and resets + Blinker statusLED; // indicates HomeSpan status + PushButton controlButton; // controls HomeSpan configuration and resets SpanConfig hapConfig; // track configuration changes to the HAP Accessory database; used to increment the configuration number (c#) when changes found vector Accessories; // vector of pointers to all Accessories @@ -83,6 +85,9 @@ struct Span{ void clearNotify(int slotNum); // set ev notification flags for connection 'slotNum' to false across all characteristics int sprintfNotify(SpanBuf *pObj, int nObj, char *cBuf, int conNum); // prints notification JSON into buf based on SpanBuf objects and specified connection number + void setControlPin(uint8_t pin){controlPin=pin;} // sets Control Pin + void setStatusPin(uint8_t pin){statusPin=pin;} // sets Status Pin + }; /////////////////////////////// diff --git a/src/Network.cpp b/src/Network.cpp index 753e8be..3ab9ed0 100644 --- a/src/Network.cpp +++ b/src/Network.cpp @@ -64,7 +64,7 @@ boolean Network::serialConfigure(){ if(WiFi.begin(wifiData.ssid,wifiData.pwd)!=WL_CONNECTED){ char buf[8]=""; - Serial.print("Can't connect. Re-trying in 5 seconds (or type 'X ' to cancel)..."); + Serial.print("Can't connect. Re-trying in 5 seconds. Type 'X ' to cancel..."); long sTime=millis(); while(millis()-sTime<5000){ if(Serial.available()){ diff --git a/src/Settings.h b/src/Settings.h index 71f88d3..500ec4e 100644 --- a/src/Settings.h +++ b/src/Settings.h @@ -6,17 +6,14 @@ ////////////////////////////////////////////////////// // HomeSpan Version // -const char HOMESPAN_VERSION[]="1.0.0"; +#define HOMESPAN_VERSION "1.0.0" ////////////////////////////////////////////////////// -// DEFAULT SETUP CODE // +// DEFAULT SETTINGS -const char DEFAULT_SETUP_CODE[]="46637726"; - -////////////////////////////////////////////////////// -// CONTROL BUTTON PIN // - -const int CONTROL_PIN=21; +#define DEFAULT_SETUP_CODE "46637726" // changed during network setup or with 'S' command +#define DEFAULT_CONTROL_PIN 21 // changed with homeSpan.setControlPin(pin) +#define DEFAULT_STATUS_PIN LED_BUILTIN // changed with homeSpan.setStatusPin(pin) ////////////////////////////////////////////////////// // Maximum number of simultaenous IP connections // diff --git a/src/Utils.cpp b/src/Utils.cpp index 0b49ff9..1fe2db4 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -58,9 +58,19 @@ String Utils::mask(char *c, int n){ // PushButton // //////////////////////////////// +PushButton::PushButton(){} + +////////////////////////////////////// + PushButton::PushButton(uint8_t pin){ - this->pin=pin; + init(pin); +} + +////////////////////////////////////// + +void PushButton::init(uint8_t pin){ status=0; + this->pin=pin; pinMode(pin, INPUT_PULLUP); } @@ -120,7 +130,18 @@ void PushButton::reset(){ // Blinker // //////////////////////////////// +Blinker::Blinker(){ +} + +////////////////////////////////////// + Blinker::Blinker(int pin, int timerNum){ + init(pin, timerNum); +} + +////////////////////////////////////// + +void Blinker::init(int pin, int timerNum){ this->pin=pin; pinMode(pin,OUTPUT); digitalWrite(pin,0); diff --git a/src/Utils.h b/src/Utils.h index 2d3ab6e..e7316d1 100644 --- a/src/Utils.h +++ b/src/Utils.h @@ -47,16 +47,30 @@ class PushButton{ public: + PushButton(); PushButton(uint8_t pin); // Creates generic pushbutton functionality on specified pin // that is wired to connect to ground when the button is pressed. // -// pin: Pin mumber to which pushbutton is connects to ground when pressed +// In the first form, a PushButton is instantiated without specifying +// the pin. In this case the pin must be specified in a subsequent call +// to init() before the PushButton can be used. +// +// In the second form, a PushButton is instantiated and initialized with +// the specified pin, obviating the need for a separate call to init(). +// +// pin: Pin mumber to which pushbutton connects to ground when pressed + + void init(uint8_t pin); + +// Initializes PushButton, if not configured during instantiation. +// +// pin: Pin mumber to which pushbutton connects to ground when pressed void reset(); -// Resets state of pushbutton. Should be called once before any loops that will +// Resets state of PushButton. Should be called once before any loops that will // repeatedly check the button for a trigger event. boolean triggered(uint16_t shortTime, uint16_t longTime); @@ -98,14 +112,29 @@ class Blinker { public: + Blinker(); Blinker(int pin, int timerNum=0); // Creates a generic blinking LED on specified pin controlled // in background via interrupts generated by an ESP32 Alarm Timer. // +// In the first form, a Blinker is instantiated without specifying +// the pin. In this case the pin must be specified in a subsequent call +// to init() before the Blinker can be used. +// +// In the second form, a Blinker is instantiated and initialized with +// the specified pin, obviating the need for a separate call to init(). +// // pin: Pin mumber to control. Blinker will set pinMode to OUTPUT automatically // timerNum: ESP32 Alarm Timer to use. 0=Group0/Timer0, 1=Group0/Timer1, 2=Group1/Timer0, 3=Group1/Timer1 + void init(int pin, int timerNum=0); + +// Initializes Blinker, if not configured during instantiation. +// +// pin: Pin mumber to control. Blinker will set pinMode to OUTPUT automatically +// timerNum: ESP32 Alarm Timer to use. 0=Group0/Timer0, 1=Group0/Timer1, 2=Group1/Timer0, 3=Group1/Timer1 + void start(int period, float dutyCycle=0.5); // Starts simple ON/OFF blinking. diff --git a/src/src.ino b/src/src.ino index 79ceba5..7217adc 100644 --- a/src/src.ino +++ b/src/src.ino @@ -7,26 +7,26 @@ void setup() { Serial.begin(115200); + + homeSpan.setStatusPin(22); homeSpan.begin(Category::Lighting,"HomeSpan Benchmark"); - char *version=(char *)HOMESPAN_VERSION; - new SpanAccessory(); // Begin by creating a new Accessory using SpanAccessory(), which takes no arguments - new Service::AccessoryInformation(); // HAP requires every Accessory to implement an AccessoryInformation Service, which has 6 required Characteristics - new Characteristic::Name("HomeSpan Test"); // Name of the Accessory, which shows up on the HomeKit "tiles", and should be unique across Accessories - new Characteristic::Manufacturer("HomeSpan"); // Manufacturer of the Accessory (arbitrary text string, and can be the same for every Accessory) - new Characteristic::SerialNumber("HSL-123"); // Serial Number of the Accessory (arbitrary text string, and can be the same for every Accessory) - new Characteristic::Model("HSL Test"); // Model of the Accessory (arbitrary text string, and can be the same for every Accessory) - new Characteristic::FirmwareRevision(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::AccessoryInformation(); // HAP requires every Accessory to implement an AccessoryInformation Service, which has 6 required Characteristics + new Characteristic::Name("HomeSpan Test"); // Name of the Accessory, which shows up on the HomeKit "tiles", and should be unique across Accessories + new Characteristic::Manufacturer("HomeSpan"); // Manufacturer of the Accessory (arbitrary text string, and can be the same for every Accessory) + new Characteristic::SerialNumber("HSL-123"); // Serial Number of the Accessory (arbitrary text string, and can be the same for every Accessory) + new Characteristic::Model("HSL Test"); // Model of the Accessory (arbitrary text string, and can be the same for every Accessory) + 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 Characteristic::Version("1.1.0"); // Set the Version Characteristicto "1.1.0" as required by HAP + new Service::HAPProtocolInformation(); // Create the HAP Protcol Information Service + new Characteristic::Version("1.1.0"); // Set the Version Characteristicto "1.1.0" as required by HAP - new Service::LightBulb(); // Create the Light Bulb Service - new Characteristic::On(); // This Service requires the "On" Characterstic to turn the light on and off + new Service::LightBulb(); // Create the Light Bulb Service + new Characteristic::On(); // This Service requires the "On" Characterstic to turn the light on and off } // end of setup()