Priyanka Pashte
/
ESD_Project_USBMouse
USBMouse
Revision 0:e062501cfe81, committed 2015-12-13
- Comitter:
- priyankapashte
- Date:
- Sun Dec 13 10:06:24 2015 +0000
- Commit message:
- USBDevice library modified
Changed in this revision
diff -r 000000000000 -r e062501cfe81 Accelerometer/Accelerometer.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Accelerometer/Accelerometer.cpp Sun Dec 13 10:06:24 2015 +0000 @@ -0,0 +1,55 @@ +#include "Accelerometer.h" + + +#define REG_CTRL_REG_1 0x2A +#define REG_OUT_X_MSB 0x01 +#define REG_OUT_Y_MSB 0x03 +#define REG_OUT_Z_MSB 0x05 + +Accelerometer::Accelerometer(PinName sda, PinName scl, int address) : i2c(sda, scl), addr(address) { + // activate the peripheral + uint8_t data[2] = {REG_CTRL_REG_1, 0x01}; + writeRegs(data, 2); +} + +Accelerometer::~Accelerometer() { } + + +float Accelerometer::Acc_X() { +//divide by 4096 b/c MMA output is 4096 counts per g so this f outputs accelorometer value formatted to g (gravity) + return (float(getAccAxis(REG_OUT_X_MSB))/4096.0); +} + +float Accelerometer::Acc_Y() { + return (float(getAccAxis(REG_OUT_Y_MSB))/4096.0); +} + +float Accelerometer::Acc_Z() { + return (float(getAccAxis(REG_OUT_Z_MSB))/4096.0); +} + + + +int16_t Accelerometer::getAccAxis(uint8_t addr) { + int16_t acc; + uint8_t result[2]; + readRegs(addr, result, 2); + + acc = (result[0] << 6) | (result[1] >> 2); + if (acc > 16383/2) + acc -= 16383; + + return acc; +} + +void Accelerometer::readRegs(int addr, uint8_t * data, int len) { + char t[1] = {addr}; + i2c.write(addr, t, 1, true); + i2c.read(addr, (char *)data, len); +} + + + +void Accelerometer::writeRegs(uint8_t * data, int len) { + i2c.write(addr, (char *)data, len); +}
diff -r 000000000000 -r e062501cfe81 Accelerometer/Accelerometer.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Accelerometer/Accelerometer.h Sun Dec 13 10:06:24 2015 +0000 @@ -0,0 +1,29 @@ +#include "mbed.h" + +class Accelerometer +{ +public: + Accelerometer(PinName sda, PinName scl, int address); // Accelerometer pin definition + ~Accelerometer(); // Accelerometer Destructor + + float Acc_X(); // Get X axis Acceleration + + float Acc_Y(); // Get Y axis Acceleration + + float Acc_Z(); // Get Z axis Acceleration + + +private: + I2C i2c; + int addr; + void readRegs(int addr, uint8_t * data, int len); + void writeRegs(uint8_t * data, int len); + int16_t getAccAxis(uint8_t addr); +}; + + + + + + +
diff -r 000000000000 -r e062501cfe81 TouchSensor/TouchSensor.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TouchSensor/TouchSensor.cpp Sun Dec 13 10:06:24 2015 +0000 @@ -0,0 +1,164 @@ +/* Freescale Semiconductor Inc. + * (c) Copyright 2004-2005 Freescale Semiconductor, Inc. + * (c) Copyright 2001-2004 Motorola, Inc. + * + * mbed Microcontroller Library + * (c) Copyright 2009-2012 ARM Limited. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + + /*The code written by Freescale Semiconductors is used for reference*/ + +#include "TouchSensor.h" + +#define SLIDER_LENGTH 40 //LENGTH in mm + + +static uint8_t total_electrode = 3; +static uint8_t elec_array[16]={9,10,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; +static uint16_t gu16TSICount[16]; +static uint16_t gu16Baseline[16]; +static uint16_t gu16Threshold[16]={100,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; +static uint16_t gu16Delta[16]; +static uint8_t ongoing_elec; +static uint8_t end_flag = 1; + +static uint8_t SliderPercentegePosition[2] = {0,0}; +static uint32_t AbsolutePercentegePosition = 0; + +static void tsi_irq(); + +TouchSensor::TouchSensor() { + SIM->SCGC5 |= SIM_SCGC5_PORTB_MASK; + SIM->SCGC5 |= SIM_SCGC5_TSI_MASK; + + TSI0->GENCS |= (TSI_GENCS_ESOR_MASK + | TSI_GENCS_MODE(0) + | TSI_GENCS_REFCHRG(4) + | TSI_GENCS_DVOLT(0) + | TSI_GENCS_EXTCHRG(7) + | TSI_GENCS_PS(4) + | TSI_GENCS_NSCN(11) + | TSI_GENCS_TSIIEN_MASK + | TSI_GENCS_STPE_MASK + ); + + TSI0->GENCS |= TSI_GENCS_TSIEN_MASK; + + NVIC_SetVector(TSI0_IRQn, (uint32_t)&tsi_irq); + NVIC_EnableIRQ(TSI0_IRQn); + + selfCalibration(); +} + + +void TouchSensor::selfCalibration(void) +{ + unsigned char cnt; + unsigned char trigger_backup; + + TSI0->GENCS |= TSI_GENCS_EOSF_MASK; // Clear End of Scan Flag + TSI0->GENCS &= ~TSI_GENCS_TSIEN_MASK; // Disable TSI module + + if(TSI0->GENCS & TSI_GENCS_STM_MASK) // Back-up TSI Trigger mode from Application + trigger_backup = 1; + else + trigger_backup = 0; + + TSI0->GENCS &= ~TSI_GENCS_STM_MASK; // Use SW trigger + TSI0->GENCS &= ~TSI_GENCS_TSIIEN_MASK; // Enable TSI interrupts + + TSI0->GENCS |= TSI_GENCS_TSIEN_MASK; // Enable TSI module + + for(cnt=0; cnt < total_electrode; cnt++) // Get Counts when Electrode not pressed + { + TSI0->DATA = ((elec_array[cnt] << TSI_DATA_TSICH_SHIFT) ); + TSI0->DATA |= TSI_DATA_SWTS_MASK; + while(!(TSI0->GENCS & TSI_GENCS_EOSF_MASK)); + TSI0->GENCS |= TSI_GENCS_EOSF_MASK; + gu16Baseline[cnt] = (TSI0->DATA & TSI_DATA_TSICNT_MASK); + } + + TSI0->GENCS &= ~TSI_GENCS_TSIEN_MASK; // Disable TSI module + TSI0->GENCS |= TSI_GENCS_TSIIEN_MASK; // Enale TSI interrupt + if(trigger_backup) // Restore trigger mode + TSI0->GENCS |= TSI_GENCS_STM_MASK; + else + TSI0->GENCS &= ~TSI_GENCS_STM_MASK; + + TSI0->GENCS |= TSI_GENCS_TSIEN_MASK; // Enable TSI module + + TSI0->DATA = ((elec_array[0]<<TSI_DATA_TSICH_SHIFT) ); + TSI0->DATA |= TSI_DATA_SWTS_MASK; +} + +void TouchSensor::sliderRead(void ) { + if(end_flag) { + end_flag = 0; + if((gu16Delta[0] > gu16Threshold[0])||(gu16Delta[1] > gu16Threshold[1])) { + SliderPercentegePosition[0] = (gu16Delta[0]*100)/(gu16Delta[0]+gu16Delta[1]); + SliderPercentegePosition[1] = (gu16Delta[1]*100)/(gu16Delta[0]+gu16Delta[1]); + AbsolutePercentegePosition = ((100 - SliderPercentegePosition[0]) + SliderPercentegePosition[1])/2; + } else { + SliderPercentegePosition[0] = 0; + SliderPercentegePosition[1] = 0; + AbsolutePercentegePosition = 0; + } + } +} + +float TouchSensor::readPercentage() { + sliderRead(); + return (float)AbsolutePercentegePosition/100.0; +} + + +uint16_t TouchSensor::readValue(uint8_t index) +{ + return gu16TSICount[index]; +} + +static void changeElectrode(void) +{ + int16_t u16temp_delta; + + gu16TSICount[ongoing_elec] = (TSI0->DATA & TSI_DATA_TSICNT_MASK); // Save Counts for current electrode + u16temp_delta = gu16TSICount[ongoing_elec] - gu16Baseline[ongoing_elec]; // Obtains Counts Delta from callibration reference + if(u16temp_delta < 0) + gu16Delta[ongoing_elec] = 0; + else + gu16Delta[ongoing_elec] = u16temp_delta; + + //Change Electrode to Scan + if(total_electrode > 1) + { + if((total_electrode-1) > ongoing_elec) + ongoing_elec++; + else + ongoing_elec = 0; + + TSI0->DATA = ((elec_array[ongoing_elec]<<TSI_DATA_TSICH_SHIFT) ); + TSI0->DATA |= TSI_DATA_SWTS_MASK; + } +} + +void tsi_irq(void) +{ + end_flag = 1; + TSI0->GENCS |= TSI_GENCS_EOSF_MASK; // Clear End of Scan Flag + changeElectrode(); +}
diff -r 000000000000 -r e062501cfe81 TouchSensor/TouchSensor.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TouchSensor/TouchSensor.h Sun Dec 13 10:06:24 2015 +0000 @@ -0,0 +1,13 @@ +#include "mbed.h" + +class TouchSensor { +public: + TouchSensor(); // Initialize Touch Sensor + float readPercentage(); // Read Touch values in terms of percentage + +private: + void sliderRead(void); + uint16_t readValue(uint8_t); + void selfCalibration(void); +}; +
diff -r 000000000000 -r e062501cfe81 USBDevice.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/USBDevice.lib Sun Dec 13 10:06:24 2015 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/priyankapashte/code/USBDevice/#78a4faee5b0f
diff -r 000000000000 -r e062501cfe81 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sun Dec 13 10:06:24 2015 +0000 @@ -0,0 +1,53 @@ +#include "mbed.h" +#include "USBMouse.h" +#include "Accelerometer.h" +#include "TouchSensor.h" + +#define MMA8451_I2C_ADDRESS (0x1d<<1) + +USBMouse mouse; +Accelerometer acc(PTE25, PTE24, MMA8451_I2C_ADDRESS); +TouchSensor tsi; + +typedef enum {NONE = 0, LEFT, RIGHT} Click; + +int main() { + int16_t x = 0, y = 0; + float t; + Click in_click = NONE; + + while (1) { + t = acc.Acc_X(); + t *= 10/1.5; + y = (int16_t) t; + + t = acc.Acc_Y(); + t *= 10/1.5; + x = - (int16_t) t; + + t = tsi.readPercentage(); + + if (in_click == NONE) { + if (t > 0.6) { + mouse.press(MOUSE_LEFT); + in_click = LEFT; + } else if (t > 0.1) { + mouse.press(MOUSE_RIGHT); + in_click = RIGHT; + } + } else if (in_click == LEFT) { + if (t <= 0.6) { + mouse.release(MOUSE_LEFT); + in_click = NONE; + } + } else { + if (t > 0.6 || t <= 0.1) { + mouse.release(MOUSE_RIGHT); + in_click = NONE; + } + } + + mouse.move(x, y); + wait(0.001); + } +} \ No newline at end of file
diff -r 000000000000 -r e062501cfe81 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sun Dec 13 10:06:24 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/165afa46840b \ No newline at end of file