Los Putacos / Mbed OS WearableDevice_Nucleo_New

Dependencies:   MPU9250_SPI

Fork of WearableDevice_Nucleo by Los Putacos

Revision:
23:aad5fd1b3ef9
Parent:
20:197c69b38adf
Child:
24:eed68c95160c
diff -r ef9a7a43e21e -r aad5fd1b3ef9 main.cpp
--- a/main.cpp	Thu Oct 19 09:05:47 2017 +0000
+++ b/main.cpp	Wed Oct 25 13:24:05 2017 +0000
@@ -7,6 +7,7 @@
 *       Date: 11-Oct-2017
 *    Version: V0.1
 */
+//-----------------------------------------------------------------
 
 //-----------------------------------------------------------------
 //     Board: NUCLEO - F401RE
@@ -16,13 +17,12 @@
 //-----------------------------------------------------------------
 // Includes
 #include "mbed.h"
-#include "rtos.h"
-#include "mbed_events.h"
+#include "configuration.h"
+#include "pin.h"
 
-#include "configuration.h"
+// #include "rtos.h"
 #include "event.h"
-#include "pin.h"
-#include "MPU9250.h"
+// #include "mbed_events.h"
 
 #include "XBeeLib.h"
 #include "SDFileSystem.h"
@@ -30,145 +30,114 @@
 
 //-----------------------------------------------------------------
 // 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[] = "";
 
-Serial PC(USBTX, USBRX);    // PC Serial (Debug)
-// Serial XBeeee(PA_11, PA_12);    // Xbee
+// 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);
 
-// MPU9250 imu; // IMU MPU-9255 by Kris Winer
+// Create an SDFileSystem object
+SDFileSystem sd(PC_12, PC_11, PC_10, PD_2, "sd");
+
 //-----------------------------------------------------------------
 
 //-----------------------------------------------------------------
-// Global Variables
-// Your code here!
-//-----------------------------------------------------------------
-/*
-//-----------------------------------------------------------------
-void print_event(void const *argv, int i) {
-    pc.printf("%d ",i);
-    pc.printf((const char*)argv);
-    pc.printf("\r\n");
-}
-//-----------------------------------------------------------------
-*/
-//-----------------------------------------------------------------
-/*void blink_event(DigitalOut pin){
-    pin = !pin;
+/* Callback function, invoked at packet reception */
+static void receive_cb(const XBeeLib::RemoteXBeeZB& remote, bool broadcast, const uint8_t *const data, uint16_t len)
+{
+    PC.printf("Package Length: %i \r\n", len);
+    for (int i = 0; i < len; i++) {
+        Data_Buffer[i] = char(data[i]);
+        PC.printf("%c", Data_Buffer[i]);
+    }
+    
+    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
+            PC.printf(" - Non-valid Command Delimiter!\r\n");
+    } 
+    else
+        PC.printf(" - Non-valid Package Length!\r\n");
+        
+    extern int task1_id;
+    queue.cancel(task1_id);
 }
 //-----------------------------------------------------------------
 
 //-----------------------------------------------------------------
-void println_event(){
-    pc.printf("Arroz, feijao e batata\r\n");
-}
-//-----------------------------------------------------------------
-*/
-//-----------------------------------------------------------------
 void Setup()
 {
-    // PC Serial (Debug) baudrate
     PC.baud(9600);
-    PC.printf("CPU SystemCoreClock is %d Hz\r\n", SystemCoreClock);
+    PC.printf("\r\n-------------------------------------\r\n");
+    PC.printf("CPU SystemCoreClock is %d Hz", SystemCoreClock);
 
-    // XBeeee.baud(9600);
+    XBee.init();
+    XBee.register_receive_cb(&receive_cb);       // Register Callbacks
+    // time_stamp.start();     // Start Timer
 
     // Task queue
-    // int task1_id = queue.call_every(1000, printf, "called every 1 seconds\n");
-    // int task2_id = queue.call_every(2500, printf, "called every 2.5 seconds\n");
+    // int task1_id = queue.call_every(200, CheckInputBuffer);
+    // int task2_id = queue.call_every(50, ReadIMUData);
     // int task3_id = queue.call_every(3000, &print_event, (void *)"called every 3 seconds\n", (int) time_stamp.read());
     // int task4_id = queue.call_every(50, blink_event, led1);
     // int task5_id = queue.call_every(500, println_event);
     // int task6_id = queue.call_every(500, readIMU);
     // int task7_id = queue.call_every(500, gayEvent);
 }
-//-----------------------------------------------------------------
-
-//-----------------------------------------------------------------
-/*void initMPU9250()
-{
-// Initialize MPU9250 device
-// wake up device
-    imu.writeByte(MPU9250_ADDRESS, PWR_MGMT_1, 0x00); // Clear sleep mode bit (6), enable all sensors
-}*/
-
-//-----------------------------------------------------------------
 
-using namespace XBeeLib;
-
-/** Callback function, invoked at packet reception */
-static void receive_cb(const RemoteXBeeZB& remote, bool broadcast, const uint8_t *const data, uint16_t len)
+void SDTest()
 {
-    for (int i = 0; i < len; i++)
-        PC.printf("%c", data[i]);
+    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");
+}
 
-    PC.printf("\r\n");
-}
-// Create an SDFileSystem object
-SDFileSystem sd(SD_MOSI, SD_MISO, SD_CLK, SD_CS, "sd");
+    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);
+            }
+//-----------------------------------------------------------------
 
 //-----------------------------------------------------------------
 int main()
 {
     Setup();    // Initial Setups
-    const uint8_t data[] = "Hello Master!";
-    const uint16_t data_len = sizeof data / sizeof data[0] - 1;
-
-    XBeeZB XBee = XBeeZB(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 115200);
-    XBee.init();
-
-    /* Register callbacks */
-    XBee.register_receive_cb(&receive_cb);
-    time_stamp.start(); // Start Timer
-
-    // Mount the filesystem
-    sd.mount();
-
-    if (sd.card_present())
-        PC.printf("Good");
-    else
-        PC.printf("Meh");
 
     // Reading the analog input (Microphone)
     // array = micro.read_u16();
 
-
-    // Performing Write Test
-    PC.printf("\nWriting to SD card...");
-    FILE *fp = fopen("sdtest.txt", "w");
-    if (fp != NULL) {
-        fclose(fp);
-        PC.printf(" Success!\n");
-    } else {
-        PC.printf(" Failed!\n");
+    led1 = 1;
+    PC.printf(" - Ready!\r\n\r\n");
+    
+    SDTest();
+    // queue.dispatch();
+    while (true) {
+        // PC.printf("In the while");
     }
-
-    // Performing Read Test
-    PC.printf("Reading from SD card...");
-    fp = fopen("/sd/sdtest.txt", "r");
-    if (fp != NULL) {
-        char c = fgetc(fp);
-        if (c == 'W')
-            PC.printf(" Success!\n");
-        else
-            PC.printf("Incorrect Character! (%c)!\n", c);
-        fclose(fp);
-    } else {
-        PC.printf(" Failed!\n");
-    }
-
-    led1 = 1;
-    while (true) {
-        // queue.dispatch();
-        XBee.process_rx_frames();
-
-        TxStatus txStatus = XBee.send_data_to_coordinator(data, data_len);
-        if (txStatus != TxStatusSuccess) {
-            PC.printf("send_data_broadcast() failed with error %d\n", (int)txStatus);
-        }
-    }
-
-    // Unmount the Filesystem
-    sd.unmount();
 }
 //-----------------------------------------------------------------
\ No newline at end of file