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

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