Added the EventHandler class that monitors button presses
This commit is contained in:
parent
281e07c30a
commit
e66fd11e68
58
src/app/EventHandler.cpp
Normal file
58
src/app/EventHandler.cpp
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
#include "EventHandler.h"
|
||||||
|
|
||||||
|
EventHandler::EventHandler(uint8_t queueSize) : _maxQueueSize(queueSize)
|
||||||
|
{
|
||||||
|
//We set the various inputs needed
|
||||||
|
pinMode(GPIO_0, INPUT);
|
||||||
|
}
|
||||||
|
|
||||||
|
void EventHandler::run()
|
||||||
|
{
|
||||||
|
//FLASH_BUTTON PART
|
||||||
|
if(!digitalRead(GPIO_0) && !_flashBtnPushed && millis() - _flashBtnDebounceTs > 200)
|
||||||
|
{
|
||||||
|
_flashBtnPushed = true;
|
||||||
|
_flashBtnTs = millis();
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!digitalRead(GPIO_0) && _flashBtnPushed && millis() - _flashBtnTs > 500 && !_flashBtnLongPressFired)
|
||||||
|
{
|
||||||
|
_flashBtnLongPressFired = true;
|
||||||
|
addEventCheckSizeLimit(FLASH_BUTTON_LONG_PRESS);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(digitalRead(GPIO_0) && _flashBtnPushed && millis() - _flashBtnTs < 500)
|
||||||
|
{
|
||||||
|
_flashBtnPushed = false;
|
||||||
|
addEventCheckSizeLimit(FLASH_BUTTON_PRESS);
|
||||||
|
_flashBtnDebounceTs = millis();
|
||||||
|
}
|
||||||
|
else if(digitalRead(GPIO_0) && _flashBtnPushed && _flashBtnLongPressFired)
|
||||||
|
{
|
||||||
|
_flashBtnPushed = false;
|
||||||
|
_flashBtnLongPressFired = false;
|
||||||
|
_flashBtnDebounceTs = millis();
|
||||||
|
}
|
||||||
|
//FLASH_BUTTON END PART
|
||||||
|
}
|
||||||
|
|
||||||
|
EventHandler::Event EventHandler::getEvent()
|
||||||
|
{
|
||||||
|
if(_eventQueue.count() > 0)
|
||||||
|
return _eventQueue.remove();
|
||||||
|
|
||||||
|
return NO_EVENT;
|
||||||
|
}
|
||||||
|
|
||||||
|
void EventHandler::addEventCheckSizeLimit(Event e)
|
||||||
|
{
|
||||||
|
if(_eventQueue.count() < _maxQueueSize)
|
||||||
|
{
|
||||||
|
_eventQueue.add(e);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
delete _eventQueue.removeRef();
|
||||||
|
_eventQueue.add(e);
|
||||||
|
}
|
||||||
|
}
|
28
src/app/EventHandler.h
Normal file
28
src/app/EventHandler.h
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
/**
|
||||||
|
* Anatole SCHRAMM-HENRY
|
||||||
|
* 27/04/2020 à 19:45
|
||||||
|
* Tested, working
|
||||||
|
*/
|
||||||
|
#ifndef EVENTHANDLER_H
|
||||||
|
#define EVENTHANDLER_H
|
||||||
|
#include "definition.h"
|
||||||
|
#include "Queue.h"
|
||||||
|
|
||||||
|
class EventHandler
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
enum Event {NO_EVENT = -1,FLASH_BUTTON_PRESS, FLASH_BUTTON_LONG_PRESS};
|
||||||
|
EventHandler(uint8_t queueSize = 10);
|
||||||
|
void run();
|
||||||
|
Event getEvent();
|
||||||
|
protected:
|
||||||
|
private:
|
||||||
|
void addEventCheckSizeLimit(Event e);
|
||||||
|
|
||||||
|
Queue<Event> _eventQueue;
|
||||||
|
uint8_t _maxQueueSize;
|
||||||
|
unsigned long _flashBtnTs = 0, _flashBtnDebounceTs = 0;
|
||||||
|
boolean _flashBtnPushed = false, _flashBtnLongPressFired = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif //EVENTHANDLER_H
|
Loading…
Reference in New Issue
Block a user