annotate service/busyboxArduino/DFR_Key.cpp @ 432:f134b64a0ab7

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