-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeys.cpp
68 lines (68 loc) · 1.22 KB
/
keys.cpp
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
#include <Arduino.h>
/*********************************************************
Keyboard lib*/
#define APIN 0
#define VK_UP 38
#define VK_DOWN 40
#define VK_LEFT 37
#define VK_RIGHT 39
#define VK_RETURN 13
int previousKey = 0;
int previousPreviousKey = 0;
int countKey = 1;
int durationKey = 1;
int readKeysInt() {
int val = analogRead(APIN);
val = val >> 4;
switch (val)
{
case 0:
return VK_RIGHT;
case 6:
return VK_UP;
case 15:
return VK_DOWN;
case 25:
return VK_LEFT;
case 39:
return VK_RETURN;
case 63:
default:
return 0;
}
}
int readKeys(int* code, int*duration) {
int ret = 0;
int currentKey = readKeysInt();
if (previousKey == currentKey && currentKey != 0)
{
countKey--;
if (countKey == 0)
{
ret = currentKey;
if (previousPreviousKey != currentKey)
{
previousPreviousKey = currentKey;
countKey = 4;
}
else
{
countKey = 4;
durationKey++;
}
}
}
if (currentKey == 0)
{
previousPreviousKey = 0;
durationKey = 1;
}
previousKey = currentKey;
if (code != NULL)
*code = ret;
if (duration != NULL)
{
*duration = durationKey;
}
return ret;
}