annotate projects/d-box/I2c_TouchKey.cpp @ 68:59edd5780fef

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