Fully implemented Generic PushButton

Generic PushButton now used for all device control functions.
This commit is contained in:
Gregg 2020-09-26 17:13:17 -05:00
parent b7de0cf710
commit ac25b94b67
3 changed files with 41 additions and 74 deletions

View File

@ -24,8 +24,6 @@ void Span::begin(Category catID, char *displayName, char *hostNameBase, char *mo
this->modelName=modelName; this->modelName=modelName;
sprintf(this->category,"%d",catID); sprintf(this->category,"%d",catID);
pinMode(resetPin,INPUT_PULLUP);
delay(2000); delay(2000);
Serial.print("\n************************************************************\n" Serial.print("\n************************************************************\n"
@ -35,7 +33,7 @@ void Span::begin(Category catID, char *displayName, char *hostNameBase, char *mo
"** Please ensure serial monitor is set to transmit <newlines>\n\n"); "** Please ensure serial monitor is set to transmit <newlines>\n\n");
Serial.print("Device Control: Pin "); Serial.print("Device Control: Pin ");
Serial.print(resetPin); Serial.print(CONTROL_PIN);
Serial.print("\nHomeSpan Version: "); Serial.print("\nHomeSpan Version: ");
Serial.print(HOMESPAN_VERSION); Serial.print(HOMESPAN_VERSION);
Serial.print("\nESP-IDF Version: "); Serial.print("\nESP-IDF Version: ");
@ -46,9 +44,9 @@ void Span::begin(Category catID, char *displayName, char *hostNameBase, char *mo
Serial.print(__TIME__); Serial.print(__TIME__);
Serial.print("\n\n"); Serial.print("\n\n");
if(!digitalRead(resetPin)){ // factory reset pin is low upon start-up if(!digitalRead(CONTROL_PIN)){ // factory reset pin is low upon start-up
nvs_flash_erase(); // erase NVS storage nvs_flash_erase(); // erase NVS storage
Serial.print("** FACTORY RESET PIN LOW! ALL STORED DATA ERASED **\n\n"); Serial.print("** CONTROL BUTTON PRESSED DURING STARTUP! ALL STORED DATA ERASED **\n\n");
statusLED.start(100); statusLED.start(100);
delay(5000); delay(5000);
Serial.print("Re-starting...\n\n"); Serial.print("Re-starting...\n\n");
@ -79,6 +77,8 @@ void Span::poll() {
statusLED.on(); statusLED.on();
} }
controlButton.reset();
Serial.print(displayName); Serial.print(displayName);
Serial.print(" is READY!\n\n"); Serial.print(" is READY!\n\n");
} }
@ -150,38 +150,17 @@ void Span::poll() {
HAPClient::checkNotifications(); HAPClient::checkNotifications();
HAPClient::checkTimedWrites(); HAPClient::checkTimedWrites();
switch(resetPressed){ if(controlButton.triggered(2000,10000)){
case 0: if(controlButton.longPress()){
if(!digitalRead(resetPin)){ statusLED.start(200);
resetPressed=1; delay(2000);
resetTime=millis()+5000;
}
break;
case 1:
if(digitalRead(resetPin)){
resetPressed=0;
} else
if(millis()>resetTime){
resetPressed=2;
statusLED.start(200,0.5,4,800);
resetTime=millis()+6000;
}
break;
case 2:
if(digitalRead(resetPin)){
statusLED.off();
resetPressed=0;
processSerialCommand("U"); // UPAIR Device
} else
if(millis()>resetTime){
statusLED.on();
delay(1000);
processSerialCommand("W"); // Delete WiFi Data and Restart processSerialCommand("W"); // Delete WiFi Data and Restart
} else {
statusLED.off();
controlButton.reset();
processSerialCommand("U"); // UPAIR Device
}
} }
break;
} // switch
} // poll } // poll
@ -222,10 +201,11 @@ void Span::initWifi(){
network.scan(); // scan for networks network.scan(); // scan for networks
resetPressed=0;
int status=-1; int status=-1;
char key[2]; char key[2];
controlButton.reset();
while(status<1){ // loop until a configuration method is chosen and completed while(status<1){ // loop until a configuration method is chosen and completed
if(status==-1){ if(status==-1){
@ -243,19 +223,12 @@ void Span::initWifi(){
Serial.print("\n"); Serial.print("\n");
} }
Serial.print("\nType 'W' <return> to set WiFi credentials or press control button for 3 seconds to start Access Point...\n\n"); Serial.print("\nType 'W <return>' to set WiFi credentials or press Control Button for 3 seconds to start Access Point...\n\n");
status=0; status=0;
sprintf(key,""); sprintf(key,"");
} }
if(!resetPressed){ if(controlButton.triggered(9999,3000)){
if(!digitalRead(resetPin)){
resetPressed=1;
resetTime=millis()+3000;
}
} else if(digitalRead(resetPin)){
resetPressed=0;
} else if(millis()>resetTime){
network.apConfigure(hostName); network.apConfigure(hostName);
status=1; status=1;
} }
@ -299,6 +272,7 @@ void Span::initWifi(){
int nTries=0; int nTries=0;
statusLED.start(1000); statusLED.start(1000);
controlButton.reset();
while(WiFi.status()!=WL_CONNECTED){ while(WiFi.status()!=WL_CONNECTED){
Serial.print("Connecting to: "); Serial.print("Connecting to: ");
@ -309,23 +283,22 @@ void Span::initWifi(){
if(WiFi.begin(network.wifiData.ssid,network.wifiData.pwd)!=WL_CONNECTED){ if(WiFi.begin(network.wifiData.ssid,network.wifiData.pwd)!=WL_CONNECTED){
int delayTime=nTries%6?5000:60000; int delayTime=nTries%6?5000:60000;
char buf[8]=""; 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(delayTime/1000);
Serial.print(" seconds (or type 'W <return>' to reset WiFi data)...\n"); Serial.print(" seconds. Type 'W <return>' or press Control Button for 3 seconds to reset WiFi data...\n");
long sTime=millis(); long sTime=millis();
while(millis()-sTime<delayTime){ while(millis()-sTime<delayTime){
if(Serial.available()){ if(controlButton.triggered(9999,3000) || (Serial.available() && readSerial(buf,1) && (buf[0]=='W'))){
readSerial(buf,1); statusLED.start(100);
if(buf[0]=='W'){ Serial.print("\n** Deleting WIFI Network Data **\n** Restarting...\n\n");
nvs_erase_all(HAPClient::wifiNVS); nvs_erase_all(HAPClient::wifiNVS);
nvs_commit(HAPClient::wifiNVS); nvs_commit(HAPClient::wifiNVS);
Serial.print("\n** WIFI Network Data DELETED **\n** Restarting...\n\n");
delay(2000); delay(2000);
ESP.restart(); ESP.restart();
} }
} }
} }
}
} // WiFi not yet connected } // WiFi not yet connected
Serial.print("Success! IP: "); Serial.print("Success! IP: ");

View File

@ -51,12 +51,8 @@ struct Span{
char *defaultSetupCode=(char *)DEFAULT_SETUP_CODE; // default Setup Code upon factory reset; user will change to desired code when configuring network char *defaultSetupCode=(char *)DEFAULT_SETUP_CODE; // default Setup Code upon factory reset; user will change to desired code when configuring network
int resetPin=21; // drive this pin low to "factory" reset NVS data on start-up
int resetPressed=0; // tracks pressing of reset button
unsigned long resetTime; // tracks time once reset button is pressed
Blinker statusLED{LED_BUILTIN}; // indicates HomeSpan status Blinker statusLED{LED_BUILTIN}; // indicates HomeSpan status
PushButton controlButton{DEFAULT_CONTROL_PIN}; // controls HomeSpan configuration and resets PushButton controlButton{CONTROL_PIN}; // 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 SpanConfig hapConfig; // track configuration changes to the HAP Accessory database; used to increment the configuration number (c#) when changes found
vector<SpanAccessory *> Accessories; // vector of pointers to all Accessories vector<SpanAccessory *> Accessories; // vector of pointers to all Accessories
@ -87,8 +83,6 @@ struct Span{
void clearNotify(int slotNum); // set ev notification flags for connection 'slotNum' to false across all characteristics 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 int sprintfNotify(SpanBuf *pObj, int nObj, char *cBuf, int conNum); // prints notification JSON into buf based on SpanBuf objects and specified connection number
void setResetPin(int pin){resetPin=pin;} // sets new pin to be used for factory reset
}; };
/////////////////////////////// ///////////////////////////////

View File

@ -14,9 +14,9 @@ const char HOMESPAN_VERSION[]="1.0.0";
const char DEFAULT_SETUP_CODE[]="46637726"; const char DEFAULT_SETUP_CODE[]="46637726";
////////////////////////////////////////////////////// //////////////////////////////////////////////////////
// DEFAULT CONTROL BUTTON PIN // // CONTROL BUTTON PIN //
const int DEFAULT_CONTROL_PIN=21; const int CONTROL_PIN=21;
////////////////////////////////////////////////////// //////////////////////////////////////////////////////
// Maximum number of simultaenous IP connections // // Maximum number of simultaenous IP connections //