Martin Sturm
/
Project
Diff: main.cpp
- Revision:
- 0:2fab16867d73
- Child:
- 1:44f0b9d5883b
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Nov 30 18:06:50 2011 +0000 @@ -0,0 +1,117 @@ +#include <mbed.h> +#include <mpr121.h> +#include "strings.h" +DigitalOut led1(LED1); +DigitalOut led2(LED2); +DigitalOut led3(LED3); +DigitalOut led4(LED4); + +InterruptIn interrupt0(p8); //interrupt pin for touch0 +InterruptIn interrupt1(p21); //interrupt for touch1 + +// Setup the i2c bus on pins 9 and 10 +I2C i2c2(p9, p10); // I2C is used by both touch sensors +I2C i2c(p28, p27); // I2C is used by both touch sensors + +Mpr121 touch1(&i2c, Mpr121::ADD_VSS);//setup touch1 address for vdd +Mpr121 touch0(&i2c2, Mpr121::ADD_VSS);//setup touch0 address for vss + + +//set serial port +Serial device(p13, p14); // tx, rx + + +Serial pc(USBTX, USBRX); + +unsigned int key_code0; +unsigned int key_code1; + + +// Key hit/release interrupt routine for touch 0 +void fallInterrupt0() { + key_code0=0; + char i=0; + int value=touch0.read(0x00); + value +=touch0.read(0x01)<<8; + // LED demo mod + i=0; + // puts key number out to LEDs for demo + for (i=0; i<12; i++) { + if (((value>>i)&0x01)==1) key_code0=i+1; + } + led4=key_code0 & 0x01; + led3=(key_code0>>1) & 0x01; + led2=(key_code0>>2) & 0x01; + led1=(key_code0>>3) & 0x01; + +} + + +//fall interrupt for touch1 +void fallInterrupt1() { + + key_code1=0; + char i=0; + int value=touch1.read(0x00); + value +=touch1.read(0x01)<<8; + // LED demo mod + i=0; + // puts key number out to LEDs for demo + for (i=0; i<12; i++) { + if (((value>>i)&0x01)==1) key_code1=i+1; + } + led4=key_code1 & 0x01; + led3=(key_code1>>1) & 0x01; + led2=(key_code1>>2) & 0x01; + led1=(key_code1>>3) & 0x01; +} + + + + + + + +int main() { + +pc.printf("Starting \r\n"); + + +char *key_buf;//buffer for data transmit to pc via serial +char frompc=0; + device.baud(9600); + interrupt0.fall(&fallInterrupt0); + interrupt0.mode(PullUp); + interrupt1.fall(&fallInterrupt1); + interrupt1.mode(PullUp); + + while (1) { + wait(10); + if(device.readable()) //checks if ebox sends request + frompc=device.getc(); + pc.printf(&frompc);//writes received data to pc for testing options + + switch(device.getc())//read character and check it + { + case '0': //if character is '0' send key data from touch 0 + sprintf(key_buf,"%d",key_code0); + device.printf(key_buf); + break; + + case '1': //if character is '1' send key data from touch 1 + sprintf(key_buf,"%d",key_code1); + device.printf(key_buf); + break; + + case 'A': //case for string value + sprintf(key_buf,"%d",Strings()); + device.printf(key_buf); + break; + + default: + break; + } + + pc.printf(key_buf);//writes received data to pc for testing options + } +} \ No newline at end of file