-
Notifications
You must be signed in to change notification settings - Fork 7
/
Trill.cpp
431 lines (359 loc) · 9.72 KB
/
Trill.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*
* Trill library for Arduino
* (c) 2020 bela.io
*
* This library communicates with the Trill sensors
* using I2C.
*
* BSD license
*/
#include "Trill.h"
// some implementations of Wire (e.g.: Particle OS) do not define BUFFER_LENGTH
#ifndef BUFFER_LENGTH
#define BUFFER_LENGTH 32
#endif // BUFFER_LENGTH
#define MAX_TOUCH_1D_OR_2D (((device_type_ == TRILL_SQUARE || device_type_ == TRILL_HEX) ? kMaxTouchNum2D : kMaxTouchNum1D))
#define RAW_LENGTH ((device_type_ == TRILL_BAR ? 2 * kNumChannelsBar \
: device_type_ == TRILL_RING ? 2 * kNumChannelsRing \
: 2 * kNumChannelsMax))
Trill::Trill()
: device_type_(TRILL_NONE), firmware_version_(0),
mode_(AUTO), last_read_loc_(0xFF), raw_bytes_left_(0), wire_(&Wire)
{
}
/* Initialise the hardware. Returns the type of device attached, or 0
if none is attached. */
int Trill::begin(Device device, uint8_t i2c_address, TwoWire* wire) {
wire_ = wire;
if(128 <= i2c_address)
i2c_address = trillDefaults[device+1].address;
/* Unknown default address */
if(128 <= i2c_address) {
return -2;
}
i2c_address_ = i2c_address;
/* Start I2C */
wire_->begin();
/* Check the type of device attached */
if(identify() != 0) {
// Unable to identify device
return 2;
}
/* Check for wrong device type */
if(TRILL_UNKNOWN != device && device_type_ != device) {
device_type_ = TRILL_NONE;
return -3;
}
/* Check for device mode */
Mode mode = trillDefaults[device+1].mode;
if(AUTO == mode) {
return -1;
}
/* Put the device in the correspondent mode */
setMode(mode);
delay(interCommandDelay);
Touches::centroids = buffer_;
Touches::sizes = buffer_ + MAX_TOUCH_1D_OR_2D;
if(is2D()) {
horizontal.centroids = buffer_ + 2 * MAX_TOUCH_1D_OR_2D;
horizontal.sizes = buffer_ + 3 * MAX_TOUCH_1D_OR_2D;
} else
horizontal.num_touches = 0;
/* Set default scan settings */
setScanSettings(0, 12);
delay(interCommandDelay);
updateBaseline();
delay((firmware_version_ >= 3 ? 10 : 1) * interCommandDelay); // not really needed, but it ensures the first command the user sends after calling setup() will be adequately timed. Hopefully this is not a source of confusion...
return 0;
}
/* Return the type of device attached, or 0 if none is attached. */
int Trill::identify() {
wire_->beginTransmission(i2c_address_);
wire_->write(kOffsetCommand);
wire_->write(kCommandIdentify);
int ret = wire_->endTransmission();
if(ret)
return ret;
/* Give Trill time to process this command */
delay(25);
last_read_loc_ = kOffsetCommand;
wire_->requestFrom(i2c_address_, (uint8_t)3);
if(wire_->available() < 3) {
/* Unexpected or no response; no valid device connected */
device_type_ = TRILL_NONE;
firmware_version_ = 0;
return -1;
}
wire_->read(); // Discard first input
device_type_ = (Device)wire_->read();
firmware_version_ = wire_->read();
return 0;
}
/* Get the name of a given device */
const char* Trill::getNameFromDevice(Device device) {
switch(device) {
case TRILL_BAR:
return "Bar";
case TRILL_SQUARE:
return "Square";
case TRILL_RING:
return "Ring";
case TRILL_FLEX:
return "Flex";
case TRILL_HEX:
return "Hex";
default:
return "NO DEVICE";
}
}
/* Read the latest scan value from the sensor. Returns true on success. */
boolean Trill::read() {
if(CENTROID != mode_)
return false;
uint8_t loc = 0;
uint8_t length = kCentroidLengthDefault;
/* Set the read location to the right place if needed */
prepareForDataRead();
if(device_type_ == TRILL_SQUARE || device_type_ == TRILL_HEX)
length = kCentroidLength2D;
if(device_type_ == TRILL_RING)
length = kCentroidLengthRing;
wire_->requestFrom(i2c_address_, length);
while(wire_->available() >= 2) {
uint8_t msb = wire_->read();
uint8_t lsb = wire_->read();
buffer_[loc] = lsb + (msb << 8);
++loc;
}
uint8_t maxNumCentroids = MAX_TOUCH_1D_OR_2D;
boolean ret = true;
/* Check for read error */
if(loc * 2 < length) {
maxNumCentroids = 0;
ret = false;
}
processCentroids(maxNumCentroids);
if(is2D())
horizontal.processCentroids(maxNumCentroids);
return ret;
}
/* Update the baseline value on the sensor */
void Trill::updateBaseline() {
wire_->beginTransmission(i2c_address_);
wire_->write(kOffsetCommand);
wire_->write(kCommandBaselineUpdate);
wire_->endTransmission();
last_read_loc_ = kOffsetCommand;
}
/* Request raw data; wrappers for Wire */
boolean Trill::requestRawData(uint8_t max_length) {
uint8_t length = 0;
prepareForDataRead();
if(max_length == 0xFF) {
length = RAW_LENGTH;
}
if(length > kRawLength)
length = kRawLength;
/* The raw data might be longer than the Wire.h maximum buffer
* (BUFFER_LENGTH in Wire.h).
* If so, split it into two reads. */
if(length <= BUFFER_LENGTH) {
wire_->requestFrom(i2c_address_, length);
raw_bytes_left_ = 0;
}
else {
int ret = wire_->requestFrom(i2c_address_, (uint8_t)BUFFER_LENGTH);
if(ret > 0)
raw_bytes_left_ = length - ret;
else {
// failed transmission. Device died?
raw_bytes_left_ = 0;
return false;
}
}
return true;
}
int Trill::rawDataAvailable() {
/* Raw data items are 2 bytes long; return number of them available */
return ((wire_->available() + raw_bytes_left_) >> 1);
}
/* Raw data is in 16-bit big-endian format */
int Trill::rawDataRead() {
if(wire_->available() < 2) {
/* Read more bytes if we need it */
if(raw_bytes_left_ > 0) {
/* Move read pointer on device */
wire_->beginTransmission(i2c_address_);
wire_->write(kOffsetData + BUFFER_LENGTH);
wire_->endTransmission();
last_read_loc_ = kOffsetData + BUFFER_LENGTH;
/* Now gather what's left */
wire_->requestFrom(i2c_address_, raw_bytes_left_);
raw_bytes_left_ = 0;
}
/* Check again if we've got anything... */
if(wire_->available() < 2)
return 0;
}
int result = ((uint8_t)wire_->read()) << 8;
result += (int)wire_->read();
return result;
}
/* Scan configuration settings */
void Trill::setMode(Mode mode) {
wire_->beginTransmission(i2c_address_);
wire_->write(kOffsetCommand);
wire_->write(kCommandMode);
wire_->write(mode);
wire_->endTransmission();
mode_ = mode;
last_read_loc_ = kOffsetCommand;
num_touches = 0;
}
void Trill::setScanSettings(uint8_t speed, uint8_t num_bits) {
if(speed > 3)
speed = 3;
if(num_bits < 9)
num_bits = 9;
if(num_bits > 16)
num_bits = 16;
wire_->beginTransmission(i2c_address_);
wire_->write(kOffsetCommand);
wire_->write(kCommandScanSettings);
wire_->write(speed);
wire_->write(num_bits);
wire_->endTransmission();
last_read_loc_ = kOffsetCommand;
}
void Trill::setPrescaler(uint8_t prescaler) {
wire_->beginTransmission(i2c_address_);
wire_->write(kOffsetCommand);
wire_->write(kCommandPrescaler);
wire_->write(prescaler);
wire_->endTransmission();
last_read_loc_ = kOffsetCommand;
}
void Trill::setNoiseThreshold(uint8_t threshold) {
wire_->beginTransmission(i2c_address_);
wire_->write(kOffsetCommand);
wire_->write(kCommandNoiseThreshold);
wire_->write(threshold);
wire_->endTransmission();
last_read_loc_ = kOffsetCommand;
}
void Trill::setIDACValue(uint8_t value) {
wire_->beginTransmission(i2c_address_);
wire_->write(kOffsetCommand);
wire_->write(kCommandIdac);
wire_->write(value);
wire_->endTransmission();
last_read_loc_ = kOffsetCommand;
}
void Trill::setMinimumTouchSize(uint16_t size) {
wire_->beginTransmission(i2c_address_);
wire_->write(kOffsetCommand);
wire_->write(kCommandMinimumSize);
wire_->write(size >> 8);
wire_->write(size & 0xFF);
wire_->endTransmission();
last_read_loc_ = kOffsetCommand;
}
void Trill::setAutoScanInterval(uint16_t interval) {
wire_->beginTransmission(i2c_address_);
wire_->write(kOffsetCommand);
wire_->write(kCommandAutoScanInterval);
wire_->write(interval >> 8);
wire_->write(interval & 0xFF);
wire_->endTransmission();
last_read_loc_ = kOffsetCommand;
}
/* Prepare the device to read data if it is not already prepared */
void Trill::prepareForDataRead() {
if(last_read_loc_ != kOffsetData) {
wire_->beginTransmission(i2c_address_);
wire_->write(kOffsetData);
wire_->endTransmission();
last_read_loc_ = kOffsetData;
}
}
int Trill::getButtonValue(uint8_t button_num)
{
if(mode_ != CENTROID)
return -1;
if(button_num > 1)
return -1;
if(device_type_ != TRILL_RING)
return -1;
return buffer_[2 * MAX_TOUCH_1D_OR_2D + button_num];
}
unsigned int Trill::getNumChannels()
{
switch(device_type_) {
case TRILL_BAR: return kNumChannelsBar;
case TRILL_RING: return kNumChannelsRing;
default: return kNumChannelsMax;
}
}
bool Trill::is1D()
{
if(CENTROID != mode_)
return false;
switch(device_type_) {
case TRILL_BAR:
case TRILL_RING:
case TRILL_CRAFT:
case TRILL_FLEX:
return true;
default:
return false;
}
}
bool Trill::is2D()
{
if(CENTROID != mode_)
return false;
switch(device_type_) {
case TRILL_SQUARE:
case TRILL_HEX:
return true;
default:
return false;
}
}
uint8_t Touches::getNumTouches() const
{
return num_touches;
}
int Touches::touchLocation(uint8_t touch_num) const
{
if(touch_num < num_touches)
return centroids[touch_num];
else
return -1;
}
int Touches::touchSize(uint8_t touch_num) const
{
if(touch_num < num_touches)
return sizes[touch_num];
else
return -1;
}
unsigned int Touches2D::getNumHorizontalTouches() {
return horizontal.getNumTouches();
}
void Touches::processCentroids(uint8_t maxCentroids) {
// Look for 1st instance of 0xFFFF (no touch) in the buffer
for(num_touches = 0; num_touches < maxCentroids; ++num_touches)
{
if(0xffff == centroids[num_touches])
break;// at the first non-touch, break
}
// now num_touches is the number of active touches in the array
}
/* These methods for horizontal touches on 2D sliders */
int Touches2D::touchHorizontalLocation(uint8_t touch_num) {
return horizontal.touchLocation(touch_num);
}
int Touches2D::touchHorizontalSize(uint8_t touch_num) {
return horizontal.touchSize(touch_num);
}