skm
Fork of Hello_FXOS8700Q by
main.cpp@7:4c834e4dad1f, 2015-06-18 (annotated)
- Committer:
- marcus255
- Date:
- Thu Jun 18 16:25:26 2015 +0000
- Revision:
- 7:4c834e4dad1f
- Parent:
- 6:02bfeec82bc1
skm
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JimCarver | 0:748fe54f0947 | 1 | #include "mbed.h" |
JimCarver | 0:748fe54f0947 | 2 | #include "FXOS8700Q.h" |
marcus255 | 7:4c834e4dad1f | 3 | #include "cli.h" |
JimCarver | 4:4b494ca218ff | 4 | |
marcus255 | 7:4c834e4dad1f | 5 | FXOS8700Q_acc acc(PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // Proper ports and I2C Address for FXOS8700Q accelometer |
marcus255 | 7:4c834e4dad1f | 6 | Serial pc(USBTX, USBRX); // Proper ports for USART |
JimCarver | 5:061ab9f2c002 | 7 | MotionSensorDataUnits acc_data; |
JimCarver | 5:061ab9f2c002 | 8 | MotionSensorDataCounts acc_raw; |
JimCarver | 4:4b494ca218ff | 9 | |
marcus255 | 7:4c834e4dad1f | 10 | int msDelay = 300; // default measurement delay in ms |
marcus255 | 7:4c834e4dad1f | 11 | int threshold = 2000; // default acceleration threshold in mg |
marcus255 | 7:4c834e4dad1f | 12 | DigitalOut redLed(LED_RED); |
marcus255 | 7:4c834e4dad1f | 13 | DigitalOut greenLed(LED_GREEN); |
marcus255 | 7:4c834e4dad1f | 14 | DigitalOut blueLed(LED_BLUE); |
JimCarver | 0:748fe54f0947 | 15 | |
JimCarver | 0:748fe54f0947 | 16 | int main() { |
marcus255 | 7:4c834e4dad1f | 17 | pc.baud(115200); |
marcus255 | 7:4c834e4dad1f | 18 | acc.enable(); |
marcus255 | 7:4c834e4dad1f | 19 | //printf("\n\rFXOS8700Q Device Address = %X\r\n", acc.whoAmI()); |
marcus255 | 7:4c834e4dad1f | 20 | //uint8_t scopeValues[6]; // unmodified data frame from accelometer device |
marcus255 | 7:4c834e4dad1f | 21 | float a, b, c; |
marcus255 | 7:4c834e4dad1f | 22 | |
JimCarver | 0:748fe54f0947 | 23 | while (true) { |
marcus255 | 7:4c834e4dad1f | 24 | command commandStruct; |
JimCarver | 5:061ab9f2c002 | 25 | acc.getAxis(acc_data); |
marcus255 | 7:4c834e4dad1f | 26 | //acc.getAxis(acc_raw, scopeValues); |
marcus255 | 7:4c834e4dad1f | 27 | //printf("Dane z odebranej ramki: "); |
marcus255 | 7:4c834e4dad1f | 28 | //for (uint8_t i = 0; i < 6; i++) |
marcus255 | 7:4c834e4dad1f | 29 | //printf("%04x ",scopeValues[i]); |
marcus255 | 7:4c834e4dad1f | 30 | |
marcus255 | 7:4c834e4dad1f | 31 | printf("\r"); |
marcus255 | 7:4c834e4dad1f | 32 | if ((a=acc_data.x) < 0) printf("X=-%1.3fg ", abs(a)); else printf("X=+%1.3fg ", abs(a)); |
marcus255 | 7:4c834e4dad1f | 33 | if ((b=acc_data.y) < 0) printf("Y=-%1.3fg ", abs(b)); else printf("Y=+%1.3fg ", abs(b)); |
marcus255 | 7:4c834e4dad1f | 34 | if ((c=acc_data.z) < 0) printf("Z=-%1.3fg ", abs(c)); else printf("Z=+%1.3fg ", abs(c)); |
marcus255 | 7:4c834e4dad1f | 35 | |
marcus255 | 7:4c834e4dad1f | 36 | float th = threshold / 1000.0; // to get threshold in g (user defines it in mg) |
marcus255 | 7:4c834e4dad1f | 37 | if (abs(a) > th) redLed = 0; else redLed = 1; // lights the led on if absolute value of acceleration is greater than defined threshold |
marcus255 | 7:4c834e4dad1f | 38 | if (abs(b) > th) greenLed = 0; else greenLed = 1; // otherwise lights the led off |
marcus255 | 7:4c834e4dad1f | 39 | if (abs(c) > th) blueLed = 0; else blueLed = 1; // different colors are assigned with corresponding axis |
marcus255 | 7:4c834e4dad1f | 40 | |
marcus255 | 7:4c834e4dad1f | 41 | wait(msDelay/(1000.0*2.0)); |
marcus255 | 7:4c834e4dad1f | 42 | |
marcus255 | 7:4c834e4dad1f | 43 | if(pc.readable()) { // if USART buffor is not empty (if user enters a command) |
marcus255 | 7:4c834e4dad1f | 44 | clearConsole(); |
marcus255 | 7:4c834e4dad1f | 45 | commandService(&commandStruct); |
marcus255 | 7:4c834e4dad1f | 46 | clearConsole(); |
marcus255 | 7:4c834e4dad1f | 47 | commandValidate(&commandStruct); |
marcus255 | 7:4c834e4dad1f | 48 | wait(3.0); |
marcus255 | 7:4c834e4dad1f | 49 | clearConsole(); |
marcus255 | 7:4c834e4dad1f | 50 | } |
marcus255 | 7:4c834e4dad1f | 51 | } |
marcus255 | 7:4c834e4dad1f | 52 | } |
marcus255 | 7:4c834e4dad1f | 53 |