annotate service/busyboxArduino/DFR_Key.cpp @ 984:5da9200418db

reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands Ignore-this: 7646fba7b5496eb1f33a87452af6c6de darcs-hash:20150514083000-312f9-ef4db22cf33250e690b6021b6b1968cc788d14cb
author drewp <drewp@bigasterisk.com>
date Thu, 14 May 2015 01:30:00 -0700
parents 70fd4c07a326
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
962
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
1 // rewritten from distro version by drewp 2015-01-04
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
2
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
3 #include "Arduino.h"
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
4 #include "DFR_Key.h"
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
5
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
6 static int DEFAULT_KEY_PIN = 0;
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
7 static int DEFAULT_THRESHOLD = 5;
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
8
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
9 // Updated for my board
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
10 static int UPKEY_ARV = 103; //that's read "analogue read value"
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
11 static int DOWNKEY_ARV = 266;
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
12 static int LEFTKEY_ARV = 426;
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
13 static int RIGHTKEY_ARV = 0;
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
14 static int SELKEY_ARV = 668;
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
15 static int NOKEY_ARV = 1023;
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
16
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
17 DFR_Key::DFR_Key()
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
18 {
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
19 _refreshRate = 10;
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
20 _keyPin = DEFAULT_KEY_PIN;
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
21 _threshold = DEFAULT_THRESHOLD;
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
22 _keyIn = NO_KEY;
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
23 _curInput = NO_KEY;
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
24 _curKey = NO_KEY;
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
25 _prevInput = NO_KEY;
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
26 _prevKey = NO_KEY;
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
27 _oldTime = 0;
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
28 }
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
29
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
30 int DFR_Key::getKey()
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
31 {
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
32 if (millis() < _oldTime + _refreshRate) {
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
33 return SAMPLE_WAIT;
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
34 }
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
35
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
36 _prevInput = _curInput;
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
37 _curInput = analogRead(_keyPin);
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
38 _oldTime = millis();
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
39
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
40 if (_curInput != _prevInput) {
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
41 // We could be in the middle of a key change
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
42 return SAMPLE_WAIT;
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
43 }
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
44
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
45 _prevKey = _curKey;
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
46
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
47 int curLo = _curInput - _threshold;
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
48 int curHi = _curInput + _threshold;
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
49 if ( curHi > UPKEY_ARV && curLo < UPKEY_ARV) _curKey = UP_KEY;
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
50 else if ( curHi > DOWNKEY_ARV && curLo < DOWNKEY_ARV) _curKey = DOWN_KEY;
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
51 else if ( curHi > RIGHTKEY_ARV && curLo < RIGHTKEY_ARV) _curKey = RIGHT_KEY;
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
52 else if ( curHi > LEFTKEY_ARV && curLo < LEFTKEY_ARV) _curKey = LEFT_KEY;
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
53 else if ( curHi > SELKEY_ARV && curLo < SELKEY_ARV) _curKey = SELECT_KEY;
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
54 else _curKey = NO_KEY;
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
55
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
56 return _curKey;
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
57 }
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
58
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
59 void DFR_Key::setRate(int rate)
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
60 {
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
61 _refreshRate = rate;
70fd4c07a326 busybox py and arduino
drewp <drewp@bigasterisk.com>
parents:
diff changeset
62 }