Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
tsi_sensor.h
00001 /* Freescale Semiconductor Inc. 00002 * 00003 * mbed Microcontroller Library 00004 * (c) Copyright 2009-2012 ARM Limited. 00005 * 00006 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00007 * and associated documentation files (the "Software"), to deal in the Software without 00008 * restriction, including without limitation the rights to use, copy, modify, merge, publish, 00009 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the 00010 * Software is furnished to do so, subject to the following conditions: 00011 * 00012 * The above copyright notice and this permission notice shall be included in all copies or 00013 * substantial portions of the Software. 00014 * 00015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00016 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00017 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00018 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00020 */ 00021 00022 /* This defines will be replaced by PinNames soon */ 00023 #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) 00024 #define ELEC0 9 00025 #define ELEC1 10 00026 #elif defined (TARGET_KL05Z) 00027 #define ELEC0 9 00028 #define ELEC1 8 00029 #else 00030 #error TARGET NOT DEFINED 00031 #endif 00032 00033 #ifndef TSISENSOR_H 00034 #define TSISENSOR_H 00035 00036 00037 /** 00038 * TSISensor example 00039 * 00040 * @code 00041 * #include "mbed.h" 00042 * #include "TSISensor.h" 00043 * 00044 * int main(void) { 00045 * DigitalOut led(LED_GREEN); 00046 * TSIElectrode elec0(9); 00047 * TSIElectrode elec1(10); 00048 * TSIAnalogSlider tsi(elec0, elec1, 40); 00049 * 00050 * while (true) { 00051 * printf("slider percentage: %f%\r\n", tsi.readPercentage()); 00052 * printf("slider distance: %dmm\r\n", tsi.readDistance()); 00053 * wait(1); 00054 * led = !led; 00055 * } 00056 * } 00057 * @endcode 00058 */ 00059 #define NO_TOUCH 0 00060 00061 /** TSI Electrode with simple data required for touch detection. 00062 */ 00063 class TSIElectrode { 00064 public: 00065 /** Initialize electrode. 00066 */ 00067 TSIElectrode(PinName pin) : _threshold(100) { 00068 _channel = getTSIChannel(pin); 00069 } 00070 00071 /** Initialize electrode. 00072 */ 00073 TSIElectrode(uint32_t tsi_channel) : _threshold(100) { 00074 _channel = (uint8_t)tsi_channel; 00075 } 00076 /** Set baseline. 00077 */ 00078 void setBaseline(uint32_t baseline) { 00079 _baseline = (uint16_t)baseline; 00080 } 00081 /** Set threshold. 00082 */ 00083 void setThreshold(uint32_t threshold) { 00084 _threshold = (uint16_t)threshold; 00085 } 00086 /** Set signal. 00087 */ 00088 void setSignal(uint32_t signal) { 00089 _signal = (uint16_t)signal; 00090 } 00091 /** Get baseline. 00092 */ 00093 uint32_t getBaseline() { 00094 return _baseline; 00095 } 00096 /** Get delta. 00097 */ 00098 uint32_t getDelta() { 00099 int32_t delta = getSignal() - getBaseline(); 00100 if (delta < 0) { 00101 return 0; 00102 } else { 00103 return delta; 00104 } 00105 } 00106 /** Get signal. 00107 */ 00108 uint32_t getSignal() { 00109 return _signal; 00110 } 00111 /** Get threshold. 00112 */ 00113 uint32_t getThreshold() { 00114 return _threshold; 00115 } 00116 /** Get channel. 00117 */ 00118 uint32_t getChannel() { 00119 return _channel; 00120 } 00121 /** Get TSI Channel for PinName. 00122 * 00123 * @returns TSI channel ID for use in constructor of TSIAnalogSlider and TSIElectrode. 00124 * @throws compile-time error if target is not supported, or runtime error if pin does not match any channel. 00125 */ 00126 static uint8_t getTSIChannel(PinName pin) { 00127 #if defined(TARGET_KL25Z) || defined(TARGET_KL46Z) || defined (TARGET_KL05Z) 00128 switch(pin) { 00129 //these are 00130 case PTA0: return 1; 00131 case PTA1: return 2; 00132 case PTA2: return 3; 00133 case PTA3: return 4; 00134 case PTA4: return 5; 00135 case PTB0: return 0; 00136 case PTB1: return 6; 00137 case PTB2: return 7; 00138 case PTB3: return 8; 00139 case PTB16: return 9; 00140 case PTB17: return 10; 00141 case PTB18: return 11; 00142 case PTB19: return 12; 00143 #if defined(TARGET_KL25Z) || defined(TARGET_KL46Z) 00144 case PTC0: return 13; 00145 case PTC1: return 14; 00146 case PTC2: return 15; 00147 #endif 00148 default: error("PinName provided to TSIElectrode::getTSIChannel() does not correspond to any known TSI channel."); 00149 } 00150 # else 00151 #error "Unknown target for TSIElectrode::getTSIChannel() - only supports KL25Z so far." 00152 # endif 00153 return 0xFF; //should never get here 00154 } 00155 00156 private: 00157 uint8_t _channel; 00158 uint16_t _signal; 00159 uint16_t _baseline; 00160 uint16_t _threshold; 00161 }; 00162 00163 /** Analog slider which consists of two electrodes. 00164 */ 00165 class TSIAnalogSlider { 00166 public: 00167 /** 00168 * 00169 * Initialize the TSI Touch Sensor with the given PinNames 00170 */ 00171 TSIAnalogSlider(PinName elec0, PinName elec1, uint32_t range); 00172 /** 00173 * Initialize the TSI Touch Sensor 00174 */ 00175 TSIAnalogSlider(uint32_t elec0, uint32_t elec1, uint32_t range); 00176 /** 00177 * Read Touch Sensor percentage value 00178 * 00179 * @returns percentage value between [0 ... 1] 00180 */ 00181 float readPercentage(); 00182 /** 00183 * Read Touch Sensor distance 00184 * 00185 * @returns distance in mm. The value is between [0 ... _range] 00186 */ 00187 uint32_t readDistance(); 00188 /** Get current electrode. 00189 */ 00190 TSIElectrode* getCurrentElectrode() { 00191 return _current_elec; 00192 } 00193 /** Set current electrode which is being measured. 00194 */ 00195 void setCurrentElectrode(TSIElectrode *elec){ 00196 _current_elec = elec; 00197 } 00198 /** Get next electrode. 00199 */ 00200 TSIElectrode* getNextElectrode(TSIElectrode* electrode) { 00201 if (electrode->getChannel() == _elec0.getChannel()) { 00202 return &_elec1; 00203 } else { 00204 return &_elec0; 00205 } 00206 } 00207 /** Return absolute distance position. 00208 */ 00209 uint32_t getAbsoluteDistance() { 00210 return _absolute_distance_pos; 00211 } 00212 /** Return absolute precentage position. 00213 */ 00214 uint32_t getAbsolutePosition() { 00215 return _absolute_percentage_pos; 00216 } 00217 /** Set value to the scan in progress flag. 00218 */ 00219 void setScan(uint32_t scan) { 00220 _scan_in_progress = scan; 00221 } 00222 /** Return instance to Analog slider. Used in tsi irq. 00223 */ 00224 static TSIAnalogSlider *getInstance() { 00225 return _instance; 00226 } 00227 private: 00228 void initObject(void); //shared constructor code 00229 void sliderRead(void); 00230 void selfCalibration(void); 00231 void setSliderPercPosition(uint32_t elec_num, uint32_t position) { 00232 _percentage_position[elec_num] = position; 00233 } 00234 void setSliderDisPosition(uint32_t elec_num, uint32_t position) { 00235 _distance_position[elec_num] = position; 00236 } 00237 void setAbsolutePosition(uint32_t position) { 00238 _absolute_percentage_pos = position; 00239 } 00240 void setAbsoluteDistance(uint32_t distance) { 00241 _absolute_distance_pos = distance; 00242 } 00243 private: 00244 TSIElectrode _elec0; 00245 TSIElectrode _elec1; 00246 uint8_t _scan_in_progress; 00247 TSIElectrode* _current_elec; 00248 uint8_t _percentage_position[2]; 00249 uint8_t _distance_position[2]; 00250 uint32_t _absolute_percentage_pos; 00251 uint32_t _absolute_distance_pos; 00252 uint8_t _range; 00253 protected: 00254 static TSIAnalogSlider *_instance; 00255 }; 00256 00257 #endif
Generated on Tue Aug 23 2022 23:44:59 by
1.7.2