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:
Gregg 2021-12-26 23:07:37 -06:00
parent 81776f3366
commit d667f5e81d
4 changed files with 71 additions and 18 deletions

View File

@ -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
controlButton.init(controlPin);
statusLED.init(statusPin);
statusLED.init(statusPin,0,autoOffLED);
int maxLimit=CONFIG_LWIP_MAX_SOCKETS-2-otaEnabled;
if(maxConnections>maxLimit)
@ -95,8 +95,11 @@ void Span::begin(Category catID, const char *displayName, const char *hostNameBa
Serial.print("Message Logs: Level ");
Serial.print(logLevel);
Serial.print("\nStatus LED: Pin ");
if(statusPin>=0)
if(statusPin>=0){
Serial.print(statusPin);
if(autoOffLED>0)
Serial.printf(" (Auto Off=%d sec)",autoOffLED);
}
else
Serial.print("- *** WARNING: Status LED Pin is UNDEFINED");
Serial.print("\nDevice Control: Pin ");
@ -292,6 +295,8 @@ void Span::poll() {
}
}
statusLED.check();
} // poll
///////////////////////////////

View File

@ -119,7 +119,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
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
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
@ -173,6 +174,7 @@ struct Span{
void setControlPin(uint8_t pin){controlPin=pin;} // sets Control 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
void setApSSID(const char *ssid){network.apSSID=ssid;} // sets Access Point SSID
void setApPassword(const char *pwd){network.apPassword=pwd;} // sets Access Point Password

View File

@ -232,13 +232,13 @@ Blinker::Blinker(){
//////////////////////////////////////
Blinker::Blinker(int pin, int timerNum){
init(pin, timerNum);
Blinker::Blinker(int pin, int timerNum, uint16_t autoOffDuration){
init(pin, timerNum, autoOffDuration);
}
//////////////////////////////////////
void Blinker::init(int pin, int timerNum){
void Blinker::init(int pin, int timerNum, uint16_t autoOffDuration){
this->pin=pin;
if(pin<0)
@ -247,6 +247,8 @@ void Blinker::init(int pin, int timerNum){
pinMode(pin,OUTPUT);
digitalWrite(pin,0);
pauseDuration=autoOffDuration*1000;
#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;
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)
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;
onTime=dutyCycle*period;
@ -357,6 +361,10 @@ void Blinker::on(){
if(pin<0)
return;
pauseTime=millis();
isPaused=false;
gpio_set_direction((gpio_num_t)pin, GPIO_MODE_INPUT_OUTPUT);
stop();
digitalWrite(pin,1);
}
@ -368,6 +376,25 @@ void Blinker::off(){
if(pin<0)
return;
pauseTime=millis();
isPaused=false;
gpio_set_direction((gpio_num_t)pin, GPIO_MODE_INPUT_OUTPUT);
stop();
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);
}

View File

@ -166,12 +166,16 @@ class Blinker {
int delayTime;
int count;
unsigned long pauseDuration;
unsigned long pauseTime;
boolean isPaused=false;
static void isrTimer(void *arg);
public:
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
// 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
// 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.
// 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
// 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
// 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.
//
// 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
// For ESP32-C3: 0=Group0/Timer0, 1=Group1/Timer0
// 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
// 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);
@ -225,4 +239,9 @@ class Blinker {
// 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)
};