comparison service/busyboxArduino/DFR_Key.cpp @ 157:28c2db876548

busybox py and arduino Ignore-this: eea165c21e11600b5ac6787cbbb2239
author drewp@bigasterisk.com
date Mon, 19 Jan 2015 18:02:47 -0800
parents
children
comparison
equal deleted inserted replaced
156:08b398a0626b 157:28c2db876548
1 // rewritten from distro version by drewp 2015-01-04
2
3 #include "Arduino.h"
4 #include "DFR_Key.h"
5
6 static int DEFAULT_KEY_PIN = 0;
7 static int DEFAULT_THRESHOLD = 5;
8
9 // Updated for my board
10 static int UPKEY_ARV = 103; //that's read "analogue read value"
11 static int DOWNKEY_ARV = 266;
12 static int LEFTKEY_ARV = 426;
13 static int RIGHTKEY_ARV = 0;
14 static int SELKEY_ARV = 668;
15 static int NOKEY_ARV = 1023;
16
17 DFR_Key::DFR_Key()
18 {
19 _refreshRate = 10;
20 _keyPin = DEFAULT_KEY_PIN;
21 _threshold = DEFAULT_THRESHOLD;
22 _keyIn = NO_KEY;
23 _curInput = NO_KEY;
24 _curKey = NO_KEY;
25 _prevInput = NO_KEY;
26 _prevKey = NO_KEY;
27 _oldTime = 0;
28 }
29
30 int DFR_Key::getKey()
31 {
32 if (millis() < _oldTime + _refreshRate) {
33 return SAMPLE_WAIT;
34 }
35
36 _prevInput = _curInput;
37 _curInput = analogRead(_keyPin);
38 _oldTime = millis();
39
40 if (_curInput != _prevInput) {
41 // We could be in the middle of a key change
42 return SAMPLE_WAIT;
43 }
44
45 _prevKey = _curKey;
46
47 int curLo = _curInput - _threshold;
48 int curHi = _curInput + _threshold;
49 if ( curHi > UPKEY_ARV && curLo < UPKEY_ARV) _curKey = UP_KEY;
50 else if ( curHi > DOWNKEY_ARV && curLo < DOWNKEY_ARV) _curKey = DOWN_KEY;
51 else if ( curHi > RIGHTKEY_ARV && curLo < RIGHTKEY_ARV) _curKey = RIGHT_KEY;
52 else if ( curHi > LEFTKEY_ARV && curLo < LEFTKEY_ARV) _curKey = LEFT_KEY;
53 else if ( curHi > SELKEY_ARV && curLo < SELKEY_ARV) _curKey = SELECT_KEY;
54 else _curKey = NO_KEY;
55
56 return _curKey;
57 }
58
59 void DFR_Key::setRate(int rate)
60 {
61 _refreshRate = rate;
62 }