Mercurial > hg > beaglert
comparison core/I2c_TouchKey.cpp @ 371:361d0c2335cf prerelease
Moved I2c_TouchKey files from the dbox project into the core
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Thu, 09 Jun 2016 17:31:44 +0100 |
parents | examples/d-box/I2c_TouchKey.cpp@dbeed520b014 |
children |
comparison
equal
deleted
inserted
replaced
370:137a87b745d2 | 371:361d0c2335cf |
---|---|
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 sensorType = kSensorTypeTouchKey; | |
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(int sensorTypeToUse) | |
25 { | |
26 sensorType = sensorTypeToUse; | |
27 if(sensorType > 2 || sensorType < 0) | |
28 sensorType = 2; | |
29 | |
30 numBytesToRead = kSensorBytes[sensorType]; | |
31 | |
32 char buf[3] = { 0x00, 0x01, 0x00 }; // code for centroid mode | |
33 if(write(i2C_file, buf, 3) !=3) | |
34 { | |
35 cout << "Failed to set TouchKey in \"Centroid Mode\" " << endl; | |
36 return 1; | |
37 } | |
38 | |
39 usleep(5000); // need to give TouchKey enough time to process command | |
40 | |
41 char buf4[4] = { 0x00, 0x07, 0x00, 0x64}; // code for change minimum touch area | |
42 if(write(i2C_file, buf4, 4) !=4) | |
43 { | |
44 cout << "Failed to set TouchKey minimum touch size" << endl; | |
45 return 1; | |
46 } | |
47 | |
48 usleep(5000); // need to give TouchKey enough time to process command | |
49 | |
50 if(sensorType == kSensorTypeDBox2) | |
51 buf[0] = 0x04; // code for data collection | |
52 else | |
53 buf[0] = 0x06; // code for data collection | |
54 | |
55 if(write(i2C_file, buf, 1) !=1) | |
56 { | |
57 cout << "Failed to prepare data collection " << endl; | |
58 return 2; | |
59 } | |
60 | |
61 usleep(5000); // need to give TouchKey enough time to process command | |
62 | |
63 isReady = true; | |
64 | |
65 return 0; | |
66 } | |
67 | |
68 | |
69 int I2c_TouchKey::readI2C() | |
70 { | |
71 bytesRead = read(i2C_file, dataBuffer, numBytesToRead); | |
72 if (bytesRead != numBytesToRead) | |
73 { | |
74 cout << "Failure to read Byte Stream" << endl; | |
75 return 2; | |
76 } | |
77 /*cout << NUM_BYTES << " bytes read" << endl; | |
78 for(int j=0; j<9; j++) | |
79 cout << "\t" << (int)dataBuffer[j]; | |
80 cout << endl; | |
81 */ | |
82 | |
83 touchCount = 0; | |
84 | |
85 // Old TouchKeys sensors have 3 touch locations plus horizontal positions | |
86 // New D-Box sensors have 5 touch locations but no horizontal position | |
87 // Later D-Box sensors have same data in a different format | |
88 if(sensorType == kSensorTypeDBox1) { | |
89 rawSliderPosition[0] = (((dataBuffer[0] & 0xF0) << 4) + dataBuffer[1]); | |
90 rawSliderPosition[1] = (((dataBuffer[0] & 0x0F) << 8) + dataBuffer[2]); | |
91 rawSliderPosition[2] = (((dataBuffer[3] & 0xF0) << 4) + dataBuffer[4]); | |
92 rawSliderPosition[3] = (((dataBuffer[5] & 0xF0) << 4) + dataBuffer[6]); | |
93 rawSliderPosition[4] = (((dataBuffer[5] & 0x0F) << 8) + dataBuffer[7]); | |
94 rawSliderPositionH = 0x0FFF; | |
95 } | |
96 else if(sensorType == kSensorTypeDBox2) { | |
97 rawSliderPosition[0] = ((dataBuffer[0] << 8) + dataBuffer[1]) & 0x0FFF; | |
98 rawSliderPosition[1] = ((dataBuffer[2] << 8) + dataBuffer[3]) & 0x0FFF; | |
99 rawSliderPosition[2] = ((dataBuffer[4] << 8) + dataBuffer[5]) & 0x0FFF; | |
100 rawSliderPosition[3] = ((dataBuffer[6] << 8) + dataBuffer[7]) & 0x0FFF; | |
101 rawSliderPosition[4] = ((dataBuffer[8] << 8) + dataBuffer[9]) & 0x0FFF; | |
102 rawSliderPositionH = 0x0FFF; | |
103 } | |
104 else { | |
105 rawSliderPosition[0] = (((dataBuffer[0] & 0xF0) << 4) + dataBuffer[1]); | |
106 rawSliderPosition[1] = (((dataBuffer[0] & 0x0F) << 8) + dataBuffer[2]); | |
107 rawSliderPosition[2] = (((dataBuffer[3] & 0xF0) << 4) + dataBuffer[4]); | |
108 rawSliderPosition[3] = 0x0FFF; | |
109 rawSliderPosition[4] = 0x0FFF; | |
110 rawSliderPositionH = (((dataBuffer[3] & 0x0F) << 8) + dataBuffer[5]); | |
111 } | |
112 | |
113 for(int i = 0; i < 5; i++) | |
114 { | |
115 if(rawSliderPosition[i] != 0x0FFF) // 0x0FFF means no touch | |
116 { | |
117 if(sensorType != kSensorTypeTouchKey) | |
118 sliderPosition[i] = (float)rawSliderPosition[i] / 3200.0; // New sensors; 26 pads (128 * 25) | |
119 else | |
120 sliderPosition[i] = (float)rawSliderPosition[i] / 2432.0; // White keys, vertical (128 * 19) | |
121 | |
122 if(sliderPosition[i] > 1.0) | |
123 sliderPosition[i] = 1.0; | |
124 | |
125 if(sensorType == kSensorTypeDBox2) { | |
126 sliderSize[i] = (float)((dataBuffer[2*i + 10] << 8) + dataBuffer[2*i + 11]) / 5000.0; | |
127 if(sliderSize[i] > 1.0) | |
128 sliderSize[i] = 1.0; | |
129 } | |
130 else if(sensorType == kSensorTypeDBox1) | |
131 sliderSize[i] = (float)dataBuffer[i + 8] / 255.0; | |
132 else { | |
133 if(i < 3) | |
134 sliderSize[i] = (float)dataBuffer[i + 6] / 255.0; | |
135 else | |
136 sliderSize[i] = 0.0; | |
137 } | |
138 touchCount++; | |
139 } | |
140 else { | |
141 sliderPosition[i] = -1.0; | |
142 sliderSize[i] = 0.0; | |
143 } | |
144 } | |
145 | |
146 | |
147 | |
148 if(rawSliderPositionH != 0x0FFF) | |
149 { | |
150 sliderPositionH = (float)rawSliderPositionH / 256.0; // White keys, horizontal (1 byte + 1 bit) | |
151 } | |
152 else | |
153 sliderPositionH = -1.0; | |
154 | |
155 #ifdef DEBUG_I2C_TOUCHKEY | |
156 for(int i = 0; i < bytesRead; i++) { | |
157 printf("%2X ", dataBuffer[i]); | |
158 } | |
159 cout << touchCount << " touches: "; | |
160 for(int i = 0; i < touchCount; i++) { | |
161 cout << "(" << sliderPosition[i] << ", " << sliderSize[i] << ") "; | |
162 } | |
163 cout << "H = " << sliderPositionH << endl; | |
164 #endif | |
165 | |
166 return 0; | |
167 } | |
168 | |
169 | |
170 I2c_TouchKey::~I2c_TouchKey() | |
171 {} | |
172 |