Skip to content

Commit

Permalink
Add support for detecting multi zone touch events to EFTouch
Browse files Browse the repository at this point in the history
  • Loading branch information
ngandrass committed Sep 6, 2024
1 parent b7ad966 commit 75d3d30
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
55 changes: 55 additions & 0 deletions lib/EFTouch/EFTouch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,28 @@ void ARDUINO_ISR_ATTR EFTouchClass::_handleInterrupt(EFTouchZone zone, bool rais
// Fire onFingerprintShortpressIsr() if applicable
if (this->onFingerprintShortpressIsr != nullptr) {
if (this->last_touch_millis_fingerprint + EFTOUCH_SHORTPRESS_DURATION_MS < millis()) {
// Check if all touch zones were short pressed
if (this->onAllShortpressIsr != nullptr) {
if (this->isNoseTouched() && this->last_touch_millis_nose + EFTOUCH_SHORTPRESS_DURATION_MS < millis()) {
this->onAllShortpressIsr();
}
}

// Execute zone ISR
this->onFingerprintShortpressIsr();
}
}
// Fire onFingerperintLongpressIsr() if applicable
if (this->onFingerprintLongpressIsr != nullptr) {
if (this->last_touch_millis_fingerprint + EFTOUCH_LONGPRESS_DURATION_MS < millis()) {
// Check if all touch zones were long pressed
if (this->onAllLongpressIsr != nullptr) {
if (this->isNoseTouched() && this->last_touch_millis_nose + EFTOUCH_LONGPRESS_DURATION_MS < millis()) {
this->onAllLongpressIsr();
}
}

// Execute zone ISR
this->onFingerprintLongpressIsr();
}
}
Expand All @@ -237,12 +253,27 @@ void ARDUINO_ISR_ATTR EFTouchClass::_handleInterrupt(EFTouchZone zone, bool rais
// Fire onNoseShortpressIsr() if applicable
if (this->onNoseShortpressIsr != nullptr) {
if (this->last_touch_millis_nose + EFTOUCH_SHORTPRESS_DURATION_MS < millis()) {
// Check if all touch zones were short pressed
if (this->onAllShortpressIsr != nullptr) {
if (this->isFingerprintTouched() && this->last_touch_millis_fingerprint + EFTOUCH_SHORTPRESS_DURATION_MS < millis()) {
this->onAllShortpressIsr();
}
}
// Execute zone ISR
this->onNoseShortpressIsr();
}
}
// Fire onFingerperintLongpressIsr() if applicable
if (this->onNoseLongpressIsr != nullptr) {
if (this->last_touch_millis_nose + EFTOUCH_LONGPRESS_DURATION_MS < millis()) {
// Check if all touch zones were long pressed
if (this->onAllLongpressIsr != nullptr) {
if (this->isFingerprintTouched() && this->last_touch_millis_fingerprint + EFTOUCH_LONGPRESS_DURATION_MS < millis()) {
this->onAllLongpressIsr();
}
}

// Execute zone ISR
this->onNoseLongpressIsr();
}
}
Expand All @@ -257,6 +288,9 @@ void ARDUINO_ISR_ATTR EFTouchClass::_handleInterrupt(EFTouchZone zone, bool rais

void EFTouchClass::attachInterruptOnTouch(EFTouchZone zone, void ARDUINO_ISR_ATTR (*isr)(void)) {
switch (zone) {
case EFTouchZone::All:
LOG_ERROR("(EFTouch) Attaching onTouch interrupt to all zones is currently not supported");
break;
case EFTouchZone::Fingerprint:
if (isr) {
this->onFingerprintTouchIsr = isr;
Expand All @@ -283,6 +317,9 @@ void EFTouchClass::attachInterruptOnTouch(EFTouchZone zone, void ARDUINO_ISR_ATT

void EFTouchClass::attachInterruptOnRelease(EFTouchZone zone, void ARDUINO_ISR_ATTR (*isr)(void)) {
switch (zone) {
case EFTouchZone::All:
LOG_ERROR("(EFTouch) Attaching onRelease interrupt to all zones is currently not supported");
break;
case EFTouchZone::Fingerprint:
if (isr) {
this->onFingerprintReleaseIsr = isr;
Expand All @@ -309,6 +346,15 @@ void EFTouchClass::attachInterruptOnRelease(EFTouchZone zone, void ARDUINO_ISR_A

void EFTouchClass::attachInterruptOnShortpress(EFTouchZone zone, void ARDUINO_ISR_ATTR (*isr)(void)) {
switch (zone) {
case EFTouchZone::All:
if (isr) {
this->onAllShortpressIsr = isr;
LOG_INFO("(EFTouch) Attached onShortpress interrupt to the combined 'All' zone");
} else {
this->onAllShortpressIsr = nullptr;
LOG_INFO("(EFTouch) Detached onShortpress interrupt from the combined 'All' zone");
}
break;
case EFTouchZone::Fingerprint:
if (isr) {
this->onFingerprintShortpressIsr = isr;
Expand All @@ -335,6 +381,15 @@ void EFTouchClass::attachInterruptOnShortpress(EFTouchZone zone, void ARDUINO_IS

void EFTouchClass::attachInterruptOnLongpress(EFTouchZone zone, void ARDUINO_ISR_ATTR (*isr)(void)) {
switch (zone) {
case EFTouchZone::All:
if (isr) {
this->onAllLongpressIsr = isr;
LOG_INFO("(EFTouch) Attached onLongpress interrupt to the combined 'All' touch zone");
} else {
this->onAllLongpressIsr = nullptr;
LOG_INFO("(EFTouch) Detached onLongpress interrupt from the combined 'All' touch zone");
}
break;
case EFTouchZone::Fingerprint:
if (isr) {
this->onFingerprintLongpressIsr = isr;
Expand Down
7 changes: 5 additions & 2 deletions lib/EFTouch/EFTouch.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ class EFTouchClass {
touch_value_t noise_fingerprint; //!< Calibrated noise floor for fingerprint touch pad
touch_value_t noise_nose; //!< Calibrated noise floor for nose touch pad

unsigned long last_touch_millis_fingerprint; //!< Timestamp when the fingerprint was last touched
unsigned long last_touch_millis_nose; //!< Timestamp when the nose was last touched
volatile unsigned long last_touch_millis_fingerprint; //!< Timestamp when the fingerprint was last touched
volatile unsigned long last_touch_millis_nose; //!< Timestamp when the nose was last touched

void (*onFingerprintTouchIsr)(void); //!< ISR to execute if the fingerprint is first touched
void (*onFingerprintReleaseIsr)(void); //!< ISR to execute if the fingerprint is fully released
Expand All @@ -66,6 +66,9 @@ class EFTouchClass {
void (*onNoseShortpressIsr)(void); //!< ISR to execute if the nose was held for at least a short amount of time
void (*onNoseLongpressIsr)(void); //!< ISR to execute if the nose was held for a long amount of time

void (*onAllShortpressIsr)(void); //!< ISR to execute if all touch zones were held for at least a short amount of time
void (*onAllLongpressIsr)(void); //!< ISR to execute if all touch zones were held for a long amount of time

public:

/**
Expand Down
1 change: 1 addition & 0 deletions lib/EFTouch/EFTouchZone.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* @brief Available touch zones
*/
enum EFTouchZone {
All,
Fingerprint,
Nose
};
Expand Down
15 changes: 15 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ volatile struct ISREventsType {
unsigned char noseRelease: 1;
unsigned char noseShortpress: 1;
unsigned char noseLongpress: 1;
unsigned char allShortpress: 1;
unsigned char allLongpress: 1;
} isrEvents;

// Interrupt service routines to update ISR struct upon triggering
Expand All @@ -74,6 +76,8 @@ void ARDUINO_ISR_ATTR isr_noseTouch() { isrEvents.noseTouch = 1; }
void ARDUINO_ISR_ATTR isr_noseRelease() { isrEvents.noseRelease = 1; }
void ARDUINO_ISR_ATTR isr_noseShortpress() { isrEvents.noseShortpress = 1; }
void ARDUINO_ISR_ATTR isr_noseLongpress() { isrEvents.noseLongpress = 1; }
void ARDUINO_ISR_ATTR isr_allShortpress() { isrEvents.allShortpress = 1; }
void ARDUINO_ISR_ATTR isr_allLongpress() { isrEvents.allLongpress = 1; }

/**
* @brief Handles hard brown out events
Expand Down Expand Up @@ -230,6 +234,8 @@ void setup() {
EFTouch.attachInterruptOnRelease(EFTouchZone::Nose, isr_noseRelease);
EFTouch.attachInterruptOnShortpress(EFTouchZone::Nose, isr_noseShortpress);
EFTouch.attachInterruptOnLongpress(EFTouchZone::Nose, isr_noseLongpress);
EFTouch.attachInterruptOnShortpress(EFTouchZone::All, isr_allShortpress);
EFTouch.attachInterruptOnLongpress(EFTouchZone::All, isr_allLongpress);

// Get FSM going
fsm.resume();
Expand Down Expand Up @@ -279,6 +285,15 @@ void loop() {
fsm.queueEvent(FSMEvent::NoseRelease);
isrEvents.noseRelease = false;
}
if (isrEvents.allLongpress) {
LOG_INFO("ALL LONG");
isrEvents.allLongpress = false;
isrEvents.allShortpress = false;
}
if (isrEvents.allShortpress) {
LOG_INFO("ALL SHORT");
isrEvents.allShortpress = false;
}

// Task: Handle FSM
if (task_fsm_handle < millis()) {
Expand Down

0 comments on commit 75d3d30

Please sign in to comment.