-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathAtTouch.cpp
executable file
·294 lines (239 loc) · 7.18 KB
/
AtTouch.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/************************************************************************************
*
* Name : AtTouch
* Author : Noah Shibley, NoMi Design nomidesign.net
* Date : July 18th 2011
* Version : 0.1
* Notes : Arduino Library for use with the AT42QT1070 Q-touch chip via i2c
*
* Copyright (c) 2011 NoMi Design All right reserved.
*
* This file is part of AtTouch.
*
* AtTouch is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AtTouch 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AtTouch. If not, see <http://www.gnu.org/licenses/>.
*
***********************************************************************************/
#include "AtTouch.h"
AtTouch* AtTouch::pAtTouch = 0;
/***********************************************************
*
* AtTouch
*
***********************************************************/
AtTouch::AtTouch()
{
pAtTouch = this; //the ptr points to this object
}
/***********************************************************
*
* begin
*
***********************************************************/
void AtTouch::begin(int interruptPin,boolean disableAutoCal){
_changePin = (byte) interruptPin;
_interruptVal = (byte) interruptPin-2; //arduino interupt values 0=pin2, 1=pin3
keyHit = false;
holdDown_ = false;
startTime = 0;
holdRefreshInterval = 1; //1 for 100ms 1000ms/100 = 10 so 100ms/100 = 1
#ifdef WIRE //use the Wire lib
Wire.begin();
// reset device by writing non-zero value to register 0x39
Wire.beginTransmission(0x1B); // transmit to device
Wire.write(0x39); // sets register pointer to the reset register (0x39)
Wire.write(0xFF); // send non-zero value to initiate a reset
Wire.endTransmission(); // stop transmitting
delay(100); // wait for device to restart
Wire.begin(); // re-open the i2c after device has restarted
#else //or use the I2C lib
I2c.begin();
I2c.pullup(0);
// reset device by writing non-zero value to register 0x39
I2c.write(0x1B, 0x39, 0xFF);
delay(100); // wait for device to restart
I2c.begin(); // re-open the i2c after device has restarted
I2c.pullup(0);
#endif
if(disableAutoCal == true)
{
#ifdef WIRE
delay(100);
Wire.beginTransmission(0x1B); //
Wire.write(0x37); //set to register 0x37, disable hold down auto calibration
Wire.write(0);
Wire.endTransmission();
#else
delay(100);
I2c.write(0x1B, 0x37, 0);
#endif
}
pinMode(_changePin, INPUT);
attachInterrupt(_interruptVal,bttnPressISR,FALLING); //setup the key change interrupt, call bttnpress on interrupt
}
/***********************************************************
*
* hit
*
***********************************************************/
boolean AtTouch::hit()
{
return keyHit;
}
/***********************************************************
*
* getStartTime
*
***********************************************************/
unsigned long AtTouch::getStartTime()
{
return startTime;
}
/***********************************************************
*
* setRefreshSpeed
*
***********************************************************/
void AtTouch::setRefreshSpeed(int intervalMilSec)
{
holdRefreshInterval = intervalMilSec/100;
}
/***********************************************************
*
* update
*
***********************************************************/
void AtTouch::update()
{
if (keyHit == true)
{
activeKey_ = readActiveKey();
}
}
/***********************************************************
*
* getKey
*
***********************************************************/
int AtTouch::getKey()
{
return (int) activeKey_;
}
/***********************************************************
*
* readActiveKey
*
***********************************************************/
int AtTouch::readActiveKey()
{
int keyAddress = readActiveAddress();
//Serial.println(keyAddress);
int keyValue = 0; //start at one, for key0
for(int i = keyAddress ;i>0;i = i>>1) //shift until you hit 1, thats which bit place
{
keyValue++; //increment keyValue to find bit place
}
activeKey_ = (byte) keyValue; //save the keyValue for later
if(keyValue == 9) { holdDown_ = false; } //9 is key up, so stop press and hold timer
return keyValue;
}
/***********************************************************
*
* readActiveAddress
*
***********************************************************/
int AtTouch::readActiveAddress(){
if(keyHit == false) //not active return right away
{
return -1;
}
else
{
int readstatus = 0;
int bttnNum = 0;
#ifdef WIRE
// to clear change int we must read both status bytes 02 and 03
Wire.beginTransmission(0x1B); // transmit to device
Wire.write(0x02); // want to read detection status // set pointer
Wire.endTransmission(); // stop transmitting
Wire.requestFrom(0x1B, 1); // request 1 byte from slave device
readstatus = Wire.read();
Wire.beginTransmission(0x1B); // transmit to device
Wire.write(0x03); // want to read key status // set pointer
Wire.endTransmission(); // stop transmitting
Wire.requestFrom(0x1B, 1); // request 1 byte from slave device
bttnNum = Wire.read();
#else
// to clear change int we must read both status bytes 02 and 03
I2c.read(0x1B, 0x02, 1);
readstatus = I2c.receive();
I2c.read(0x1B, 0x03, 1);
bttnNum = I2c.receive();
#endif
keyHit = false;
if (readstatus == 1) // seems to trigger int twice for each press... this filters the second one...
{
if(bttnNum == 0)
{
#ifdef SERIAL_DEBUG
Serial.println("read error");
#endif
}
return bttnNum;
}
}
}
/***********************************************************
*
* hold
*
***********************************************************/
boolean AtTouch::hold()
{
if(holdDown_ == true)
{
unsigned long elapsedTime = millis() - startTime;
if (elapsedTime >= 1100) //hold interval time has passed
{
unsigned int holdRefreshTime = millis()/100 - updateTime;
if(holdRefreshTime >= holdRefreshInterval) //restrict update 100ms refresh
{
updateTime = millis()/100; //divide by 100, lose some precision, 100ms minimum
return true;
}
else //don't update too fast
{
return false;
}
}
else //haven't held down long enough
{
return false;
}
}
else //not holding down at all
{
return false;
}
}
/***********************************************************
*
* bttnPressISR
*
***********************************************************/
void bttnPressISR()
{
AtTouch::pAtTouch->keyHit = true;
AtTouch::pAtTouch->holdDown_ = true;
AtTouch::pAtTouch->startTime = millis(); //start hold time,TODO figure out how to do this without reading a function
}