ee202hw1
Dependencies: Useless SB TSI mbed
Fork of Final_Homework1 by
Revision 1:a00bb47c1028, committed 2014-02-15
- Comitter:
- bmdlh
- Date:
- Sat Feb 15 06:10:17 2014 +0000
- Parent:
- 0:c8a7071d8692
- Commit message:
- ee202hw1(main function)
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r c8a7071d8692 -r a00bb47c1028 main.cpp --- a/main.cpp Sat Feb 08 22:23:55 2014 +0000 +++ b/main.cpp Sat Feb 15 06:10:17 2014 +0000 @@ -1,26 +1,44 @@ -//KL46Z try out #include "mbed.h" #include "TSISensor.h" #include "MMA8451Q.h" #include "MAG3110.h" -#include <cstdlib> -#include <iostream> -#define OFF 0; -#define ON 1; + +#define OFF 0 +#define ON 1 Serial pc(USBTX, USBRX); -TSISensor tsi; -MMA8451Q acc51(PTE25, PTE24, 0x1D<<1); -MAG3110 mag(PTE25, PTE24); - PwmOut rled(PTE29); PwmOut gled(PTD5); + +MMA8451Q acc(PTE25, PTE24,0x1d<<1); +MAG3110 mag(PTE25, PTE24); AnalogIn lightSensor(PTE22); -DigitalIn SW12(PTC3); -DigitalIn SW32(PTC12); -//AnalogOut lcd03(PTB23); -//PwmOut lcd07(PTB7); +TSISensor tsi; +AnalogIn sinwave(PTB1); + +struct +{ + int mag : 1; + int acc : 1; + int light : 1; + int touch : 1; + int sine : 1; +} status; + +Timer timer_mag; +Timer timer_acc; +Timer timer_light; +Timer timer_touch; +Timer timer_sin; + +int threshold1 = 32767; +int threshold2 = 32767; +int threshold3 = 32767; +int threshold4 = 32767; +int threshold5 = 32767; + +int START = OFF; void calXY() //magnetometer calibration: finding max and min of X, Y axis { @@ -79,44 +97,188 @@ gled = OFF; wait(1.0); } +void send4bytes(char label,void* x){ + char *p = (char *)x; + pc.putc(label); + pc.putc(*p); + pc.putc(*(p+1)); + pc.putc(*(p+2)); + pc.putc(*(p+3)); +} +void mag_send(){ + int x = 0, y = 0, z = 0; + float h = mag.getHeading(); + mag.getValues(&x, &y, &z); + __disable_irq(); + send4bytes(0x00,&x); + send4bytes(0x01,&y); + send4bytes(0x02,&z); + send4bytes(0x03,&h); + __enable_irq(); +} +void accl_send(){ + float x = acc.getAccX(); + float y = acc.getAccY(); + float z = acc.getAccZ(); + __disable_irq(); + send4bytes(0x04,&x); + send4bytes(0x05,&y); + send4bytes(0x06,&z); + __enable_irq(); +} -int main() -{ - using namespace std; - - float onTime = 1.0; - float offTime = 0.0; - float holdTime = 3.0; - bool on = true; - bool off = false; - int magX = 0, magY = 0, magZ = 0; - //lcd03.write(0.33); - //lcd07.write(0.25); - //lcd07.period_ms(10); - pc.baud(9600); +void light_send(){ + float cur_value = lightSensor; + __disable_irq(); + send4bytes(0x07,&cur_value); + __enable_irq(); +} + void touch_send(){ + float cur_value = tsi.readPercentage();; + __disable_irq(); + send4bytes(0x08,&cur_value); + __enable_irq(); +} +void sin_send(){ + float cur_value = sinwave.read();; + __disable_irq(); + send4bytes(0x09,&cur_value); + __enable_irq(); +} + +void configuration(){ + rled = OFF; + char command = pc.getc(); + command = pc.getc(); + float sr; + char temp[4]; + if (command == 0x01){ // magnetometer + status.mag = ON; + temp[0] = pc.getc(); + temp[1] = pc.getc(); + temp[2] = pc.getc(); + temp[3] = pc.getc(); + sr = *(float*)temp; + threshold1 =(int) 1/sr *1000; + timer_mag.reset(); + } + else{ + status.mag = OFF; + timer_mag.stop(); + } + command = pc.getc(); + if (command == 0x01){ // accelerometer + status.acc = ON; + temp[0] = pc.getc(); + temp[1] = pc.getc(); + temp[2] = pc.getc(); + temp[3] = pc.getc(); + sr = *(float*)temp; + threshold2 =(int) 1/sr *1000; + timer_acc.reset(); + } + else{ + status.acc = OFF; + timer_acc.stop(); + } + command = pc.getc(); + if (command == 0x01){ // light sensor + status.light = ON; + temp[0] = pc.getc(); + temp[1] = pc.getc(); + temp[2] = pc.getc(); + temp[3] = pc.getc(); + sr = *(float*)temp; + threshold3 =(int) 1/sr *1000; + timer_light.reset(); + } + else{ + status.light = OFF; + timer_light.stop(); + } + command = pc.getc(); + if (command == 0x01){ // touch sensor + status.touch = ON; + temp[0] = pc.getc(); + temp[1] = pc.getc(); + temp[2] = pc.getc(); + temp[3] = pc.getc(); + sr = *(float*)temp; + threshold4 =(int) 1/sr *1000; + timer_touch.reset(); + } + else{ + status.touch = OFF; + timer_touch.stop(); + } + command = pc.getc(); + if (command == 0x01){ // sinewave receiver + status.sine = ON; + temp[0] = pc.getc(); + temp[1] = pc.getc(); + temp[2] = pc.getc(); + temp[3] = pc.getc(); + sr = *(float*)temp; + threshold5 =(int) 1/sr *1000; + timer_sin.reset(); + } + else{ + status.sine = OFF; + timer_sin.stop(); + } + rled = ON; + START = ON; +} + +int main(){ + status.mag = 0; + status.acc = 0; + status.light = 0; + status.touch = 0; + status.sine = 0; calXY(); - mag.Overwrite_dr_osr(40,16); - acc51.Overwrite_dr(200); - tsi.Overwrite_ps_nscn(5,5); - while(true) { - - rled = onTime - abs(acc51.getAccX()); - gled = onTime - abs(acc51.getAccY()); - mag.getValues(&magX, &magY, &magZ); + pc.attach(&configuration); + while(START == OFF){ + pc.putc(0xFF); + } + pc.putc(0xFE); + - cout << "MMA8451: " << acc51.getAccX() << "\t" << acc51.getAccY() << "\t" << acc51.getAccZ() << "\n\r" << endl; - cout << "MAG3110: " << magX << "\t" << magY << "\t" << magZ << "\n\r" << endl; - cout << "MAG3110: " << mag.getHeading() << "\n\r" << endl; - wait(holdTime); - - rled = tsi.readPercentage(); - gled = tsi.readPercentage(); - - cout << "Touch: " << tsi.readPercentage() << "\n\r" << endl; - cout << "SW12: " << SW12 << "\n\r" << endl; - cout << "SW32: " << SW32 << "\n\r" << endl; - cout << "Light Sensor: " << lightSensor << "\n\r" << endl; - wait(holdTime); + timer_mag.start(); + timer_acc.start(); + timer_light.start(); + timer_touch.start(); + timer_sin.start(); + while(1) { + if (status.mag == ON){ + if (timer_mag.read_ms()>threshold1){ + mag_send(); + timer_mag.reset(); + } + } + if (status.acc == ON){ + if (timer_acc.read_ms()>threshold2){ + accl_send(); + timer_acc.reset(); + } + } + if (status.light == ON){ + if (timer_light.read_ms()>threshold3){ + light_send(); + timer_light.reset(); + } + } + if (status.touch == ON){ + if (timer_touch.read_ms()>threshold4){ + touch_send(); + timer_touch.reset(); + } + } + if (status.sine == ON){ + if (timer_sin.read_ms()>threshold5){ + sin_send(); + timer_sin.reset(); + } + } } - return 0; -} +} \ No newline at end of file