FXOS8700CQ demo for mbed OS 5

Dependencies:   FXOS8700CQ

Fork of fxos8700cq_example by Thomas Murphy

Committer:
janjongboom
Date:
Thu Mar 02 17:12:56 2017 +0000
Revision:
1:cd2525cbbe4e
Parent:
0:6c6060a8a2f6
Update to mbed OS 5 and have the lib maintain its own memory

Who changed what in which revision?

UserRevisionLine numberNew contents of line
trm 0:6c6060a8a2f6 1 #include "mbed.h"
trm 0:6c6060a8a2f6 2 #include "FXOS8700CQ.h"
trm 0:6c6060a8a2f6 3
trm 0:6c6060a8a2f6 4 #define DATA_RECORD_TIME_MS 1000
trm 0:6c6060a8a2f6 5
trm 0:6c6060a8a2f6 6 Serial pc(USBTX, USBRX); // Primary output to demonstrate library
trm 0:6c6060a8a2f6 7
trm 0:6c6060a8a2f6 8 // Pin names for FRDM-K64F
trm 0:6c6060a8a2f6 9 FXOS8700CQ fxos(PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // SDA, SCL, (addr << 1)
trm 0:6c6060a8a2f6 10
trm 0:6c6060a8a2f6 11 DigitalOut green(LED_GREEN); // waiting light
trm 0:6c6060a8a2f6 12 DigitalOut blue(LED_BLUE); // collection-in-progress light
trm 0:6c6060a8a2f6 13 DigitalOut red(LED_RED); // completed/error ligt
trm 0:6c6060a8a2f6 14
trm 0:6c6060a8a2f6 15 Timer t; // Microsecond timer, 32 bit int, maximum count of ~30 minutes
trm 0:6c6060a8a2f6 16 InterruptIn fxos_int1(PTC6); // unused, common with SW2 on FRDM-K64F
trm 0:6c6060a8a2f6 17 InterruptIn fxos_int2(PTC13); // should just be the Data-Ready interrupt
trm 0:6c6060a8a2f6 18 InterruptIn start_sw(PTA4); // switch SW3
trm 0:6c6060a8a2f6 19
trm 0:6c6060a8a2f6 20 // Interrupt status flags and data
trm 0:6c6060a8a2f6 21 bool fxos_int1_triggered = false;
trm 0:6c6060a8a2f6 22 bool fxos_int2_triggered = false;
trm 0:6c6060a8a2f6 23 uint32_t us_ellapsed = 0;
trm 0:6c6060a8a2f6 24 bool start_sw_triggered = false;
trm 0:6c6060a8a2f6 25
trm 0:6c6060a8a2f6 26 void trigger_fxos_int1(void)
trm 0:6c6060a8a2f6 27 {
trm 0:6c6060a8a2f6 28 fxos_int1_triggered = true;
trm 0:6c6060a8a2f6 29 }
trm 0:6c6060a8a2f6 30
trm 0:6c6060a8a2f6 31 void trigger_fxos_int2(void)
trm 0:6c6060a8a2f6 32 {
trm 0:6c6060a8a2f6 33 fxos_int2_triggered = true;
trm 0:6c6060a8a2f6 34 us_ellapsed = t.read_us();
trm 0:6c6060a8a2f6 35 }
trm 0:6c6060a8a2f6 36
trm 0:6c6060a8a2f6 37 void trigger_start_sw(void)
trm 0:6c6060a8a2f6 38 {
trm 0:6c6060a8a2f6 39 start_sw_triggered = true;
trm 0:6c6060a8a2f6 40 }
trm 0:6c6060a8a2f6 41
trm 0:6c6060a8a2f6 42 void print_reading()
trm 0:6c6060a8a2f6 43 {
trm 0:6c6060a8a2f6 44 pc.printf("%d A X:%5d,Y:%5d,Z:%5d M X:%5d,Y:%5d,Z:%5d\r\n",
trm 0:6c6060a8a2f6 45 us_ellapsed,
janjongboom 1:cd2525cbbe4e 46 fxos.getAccelX(), fxos.getAccelY(), fxos.getAccelZ(),
janjongboom 1:cd2525cbbe4e 47 fxos.getMagnetX(), fxos.getMagnetY(), fxos.getMagnetZ());
trm 0:6c6060a8a2f6 48 }
trm 0:6c6060a8a2f6 49
trm 0:6c6060a8a2f6 50 int main(void)
trm 0:6c6060a8a2f6 51 {
trm 0:6c6060a8a2f6 52 // Setup
trm 0:6c6060a8a2f6 53 t.reset();
trm 0:6c6060a8a2f6 54 pc.baud(115200); // Print quickly! 200Hz x line of output data!
trm 0:6c6060a8a2f6 55
trm 0:6c6060a8a2f6 56 // Lights off (FRDM-K64F has active-low LEDs)
trm 0:6c6060a8a2f6 57 green.write(1);
trm 0:6c6060a8a2f6 58 red.write(1);
trm 0:6c6060a8a2f6 59 blue.write(1);
trm 0:6c6060a8a2f6 60
trm 0:6c6060a8a2f6 61 // Diagnostic printing of the FXOS WHOAMI register value
trm 0:6c6060a8a2f6 62 printf("\r\n\nFXOS8700Q Who Am I= %X\r\n", fxos.get_whoami());
trm 0:6c6060a8a2f6 63
trm 0:6c6060a8a2f6 64 // Iterrupt for active-low interrupt line from FXOS
trm 0:6c6060a8a2f6 65 // Configured with only one interrupt on INT2 signaling Data-Ready
trm 0:6c6060a8a2f6 66 fxos_int2.fall(&trigger_fxos_int2);
trm 0:6c6060a8a2f6 67 fxos.enable();
trm 0:6c6060a8a2f6 68
trm 0:6c6060a8a2f6 69 // Interrupt for SW3 button-down state
trm 0:6c6060a8a2f6 70 start_sw.mode(PullUp); // Since the FRDM-K64F doesn't have its SW2/SW3 pull-ups populated
trm 0:6c6060a8a2f6 71 start_sw.fall(&trigger_start_sw);
trm 0:6c6060a8a2f6 72
trm 0:6c6060a8a2f6 73 green.write(0); // ready-green on
trm 0:6c6060a8a2f6 74
trm 0:6c6060a8a2f6 75 // Example data printing
janjongboom 1:cd2525cbbe4e 76 fxos.get_data();
trm 0:6c6060a8a2f6 77 print_reading();
trm 0:6c6060a8a2f6 78
trm 0:6c6060a8a2f6 79 pc.printf("Waiting for data collection trigger on SW3\r\n");
trm 0:6c6060a8a2f6 80
trm 0:6c6060a8a2f6 81 while(true) {
trm 0:6c6060a8a2f6 82 if(start_sw_triggered) {
trm 0:6c6060a8a2f6 83 break;
trm 0:6c6060a8a2f6 84 }
trm 0:6c6060a8a2f6 85 wait_ms(50); // just to slow the loop, fast enough for UX
trm 0:6c6060a8a2f6 86 }
trm 0:6c6060a8a2f6 87
trm 0:6c6060a8a2f6 88 green.write(1); // ready-green off
trm 0:6c6060a8a2f6 89 blue.write(0); // working-blue on
trm 0:6c6060a8a2f6 90
trm 0:6c6060a8a2f6 91 pc.printf("Started data collection. Accelerometer at max %dg.\r\n",
trm 0:6c6060a8a2f6 92 fxos.get_accel_scale());
trm 0:6c6060a8a2f6 93
janjongboom 1:cd2525cbbe4e 94 fxos.get_data(); // clear interrupt from device
trm 0:6c6060a8a2f6 95 fxos_int2_triggered = false; // un-trigger
trm 0:6c6060a8a2f6 96
trm 0:6c6060a8a2f6 97 t.start(); // start timer and enter collection loop
trm 0:6c6060a8a2f6 98 while (t.read_ms() <= DATA_RECORD_TIME_MS) {
trm 0:6c6060a8a2f6 99
trm 0:6c6060a8a2f6 100 if(fxos_int2_triggered) {
trm 0:6c6060a8a2f6 101 fxos_int2_triggered = false; // un-trigger
janjongboom 1:cd2525cbbe4e 102 fxos.get_data();
trm 0:6c6060a8a2f6 103 print_reading(); // outpouring of data !!
trm 0:6c6060a8a2f6 104 }
trm 0:6c6060a8a2f6 105
trm 0:6c6060a8a2f6 106 // Continuous polling of interrupt status is not efficient, but...
trm 0:6c6060a8a2f6 107 wait_us(500); // 1/10th the period of the 200Hz sample rate
trm 0:6c6060a8a2f6 108 }
trm 0:6c6060a8a2f6 109
trm 0:6c6060a8a2f6 110 blue.write(1); // working-blue off
trm 0:6c6060a8a2f6 111 red.write(0); // complete-red on
trm 0:6c6060a8a2f6 112
trm 0:6c6060a8a2f6 113 pc.printf("Done. Reset to repeat.\r\n");
trm 0:6c6060a8a2f6 114
trm 0:6c6060a8a2f6 115 while(true) {
trm 0:6c6060a8a2f6 116 pc.putc('.'); // idle dots
trm 0:6c6060a8a2f6 117 wait(1.0);
trm 0:6c6060a8a2f6 118 }
trm 0:6c6060a8a2f6 119 }