comparison projects/d-box/I2c_TouchKey.cpp @ 0:8a575ba3ab52

Initial commit.
author andrewm
date Fri, 31 Oct 2014 19:10:17 +0100
parents
children 901d205d1a3c
comparison
equal deleted inserted replaced
-1:000000000000 0:8a575ba3ab52
1 /*
2 * I2c_TouchKey.cpp
3 *
4 * Created on: Oct 14, 2013
5 * Author: Victor Zappi
6 */
7
8
9
10 #include "I2c_TouchKey.h"
11
12 #undef DEBUG_I2C_TOUCHKEY
13
14 I2c_TouchKey::I2c_TouchKey()
15 {
16 isReady = false;
17 newSensor = false;
18 touchCount = 0;
19 sliderSize[0] = sliderSize[1] = sliderSize[2] = -1;
20 sliderPosition[0] = sliderPosition[1] = sliderPosition[2] = -1;
21 sliderPositionH = -1;
22 }
23
24 int I2c_TouchKey::initTouchKey(bool useNewSensor)
25 {
26 newSensor = useNewSensor;
27 numBytesToRead = newSensor ? NUM_BYTES_NEW : NUM_BYTES_OLD;
28
29 char buf[3] = { 0x00, 0x01, 0x00 }; // code for centroid mode
30 if(write(i2C_file, buf, 3) !=3)
31 {
32 cout << "Failed to set TouchKey in \"Centroid Mode\" " << endl;
33 return 1;
34 }
35
36 usleep(5000); // need to give TouchKey enough time to process command
37
38 char buf4[4] = { 0x00, 0x07, 0x00, 0x64}; // code for change minimum touch area
39 if(write(i2C_file, buf4, 4) !=4)
40 {
41 cout << "Failed to set TouchKey minimum touch size" << endl;
42 return 1;
43 }
44
45 usleep(5000); // need to give TouchKey enough time to process command
46
47 buf[0] = 0x06; // code for data collection
48 if(write(i2C_file, buf, 1) !=1)
49 {
50 cout << "Failed to prepare data collection " << endl;
51 return 2;
52 }
53
54 usleep(5000); // need to give TouchKey enough time to process command
55
56 isReady = true;
57
58 return 0;
59 }
60
61
62 int I2c_TouchKey::readI2C()
63 {
64 bytesRead = read(i2C_file, dataBuffer, numBytesToRead);
65 if (bytesRead != numBytesToRead)
66 {
67 cout << "Failure to read Byte Stream" << endl;
68 return 2;
69 }
70 /*cout << NUM_BYTES << " bytes read" << endl;
71 for(int j=0; j<9; j++)
72 cout << "\t" << (int)dataBuffer[j];
73 cout << endl;
74 */
75
76 touchCount = 0;
77
78 rawSliderPosition[0] = (((dataBuffer[0] & 0xF0) << 4) + dataBuffer[1]);
79 rawSliderPosition[1] = (((dataBuffer[0] & 0x0F) << 8) + dataBuffer[2]);
80 rawSliderPosition[2] = (((dataBuffer[3] & 0xF0) << 4) + dataBuffer[4]);
81
82 // Old TouchKeys sensors have 3 touch locations plus horizontal positions
83 // New D-Box sensors have 5 touch locations but no horizontal position
84 if(newSensor)
85 {
86 rawSliderPosition[3] = (((dataBuffer[5] & 0xF0) << 4) + dataBuffer[6]);
87 rawSliderPosition[4] = (((dataBuffer[5] & 0x0F) << 8) + dataBuffer[7]);
88 rawSliderPositionH = 0x0FFF;
89 }
90 else
91 {
92 rawSliderPosition[3] = 0x0FFF;
93 rawSliderPosition[4] = 0x0FFF;
94 rawSliderPositionH = (((dataBuffer[3] & 0x0F) << 8) + dataBuffer[5]);
95 }
96
97
98 for(int i = 0; i < 5; i++)
99 {
100 if(rawSliderPosition[i] != 0x0FFF) // 0x0FFF means no touch
101 {
102 //sliderPosition[i] = (float)rawSliderPosition[i] / 1536.0; // Black keys, vertical (128 * 12)
103 //sliderPosition[i] = (float)rawSliderPosition[i] / 768.0; // Cute white key, for simple instrument
104 if(newSensor)
105 sliderPosition[i] = (float)rawSliderPosition[i] / 3200.0; // New sensors; 26 pads (128 * 25)
106 else
107 sliderPosition[i] = (float)rawSliderPosition[i] / 2432.0; // White keys, vertical (128 * 19)
108 if(sliderPosition[i]>1.0)
109 sliderPosition[i] = 1.0;
110 if(newSensor)
111 sliderSize[i] = (float)dataBuffer[i + 8] / 255.0;
112 else {
113 if(i < 3)
114 sliderSize[i] = (float)dataBuffer[i + 6] / 255.0;
115 else
116 sliderSize[i] = 0.0;
117 }
118 touchCount++;
119 }
120 else {
121 sliderPosition[i] = -1.0;
122 sliderSize[i] = 0.0;
123 }
124 }
125
126
127
128 if(rawSliderPositionH != 0x0FFF)
129 {
130 sliderPositionH = (float)rawSliderPositionH / 256.0; // White keys, horizontal (1 byte + 1 bit)
131 }
132 else
133 sliderPositionH = -1.0;
134
135 #ifdef DEBUG_I2C_TOUCHKEY
136 for(int i = 0; i < bytesRead; i++) {
137 printf("%2X ", dataBuffer[i]);
138 }
139 cout << touchCount << " touches: ";
140 for(int i = 0; i < touchCount; i++) {
141 cout << "(" << sliderPosition[i] << ", " << sliderSize[i] << ") ";
142 }
143 cout << "H = " << sliderPositionH << endl;
144 #endif
145
146 return 0;
147 }
148
149
150 I2c_TouchKey::~I2c_TouchKey()
151 {}
152