-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathHover.cpp
More file actions
353 lines (302 loc) · 6.78 KB
/
Hover.cpp
File metadata and controls
353 lines (302 loc) · 6.78 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/* ===========================================================================
# This is the library for Hover.
#
# Hover is a development kit that lets you control your hardware projects in a whole new way.
# Wave goodbye to physical buttons. Hover detects hand movements in the air for touch-less interaction.
# It also features five touch-sensitive regions for even more options.
# Hover uses I2C and 2 digital pins. It is compatible with Arduino, Raspberry Pi and more.
#
# Hover can be purchased here: http://www.hoverlabs.co
#
# Written by Emran Mahbub and Jonathan Li for Hover Labs.
# BSD license, all text above must be included in any redistribution
# ===========================================================================
#
# INSTALLATION
# The 3 library files (Hover.cpp, Hover.h and keywords.txt) in the Hover folder should be placed in your Arduino Library folder.
# Run the HoverDemo.ino file from your Arduino IDE.
#
# SUPPORT
# For questions and comments, email us at support@hoverlabs.co
# v3.1
# ===========================================================================*/
#include "application.h"
#include "Hover.h"
Hover::Hover(uint8_t ts, uint8_t rst, uint8_t gestmode, uint8_t touchmode, uint8_t tapmode, uint8_t posmode) {
_i2caddr = 0x42;
_ts = ts;
_rst = rst;
_gestmode = gestmode;
_touchmode = touchmode;
_tapmode = tapmode;
_posmode = posmode;
}
void Hover::begin() {
Wire.begin();
pinMode(_ts, INPUT);
pinMode(_rst, OUTPUT);
digitalWrite(_rst, LOW);
delay(50);
pinMode(_rst, INPUT);
/*disable autocal for smoother operation*/
uint8_t autoCal[] = {0x10, 0x00, 0x00, 0xA2, 0x80, 0x00 , 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF};
Wire.beginTransmission(_i2caddr);
Wire.write(autoCal, 16);
Wire.endTransmission();
Serial.println("Hover is ready");
}
void Hover::setRelease() {
digitalWrite(_ts, HIGH);
pinMode(_ts, INPUT);
}
boolean Hover::getStatus() {
if (digitalRead(_ts) == 0) {
pinMode(_ts, OUTPUT);
digitalWrite(_ts, LOW);
return 0;
}
return 1;
}
void Hover::readI2CData(void) {
if (getStatus() == 0){
int c = 0;
Wire.requestFrom((uint8_t)_i2caddr, (uint8_t)26); // request 26 bytes from slave device at 0x42
while(Wire.available()) // slave may send less than requested
{
_dat[c] = Wire.read(); // receive a byte as character
c++;
}
setRelease();
_valid = 1;
delay(6);
} else {
_valid = 0;
}
}
Gesture::Gesture(){
strcpy(gestureType, "none");
gestureID = 0;
gestureValue = 0;
}
Gesture::Gesture (char * type, uint8_t id, uint8_t value){
strcpy(gestureType, type);
gestureID = id;
gestureValue = value;
}
Gesture Hover::getGesture(void) {
char gtype[15];
uint8_t gid;
uint8_t gval;
if (_valid == 1 && _gestmode == 0x01) {
readGestureData(gtype, &gid, &gval);
} else {
strcpy(gtype, "none");
gid = 0x00;
gval = 0x00;
}
return Gesture(gtype, gid, gval);
}
void Hover::readGestureData(char * gtype, uint8_t * gid, uint8_t * gval) {
switch (_dat[10]){
case 2:
strcpy(gtype, "Right Swipe");
*gid = 0x01;
*gval = 0x01;
break;
case 3:
strcpy(gtype, "Left Swipe");
*gid = 0x01;
*gval = 0x02;
break;
case 4:
strcpy(gtype, "Up Swipe");
*gid = 0x01;
*gval = 0x03;
break;
case 5:
strcpy(gtype, "Down Swipe");
*gid = 0x01;
*gval = 0x04;
break;
default:
strcpy(gtype, "None");
*gid = 0x00;
*gval = 0x00;
break;
}
if (_dat[7] & 0x02){
strcpy(gtype, "Airspin");
*gid = 0x02;
*gval = _dat[18];
}
}
Touch::Touch(){
strcpy(touchType, "none");
touchID = 0;
touchValue = 0;
}
Touch::Touch (char * type, uint8_t id, uint8_t value){
strcpy(touchType, type);
touchID = id;
touchValue = value;
}
Touch Hover::getTouch(void) {
char ttype[20];
uint8_t tid;
uint8_t tval;
if (_valid == 1) {
readTouchData(ttype, &tid, &tval);
} else {
strcpy(ttype, "None");
tid = 0x00;
tval = 0x00;
}
return Touch(ttype, tid, tval);
}
void Hover::readTouchData(char * ttype, uint8_t * tid, uint8_t * tval) {
uint8_t touch, tap, doubleTap;
touch = (_touchmode==0x01) ? (touch = (_dat[14] & 0x1F)) : (0);
if (_tapmode!=0x00){
if (_tapmode == 0x01){
tap = (((_dat[15] & 0x03) << 3 ) | (_dat[14]>>5));
doubleTap = ((_dat[15]>>2) & 0x1F);
} else {
tap = (_tapmode==0x02) ? (tap = (((_dat[15] & 0x03) << 3 ) | (_dat[14]>>5))) : (0);
doubleTap = (_tapmode==0x03) ? (doubleTap = ((_dat[15]>>2) & 0x1F)) : (0);
}
} else {
tap = 0;
doubleTap = 0;
}
if (doubleTap != 0) {
switch (doubleTap) {
case 1:
strcpy(ttype, "Double Tap South");
*tid = 0x03;
*tval = 0x04;
break;
case 2:
strcpy(ttype, "Double Tap West");
*tid = 0x03;
*tval = 0x02;
break;
case 4:
strcpy(ttype, "Double Tap North");
*tid = 0x03;
*tval = 0x03;
break;
case 8:
strcpy(ttype, "Double Tap East");
*tid = 0x03;
*tval = 0x01;
break;
case 16:
strcpy(ttype, "Double Tap Center");
*tid = 0x03;
*tval = 0x05;
break;
default:
strcpy(ttype, "None");
*tid = 0x00;
*tval = 0x00;
break;
}
} else if (tap != 0) {
switch (tap){
case 1:
strcpy(ttype, "Tap South");
*tid = 0x02;
*tval = 0x04;
break;
case 2:
strcpy(ttype, "Tap West");
*tid = 0x02;
*tval = 0x02;
break;
case 4:
strcpy(ttype, "Tap North");
*tid = 0x02;
*tval = 0x03;
break;
case 8:
strcpy(ttype, "Tap East");
*tid = 0x02;
*tval = 0x01;
break;
case 16:
strcpy(ttype, "Tap Center");
*tid = 0x02;
*tval = 0x05;
break;
default:
strcpy(ttype, "None");
*tid = 0x00;
*tval = 0x00;
break;
}
} else if (touch!=0) {
switch (touch){
case 1:
strcpy(ttype, "Touch South");
*tid = 0x01;
*tval = 0x04;
break;
case 2:
strcpy(ttype, "Touch West");
*tid = 0x01;
*tval = 0x02;
break;
case 4:
strcpy(ttype, "Touch North");
*tid = 0x01;
*tval = 0x03;
break;
case 8:
strcpy(ttype, "Touch East");
*tid = 0x01;
*tval = 0x01;
break;
case 16:
strcpy(ttype, "Touch Center");
*tid = 0x01;
*tval = 0x05;
break;
default:
strcpy(ttype, "None");
*tid = 0x00;
*tval = 0x00;
break;
}
} else {
strcpy(ttype, "None");
*tid = 0x00;
*tval = 0x00;
}
}
Position::Position (uint16_t x0, uint16_t y0, uint16_t z0) {
x = x0;
y = y0;
z = z0;
}
Position Hover::getPosition(void) {
uint16_t x, y, z;
if (_valid == 1 && _posmode == 0x01) {
readPositionData(&x, &y, &z);
} else {
x = 0;
y = 0;
z = 0;
}
return Position(x, y, z);
}
void Hover::readPositionData(uint16_t *x, uint16_t *y, uint16_t *z) {
if ((_dat[7] & 0x01) == 1){
*x = ((_dat[21] << 8) | _dat[20]);
*y = ((_dat[23] << 8) | _dat[22]);
*z = ((_dat[25] << 8) | _dat[24]);
} else {
*x = 0;
*y = 0;
*z = 0;
}
}