USBMouse

Dependencies:   USBDevice mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TouchSensor.cpp Source File

TouchSensor.cpp

00001 /* Freescale Semiconductor Inc.
00002  * (c) Copyright 2004-2005 Freescale Semiconductor, Inc.
00003  * (c) Copyright 2001-2004 Motorola, Inc. 
00004  *
00005  * mbed Microcontroller Library
00006  * (c) Copyright 2009-2012 ARM Limited.
00007  *
00008  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00009  * and associated documentation files (the "Software"), to deal in the Software without
00010  * restriction, including without limitation the rights to use, copy, modify, merge, publish,
00011  * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
00012  * Software is furnished to do so, subject to the following conditions:
00013  *
00014  * The above copyright notice and this permission notice shall be included in all copies or
00015  * substantial portions of the Software.
00016  *
00017  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00018  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00019  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00020  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00021  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00022  */
00023  
00024  /*The code written by Freescale Semiconductors is used for reference*/
00025  
00026 #include "TouchSensor.h"
00027 
00028 #define SLIDER_LENGTH           40 //LENGTH in mm
00029 
00030 
00031 static uint8_t total_electrode = 3;
00032 static uint8_t elec_array[16]={9,10,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
00033 static uint16_t gu16TSICount[16];
00034 static uint16_t gu16Baseline[16];
00035 static uint16_t gu16Threshold[16]={100,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
00036 static uint16_t gu16Delta[16];
00037 static uint8_t ongoing_elec;
00038 static uint8_t end_flag = 1;
00039 
00040 static uint8_t SliderPercentegePosition[2] = {0,0};
00041 static uint32_t AbsolutePercentegePosition = 0;
00042 
00043 static void tsi_irq();
00044 
00045 TouchSensor::TouchSensor() {
00046     SIM->SCGC5 |= SIM_SCGC5_PORTB_MASK;
00047     SIM->SCGC5 |= SIM_SCGC5_TSI_MASK;
00048 
00049     TSI0->GENCS |= (TSI_GENCS_ESOR_MASK
00050                    | TSI_GENCS_MODE(0)
00051                    | TSI_GENCS_REFCHRG(4)
00052                    | TSI_GENCS_DVOLT(0)
00053                    | TSI_GENCS_EXTCHRG(7)
00054                    | TSI_GENCS_PS(4)
00055                    | TSI_GENCS_NSCN(11)
00056                    | TSI_GENCS_TSIIEN_MASK
00057                    | TSI_GENCS_STPE_MASK
00058                    );
00059 
00060     TSI0->GENCS |= TSI_GENCS_TSIEN_MASK;
00061 
00062     NVIC_SetVector(TSI0_IRQn, (uint32_t)&tsi_irq);
00063     NVIC_EnableIRQ(TSI0_IRQn);
00064 
00065     selfCalibration();
00066 }
00067 
00068 
00069 void TouchSensor::selfCalibration(void)
00070 {
00071     unsigned char cnt;
00072     unsigned char trigger_backup;
00073 
00074     TSI0->GENCS |= TSI_GENCS_EOSF_MASK;      // Clear End of Scan Flag
00075     TSI0->GENCS &= ~TSI_GENCS_TSIEN_MASK;    // Disable TSI module
00076 
00077     if(TSI0->GENCS & TSI_GENCS_STM_MASK)     // Back-up TSI Trigger mode from Application
00078         trigger_backup = 1;
00079     else
00080         trigger_backup = 0;
00081 
00082     TSI0->GENCS &= ~TSI_GENCS_STM_MASK;      // Use SW trigger
00083     TSI0->GENCS &= ~TSI_GENCS_TSIIEN_MASK;    // Enable TSI interrupts
00084 
00085     TSI0->GENCS |= TSI_GENCS_TSIEN_MASK;     // Enable TSI module
00086 
00087     for(cnt=0; cnt < total_electrode; cnt++)  // Get Counts when Electrode not pressed
00088     {
00089         TSI0->DATA = ((elec_array[cnt] << TSI_DATA_TSICH_SHIFT) );
00090         TSI0->DATA |= TSI_DATA_SWTS_MASK;
00091         while(!(TSI0->GENCS & TSI_GENCS_EOSF_MASK));
00092         TSI0->GENCS |= TSI_GENCS_EOSF_MASK;
00093         gu16Baseline[cnt] = (TSI0->DATA & TSI_DATA_TSICNT_MASK);
00094     }
00095 
00096     TSI0->GENCS &= ~TSI_GENCS_TSIEN_MASK;    // Disable TSI module
00097     TSI0->GENCS |= TSI_GENCS_TSIIEN_MASK;     // Enale TSI interrupt
00098     if(trigger_backup)                      // Restore trigger mode
00099         TSI0->GENCS |= TSI_GENCS_STM_MASK;
00100     else
00101         TSI0->GENCS &= ~TSI_GENCS_STM_MASK;
00102 
00103     TSI0->GENCS |= TSI_GENCS_TSIEN_MASK;     // Enable TSI module
00104 
00105     TSI0->DATA = ((elec_array[0]<<TSI_DATA_TSICH_SHIFT) );
00106     TSI0->DATA |= TSI_DATA_SWTS_MASK;
00107 }
00108 
00109 void TouchSensor::sliderRead(void ) {
00110     if(end_flag) {
00111         end_flag = 0;
00112         if((gu16Delta[0] > gu16Threshold[0])||(gu16Delta[1] > gu16Threshold[1])) {
00113             SliderPercentegePosition[0] = (gu16Delta[0]*100)/(gu16Delta[0]+gu16Delta[1]);
00114             SliderPercentegePosition[1] = (gu16Delta[1]*100)/(gu16Delta[0]+gu16Delta[1]);
00115             AbsolutePercentegePosition = ((100 - SliderPercentegePosition[0]) + SliderPercentegePosition[1])/2;
00116          } else {
00117             SliderPercentegePosition[0] = 0;
00118             SliderPercentegePosition[1] = 0;
00119             AbsolutePercentegePosition = 0;
00120          }
00121     }
00122 }
00123 
00124 float TouchSensor::readPercentage() {
00125     sliderRead();
00126     return (float)AbsolutePercentegePosition/100.0;
00127 }
00128 
00129 
00130 uint16_t TouchSensor::readValue(uint8_t index)
00131 {
00132     return gu16TSICount[index];
00133 }
00134 
00135 static void changeElectrode(void)
00136 {
00137     int16_t u16temp_delta;
00138 
00139     gu16TSICount[ongoing_elec] = (TSI0->DATA & TSI_DATA_TSICNT_MASK);          // Save Counts for current electrode
00140     u16temp_delta = gu16TSICount[ongoing_elec] - gu16Baseline[ongoing_elec];  // Obtains Counts Delta from callibration reference
00141     if(u16temp_delta < 0)
00142         gu16Delta[ongoing_elec] = 0;
00143     else
00144         gu16Delta[ongoing_elec] = u16temp_delta;
00145 
00146     //Change Electrode to Scan
00147     if(total_electrode > 1)  
00148     {
00149         if((total_electrode-1) > ongoing_elec)
00150             ongoing_elec++;
00151         else
00152             ongoing_elec = 0;
00153 
00154         TSI0->DATA = ((elec_array[ongoing_elec]<<TSI_DATA_TSICH_SHIFT) );
00155         TSI0->DATA |= TSI_DATA_SWTS_MASK;
00156     }
00157 }
00158 
00159 void tsi_irq(void)
00160 {
00161     end_flag = 1;
00162     TSI0->GENCS |= TSI_GENCS_EOSF_MASK; // Clear End of Scan Flag
00163     changeElectrode();
00164 }