Los Putacos / Mbed OS WearableDevice_Nucleo_New

Dependencies:   MPU9250_SPI

Fork of WearableDevice_Nucleo by Los Putacos

main.cpp

Committer:
Muglug
Date:
2017-10-27
Revision:
24:eed68c95160c
Parent:
23:aad5fd1b3ef9
Child:
25:86137c182a17

File content as of revision 24:eed68c95160c:

/*
* Los Putacos
* Copyright (C) 2017, All rights reserved.
* ________________________________________
*
* Created by: Gustavo Campana, Michael Schmidt, Miguel Lopez
*       Date: 11-Oct-2017
*    Version: V0.1
*/
//-----------------------------------------------------------------

//-----------------------------------------------------------------
//     Board: NUCLEO - F401RE
//   Version: MR1136 rev C
//-----------------------------------------------------------------

//-----------------------------------------------------------------
// Includes
#include "mbed.h"
// #include "rtos.h"
// #include "mbed_events.h"

#include "configuration.h"
#include "pin.h"

#include "XBeeLib.h"
#include "SDFileSystem.h"
#include "MPU9250.h"

#include "event.h"
//-----------------------------------------------------------------

//-----------------------------------------------------------------
// Declarations
Timer time_stamp;   //Timer µS time-stamp
EventQueue queue(32 * EVENTS_EVENT_SIZE);   //Event
const char DeviceNr[6] = "DEV01";
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(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);

//-----------------------------------------------------------------

//-----------------------------------------------------------------
/* 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");
}
//-----------------------------------------------------------------

//-----------------------------------------------------------------
void Setup()
{
    PC.baud(9600);
    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
    // 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);
    // 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 SDTest()
{
    mkdir("/sd/Log", 0777);

    FILE *fp = fopen("/sd/Log/Data_Log.txt", "w");
    if(fp == NULL) {
        error("File Writing Failed!\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");
}

//-----------------------------------------------------------------

//-----------------------------------------------------------------
int main()
{
    Setup();    // Initial Setups

    led1 = 1;
    PC.printf("\r\n-------------- Ready! --------------\r\n");
    SDTest();
    
    time_stamp.reset();
    time_stamp.start();
    queue.dispatch();   
    while (true) {
        
        
    }
}
//-----------------------------------------------------------------