Fork of tsi_sensor library that allows object construction with pin names instead of TSI channels
Fork of tsi_sensor by
tsi_sensor.h@3:20ffa9b18488, 2014-02-23 (annotated)
- Committer:
- Kojto
- Date:
- Sun Feb 23 18:31:58 2014 +0000
- Revision:
- 3:20ffa9b18488
- Parent:
- 2:b7074bcc2376
- Child:
- 4:f64097679f27
Analog slider contain own instances of electrodes.
Who changed what in which revision?
User | Revision | Line number | New 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 | */ |
Kojto | 0:9331e373c138 | 55 | TSIElectrode(uint32_t tsi_channel) : _threshold(100) { |
Kojto | 0:9331e373c138 | 56 | _channel = (uint8_t)tsi_channel; |
Kojto | 0:9331e373c138 | 57 | } |
Kojto | 0:9331e373c138 | 58 | /** Set baseline. |
Kojto | 0:9331e373c138 | 59 | */ |
Kojto | 0:9331e373c138 | 60 | void setBaseline(uint32_t baseline) { |
Kojto | 0:9331e373c138 | 61 | _baseline = (uint16_t)baseline; |
Kojto | 0:9331e373c138 | 62 | } |
Kojto | 0:9331e373c138 | 63 | /** Set threshold. |
Kojto | 0:9331e373c138 | 64 | */ |
Kojto | 0:9331e373c138 | 65 | void setThreshold(uint32_t threshold) { |
Kojto | 0:9331e373c138 | 66 | _threshold = (uint16_t)threshold; |
Kojto | 0:9331e373c138 | 67 | } |
Kojto | 0:9331e373c138 | 68 | /** Set signal. |
Kojto | 0:9331e373c138 | 69 | */ |
Kojto | 0:9331e373c138 | 70 | void setSignal(uint32_t signal) { |
Kojto | 0:9331e373c138 | 71 | _signal = (uint16_t)signal; |
Kojto | 0:9331e373c138 | 72 | } |
Kojto | 0:9331e373c138 | 73 | /** Get baseline. |
Kojto | 0:9331e373c138 | 74 | */ |
Kojto | 0:9331e373c138 | 75 | uint32_t getBaseline() { |
Kojto | 0:9331e373c138 | 76 | return _baseline; |
Kojto | 0:9331e373c138 | 77 | } |
Kojto | 0:9331e373c138 | 78 | /** Get delta. |
Kojto | 0:9331e373c138 | 79 | */ |
Kojto | 0:9331e373c138 | 80 | uint32_t getDelta() { |
Kojto | 0:9331e373c138 | 81 | int32_t delta = getSignal() - getBaseline(); |
Kojto | 0:9331e373c138 | 82 | if (delta < 0) { |
Kojto | 0:9331e373c138 | 83 | return 0; |
Kojto | 0:9331e373c138 | 84 | } else { |
Kojto | 0:9331e373c138 | 85 | return delta; |
Kojto | 0:9331e373c138 | 86 | } |
Kojto | 0:9331e373c138 | 87 | } |
Kojto | 0:9331e373c138 | 88 | /** Get signal. |
Kojto | 0:9331e373c138 | 89 | */ |
Kojto | 0:9331e373c138 | 90 | uint32_t getSignal() { |
Kojto | 0:9331e373c138 | 91 | return _signal; |
Kojto | 0:9331e373c138 | 92 | } |
Kojto | 0:9331e373c138 | 93 | /** Get threshold. |
Kojto | 0:9331e373c138 | 94 | */ |
Kojto | 0:9331e373c138 | 95 | uint32_t getThreshold() { |
Kojto | 0:9331e373c138 | 96 | return _threshold; |
Kojto | 0:9331e373c138 | 97 | } |
Kojto | 0:9331e373c138 | 98 | /** Get channel. |
Kojto | 0:9331e373c138 | 99 | */ |
Kojto | 0:9331e373c138 | 100 | uint32_t getChannel() { |
Kojto | 0:9331e373c138 | 101 | return _channel; |
Kojto | 0:9331e373c138 | 102 | } |
Kojto | 0:9331e373c138 | 103 | private: |
Kojto | 0:9331e373c138 | 104 | uint8_t _channel; |
Kojto | 0:9331e373c138 | 105 | uint16_t _signal; |
Kojto | 0:9331e373c138 | 106 | uint16_t _baseline; |
Kojto | 0:9331e373c138 | 107 | uint16_t _threshold; |
Kojto | 0:9331e373c138 | 108 | }; |
Kojto | 0:9331e373c138 | 109 | |
Kojto | 0:9331e373c138 | 110 | /** Analog slider which consists of two electrodes. |
Kojto | 0:9331e373c138 | 111 | */ |
Kojto | 0:9331e373c138 | 112 | class TSIAnalogSlider { |
Kojto | 0:9331e373c138 | 113 | public: |
Kojto | 0:9331e373c138 | 114 | /** |
Kojto | 0:9331e373c138 | 115 | * Initialize the TSI Touch Sensor |
Kojto | 0:9331e373c138 | 116 | */ |
Kojto | 3:20ffa9b18488 | 117 | TSIAnalogSlider(uint32_t elec0, uint32_t elec1, uint32_t range); |
Kojto | 0:9331e373c138 | 118 | /** |
Kojto | 0:9331e373c138 | 119 | * Read Touch Sensor percentage value |
Kojto | 0:9331e373c138 | 120 | * |
Kojto | 0:9331e373c138 | 121 | * @returns percentage value between [0 ... 1] |
Kojto | 0:9331e373c138 | 122 | */ |
Kojto | 0:9331e373c138 | 123 | float readPercentage(); |
Kojto | 0:9331e373c138 | 124 | /** |
Kojto | 0:9331e373c138 | 125 | * Read Touch Sensor distance |
Kojto | 0:9331e373c138 | 126 | * |
Kojto | 0:9331e373c138 | 127 | * @returns distance in mm. The value is between [0 ... _range] |
Kojto | 0:9331e373c138 | 128 | */ |
Kojto | 0:9331e373c138 | 129 | uint32_t readDistance(); |
Kojto | 0:9331e373c138 | 130 | /** Get current electrode. |
Kojto | 0:9331e373c138 | 131 | */ |
Kojto | 0:9331e373c138 | 132 | TSIElectrode* getCurrentElectrode() { |
Kojto | 0:9331e373c138 | 133 | return _current_elec; |
Kojto | 0:9331e373c138 | 134 | } |
Kojto | 0:9331e373c138 | 135 | /** Set current electrode which is being measured. |
Kojto | 0:9331e373c138 | 136 | */ |
Kojto | 0:9331e373c138 | 137 | void setCurrentElectrode(TSIElectrode *elec){ |
Kojto | 0:9331e373c138 | 138 | _current_elec = elec; |
Kojto | 0:9331e373c138 | 139 | } |
Kojto | 0:9331e373c138 | 140 | /** Get next electrode. |
Kojto | 0:9331e373c138 | 141 | */ |
Kojto | 0:9331e373c138 | 142 | TSIElectrode* getNextElectrode(TSIElectrode* electrode) { |
Kojto | 0:9331e373c138 | 143 | if (electrode->getChannel() == _elec0.getChannel()) { |
Kojto | 0:9331e373c138 | 144 | return &_elec1; |
Kojto | 0:9331e373c138 | 145 | } else { |
Kojto | 0:9331e373c138 | 146 | return &_elec0; |
Kojto | 0:9331e373c138 | 147 | } |
Kojto | 0:9331e373c138 | 148 | } |
Kojto | 0:9331e373c138 | 149 | /** Return absolute distance position. |
Kojto | 0:9331e373c138 | 150 | */ |
Kojto | 0:9331e373c138 | 151 | uint32_t getAbsoluteDistance() { |
Kojto | 0:9331e373c138 | 152 | return _absolute_distance_pos; |
Kojto | 0:9331e373c138 | 153 | } |
Kojto | 0:9331e373c138 | 154 | /** Return absolute precentage position. |
Kojto | 0:9331e373c138 | 155 | */ |
Kojto | 0:9331e373c138 | 156 | uint32_t getAbsolutePosition() { |
Kojto | 0:9331e373c138 | 157 | return _absolute_percentage_pos; |
Kojto | 0:9331e373c138 | 158 | } |
Kojto | 0:9331e373c138 | 159 | /** Set value to the scan in progress flag. |
Kojto | 0:9331e373c138 | 160 | */ |
Kojto | 0:9331e373c138 | 161 | void setScan(uint32_t scan) { |
Kojto | 0:9331e373c138 | 162 | _scan_in_progress = scan; |
Kojto | 0:9331e373c138 | 163 | } |
Kojto | 0:9331e373c138 | 164 | /** Return instance to Analog slider. Used in tsi irq. |
Kojto | 0:9331e373c138 | 165 | */ |
Kojto | 0:9331e373c138 | 166 | static TSIAnalogSlider *getInstance() { |
Kojto | 0:9331e373c138 | 167 | return _instance; |
Kojto | 0:9331e373c138 | 168 | } |
Kojto | 0:9331e373c138 | 169 | private: |
Kojto | 0:9331e373c138 | 170 | void sliderRead(void); |
Kojto | 0:9331e373c138 | 171 | void selfCalibration(void); |
Kojto | 0:9331e373c138 | 172 | void setSliderPercPosition(uint32_t elec_num, uint32_t position) { |
Kojto | 0:9331e373c138 | 173 | _percentage_position[elec_num] = position; |
Kojto | 0:9331e373c138 | 174 | } |
Kojto | 0:9331e373c138 | 175 | void setSliderDisPosition(uint32_t elec_num, uint32_t position) { |
Kojto | 0:9331e373c138 | 176 | _distance_position[elec_num] = position; |
Kojto | 0:9331e373c138 | 177 | } |
Kojto | 0:9331e373c138 | 178 | void setAbsolutePosition(uint32_t position) { |
Kojto | 0:9331e373c138 | 179 | _absolute_percentage_pos = position; |
Kojto | 0:9331e373c138 | 180 | } |
Kojto | 0:9331e373c138 | 181 | void setAbsoluteDistance(uint32_t distance) { |
Kojto | 0:9331e373c138 | 182 | _absolute_distance_pos = distance; |
Kojto | 0:9331e373c138 | 183 | } |
Kojto | 0:9331e373c138 | 184 | private: |
Kojto | 3:20ffa9b18488 | 185 | TSIElectrode _elec0; |
Kojto | 3:20ffa9b18488 | 186 | TSIElectrode _elec1; |
Kojto | 0:9331e373c138 | 187 | uint8_t _scan_in_progress; |
Kojto | 0:9331e373c138 | 188 | TSIElectrode* _current_elec; |
Kojto | 0:9331e373c138 | 189 | uint8_t _percentage_position[2]; |
Kojto | 0:9331e373c138 | 190 | uint8_t _distance_position[2]; |
Kojto | 0:9331e373c138 | 191 | uint32_t _absolute_percentage_pos; |
Kojto | 0:9331e373c138 | 192 | uint32_t _absolute_distance_pos; |
Kojto | 0:9331e373c138 | 193 | uint8_t _range; |
Kojto | 0:9331e373c138 | 194 | protected: |
Kojto | 0:9331e373c138 | 195 | static TSIAnalogSlider *_instance; |
Kojto | 0:9331e373c138 | 196 | }; |
Kojto | 0:9331e373c138 | 197 | |
Kojto | 0:9331e373c138 | 198 | #endif |