Los Putacos / Mbed OS WearableDevice_Nucleo_New

Dependencies:   MPU9250_SPI

Fork of WearableDevice_Nucleo by Los Putacos

Revision:
24:eed68c95160c
Parent:
23:aad5fd1b3ef9
Child:
25:86137c182a17
--- a/main.cpp	Wed Oct 25 13:24:05 2017 +0000
+++ b/main.cpp	Fri Oct 27 09:59:32 2017 +0000
@@ -17,31 +17,39 @@
 //-----------------------------------------------------------------
 // Includes
 #include "mbed.h"
+// #include "rtos.h"
+// #include "mbed_events.h"
+
 #include "configuration.h"
 #include "pin.h"
 
-// #include "rtos.h"
-#include "event.h"
-// #include "mbed_events.h"
-
 #include "XBeeLib.h"
 #include "SDFileSystem.h"
+#include "MPU9250.h"
+
+#include "event.h"
 //-----------------------------------------------------------------
 
 //-----------------------------------------------------------------
 // Declarations
-// Timer time_stamp;   //Timer µS time-stamp
+Timer time_stamp;   //Timer µS time-stamp
 EventQueue queue(32 * EVENTS_EVENT_SIZE);   //Event
 const char DeviceNr[6] = "DEV01";
-char Data_Buffer[] = "";
+char Data_Buffer[10] = "";
+// uint16_t data_len;
 
 // PC Serial (Debug)
 Serial PC(USBTX, USBRX);
 XBeeLib::XBeeZB XBee = XBeeLib::XBeeZB(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 115200);
+
 int task1_id = queue.call_every(200, CheckInputBuffer);
+int task2_id = queue.call_every(100, ReadIMUData);
 
 // Create an SDFileSystem object
-SDFileSystem sd(PC_12, PC_11, PC_10, PD_2, "sd");
+SDFileSystem sd(SD_MOSI, SD_MISO, SD_CLK, SD_CS, "sd");
+// Create an MPU9250 object
+SPI spi(MPU_MOSI, MPU_MISO, MPU_SCK);
+mpu9250_spi imu(spi, MPU_CS);
 
 //-----------------------------------------------------------------
 
@@ -54,26 +62,19 @@
         Data_Buffer[i] = char(data[i]);
         PC.printf("%c", Data_Buffer[i]);
     }
-    
-    if (len == 9) 
-    {
-        if ((char(Data_Buffer[0]) == '#') && (char(Data_Buffer[6]) == ',')) 
-        {
+
+    if (len == 9) {
+        if ((char(Data_Buffer[0]) == '#') && (char(Data_Buffer[6]) == ',')) {
             PC.printf(" - Command Detected!\r\n");
-            
+
             if (strstr(Data_Buffer, DeviceNr) != NULL)
                 PC.printf("Correct Package ID!\r\n");
             else
                 PC.printf("Wrong Package ID!\r\n");
-        } 
-        else
+        } else
             PC.printf(" - Non-valid Command Delimiter!\r\n");
-    } 
-    else
+    } else
         PC.printf(" - Non-valid Package Length!\r\n");
-        
-    extern int task1_id;
-    queue.cancel(task1_id);
 }
 //-----------------------------------------------------------------
 
@@ -81,13 +82,19 @@
 void Setup()
 {
     PC.baud(9600);
-    PC.printf("\r\n-------------------------------------\r\n");
+    PC.printf("\r\n------------- Booting! -------------\r\n");
     PC.printf("CPU SystemCoreClock is %d Hz", SystemCoreClock);
 
     XBee.init();
-    XBee.register_receive_cb(&receive_cb);       // Register Callbacks
+    XBee.register_receive_cb(&receive_cb);      // Register Callbacks
     // time_stamp.start();     // Start Timer
 
+    if(imu.init(1, BITS_DLPF_CFG_188HZ)){        // Initialize the MPU9250
+        PC.printf("\nCouldn't initialize MPU9250 via SPI!");
+    }
+    PC.printf("\nWHOAMI = 0x%2x", imu.whoami());    // Output I2C address to know if SPI is working, it should be 104
+    PC.printf("\nAcc_Scale = %u\n", imu.set_acc_scale(BITS_FS_16G));   // Set Full Range for Acc.
+
     // Task queue
     // int task1_id = queue.call_every(200, CheckInputBuffer);
     // int task2_id = queue.call_every(50, ReadIMUData);
@@ -101,26 +108,32 @@
 void SDTest()
 {
     mkdir("/sd/Log", 0777);
-    
+
     FILE *fp = fopen("/sd/Log/Data_Log.txt", "w");
     if(fp == NULL) {
         error("File Writing Failed!\n");
     }
-    fprintf(fp, "Data Dummy...");
-    fclose(fp); 
- 
-    printf("SD Test Successful!\n");
+    fprintf(fp, "12345");
+    fclose(fp);
+
+    fp = fopen("/sd/Log/Data_Log.txt", "r");  // open the file in 'read' mode
+
+    PC.printf("\nRead from file: ");
+    while (!feof(fp)) {     // While Not End-Of-File
+        fread((char*) Data_Buffer, 1, 100, fp);
+    }
+    PC.printf("\r\n");
+    fclose(fp);
+
+    // data_len = sizeof(Data_Buffer); // / sizeof Data_Buffer[0] - 1;
+
+    XBeeLib::TxStatus txStatus = XBee.send_data_to_coordinator((uint8_t*) Data_Buffer, sizeof(Data_Buffer));
+    if (txStatus != XBeeLib::TxStatusSuccess)
+        PC.printf("send_data_to_coordinator() failed with error %d\n", (int) txStatus);
+
+    PC.printf("SD Test Successful!\n");
 }
 
-    const uint8_t data[] = "Hello Master!";
-    const uint16_t data_len = sizeof data / sizeof data[0] - 1;
-
-void Test()
-{
-      XBeeLib::TxStatus txStatus = XBee.send_data_to_coordinator(data, data_len);
-        if (txStatus != XBeeLib::TxStatusSuccess)
-            PC.printf("send_data_broadcast() failed with error %d\n", (int)txStatus);
-            }
 //-----------------------------------------------------------------
 
 //-----------------------------------------------------------------
@@ -128,16 +141,16 @@
 {
     Setup();    // Initial Setups
 
-    // Reading the analog input (Microphone)
-    // array = micro.read_u16();
-
     led1 = 1;
-    PC.printf(" - Ready!\r\n\r\n");
-    
+    PC.printf("\r\n-------------- Ready! --------------\r\n");
     SDTest();
-    // queue.dispatch();
+    
+    time_stamp.reset();
+    time_stamp.start();
+    queue.dispatch();   
     while (true) {
-        // PC.printf("In the while");
+        
+        
     }
 }
 //-----------------------------------------------------------------
\ No newline at end of file