/**
******************************************************************************
* @file : 02_DbncdDlydMPBttn_1b.ino
* @brief : Example for the ButtonToSwitch_ESP32 library DbncdDlydMPBttn class
*
* Repository: https://github.com/GabyGold67/ButtonToSwitch_ESP32
* WOKWI simulation URL: https://wokwi.com/projects/437652665756125185
*
* Framework: Arduino
* Platform: ESP32
*
* @details The example instantiates a DbncdDlydMPBttn object using:
* - 1 push button between GND and ddmpbSwitchPin
* - 1 led with it's corresponding resistor between GND and ddmpbLoadPin
* - 1 led with it's corresponding resistor between GND and dmpbIsDisabledPin
*
* - This example doesn't create extra Tasks:
* - This example creates a software timer
*
* This simple example instantiates the DbncdDlydMPBttn object in the setup(),
* and uses the default `loop ()` (loop() is the loopTask() disguised
* in the Ardu-ESP), in it and checks it's attribute flags locally through the
* getters methods.
*
* When a change in the object's outputs attribute flags values is detected, it
* manages the loads and resources that the switch turns On and Off, in this
* example case are the output of some GPIO pins.
*
* A software timer is created so that it periodically toggles the isEnabled attribute flag
* value, showing the behavior of the instantiated object when enabled and when disabled.
*
* @author : Gabriel D. Goldman
* mail <[email protected]>
* Github <https://github.com/GabyGold67>
*
* @date : 01/08/2023 First release
* 15/09/2024 Last update
*
******************************************************************************
* @warning **Use of this library is under your own responsibility**
*
* @warning The use of this library falls in the category described by The Alan
* Parsons Project (c) 1980 "Games People play" disclaimer:
* Games people play, you take it or you leave it
* Things that they say aren't alright
* If I promised you the moon and the stars, would you believe it?
*
* Released into the public domain in accordance with "GPL-3.0-or-later" license terms.
******************************************************************************
*/
#include <Arduino.h>
#include <ButtonToSwitch_ESP32.h>
//===============================>> User Functions Prototypes BEGIN
void swpEnableCb(TimerHandle_t pvParam);
void Error_Handler();
//===============================>> User Functions Prototypes END
const uint8_t dmpbSwitchPin{GPIO_NUM_25};
const uint8_t dmpbLoadPin{GPIO_NUM_21};
const uint8_t dmpbIsDisabledPin{GPIO_NUM_18};
TimerHandle_t enableSwpTmrHndl{NULL};
BaseType_t tmrModRslt;
DbncdDlydMPBttn dmpbBttn (dmpbSwitchPin, true, true, 50, 350);
DbncdMPBttn* dmpbBttnPtr {&dmpbBttn};
void setup() {
delay(10); //FTPO Part of the WOKWI simulator additions, for simulation startup needs
pinMode(dmpbLoadPin, OUTPUT);
pinMode(dmpbIsDisabledPin, OUTPUT);
enableSwpTmrHndl = xTimerCreate(
"isEnabledSwapTimer",
10000,
pdTRUE,
dmpbBttnPtr,
swpEnableCb
);
dmpbBttn.setIsOnDisabled(true);
dmpbBttn.begin();
if (enableSwpTmrHndl != NULL){
tmrModRslt = xTimerStart(enableSwpTmrHndl, portMAX_DELAY);
}
if(tmrModRslt == pdFAIL){
Error_Handler();
}
}
void loop() {
if(dmpbBttn.getOutputsChange()){
digitalWrite(dmpbLoadPin, (dmpbBttn.getIsOn())?HIGH:LOW);
digitalWrite(dmpbIsDisabledPin, (dmpbBttn.getIsEnabled())?LOW:HIGH);
dmpbBttn.setOutputsChange(false);
}
}
//===============================>> User Timers Implementations BEGIN
/**
* @brief Timer callback function
*
* @param pvParam The callback function argument.
* In this case is a pointer to the MPB object to be enabled and disabled periodically.
* It's a DbncdMPBttn* as enable() and disable() are dynamic polymorphic so they can be invoked through a base class pointer call.
*/
void swpEnableCb(TimerHandle_t pvParam){
DbncdMPBttn* dbncdMPBLocPtr = (DbncdMPBttn*) pvTimerGetTimerID(pvParam);
// bool mpbttnIsEnbldStts{dbncdMPBLocPtr->getIsEnabled()};
if (dbncdMPBLocPtr->getIsEnabled())
dbncdMPBLocPtr->disable();
else
dbncdMPBLocPtr->enable();
return;
}
//===============================>> User Timers Implementations END
//===============================>> User Functions Implementations BEGIN
/**
* @brief Error Handling function
*
*/
void Error_Handler(){
for(;;)
{
}
return;
}
//===============================>> User Functions Implementations END
On
Disabled
(Key 1)