Initial commit
This commit is contained in:
112
src/mdw/button/ButtonEventsHandler.cpp
Normal file
112
src/mdw/button/ButtonEventsHandler.cpp
Normal file
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* ButtonEventsHandler.cpp
|
||||
*
|
||||
* Created on: 19 nov. 2023
|
||||
* Author: remi.heredero
|
||||
*/
|
||||
|
||||
#include <button/ButtonEventsHandler.h>
|
||||
#include "trace/trace.h"
|
||||
#include "board/ButtonsController.h"
|
||||
|
||||
|
||||
ButtonEventsHandler::ButtonEventsHandler() {
|
||||
for(uint8_t i = 0; i< MAX_OBSERVER; i++) {
|
||||
observer_[i] = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
ButtonEventsHandler* ButtonEventsHandler::getInstance() {
|
||||
static ButtonEventsHandler buttonEventsHandler_;
|
||||
return &buttonEventsHandler_;
|
||||
}
|
||||
|
||||
XFEventStatus ButtonEventsHandler::processEvent() {
|
||||
eEventStatus eventStatus = XFEventStatus::Unknown;
|
||||
|
||||
const XFEvent* ev = getCurrentEvent();
|
||||
XFEvent::XFEventType evType = ev->getEventType();
|
||||
int evid = ev->getId();
|
||||
|
||||
oldState_ = currentState_;
|
||||
changeState_ = false;
|
||||
|
||||
switch (currentState_) {
|
||||
case state::initial:
|
||||
if(evType == XFEvent::Initial) {
|
||||
currentState_ = state::run;
|
||||
changeState_ = true;
|
||||
eventStatus = XFEventStatus::Consumed;
|
||||
}
|
||||
break;
|
||||
|
||||
case state::run:
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
if(changeState_) {
|
||||
switch (oldState_) { // onExit
|
||||
case state::initial:
|
||||
break;
|
||||
|
||||
case state::run:
|
||||
break;
|
||||
}
|
||||
|
||||
switch (currentState_) { // onEntry
|
||||
case state::initial:
|
||||
break;
|
||||
|
||||
case state::run:
|
||||
buttonStateSm_[0].startBehavior();
|
||||
buttonStateSm_[1].startBehavior();
|
||||
buttonStateSm_[2].startBehavior();
|
||||
buttonStateSm_[3].startBehavior();
|
||||
break;
|
||||
}
|
||||
}
|
||||
return eventStatus;
|
||||
}
|
||||
|
||||
void ButtonEventsHandler::onButtonChangeState(uint16_t buttonIndex, bool pressed) {
|
||||
if(pressed) {
|
||||
buttonStateSm_[buttonIndex].genButtonPressed();
|
||||
} else {
|
||||
buttonStateSm_[buttonIndex].genButtonReleased();
|
||||
}
|
||||
}
|
||||
|
||||
bool ButtonEventsHandler::subscribe(interface::ButtonEventsHandlerObserver *observer) {
|
||||
for(uint8_t i = 0; i < MAX_OBSERVER; i++) {
|
||||
if (observer_[i] == nullptr) {
|
||||
observer_[i] = observer;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void ButtonEventsHandler::unsubscribe(interface::ButtonEventsHandlerObserver *observer) {
|
||||
for(uint8_t i = 0; i < MAX_OBSERVER; i++) {
|
||||
if (observer_[i] == observer) {
|
||||
observer_[i] = nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ButtonEventsHandler::notifyButtonShortPressed(ButtonIndex buttonIndex) {
|
||||
for(uint8_t i = 0; i < MAX_OBSERVER; i++) {
|
||||
if (observer_[i] != nullptr) {
|
||||
observer_[i]->onButtonShortPressed(buttonIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ButtonEventsHandler::notifyButtonLongPressed(ButtonIndex buttonIndex) {
|
||||
for(uint8_t i = 0; i < MAX_OBSERVER; i++) {
|
||||
if (observer_[i] != nullptr) {
|
||||
observer_[i]->onButtonLongPressed(buttonIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user