A library for Freescale MCU which contain TSI peripheral, just for Kinetis L version. Because they use "lighter" version of TSI peripheral.

Fork of tsi_sensor by Martin Kojtal

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers tsi_sensor.h Source File

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 #ifndef TSISENSOR_H
00023 #define TSISENSOR_H
00024 
00025 /**
00026 * TSISensor example
00027 *
00028 * @code
00029 * #include "mbed.h"
00030 * #include "TSISensor.h"
00031 *
00032 * int main(void) {
00033 *    DigitalOut led(LED_GREEN);
00034 *    TSIElectrode elec0(9);
00035 *    TSIElectrode elec1(10);
00036 *    TSIAnalogSlider tsi(elec0, elec1, 40);
00037 *
00038 *    while (true) {
00039 *        printf("slider percentage: %f%\r\n", tsi.readPercentage());
00040 *        printf("slider distance: %dmm\r\n", tsi.readDistance());
00041 *        wait(1);
00042 *        led = !led;
00043 *    }
00044 * }
00045 * @endcode
00046 */
00047 #define NO_TOUCH 0
00048 
00049 /** TSI Electrode with simple data required for touch detection.
00050  */
00051 class TSIElectrode {
00052 public:
00053     /** Initialize electrode.
00054      */
00055     TSIElectrode(PinName pin) : _threshold(100) {
00056         _channel = getTSIChannel(pin);
00057     }
00058 
00059     /** Initialize electrode.
00060      */
00061     TSIElectrode(uint32_t tsi_channel) : _threshold(100) {
00062         _channel = (uint8_t)tsi_channel;
00063     }
00064     /** Set baseline.
00065      */
00066     void setBaseline(uint32_t baseline) {
00067         _baseline = (uint16_t)baseline;
00068     }
00069     /** Set threshold.
00070      */
00071     void setThreshold(uint32_t threshold) {
00072         _threshold = (uint16_t)threshold;
00073     }
00074     /** Set signal.
00075      */
00076     void setSignal(uint32_t signal) {
00077         _signal = (uint16_t)signal;
00078     }
00079     /** Get baseline.
00080      */
00081     uint32_t getBaseline() {
00082         return _baseline;
00083     }
00084     /** Get delta.
00085      */
00086     uint32_t getDelta() {
00087         int32_t delta = getSignal() - getBaseline();
00088         if (delta < 0) {
00089             return 0;
00090         } else {
00091             return delta;
00092         }
00093     }
00094     /** Get signal.
00095      */
00096     uint32_t getSignal() {
00097         return _signal;
00098     }
00099     /** Get threshold.
00100      */
00101     uint32_t getThreshold() {
00102         return _threshold;
00103     }
00104     /** Get channel.
00105      */
00106     uint32_t getChannel() {
00107         return _channel;
00108     }
00109     /** Get TSI Channel for PinName.
00110      *
00111      * @returns TSI channel ID for use in constructor of TSIAnalogSlider and TSIElectrode.
00112      * @throws compile-time error if target is not supported, or runtime error if pin does not match any channel.
00113      */
00114     static uint8_t getTSIChannel(PinName pin) {
00115 #if   defined (TARGET_KL25Z) || (TARGET_KL46Z)      
00116         switch(pin) {
00117             //these are 
00118             case PTA0: return 1;
00119             case PTA1: return 2;
00120             case PTA2: return 3;
00121             case PTA3: return 4;
00122             case PTA4: return 5;
00123             case PTB0: return 0;
00124             case PTB1: return 6;
00125             case PTB2: return 7;
00126             case PTB3: return 8;
00127             case PTB16: return 9;
00128             case PTB17: return 10;
00129             case PTB18: return 11;
00130             case PTB19: return 12;
00131             case PTC0: return 13;
00132             case PTC1: return 14;
00133             default: error("PinName provided to TSIElectrode::getTSIChannel() does not correspond to any known TSI channel.");
00134         }
00135 # else
00136     #error "Unknown target for TSIElectrode::getTSIChannel() - only supports KL25Z so far."
00137 # endif   
00138         return 0xFF; //should never get here
00139     }
00140     
00141 private:
00142     uint8_t  _channel;
00143     uint16_t _signal;
00144     uint16_t _baseline;
00145     uint16_t _threshold;
00146 };
00147 
00148 /** Analog slider which consists of two electrodes.
00149  */
00150 class TSIAnalogSlider {
00151 public:
00152     /**
00153      *
00154      *   Initialize the TSI Touch Sensor with the given PinNames
00155      */
00156     TSIAnalogSlider(PinName elec0, PinName elec1, uint32_t range);
00157     /**
00158      *   Initialize the TSI Touch Sensor
00159      */
00160     TSIAnalogSlider(uint32_t elec0, uint32_t elec1, uint32_t range);
00161     /**
00162      * Read Touch Sensor percentage value
00163      *
00164      * @returns percentage value between [0 ... 1]
00165      */
00166     float readPercentage();
00167     /**
00168      * Read Touch Sensor distance
00169      *
00170      * @returns distance in mm. The value is between [0 ... _range]
00171      */
00172     uint32_t readDistance();
00173     /** Get current electrode.
00174      */
00175     TSIElectrode* getCurrentElectrode() {
00176         return _current_elec;
00177     }
00178     /** Set current electrode which is being measured.
00179      */
00180     void setCurrentElectrode(TSIElectrode *elec){
00181         _current_elec = elec;
00182     }
00183     /** Get next electrode.
00184      */
00185     TSIElectrode* getNextElectrode(TSIElectrode* electrode) {
00186         if (electrode->getChannel() == _elec0.getChannel()) {
00187             return &_elec1;
00188         } else {
00189             return &_elec0;
00190         }
00191     }
00192     /** Return absolute distance position.
00193      */
00194     uint32_t getAbsoluteDistance() {
00195         return  _absolute_distance_pos;
00196     }
00197     /** Return absolute precentage position.
00198      */
00199     uint32_t getAbsolutePosition() {
00200         return _absolute_percentage_pos;
00201     }
00202     /** Set value to the scan in progress flag.
00203      */
00204     void setScan(uint32_t scan) {
00205         _scan_in_progress = scan;
00206     }
00207     /** Return instance to Analog slider. Used in tsi irq.
00208      */
00209     static TSIAnalogSlider *getInstance() {
00210         return _instance;
00211     }
00212 private:
00213     void initObject(void); //shared constructor code
00214     void sliderRead(void);
00215     void selfCalibration(void);
00216     void setSliderPercPosition(uint32_t elec_num, uint32_t position) {
00217         _percentage_position[elec_num] = position;
00218     }
00219     void setSliderDisPosition(uint32_t elec_num, uint32_t position) {
00220         _distance_position[elec_num] = position;
00221     }
00222     void setAbsolutePosition(uint32_t position) {
00223         _absolute_percentage_pos = position;
00224     }
00225     void setAbsoluteDistance(uint32_t distance) {
00226         _absolute_distance_pos = distance;
00227     }
00228 private:
00229     TSIElectrode  _elec0;
00230     TSIElectrode  _elec1;
00231     uint8_t       _scan_in_progress;
00232     TSIElectrode* _current_elec;
00233     uint8_t       _percentage_position[2];
00234     uint8_t       _distance_position[2];
00235     uint32_t      _absolute_percentage_pos;
00236     uint32_t      _absolute_distance_pos;
00237     uint8_t       _range;
00238 protected:
00239     static TSIAnalogSlider *_instance;
00240 };
00241 
00242 #endif