Priyanka Pashte
/
ESD_Project_USBMouse
USBMouse
TouchSensor/TouchSensor.cpp@0:e062501cfe81, 2015-12-13 (annotated)
- Committer:
- priyankapashte
- Date:
- Sun Dec 13 10:06:24 2015 +0000
- Revision:
- 0:e062501cfe81
USBDevice library modified
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
priyankapashte | 0:e062501cfe81 | 1 | /* Freescale Semiconductor Inc. |
priyankapashte | 0:e062501cfe81 | 2 | * (c) Copyright 2004-2005 Freescale Semiconductor, Inc. |
priyankapashte | 0:e062501cfe81 | 3 | * (c) Copyright 2001-2004 Motorola, Inc. |
priyankapashte | 0:e062501cfe81 | 4 | * |
priyankapashte | 0:e062501cfe81 | 5 | * mbed Microcontroller Library |
priyankapashte | 0:e062501cfe81 | 6 | * (c) Copyright 2009-2012 ARM Limited. |
priyankapashte | 0:e062501cfe81 | 7 | * |
priyankapashte | 0:e062501cfe81 | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
priyankapashte | 0:e062501cfe81 | 9 | * and associated documentation files (the "Software"), to deal in the Software without |
priyankapashte | 0:e062501cfe81 | 10 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
priyankapashte | 0:e062501cfe81 | 11 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
priyankapashte | 0:e062501cfe81 | 12 | * Software is furnished to do so, subject to the following conditions: |
priyankapashte | 0:e062501cfe81 | 13 | * |
priyankapashte | 0:e062501cfe81 | 14 | * The above copyright notice and this permission notice shall be included in all copies or |
priyankapashte | 0:e062501cfe81 | 15 | * substantial portions of the Software. |
priyankapashte | 0:e062501cfe81 | 16 | * |
priyankapashte | 0:e062501cfe81 | 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
priyankapashte | 0:e062501cfe81 | 18 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
priyankapashte | 0:e062501cfe81 | 19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
priyankapashte | 0:e062501cfe81 | 20 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
priyankapashte | 0:e062501cfe81 | 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
priyankapashte | 0:e062501cfe81 | 22 | */ |
priyankapashte | 0:e062501cfe81 | 23 | |
priyankapashte | 0:e062501cfe81 | 24 | /*The code written by Freescale Semiconductors is used for reference*/ |
priyankapashte | 0:e062501cfe81 | 25 | |
priyankapashte | 0:e062501cfe81 | 26 | #include "TouchSensor.h" |
priyankapashte | 0:e062501cfe81 | 27 | |
priyankapashte | 0:e062501cfe81 | 28 | #define SLIDER_LENGTH 40 //LENGTH in mm |
priyankapashte | 0:e062501cfe81 | 29 | |
priyankapashte | 0:e062501cfe81 | 30 | |
priyankapashte | 0:e062501cfe81 | 31 | static uint8_t total_electrode = 3; |
priyankapashte | 0:e062501cfe81 | 32 | static uint8_t elec_array[16]={9,10,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; |
priyankapashte | 0:e062501cfe81 | 33 | static uint16_t gu16TSICount[16]; |
priyankapashte | 0:e062501cfe81 | 34 | static uint16_t gu16Baseline[16]; |
priyankapashte | 0:e062501cfe81 | 35 | static uint16_t gu16Threshold[16]={100,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; |
priyankapashte | 0:e062501cfe81 | 36 | static uint16_t gu16Delta[16]; |
priyankapashte | 0:e062501cfe81 | 37 | static uint8_t ongoing_elec; |
priyankapashte | 0:e062501cfe81 | 38 | static uint8_t end_flag = 1; |
priyankapashte | 0:e062501cfe81 | 39 | |
priyankapashte | 0:e062501cfe81 | 40 | static uint8_t SliderPercentegePosition[2] = {0,0}; |
priyankapashte | 0:e062501cfe81 | 41 | static uint32_t AbsolutePercentegePosition = 0; |
priyankapashte | 0:e062501cfe81 | 42 | |
priyankapashte | 0:e062501cfe81 | 43 | static void tsi_irq(); |
priyankapashte | 0:e062501cfe81 | 44 | |
priyankapashte | 0:e062501cfe81 | 45 | TouchSensor::TouchSensor() { |
priyankapashte | 0:e062501cfe81 | 46 | SIM->SCGC5 |= SIM_SCGC5_PORTB_MASK; |
priyankapashte | 0:e062501cfe81 | 47 | SIM->SCGC5 |= SIM_SCGC5_TSI_MASK; |
priyankapashte | 0:e062501cfe81 | 48 | |
priyankapashte | 0:e062501cfe81 | 49 | TSI0->GENCS |= (TSI_GENCS_ESOR_MASK |
priyankapashte | 0:e062501cfe81 | 50 | | TSI_GENCS_MODE(0) |
priyankapashte | 0:e062501cfe81 | 51 | | TSI_GENCS_REFCHRG(4) |
priyankapashte | 0:e062501cfe81 | 52 | | TSI_GENCS_DVOLT(0) |
priyankapashte | 0:e062501cfe81 | 53 | | TSI_GENCS_EXTCHRG(7) |
priyankapashte | 0:e062501cfe81 | 54 | | TSI_GENCS_PS(4) |
priyankapashte | 0:e062501cfe81 | 55 | | TSI_GENCS_NSCN(11) |
priyankapashte | 0:e062501cfe81 | 56 | | TSI_GENCS_TSIIEN_MASK |
priyankapashte | 0:e062501cfe81 | 57 | | TSI_GENCS_STPE_MASK |
priyankapashte | 0:e062501cfe81 | 58 | ); |
priyankapashte | 0:e062501cfe81 | 59 | |
priyankapashte | 0:e062501cfe81 | 60 | TSI0->GENCS |= TSI_GENCS_TSIEN_MASK; |
priyankapashte | 0:e062501cfe81 | 61 | |
priyankapashte | 0:e062501cfe81 | 62 | NVIC_SetVector(TSI0_IRQn, (uint32_t)&tsi_irq); |
priyankapashte | 0:e062501cfe81 | 63 | NVIC_EnableIRQ(TSI0_IRQn); |
priyankapashte | 0:e062501cfe81 | 64 | |
priyankapashte | 0:e062501cfe81 | 65 | selfCalibration(); |
priyankapashte | 0:e062501cfe81 | 66 | } |
priyankapashte | 0:e062501cfe81 | 67 | |
priyankapashte | 0:e062501cfe81 | 68 | |
priyankapashte | 0:e062501cfe81 | 69 | void TouchSensor::selfCalibration(void) |
priyankapashte | 0:e062501cfe81 | 70 | { |
priyankapashte | 0:e062501cfe81 | 71 | unsigned char cnt; |
priyankapashte | 0:e062501cfe81 | 72 | unsigned char trigger_backup; |
priyankapashte | 0:e062501cfe81 | 73 | |
priyankapashte | 0:e062501cfe81 | 74 | TSI0->GENCS |= TSI_GENCS_EOSF_MASK; // Clear End of Scan Flag |
priyankapashte | 0:e062501cfe81 | 75 | TSI0->GENCS &= ~TSI_GENCS_TSIEN_MASK; // Disable TSI module |
priyankapashte | 0:e062501cfe81 | 76 | |
priyankapashte | 0:e062501cfe81 | 77 | if(TSI0->GENCS & TSI_GENCS_STM_MASK) // Back-up TSI Trigger mode from Application |
priyankapashte | 0:e062501cfe81 | 78 | trigger_backup = 1; |
priyankapashte | 0:e062501cfe81 | 79 | else |
priyankapashte | 0:e062501cfe81 | 80 | trigger_backup = 0; |
priyankapashte | 0:e062501cfe81 | 81 | |
priyankapashte | 0:e062501cfe81 | 82 | TSI0->GENCS &= ~TSI_GENCS_STM_MASK; // Use SW trigger |
priyankapashte | 0:e062501cfe81 | 83 | TSI0->GENCS &= ~TSI_GENCS_TSIIEN_MASK; // Enable TSI interrupts |
priyankapashte | 0:e062501cfe81 | 84 | |
priyankapashte | 0:e062501cfe81 | 85 | TSI0->GENCS |= TSI_GENCS_TSIEN_MASK; // Enable TSI module |
priyankapashte | 0:e062501cfe81 | 86 | |
priyankapashte | 0:e062501cfe81 | 87 | for(cnt=0; cnt < total_electrode; cnt++) // Get Counts when Electrode not pressed |
priyankapashte | 0:e062501cfe81 | 88 | { |
priyankapashte | 0:e062501cfe81 | 89 | TSI0->DATA = ((elec_array[cnt] << TSI_DATA_TSICH_SHIFT) ); |
priyankapashte | 0:e062501cfe81 | 90 | TSI0->DATA |= TSI_DATA_SWTS_MASK; |
priyankapashte | 0:e062501cfe81 | 91 | while(!(TSI0->GENCS & TSI_GENCS_EOSF_MASK)); |
priyankapashte | 0:e062501cfe81 | 92 | TSI0->GENCS |= TSI_GENCS_EOSF_MASK; |
priyankapashte | 0:e062501cfe81 | 93 | gu16Baseline[cnt] = (TSI0->DATA & TSI_DATA_TSICNT_MASK); |
priyankapashte | 0:e062501cfe81 | 94 | } |
priyankapashte | 0:e062501cfe81 | 95 | |
priyankapashte | 0:e062501cfe81 | 96 | TSI0->GENCS &= ~TSI_GENCS_TSIEN_MASK; // Disable TSI module |
priyankapashte | 0:e062501cfe81 | 97 | TSI0->GENCS |= TSI_GENCS_TSIIEN_MASK; // Enale TSI interrupt |
priyankapashte | 0:e062501cfe81 | 98 | if(trigger_backup) // Restore trigger mode |
priyankapashte | 0:e062501cfe81 | 99 | TSI0->GENCS |= TSI_GENCS_STM_MASK; |
priyankapashte | 0:e062501cfe81 | 100 | else |
priyankapashte | 0:e062501cfe81 | 101 | TSI0->GENCS &= ~TSI_GENCS_STM_MASK; |
priyankapashte | 0:e062501cfe81 | 102 | |
priyankapashte | 0:e062501cfe81 | 103 | TSI0->GENCS |= TSI_GENCS_TSIEN_MASK; // Enable TSI module |
priyankapashte | 0:e062501cfe81 | 104 | |
priyankapashte | 0:e062501cfe81 | 105 | TSI0->DATA = ((elec_array[0]<<TSI_DATA_TSICH_SHIFT) ); |
priyankapashte | 0:e062501cfe81 | 106 | TSI0->DATA |= TSI_DATA_SWTS_MASK; |
priyankapashte | 0:e062501cfe81 | 107 | } |
priyankapashte | 0:e062501cfe81 | 108 | |
priyankapashte | 0:e062501cfe81 | 109 | void TouchSensor::sliderRead(void ) { |
priyankapashte | 0:e062501cfe81 | 110 | if(end_flag) { |
priyankapashte | 0:e062501cfe81 | 111 | end_flag = 0; |
priyankapashte | 0:e062501cfe81 | 112 | if((gu16Delta[0] > gu16Threshold[0])||(gu16Delta[1] > gu16Threshold[1])) { |
priyankapashte | 0:e062501cfe81 | 113 | SliderPercentegePosition[0] = (gu16Delta[0]*100)/(gu16Delta[0]+gu16Delta[1]); |
priyankapashte | 0:e062501cfe81 | 114 | SliderPercentegePosition[1] = (gu16Delta[1]*100)/(gu16Delta[0]+gu16Delta[1]); |
priyankapashte | 0:e062501cfe81 | 115 | AbsolutePercentegePosition = ((100 - SliderPercentegePosition[0]) + SliderPercentegePosition[1])/2; |
priyankapashte | 0:e062501cfe81 | 116 | } else { |
priyankapashte | 0:e062501cfe81 | 117 | SliderPercentegePosition[0] = 0; |
priyankapashte | 0:e062501cfe81 | 118 | SliderPercentegePosition[1] = 0; |
priyankapashte | 0:e062501cfe81 | 119 | AbsolutePercentegePosition = 0; |
priyankapashte | 0:e062501cfe81 | 120 | } |
priyankapashte | 0:e062501cfe81 | 121 | } |
priyankapashte | 0:e062501cfe81 | 122 | } |
priyankapashte | 0:e062501cfe81 | 123 | |
priyankapashte | 0:e062501cfe81 | 124 | float TouchSensor::readPercentage() { |
priyankapashte | 0:e062501cfe81 | 125 | sliderRead(); |
priyankapashte | 0:e062501cfe81 | 126 | return (float)AbsolutePercentegePosition/100.0; |
priyankapashte | 0:e062501cfe81 | 127 | } |
priyankapashte | 0:e062501cfe81 | 128 | |
priyankapashte | 0:e062501cfe81 | 129 | |
priyankapashte | 0:e062501cfe81 | 130 | uint16_t TouchSensor::readValue(uint8_t index) |
priyankapashte | 0:e062501cfe81 | 131 | { |
priyankapashte | 0:e062501cfe81 | 132 | return gu16TSICount[index]; |
priyankapashte | 0:e062501cfe81 | 133 | } |
priyankapashte | 0:e062501cfe81 | 134 | |
priyankapashte | 0:e062501cfe81 | 135 | static void changeElectrode(void) |
priyankapashte | 0:e062501cfe81 | 136 | { |
priyankapashte | 0:e062501cfe81 | 137 | int16_t u16temp_delta; |
priyankapashte | 0:e062501cfe81 | 138 | |
priyankapashte | 0:e062501cfe81 | 139 | gu16TSICount[ongoing_elec] = (TSI0->DATA & TSI_DATA_TSICNT_MASK); // Save Counts for current electrode |
priyankapashte | 0:e062501cfe81 | 140 | u16temp_delta = gu16TSICount[ongoing_elec] - gu16Baseline[ongoing_elec]; // Obtains Counts Delta from callibration reference |
priyankapashte | 0:e062501cfe81 | 141 | if(u16temp_delta < 0) |
priyankapashte | 0:e062501cfe81 | 142 | gu16Delta[ongoing_elec] = 0; |
priyankapashte | 0:e062501cfe81 | 143 | else |
priyankapashte | 0:e062501cfe81 | 144 | gu16Delta[ongoing_elec] = u16temp_delta; |
priyankapashte | 0:e062501cfe81 | 145 | |
priyankapashte | 0:e062501cfe81 | 146 | //Change Electrode to Scan |
priyankapashte | 0:e062501cfe81 | 147 | if(total_electrode > 1) |
priyankapashte | 0:e062501cfe81 | 148 | { |
priyankapashte | 0:e062501cfe81 | 149 | if((total_electrode-1) > ongoing_elec) |
priyankapashte | 0:e062501cfe81 | 150 | ongoing_elec++; |
priyankapashte | 0:e062501cfe81 | 151 | else |
priyankapashte | 0:e062501cfe81 | 152 | ongoing_elec = 0; |
priyankapashte | 0:e062501cfe81 | 153 | |
priyankapashte | 0:e062501cfe81 | 154 | TSI0->DATA = ((elec_array[ongoing_elec]<<TSI_DATA_TSICH_SHIFT) ); |
priyankapashte | 0:e062501cfe81 | 155 | TSI0->DATA |= TSI_DATA_SWTS_MASK; |
priyankapashte | 0:e062501cfe81 | 156 | } |
priyankapashte | 0:e062501cfe81 | 157 | } |
priyankapashte | 0:e062501cfe81 | 158 | |
priyankapashte | 0:e062501cfe81 | 159 | void tsi_irq(void) |
priyankapashte | 0:e062501cfe81 | 160 | { |
priyankapashte | 0:e062501cfe81 | 161 | end_flag = 1; |
priyankapashte | 0:e062501cfe81 | 162 | TSI0->GENCS |= TSI_GENCS_EOSF_MASK; // Clear End of Scan Flag |
priyankapashte | 0:e062501cfe81 | 163 | changeElectrode(); |
priyankapashte | 0:e062501cfe81 | 164 | } |