Mercurial > hg > beaglert
diff projects/d-box/I2c_TouchKey.cpp @ 15:901d205d1a3c
Updated to latest PRU library; external PRU file no longer needed. Also catch SIGTERM as well as SIGINT to clean up gracefully.
author | andrewm |
---|---|
date | Sat, 07 Feb 2015 16:41:56 +0000 |
parents | 8a575ba3ab52 |
children |
line wrap: on
line diff
--- a/projects/d-box/I2c_TouchKey.cpp Fri Jan 23 15:35:10 2015 +0000 +++ b/projects/d-box/I2c_TouchKey.cpp Sat Feb 07 16:41:56 2015 +0000 @@ -14,17 +14,20 @@ I2c_TouchKey::I2c_TouchKey() { isReady = false; - newSensor = false; + sensorType = kSensorTypeTouchKey; touchCount = 0; sliderSize[0] = sliderSize[1] = sliderSize[2] = -1; sliderPosition[0] = sliderPosition[1] = sliderPosition[2] = -1; sliderPositionH = -1; } -int I2c_TouchKey::initTouchKey(bool useNewSensor) +int I2c_TouchKey::initTouchKey(int sensorTypeToUse) { - newSensor = useNewSensor; - numBytesToRead = newSensor ? NUM_BYTES_NEW : NUM_BYTES_OLD; + sensorType = sensorTypeToUse; + if(sensorType > 2 || sensorType < 0) + sensorType = 2; + + numBytesToRead = kSensorBytes[sensorType]; char buf[3] = { 0x00, 0x01, 0x00 }; // code for centroid mode if(write(i2C_file, buf, 3) !=3) @@ -44,7 +47,11 @@ usleep(5000); // need to give TouchKey enough time to process command - buf[0] = 0x06; // code for data collection + if(sensorType == kSensorTypeDBox2) + buf[0] = 0x04; // code for data collection + else + buf[0] = 0x06; // code for data collection + if(write(i2C_file, buf, 1) !=1) { cout << "Failed to prepare data collection " << endl; @@ -75,39 +82,52 @@ touchCount = 0; - rawSliderPosition[0] = (((dataBuffer[0] & 0xF0) << 4) + dataBuffer[1]); - rawSliderPosition[1] = (((dataBuffer[0] & 0x0F) << 8) + dataBuffer[2]); - rawSliderPosition[2] = (((dataBuffer[3] & 0xF0) << 4) + dataBuffer[4]); - // Old TouchKeys sensors have 3 touch locations plus horizontal positions // New D-Box sensors have 5 touch locations but no horizontal position - if(newSensor) - { + // Later D-Box sensors have same data in a different format + if(sensorType == kSensorTypeDBox1) { + rawSliderPosition[0] = (((dataBuffer[0] & 0xF0) << 4) + dataBuffer[1]); + rawSliderPosition[1] = (((dataBuffer[0] & 0x0F) << 8) + dataBuffer[2]); + rawSliderPosition[2] = (((dataBuffer[3] & 0xF0) << 4) + dataBuffer[4]); rawSliderPosition[3] = (((dataBuffer[5] & 0xF0) << 4) + dataBuffer[6]); rawSliderPosition[4] = (((dataBuffer[5] & 0x0F) << 8) + dataBuffer[7]); rawSliderPositionH = 0x0FFF; } - else - { + else if(sensorType == kSensorTypeDBox2) { + rawSliderPosition[0] = ((dataBuffer[0] << 8) + dataBuffer[1]) & 0x0FFF; + rawSliderPosition[1] = ((dataBuffer[2] << 8) + dataBuffer[3]) & 0x0FFF; + rawSliderPosition[2] = ((dataBuffer[4] << 8) + dataBuffer[5]) & 0x0FFF; + rawSliderPosition[3] = ((dataBuffer[6] << 8) + dataBuffer[7]) & 0x0FFF; + rawSliderPosition[4] = ((dataBuffer[8] << 8) + dataBuffer[9]) & 0x0FFF; + rawSliderPositionH = 0x0FFF; + } + else { + rawSliderPosition[0] = (((dataBuffer[0] & 0xF0) << 4) + dataBuffer[1]); + rawSliderPosition[1] = (((dataBuffer[0] & 0x0F) << 8) + dataBuffer[2]); + rawSliderPosition[2] = (((dataBuffer[3] & 0xF0) << 4) + dataBuffer[4]); rawSliderPosition[3] = 0x0FFF; rawSliderPosition[4] = 0x0FFF; rawSliderPositionH = (((dataBuffer[3] & 0x0F) << 8) + dataBuffer[5]); } - for(int i = 0; i < 5; i++) { if(rawSliderPosition[i] != 0x0FFF) // 0x0FFF means no touch { - //sliderPosition[i] = (float)rawSliderPosition[i] / 1536.0; // Black keys, vertical (128 * 12) - //sliderPosition[i] = (float)rawSliderPosition[i] / 768.0; // Cute white key, for simple instrument - if(newSensor) + if(sensorType != kSensorTypeTouchKey) sliderPosition[i] = (float)rawSliderPosition[i] / 3200.0; // New sensors; 26 pads (128 * 25) else sliderPosition[i] = (float)rawSliderPosition[i] / 2432.0; // White keys, vertical (128 * 19) - if(sliderPosition[i]>1.0) + + if(sliderPosition[i] > 1.0) sliderPosition[i] = 1.0; - if(newSensor) + + if(sensorType == kSensorTypeDBox2) { + sliderSize[i] = (float)((dataBuffer[2*i + 10] << 8) + dataBuffer[2*i + 11]) / 5000.0; + if(sliderSize[i] > 1.0) + sliderSize[i] = 1.0; + } + else if(sensorType == kSensorTypeDBox1) sliderSize[i] = (float)dataBuffer[i + 8] / 255.0; else { if(i < 3)