Debug modifications for monitoring internals parameters

Fork of tsi_sensor by neil hancock

Committer:
neilh20
Date:
Tue May 19 16:01:02 2015 +0000
Revision:
6:6c6aef368805
Parent:
5:37ae0d169e13
Can't remember what these changes are.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kojto 0:9331e373c138 1 /* Freescale Semiconductor Inc.
Kojto 0:9331e373c138 2 *
Kojto 0:9331e373c138 3 * mbed Microcontroller Library
Kojto 0:9331e373c138 4 * (c) Copyright 2009-2012 ARM Limited.
Kojto 0:9331e373c138 5 *
Kojto 0:9331e373c138 6 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Kojto 0:9331e373c138 7 * and associated documentation files (the "Software"), to deal in the Software without
Kojto 0:9331e373c138 8 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
Kojto 0:9331e373c138 9 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Kojto 0:9331e373c138 10 * Software is furnished to do so, subject to the following conditions:
Kojto 0:9331e373c138 11 *
Kojto 0:9331e373c138 12 * The above copyright notice and this permission notice shall be included in all copies or
Kojto 0:9331e373c138 13 * substantial portions of the Software.
Kojto 0:9331e373c138 14 *
Kojto 0:9331e373c138 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
Kojto 0:9331e373c138 16 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Kojto 0:9331e373c138 17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Kojto 0:9331e373c138 18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Kojto 0:9331e373c138 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Kojto 0:9331e373c138 20 */
Kojto 0:9331e373c138 21
Kojto 0:9331e373c138 22 #ifndef TSISENSOR_H
Kojto 0:9331e373c138 23 #define TSISENSOR_H
Kojto 0:9331e373c138 24
Kojto 0:9331e373c138 25 /**
Kojto 0:9331e373c138 26 * TSISensor example
Kojto 0:9331e373c138 27 *
Kojto 0:9331e373c138 28 * @code
Kojto 0:9331e373c138 29 * #include "mbed.h"
Kojto 0:9331e373c138 30 * #include "TSISensor.h"
Kojto 0:9331e373c138 31 *
Kojto 0:9331e373c138 32 * int main(void) {
Kojto 0:9331e373c138 33 * DigitalOut led(LED_GREEN);
Kojto 0:9331e373c138 34 * TSIElectrode elec0(9);
Kojto 0:9331e373c138 35 * TSIElectrode elec1(10);
Kojto 0:9331e373c138 36 * TSIAnalogSlider tsi(elec0, elec1, 40);
Kojto 0:9331e373c138 37 *
Kojto 0:9331e373c138 38 * while (true) {
Kojto 0:9331e373c138 39 * printf("slider percentage: %f%\r\n", tsi.readPercentage());
Kojto 0:9331e373c138 40 * printf("slider distance: %dmm\r\n", tsi.readDistance());
Kojto 0:9331e373c138 41 * wait(1);
Kojto 0:9331e373c138 42 * led = !led;
Kojto 0:9331e373c138 43 * }
Kojto 0:9331e373c138 44 * }
Kojto 0:9331e373c138 45 * @endcode
Kojto 0:9331e373c138 46 */
Kojto 0:9331e373c138 47 #define NO_TOUCH 0
Kojto 0:9331e373c138 48
Kojto 0:9331e373c138 49 /** TSI Electrode with simple data required for touch detection.
Kojto 0:9331e373c138 50 */
Kojto 0:9331e373c138 51 class TSIElectrode {
Kojto 0:9331e373c138 52 public:
Kojto 0:9331e373c138 53 /** Initialize electrode.
Kojto 0:9331e373c138 54 */
bjo3rn 4:f64097679f27 55 TSIElectrode(PinName pin) : _threshold(100) {
bjo3rn 4:f64097679f27 56 _channel = getTSIChannel(pin);
bjo3rn 4:f64097679f27 57 }
bjo3rn 4:f64097679f27 58
bjo3rn 4:f64097679f27 59 /** Initialize electrode.
bjo3rn 4:f64097679f27 60 */
Kojto 0:9331e373c138 61 TSIElectrode(uint32_t tsi_channel) : _threshold(100) {
Kojto 0:9331e373c138 62 _channel = (uint8_t)tsi_channel;
Kojto 0:9331e373c138 63 }
Kojto 0:9331e373c138 64 /** Set baseline.
Kojto 0:9331e373c138 65 */
Kojto 0:9331e373c138 66 void setBaseline(uint32_t baseline) {
Kojto 0:9331e373c138 67 _baseline = (uint16_t)baseline;
Kojto 0:9331e373c138 68 }
Kojto 0:9331e373c138 69 /** Set threshold.
Kojto 0:9331e373c138 70 */
Kojto 0:9331e373c138 71 void setThreshold(uint32_t threshold) {
Kojto 0:9331e373c138 72 _threshold = (uint16_t)threshold;
Kojto 0:9331e373c138 73 }
Kojto 0:9331e373c138 74 /** Set signal.
Kojto 0:9331e373c138 75 */
Kojto 0:9331e373c138 76 void setSignal(uint32_t signal) {
Kojto 0:9331e373c138 77 _signal = (uint16_t)signal;
Kojto 0:9331e373c138 78 }
Kojto 0:9331e373c138 79 /** Get baseline.
Kojto 0:9331e373c138 80 */
Kojto 0:9331e373c138 81 uint32_t getBaseline() {
Kojto 0:9331e373c138 82 return _baseline;
Kojto 0:9331e373c138 83 }
Kojto 0:9331e373c138 84 /** Get delta.
Kojto 0:9331e373c138 85 */
Kojto 0:9331e373c138 86 uint32_t getDelta() {
Kojto 0:9331e373c138 87 int32_t delta = getSignal() - getBaseline();
Kojto 0:9331e373c138 88 if (delta < 0) {
Kojto 0:9331e373c138 89 return 0;
Kojto 0:9331e373c138 90 } else {
Kojto 0:9331e373c138 91 return delta;
Kojto 0:9331e373c138 92 }
Kojto 0:9331e373c138 93 }
Kojto 0:9331e373c138 94 /** Get signal.
Kojto 0:9331e373c138 95 */
Kojto 0:9331e373c138 96 uint32_t getSignal() {
Kojto 0:9331e373c138 97 return _signal;
Kojto 0:9331e373c138 98 }
Kojto 0:9331e373c138 99 /** Get threshold.
Kojto 0:9331e373c138 100 */
Kojto 0:9331e373c138 101 uint32_t getThreshold() {
Kojto 0:9331e373c138 102 return _threshold;
Kojto 0:9331e373c138 103 }
Kojto 0:9331e373c138 104 /** Get channel.
Kojto 0:9331e373c138 105 */
Kojto 0:9331e373c138 106 uint32_t getChannel() {
Kojto 0:9331e373c138 107 return _channel;
Kojto 0:9331e373c138 108 }
bjo3rn 4:f64097679f27 109 /** Get TSI Channel for PinName.
bjo3rn 4:f64097679f27 110 *
bjo3rn 4:f64097679f27 111 * @returns TSI channel ID for use in constructor of TSIAnalogSlider and TSIElectrode.
bjo3rn 4:f64097679f27 112 * @throws compile-time error if target is not supported, or runtime error if pin does not match any channel.
bjo3rn 4:f64097679f27 113 */
bjo3rn 4:f64097679f27 114 static uint8_t getTSIChannel(PinName pin) {
bjo3rn 4:f64097679f27 115 #if defined (TARGET_KL25Z)
bjo3rn 4:f64097679f27 116 switch(pin) {
bjo3rn 4:f64097679f27 117 //these are
bjo3rn 4:f64097679f27 118 case PTA0: return 1;
bjo3rn 4:f64097679f27 119 case PTA1: return 2;
bjo3rn 4:f64097679f27 120 case PTA2: return 3;
bjo3rn 4:f64097679f27 121 case PTA3: return 4;
bjo3rn 4:f64097679f27 122 case PTA4: return 5;
bjo3rn 4:f64097679f27 123 case PTB0: return 0;
bjo3rn 4:f64097679f27 124 case PTB1: return 6;
bjo3rn 4:f64097679f27 125 case PTB2: return 7;
bjo3rn 4:f64097679f27 126 case PTB3: return 8;
bjo3rn 4:f64097679f27 127 case PTB16: return 9;
bjo3rn 4:f64097679f27 128 case PTB17: return 10;
bjo3rn 4:f64097679f27 129 case PTB18: return 11;
bjo3rn 4:f64097679f27 130 case PTB19: return 12;
bjo3rn 4:f64097679f27 131 case PTC0: return 13;
bjo3rn 4:f64097679f27 132 case PTC1: return 14;
bjo3rn 4:f64097679f27 133 default: error("PinName provided to TSIElectrode::getTSIChannel() does not correspond to any known TSI channel.");
bjo3rn 4:f64097679f27 134 }
neilh20 6:6c6aef368805 135 #elif defined (TARGET_K20D50M)
neilh20 6:6c6aef368805 136 switch(pin) { //Need to do for K20
neilh20 6:6c6aef368805 137 //these are
neilh20 6:6c6aef368805 138 case PTA0: return 1;
neilh20 6:6c6aef368805 139 case PTA1: return 2;
neilh20 6:6c6aef368805 140 case PTA2: return 3;
neilh20 6:6c6aef368805 141 case PTA3: return 4;
neilh20 6:6c6aef368805 142 case PTA4: return 5;
neilh20 6:6c6aef368805 143 case PTB0: return 0;
neilh20 6:6c6aef368805 144 case PTB1: return 6;
neilh20 6:6c6aef368805 145 case PTB2: return 7;
neilh20 6:6c6aef368805 146 case PTB3: return 8;
neilh20 6:6c6aef368805 147 case PTB16: return 9;
neilh20 6:6c6aef368805 148 case PTB17: return 10;
neilh20 6:6c6aef368805 149 case PTB18: return 11;
neilh20 6:6c6aef368805 150 case PTB19: return 12;
neilh20 6:6c6aef368805 151 case PTC0: return 13;
neilh20 6:6c6aef368805 152 case PTC1: return 14;
neilh20 6:6c6aef368805 153 default: error("PinName provided to TSIElectrode::getTSIChannel() does not correspond to any known TSI channel.");
neilh20 6:6c6aef368805 154 }
bjo3rn 4:f64097679f27 155 # else
bjo3rn 4:f64097679f27 156 #error "Unknown target for TSIElectrode::getTSIChannel() - only supports KL25Z so far."
bjo3rn 4:f64097679f27 157 # endif
bjo3rn 4:f64097679f27 158 return 0xFF; //should never get here
bjo3rn 4:f64097679f27 159 }
bjo3rn 4:f64097679f27 160
Kojto 0:9331e373c138 161 private:
Kojto 0:9331e373c138 162 uint8_t _channel;
Kojto 0:9331e373c138 163 uint16_t _signal;
Kojto 0:9331e373c138 164 uint16_t _baseline;
Kojto 0:9331e373c138 165 uint16_t _threshold;
Kojto 0:9331e373c138 166 };
Kojto 0:9331e373c138 167
Kojto 0:9331e373c138 168 /** Analog slider which consists of two electrodes.
Kojto 0:9331e373c138 169 */
Kojto 0:9331e373c138 170 class TSIAnalogSlider {
Kojto 0:9331e373c138 171 public:
Kojto 0:9331e373c138 172 /**
bjo3rn 4:f64097679f27 173 *
bjo3rn 4:f64097679f27 174 * Initialize the TSI Touch Sensor with the given PinNames
bjo3rn 4:f64097679f27 175 */
bjo3rn 4:f64097679f27 176 TSIAnalogSlider(PinName elec0, PinName elec1, uint32_t range);
bjo3rn 4:f64097679f27 177 /**
Kojto 0:9331e373c138 178 * Initialize the TSI Touch Sensor
Kojto 0:9331e373c138 179 */
Kojto 3:20ffa9b18488 180 TSIAnalogSlider(uint32_t elec0, uint32_t elec1, uint32_t range);
Kojto 0:9331e373c138 181 /**
Kojto 0:9331e373c138 182 * Read Touch Sensor percentage value
Kojto 0:9331e373c138 183 *
Kojto 0:9331e373c138 184 * @returns percentage value between [0 ... 1]
Kojto 0:9331e373c138 185 */
Kojto 0:9331e373c138 186 float readPercentage();
Kojto 0:9331e373c138 187 /**
Kojto 0:9331e373c138 188 * Read Touch Sensor distance
Kojto 0:9331e373c138 189 *
Kojto 0:9331e373c138 190 * @returns distance in mm. The value is between [0 ... _range]
Kojto 0:9331e373c138 191 */
Kojto 0:9331e373c138 192 uint32_t readDistance();
Kojto 0:9331e373c138 193 /** Get current electrode.
Kojto 0:9331e373c138 194 */
Kojto 0:9331e373c138 195 TSIElectrode* getCurrentElectrode() {
Kojto 0:9331e373c138 196 return _current_elec;
Kojto 0:9331e373c138 197 }
Kojto 0:9331e373c138 198 /** Set current electrode which is being measured.
Kojto 0:9331e373c138 199 */
Kojto 0:9331e373c138 200 void setCurrentElectrode(TSIElectrode *elec){
Kojto 0:9331e373c138 201 _current_elec = elec;
Kojto 0:9331e373c138 202 }
Kojto 0:9331e373c138 203 /** Get next electrode.
Kojto 0:9331e373c138 204 */
Kojto 0:9331e373c138 205 TSIElectrode* getNextElectrode(TSIElectrode* electrode) {
Kojto 0:9331e373c138 206 if (electrode->getChannel() == _elec0.getChannel()) {
Kojto 0:9331e373c138 207 return &_elec1;
Kojto 0:9331e373c138 208 } else {
Kojto 0:9331e373c138 209 return &_elec0;
Kojto 0:9331e373c138 210 }
Kojto 0:9331e373c138 211 }
Kojto 0:9331e373c138 212 /** Return absolute distance position.
Kojto 0:9331e373c138 213 */
Kojto 0:9331e373c138 214 uint32_t getAbsoluteDistance() {
Kojto 0:9331e373c138 215 return _absolute_distance_pos;
Kojto 0:9331e373c138 216 }
Kojto 0:9331e373c138 217 /** Return absolute precentage position.
Kojto 0:9331e373c138 218 */
Kojto 0:9331e373c138 219 uint32_t getAbsolutePosition() {
Kojto 0:9331e373c138 220 return _absolute_percentage_pos;
Kojto 0:9331e373c138 221 }
Kojto 0:9331e373c138 222 /** Set value to the scan in progress flag.
Kojto 0:9331e373c138 223 */
Kojto 0:9331e373c138 224 void setScan(uint32_t scan) {
Kojto 0:9331e373c138 225 _scan_in_progress = scan;
Kojto 0:9331e373c138 226 }
Kojto 0:9331e373c138 227 /** Return instance to Analog slider. Used in tsi irq.
Kojto 0:9331e373c138 228 */
Kojto 0:9331e373c138 229 static TSIAnalogSlider *getInstance() {
Kojto 0:9331e373c138 230 return _instance;
Kojto 0:9331e373c138 231 }
neilh20 5:37ae0d169e13 232
neilh20 5:37ae0d169e13 233 uint32_t getDelta0() {
neilh20 5:37ae0d169e13 234 return delta0;
neilh20 5:37ae0d169e13 235 }
neilh20 5:37ae0d169e13 236 uint32_t getDelta1() {
neilh20 5:37ae0d169e13 237 return delta1;
neilh20 5:37ae0d169e13 238 }
Kojto 0:9331e373c138 239 private:
bjo3rn 4:f64097679f27 240 void initObject(void); //shared constructor code
Kojto 0:9331e373c138 241 void sliderRead(void);
Kojto 0:9331e373c138 242 void selfCalibration(void);
Kojto 0:9331e373c138 243 void setSliderPercPosition(uint32_t elec_num, uint32_t position) {
Kojto 0:9331e373c138 244 _percentage_position[elec_num] = position;
Kojto 0:9331e373c138 245 }
Kojto 0:9331e373c138 246 void setSliderDisPosition(uint32_t elec_num, uint32_t position) {
Kojto 0:9331e373c138 247 _distance_position[elec_num] = position;
Kojto 0:9331e373c138 248 }
Kojto 0:9331e373c138 249 void setAbsolutePosition(uint32_t position) {
Kojto 0:9331e373c138 250 _absolute_percentage_pos = position;
Kojto 0:9331e373c138 251 }
Kojto 0:9331e373c138 252 void setAbsoluteDistance(uint32_t distance) {
Kojto 0:9331e373c138 253 _absolute_distance_pos = distance;
Kojto 0:9331e373c138 254 }
Kojto 0:9331e373c138 255 private:
Kojto 3:20ffa9b18488 256 TSIElectrode _elec0;
Kojto 3:20ffa9b18488 257 TSIElectrode _elec1;
Kojto 0:9331e373c138 258 uint8_t _scan_in_progress;
Kojto 0:9331e373c138 259 TSIElectrode* _current_elec;
Kojto 0:9331e373c138 260 uint8_t _percentage_position[2];
Kojto 0:9331e373c138 261 uint8_t _distance_position[2];
Kojto 0:9331e373c138 262 uint32_t _absolute_percentage_pos;
Kojto 0:9331e373c138 263 uint32_t _absolute_distance_pos;
Kojto 0:9331e373c138 264 uint8_t _range;
neilh20 5:37ae0d169e13 265
neilh20 5:37ae0d169e13 266 uint32_t delta0; //= _elec0.getDelta();
neilh20 5:37ae0d169e13 267 uint32_t delta1; // = _elec1.getDelta();
Kojto 0:9331e373c138 268 protected:
Kojto 0:9331e373c138 269 static TSIAnalogSlider *_instance;
Kojto 0:9331e373c138 270 };
Kojto 0:9331e373c138 271
Kojto 0:9331e373c138 272 #endif