-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnevma_board.ino
More file actions
78 lines (71 loc) · 1.85 KB
/
Copy pathnevma_board.ino
File metadata and controls
78 lines (71 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <Keyboard.h>
#include <Mouse.h>
#include <SparkFun_APDS9960.h>
SparkFun_APDS9960 gestureSensor;
const unsigned short GESTURE_INT_PIN = 1;
volatile boolean hasMovement = false;
/**
The main business logic. Translates gestures to keyboard events.
*/
void handleGesture() {
if (gestureSensor.isGestureAvailable()) {
switch (gestureSensor.readGesture()) {
case DIR_UP:
Keyboard.press(KEY_LEFT_ARROW);
break;
case DIR_DOWN:
Keyboard.press(KEY_RIGHT_ARROW);
break;
case DIR_LEFT:
Mouse.move(0, 0, -10);
break;
case DIR_RIGHT:
Mouse.move(0, 0, 10);
break;
case DIR_NEAR:
Keyboard.press(' ');
break;
case DIR_FAR:
Keyboard.press(KEY_ESC);
break;
default:
break;
}
Keyboard.releaseAll();
}
}
/**
Gets called whenever the gesture module signals there are new data to be sent
*/
void transmissionReady() {
hasMovement = true;
}
/**
Initializes gesture sensor
@return whether gesture sensor initialization was successful
*/
boolean initializeGestureSensor() {
return gestureSensor.init() && gestureSensor.enableGestureSensor(true);
}
void setup() {
Keyboard.begin();
Mouse.begin();
attachInterrupt(digitalPinToInterrupt(GESTURE_INT_PIN), transmissionReady, FALLING);
boolean initializationSuccessful = initializeGestureSensor();
// If ADPS sensor failed to initialize, block and send error messages via Serial
if (!initializationSuccessful) {
Serial.begin(9600);
while (!initializationSuccessful) {
Serial.println("Error while initializing the gesture sensor");
delay(10000);
}
}
}
void loop() {
if (hasMovement) {
handleGesture();
hasMovement = false;
// Reinitialize as workaround for sensor hanging when gesturing too fast
initializeGestureSensor();
}
}