Added homeSpan.setStatusAutoOff(uint16_t duration)
Optional method to automatically turn off Status LED after a *duration* seconds. LED will resume normal operation any time it is re-triggered with a new pattern. This also resets the elapsed time used to check for autoOff.
This commit is contained in:
parent
81776f3366
commit
d667f5e81d
|
|
@ -58,7 +58,7 @@ void Span::begin(Category catID, const char *displayName, const char *hostNameBa
|
||||||
esp_task_wdt_delete(xTaskGetIdleTaskHandleForCPU(0)); // required to avoid watchdog timeout messages from ESP32-C3
|
esp_task_wdt_delete(xTaskGetIdleTaskHandleForCPU(0)); // required to avoid watchdog timeout messages from ESP32-C3
|
||||||
|
|
||||||
controlButton.init(controlPin);
|
controlButton.init(controlPin);
|
||||||
statusLED.init(statusPin);
|
statusLED.init(statusPin,0,autoOffLED);
|
||||||
|
|
||||||
int maxLimit=CONFIG_LWIP_MAX_SOCKETS-2-otaEnabled;
|
int maxLimit=CONFIG_LWIP_MAX_SOCKETS-2-otaEnabled;
|
||||||
if(maxConnections>maxLimit)
|
if(maxConnections>maxLimit)
|
||||||
|
|
@ -85,7 +85,7 @@ void Span::begin(Category catID, const char *displayName, const char *hostNameBa
|
||||||
nvs_get_blob(wifiNVS,"WIFIDATA",&homeSpan.network.wifiData,&len); // retrieve data
|
nvs_get_blob(wifiNVS,"WIFIDATA",&homeSpan.network.wifiData,&len); // retrieve data
|
||||||
|
|
||||||
delay(2000);
|
delay(2000);
|
||||||
|
|
||||||
Serial.print("\n************************************************************\n"
|
Serial.print("\n************************************************************\n"
|
||||||
"Welcome to HomeSpan!\n"
|
"Welcome to HomeSpan!\n"
|
||||||
"Apple HomeKit for the Espressif ESP-32 WROOM and Arduino IDE\n"
|
"Apple HomeKit for the Espressif ESP-32 WROOM and Arduino IDE\n"
|
||||||
|
|
@ -95,8 +95,11 @@ void Span::begin(Category catID, const char *displayName, const char *hostNameBa
|
||||||
Serial.print("Message Logs: Level ");
|
Serial.print("Message Logs: Level ");
|
||||||
Serial.print(logLevel);
|
Serial.print(logLevel);
|
||||||
Serial.print("\nStatus LED: Pin ");
|
Serial.print("\nStatus LED: Pin ");
|
||||||
if(statusPin>=0)
|
if(statusPin>=0){
|
||||||
Serial.print(statusPin);
|
Serial.print(statusPin);
|
||||||
|
if(autoOffLED>0)
|
||||||
|
Serial.printf(" (Auto Off=%d sec)",autoOffLED);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
Serial.print("- *** WARNING: Status LED Pin is UNDEFINED");
|
Serial.print("- *** WARNING: Status LED Pin is UNDEFINED");
|
||||||
Serial.print("\nDevice Control: Pin ");
|
Serial.print("\nDevice Control: Pin ");
|
||||||
|
|
@ -291,6 +294,8 @@ void Span::poll() {
|
||||||
commandMode(); // COMMAND MODE
|
commandMode(); // COMMAND MODE
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
statusLED.check();
|
||||||
|
|
||||||
} // poll
|
} // poll
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -119,7 +119,8 @@ struct Span{
|
||||||
unsigned long alarmConnect=0; // time after which WiFi connection attempt should be tried again
|
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
|
const char *defaultSetupCode=DEFAULT_SETUP_CODE; // Setup Code used for pairing
|
||||||
int statusPin=DEFAULT_STATUS_PIN; // pin for status LED
|
int statusPin=DEFAULT_STATUS_PIN; // pin for Status LED
|
||||||
|
uint16_t autoOffLED=0; // automatic turn-off duration (in seconds) for Status LED
|
||||||
int controlPin=DEFAULT_CONTROL_PIN; // pin for Control Pushbutton
|
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 logLevel=DEFAULT_LOG_LEVEL; // level for writing out log messages to serial monitor
|
||||||
uint8_t maxConnections=DEFAULT_MAX_CONNECTIONS; // number of simultaneous HAP connections
|
uint8_t maxConnections=DEFAULT_MAX_CONNECTIONS; // number of simultaneous HAP connections
|
||||||
|
|
@ -173,6 +174,7 @@ struct Span{
|
||||||
|
|
||||||
void setControlPin(uint8_t pin){controlPin=pin;} // sets Control Pin
|
void setControlPin(uint8_t pin){controlPin=pin;} // sets Control Pin
|
||||||
void setStatusPin(uint8_t pin){statusPin=pin;} // sets Status Pin
|
void setStatusPin(uint8_t pin){statusPin=pin;} // sets Status Pin
|
||||||
|
void setStatusAutoOff(uint16_t duration){autoOffLED=duration;} // sets Status LED auto off (seconds)
|
||||||
int getStatusPin(){return(statusPin);} // get Status Pin
|
int getStatusPin(){return(statusPin);} // get Status Pin
|
||||||
void setApSSID(const char *ssid){network.apSSID=ssid;} // sets Access Point SSID
|
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 setApPassword(const char *pwd){network.apPassword=pwd;} // sets Access Point Password
|
||||||
|
|
|
||||||
|
|
@ -232,13 +232,13 @@ Blinker::Blinker(){
|
||||||
|
|
||||||
//////////////////////////////////////
|
//////////////////////////////////////
|
||||||
|
|
||||||
Blinker::Blinker(int pin, int timerNum){
|
Blinker::Blinker(int pin, int timerNum, uint16_t autoOffDuration){
|
||||||
init(pin, timerNum);
|
init(pin, timerNum, autoOffDuration);
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////////////////////
|
//////////////////////////////////////
|
||||||
|
|
||||||
void Blinker::init(int pin, int timerNum){
|
void Blinker::init(int pin, int timerNum, uint16_t autoOffDuration){
|
||||||
|
|
||||||
this->pin=pin;
|
this->pin=pin;
|
||||||
if(pin<0)
|
if(pin<0)
|
||||||
|
|
@ -247,6 +247,8 @@ void Blinker::init(int pin, int timerNum){
|
||||||
pinMode(pin,OUTPUT);
|
pinMode(pin,OUTPUT);
|
||||||
digitalWrite(pin,0);
|
digitalWrite(pin,0);
|
||||||
|
|
||||||
|
pauseDuration=autoOffDuration*1000;
|
||||||
|
|
||||||
#if SOC_TIMER_GROUP_TIMERS_PER_GROUP>1 // ESP32 and ESP32-S2 contains two timers per timer group
|
#if SOC_TIMER_GROUP_TIMERS_PER_GROUP>1 // ESP32 and ESP32-S2 contains two timers per timer group
|
||||||
group=((timerNum/2)%2==0)?TIMER_GROUP_0:TIMER_GROUP_1;
|
group=((timerNum/2)%2==0)?TIMER_GROUP_0:TIMER_GROUP_1;
|
||||||
idx=(timerNum%2==0)?TIMER_0:TIMER_1; // ESP32-C3 only contains one timer per timer group
|
idx=(timerNum%2==0)?TIMER_0:TIMER_1; // ESP32-C3 only contains one timer per timer group
|
||||||
|
|
@ -327,7 +329,9 @@ void Blinker::start(int period, float dutyCycle, int nBlinks, int delayTime){
|
||||||
if(pin<0)
|
if(pin<0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
gpio_set_direction((gpio_num_t)pin, GPIO_MODE_INPUT_OUTPUT); // needed to ensure digitalRead() functions correctly on ESP32-C3
|
pauseTime=millis();
|
||||||
|
isPaused=false;
|
||||||
|
gpio_set_direction((gpio_num_t)pin, GPIO_MODE_INPUT_OUTPUT); // needed to ensure digitalRead() functions correctly on ESP32-C3; also needed to re-enable after pause()
|
||||||
|
|
||||||
period*=10;
|
period*=10;
|
||||||
onTime=dutyCycle*period;
|
onTime=dutyCycle*period;
|
||||||
|
|
@ -357,6 +361,10 @@ void Blinker::on(){
|
||||||
if(pin<0)
|
if(pin<0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
pauseTime=millis();
|
||||||
|
isPaused=false;
|
||||||
|
gpio_set_direction((gpio_num_t)pin, GPIO_MODE_INPUT_OUTPUT);
|
||||||
|
|
||||||
stop();
|
stop();
|
||||||
digitalWrite(pin,1);
|
digitalWrite(pin,1);
|
||||||
}
|
}
|
||||||
|
|
@ -368,6 +376,25 @@ void Blinker::off(){
|
||||||
if(pin<0)
|
if(pin<0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
pauseTime=millis();
|
||||||
|
isPaused=false;
|
||||||
|
gpio_set_direction((gpio_num_t)pin, GPIO_MODE_INPUT_OUTPUT);
|
||||||
|
|
||||||
stop();
|
stop();
|
||||||
digitalWrite(pin,0);
|
digitalWrite(pin,0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//////////////////////////////////////
|
||||||
|
|
||||||
|
void Blinker::check(){
|
||||||
|
|
||||||
|
if(pin<0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if(pauseDuration==0 || isPaused || (millis()-pauseTime)<pauseDuration)
|
||||||
|
return;
|
||||||
|
|
||||||
|
Serial.print("Pausing Status LED\n");
|
||||||
|
isPaused=true;
|
||||||
|
gpio_set_direction((gpio_num_t)pin, GPIO_MODE_DISABLE);
|
||||||
|
}
|
||||||
|
|
|
||||||
39
src/Utils.h
39
src/Utils.h
|
|
@ -165,13 +165,17 @@ class Blinker {
|
||||||
int offTime;
|
int offTime;
|
||||||
int delayTime;
|
int delayTime;
|
||||||
int count;
|
int count;
|
||||||
|
|
||||||
|
unsigned long pauseDuration;
|
||||||
|
unsigned long pauseTime;
|
||||||
|
boolean isPaused=false;
|
||||||
|
|
||||||
static void isrTimer(void *arg);
|
static void isrTimer(void *arg);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
Blinker();
|
Blinker();
|
||||||
Blinker(int pin, int timerNum=0);
|
Blinker(int pin, int timerNum=0, uint16_t autoOffDuration=0);
|
||||||
|
|
||||||
// Creates a generic blinking LED on specified pin controlled
|
// Creates a generic blinking LED on specified pin controlled
|
||||||
// in background via interrupts generated by an ESP32 Alarm Timer.
|
// in background via interrupts generated by an ESP32 Alarm Timer.
|
||||||
|
|
@ -183,19 +187,29 @@ class Blinker {
|
||||||
// In the second form, a Blinker is instantiated and initialized with
|
// In the second form, a Blinker is instantiated and initialized with
|
||||||
// the specified pin, obviating the need for a separate call to init().
|
// the specified pin, obviating the need for a separate call to init().
|
||||||
//
|
//
|
||||||
// pin: Pin mumber to control. Blinker will set pinMode to OUTPUT automatically
|
// pin: Pin mumber to control. Blinker will set pinMode to OUTPUT automatically
|
||||||
// timerNum: ESP32 Alarm Timer to use.
|
//
|
||||||
// For ESP32 and ESP32-S2: 0=Group0/Timer0, 1=Group0/Timer1, 2=Group1/Timer0, 3=Group1/Timer1
|
// timerNum: ESP32 Alarm Timer to use.
|
||||||
// For ESP32-C3: 0=Group0/Timer0, 1=Group1/Timer0
|
// For ESP32 and ESP32-S2: 0=Group0/Timer0, 1=Group0/Timer1, 2=Group1/Timer0, 3=Group1/Timer1
|
||||||
|
// For ESP32-C3: 0=Group0/Timer0, 1=Group1/Timer0
|
||||||
|
//
|
||||||
|
// autoOffDuration: If greater than zero, Blinker will automatically turn off after autoOffDuration (in seconds) has elapsed
|
||||||
|
// Blinker will resume normal operation upon next call to start(), on(), or off()
|
||||||
|
// Program must periodically call check() for auto-off functionality to work
|
||||||
|
|
||||||
void init(int pin, int timerNum=0);
|
void init(int pin, int timerNum=0, uint16_t autoOffDuration=0);
|
||||||
|
|
||||||
// Initializes Blinker, if not configured during instantiation.
|
// Initializes Blinker, if not configured during instantiation.
|
||||||
//
|
//
|
||||||
// pin: Pin mumber to control. Blinker will set pinMode to OUTPUT automatically
|
// pin: Pin mumber to control. Blinker will set pinMode to OUTPUT automatically
|
||||||
// timerNum: ESP32 Alarm Timer to use.
|
//
|
||||||
// For ESP32 and ESP32-S2: 0=Group0/Timer0, 1=Group0/Timer1, 2=Group1/Timer0, 3=Group1/Timer1
|
// timerNum: ESP32 Alarm Timer to use.
|
||||||
// For ESP32-C3: 0=Group0/Timer0, 1=Group1/Timer0
|
// For ESP32 and ESP32-S2: 0=Group0/Timer0, 1=Group0/Timer1, 2=Group1/Timer0, 3=Group1/Timer1
|
||||||
|
// For ESP32-C3: 0=Group0/Timer0, 1=Group1/Timer0
|
||||||
|
//
|
||||||
|
// autoOffDuration: If greater than zero, Blinker will automatically turn off after autoOffDuration (in seconds) has elapsed
|
||||||
|
// Blinker will resume normal operation upon next call to start(), on(), or off()
|
||||||
|
// Program must periodically call check() for auto-off functionality to work
|
||||||
|
|
||||||
void start(int period, float dutyCycle=0.5);
|
void start(int period, float dutyCycle=0.5);
|
||||||
|
|
||||||
|
|
@ -225,4 +239,9 @@ class Blinker {
|
||||||
|
|
||||||
// Stops current blinking pattern and turns off LED
|
// Stops current blinking pattern and turns off LED
|
||||||
|
|
||||||
|
void check();
|
||||||
|
|
||||||
|
// Optional check to see if LED output should be paused (check is bypassed if pauseDuration=0)
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue