Minor fixes

Dependencies:   LSM9DS1_Library SDFileSystem mbed nrf51_rtc

Fork of LSM303DLHTest by Toshihisa T

main.cpp

Committer:
afmiee
Date:
2016-07-19
Revision:
7:cbfdcc57f110
Parent:
6:9db9f4bfaf98
Child:
8:8f4d7f1c588f

File content as of revision 7:cbfdcc57f110:

// Latch Inc.
// Antonio F Mondragon
// 20160714
// for the Adafruit 9DOF Modulke and the Sparkfun microSD card shield

#include "mbed.h"
#include "LSM9DS1.h"
#include "SDFileSystem.h"
#include "nrf51_rtc.h"

#define M_PI 3.14158

// Create objects
Serial debug(USBTX,USBRX);
// For Nordic
LSM9DS1 lol(p30, p7, 0xD6, 0x3C);
I2C i2c(p30, p7);
// Create the SD filesystem
SDFileSystem sd(p25, p28, p29, p20, "sd"); // MOSI, MISO, SCLK, SSEL

// Create a ticker to use the nRF51 RTC
Ticker flipper;

// Assign interrupts to switches
InterruptIn btn1(p17); // Start sampling
InterruptIn btn2(p18); // Stop sampoling

// LED definitions
DigitalOut led1(LED1);
DigitalOut led2(LED2);

// Global variables
int start = 0;
int stop = 0;


// Generated when button 1 is pressed on rising edge START
void start_smpl()
{
    start = 1;
    stop = 0;
}

// Generated when button 1 is pressed on rising edge STOP
void stop_smpl()
{
    stop = 1;
    start = 0;
}

void parp( int times )
{
    int i;
    for( i = 0; i < times; i++) {
        led1 = 0;
        wait( 0.05);
        led1 = 1;
        wait( 0.05);
    }
    led2 = 1;
}
// Flipped every second
void flip()
{
    led2 = 0;
    wait(0.01);
    led2 = 1;
}


uint8_t I2CreadByte(uint8_t address, uint8_t subAddress)
{
    char data;
    char temp= subAddress;

    //i2c.write(address, temp, 1);
    //temp[1] = 0x00;
    i2c.write(address, &temp, 1);
    int a = i2c.read(address, &data, 1);
    return data;
}

int main()
{
    led1= 1;
    char filename[256];
    char secs_str[256];

    struct tm t;
    time_t seconds;

    FILE *fp;

    // Attach functions to interrupts
    btn1.rise(&start_smpl);
    btn2.rise(&stop_smpl);
    flipper.attach(&flip, 1.0); // the address of the function to be attached (flip) and the interval (2 seconds)

    // Enable serial port
    debug.format(8,Serial::None,1);
    debug.baud(115200);
    debug.printf("LSM303DLH Test\x0d\x0a");

    // Initialize 9DOF
    //lol.begin();
    if (!lol.begin()) {
        debug.printf("Failed to communicate with LSM9DS1.\n");
    }
    lol.calibrate();

    lol.configInt(XG_INT1, INT1_IG_G|INT1_IG_XL,     INT_ACTIVE_LOW, INT_PUSH_PULL);   //INT1_CTRL (0x0C) + CTRL_REG8 (0x22)
    lol.configInt(XG_INT2, INT2_DRDY_G|INT2_DRDY_XL, INT_ACTIVE_LOW, INT_PUSH_PULL);   //INT2_CTRL (0x0D) + CTRL_REG8 (0x22)

    lol.configAccelInt(YHIE_XL|XHIE_XL, false);                                        // INT_GEN_CFG_XL (06h)
    lol.configAccelThs((uint8_t)0x25, X_AXIS, (uint8_t)0x00, false);                   // INT_GEN_THS_X_XL (07h)
    lol.configAccelThs((uint8_t)0x25, Y_AXIS, (uint8_t)0x00, false);                   // INT_GEN_THS_Y_XL (08h)
    lol.configAccelThs((uint8_t)0x25, Z_AXIS, (uint8_t)0x00, false);                   // INT_GEN_THS_Z_XL (09h)

    lol.configGyroInt(ZLIE_G|YLIE_G|XLIE_G, false, true);                              // INT_GEN_CFG_G (30h)
    lol.configGyroThs((int16_t )0x300, X_AXIS, (uint8_t) 0x00, false);                 // INT_GEN_THS_X_G (31h - 32h)
    lol.configGyroThs((int16_t )0x300, Y_AXIS, (uint8_t) 0x00, false);                 // INT_GEN_THS_Y_G (33h - 34h)
    lol.configGyroThs((int16_t )0x300, Z_AXIS, (uint8_t) 0x00, false);                 // INT_GEN_THS_Z_G (35h - 36h)
    debug.printf( "\n\r");
    debug.printf( "GyroIntSrc (14h) %02x\n\r", lol.getGyroIntSrc());                        // INT_GEN_SRC_G (14h)
    debug.printf( "AccelIntSrc(26h) %02x\n\r", lol.getAccelIntSrc());                       // INT_GEN_SRC_XL (26h)
    debug.printf( "MagIntSrc  (31h) %02x\n\r", lol.getMagIntSrc());                         // INT_SRC_M (31h)
    debug.printf( "Inactivity (17h) %02x\n\r", lol.getInactivity());                        // STATUS_REG (17h)

    debug.printf( "INT1_CTRL        (0Ch) %02x\n\r", I2CreadByte(0xD6, 0x0C));
    debug.printf( "INT2_CTRL        (0CD) %02x\n\r", I2CreadByte(0xD6, 0x0D));
    debug.printf( "CTRL_REG8        (22h) %02x\n\r", I2CreadByte(0xD6, 0x22));
    debug.printf( "\n\r");
    debug.printf( "INT_GEN_CFG_XL   (06h) %02x\n\r", I2CreadByte(0xD6, 0x06));
    debug.printf( "INT_GEN_SRC_XL   (26h) %02x\n\r", I2CreadByte(0xD6, 0x26));
    debug.printf( "INT_GEN_THS_X_XL (07h) %02x\n\r", I2CreadByte(0xD6, 0x07));
    debug.printf( "INT_GEN_THS_Y_XL (08h) %02x\n\r", I2CreadByte(0xD6, 0x08));
    debug.printf( "INT_GEN_THS_Z_XL (09h) %02x\n\r", I2CreadByte(0xD6, 0x09));
    debug.printf( "INT_GEN_DUR_XL   (0ah) %02x\n\r", I2CreadByte(0xD6, 0x0a));
    debug.printf( "\n\r");
    debug.printf( "INT_GEN_CFG_G    (30h) %02x\n\r", I2CreadByte(0xD6, 0x30));
    debug.printf( "INT_GEN_SRC_G    (14h) %02x\n\r", I2CreadByte(0xD6, 0x14));
    debug.printf( "INT_GEN_THS_XH_G (31h) %02x\n\r", I2CreadByte(0xD6, 0x31));
    debug.printf( "INT_GEN_THS_XL_G (32h) %02x\n\r", I2CreadByte(0xD6, 0x32));
    debug.printf( "INT_GEN_THS_YH_G (33h) %02x\n\r", I2CreadByte(0xD6, 0x33));
    debug.printf( "INT_GEN_THS_YL_G (34h) %02x\n\r", I2CreadByte(0xD6, 0x34));
    debug.printf( "INT_GEN_THS_ZH_G (35h) %02x\n\r", I2CreadByte(0xD6, 0x35));
    debug.printf( "INT_GEN_THS_ZL_G (36h) %02x\n\r", I2CreadByte(0xD6, 0x36));
    debug.printf( "INT_GEN_DUR_G    (37h) %02x\n\r", I2CreadByte(0xD6, 0x37));

//    // Initialize current time if needed
//    printf("Enter current date and time:\n");
//    printf("YYYY MM DD HH MM SS[enter]\n");
//    scanf("%d %d %d %d %d %d", &t.tm_year, &t.tm_mon, &t.tm_mday
//          , &t.tm_hour, &t.tm_min, &t.tm_sec);

    // adjust for tm structure required values
    t.tm_year = t.tm_year - 1900;
    t.tm_mon = t.tm_mon - 1;

    // set the time
    rtc.set_time(mktime(&t));

    while(1) {
        debug.printf("Press Button 1 to Start sampling\n\r");
        debug.printf("Press Button 2 to Stop sampling\n\r");
        // Check for button 1 pressed
        while(!start) {
            led1 = 1;
        }
        // Start sampling
        //led1 = 0;
        parp(5);
        debug.printf("Started sampling\n\r");
        // Get the time and create a file with the number of seconds in hex appended
        seconds = rtc.time();
        sprintf(secs_str, "%s", ctime(&seconds));
        printf("Started at: %s\n\r", secs_str );
        sprintf(filename, "/sd/latch9DOF_%08x",seconds);
        fp = fopen(filename, "w");
        // Verify that file can be created
        if ( fp == NULL ) {
            debug.printf("Cannot create file %s\n\r", filename);
            wait(0.5);
            while(1) {
                led1 = !led1;
                wait(0.5);
            }
        } else
            debug.printf("File %s created successfully\n\r", filename);

        // Sample until button 2 is pressed
        while(!stop) {
            //led1 = 0;
            lol.readAccel();
            lol.readMag();
            lol.readGyro();
            debug.printf("%d, %d, %d, ", lol.ax, lol.ay, lol.az);
            debug.printf("%d, %d, %d,", lol.mx, lol.my, lol.mz);
            debug.printf("%d, %d, %d\n\r", lol.gx, lol.gy, lol.gz);
            fprintf(fp, "%d, %d, %d, ", lol.ax, lol.ay, lol.az);
            fprintf(fp, "%d, %d, %d,", lol.mx, lol.my, lol.mz);
            fprintf(fp, "%d, %d, %d\n\r", lol.gx, lol.gy, lol.gz);
            wait(0.1);
        }
        // Stop Sampling and close file
        parp(10);
        led1 = 1;
        debug.printf("Stopped sampling\n\r");
        debug.printf("Results stored in %s\n\r", filename);
        fclose(fp);
    }
}