-
Notifications
You must be signed in to change notification settings - Fork 0
/
DFR0009.cpp
86 lines (77 loc) · 2.58 KB
/
DFR0009.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* @file DFR0009.h
* implementation file of DFR0009 class
* Copyright (C) Ian Jin
* https://github.com/iancanada
* Twitter: @iancanadaTT
*
* 2015-08-26 First release Ver1.00
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "DFR0009.h"
DFR0009::DFR0009(int analogPin)
{
pin=analogPin;
currentKey=btnNONE;
changedKey=btnNONE;
flag=btnCLR;
state=WAITCHANGE;
}
void DFR0009::scan()
{
switch(state)
{
case WAITCHANGE:
changedKey=read_LCD_buttons();
if(changedKey!=currentKey)
state=CHANGED;
break;
case CHANGED:
if(read_LCD_buttons()==changedKey) //key change confimed
{
currentKey=changedKey; //update currentKEY
flag=changedKey; //set flag in this case
}
state=WAITCHANGE; //no matter what, next state will be WAITCHANGE
break;
default: //in any case, back to WAITCHANGE state as default
state=WAITCHANGE;
break;
}
}
button_t DFR0009::getflag()
{
button_t flagToReturn;
flagToReturn=flag;
flag=btnCLR; //clear flag automatically when get
return(flagToReturn);
}
//this function was revised from DFR0009 example code
button_t DFR0009::read_LCD_buttons()
{
int adc_key_in;
adc_key_in = analogRead(pin); // read the value from the sensor
// my buttons when read are centered at these valies: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
// For V1.1 us this threshold
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 250) return btnUP;
if (adc_key_in < 450) return btnDOWN;
if (adc_key_in < 650) return btnLEFT;
if (adc_key_in < 850) return btnSELECT;
return btnNONE; // when all others fail, return this...
}