Created
September 29, 2021 09:35
-
-
Save hollyhockberry/69f421ddd3386d05a31a5039254974a3 to your computer and use it in GitHub Desktop.
M5Paper: Judge touch gestures.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <M5EPD.h> | |
int lastFingers; | |
int eventIndex; | |
u_long events[4]; | |
void pushTouchEvent(bool down) { | |
if (!down && (eventIndex <= 0)) { | |
return; | |
} | |
if (down && (eventIndex >= 4)) { | |
eventIndex = 0; | |
} | |
events[eventIndex++] = ::millis(); | |
} | |
void judgeMode() { | |
if (eventIndex <= 0) { | |
return; | |
} | |
const u_long now = ::millis(); | |
const u_long last = events[eventIndex - 1]; | |
if ((now - last) < 150) { | |
return; | |
} | |
const char* name[] = { | |
"SWIPE", "TAP", "DRUG", "DOUBLETAP", | |
}; | |
Serial.printf("gesture: %s\r\n", name[eventIndex - 1]); | |
eventIndex = 0; | |
} | |
void setup() { | |
M5.begin(); | |
eventIndex = 0; | |
lastFingers = 0; | |
} | |
void loop() { | |
if (!M5.TP.avaliable()) { | |
judgeMode(); | |
return; | |
} | |
M5.TP.update(); | |
if (!M5.TP.isFingerUp()) { | |
if (lastFingers != M5.TP.getFingerNum()) { | |
if (lastFingers == 0) { | |
pushTouchEvent(true); | |
} | |
lastFingers = M5.TP.getFingerNum(); | |
Serial.printf("Fingers: %d\r\n", lastFingers); | |
} else { | |
judgeMode(); | |
} | |
} else { | |
if (lastFingers != 0) { | |
pushTouchEvent(false); | |
lastFingers = 0; | |
Serial.printf("Fingers: %d\r\n", lastFingers); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment