Nelson Santos / Mbed 2 deprecated trabalho

Dependencies:   X_NUCLEO_IKS01A1-f255a2c75ecb mbed-rtos mbed

sensor.cpp

Committer:
nlsantos
Date:
2016-05-17
Revision:
16:89ff2d66f894
Parent:
11:f80f5c4a2db9

File content as of revision 16:89ff2d66f894:

#include "mbed.h"
#include "x_nucleo_iks01a1.h"
#include <vector>
#include "sensor.h"
#include "userMethods.h"

Mail<log_data, QUEUESIZE> ExpansionBoard::mail_box;
vector<log_data> * ExpansionBoard::v;
int * ExpansionBoard::n;

//This is the producer
void ExpansionBoard::sampleData() {
    printf("about to alloc\n");
    log_data* log_d = (log_data*)mail_box.alloc();
        
    if (log_d == NULL) {
        mail_box.get();
        printf("Out of memory, last sample deleted\n");
        log_data* log_d = (log_data*)mail_box.alloc();
        printf("allocated after out of memory");
    }
    
    printf("Not null, reading values\n");
    //Store read data in a sample
    float value;
    log_d->date = time(NULL);
    temp_sensor->GetTemperature(&value);
    log_d->tempCelsius = value;
    humidity_sensor->GetHumidity(&value);
    log_d->humidity = value;
    pressure_sensor->GetPressure(&value);
    log_d->pressure = value;
    printf("All values STORED\n");
    //Send pointer to sample to the queue
    osStatus stat = mail_box.put(log_d);
    printf("MAIL_BOX.PUT run\n");
    // Check for resource error
    if (stat == osErrorResource) {
        printf("mail_box->put() Error %4Xh\n", stat);
        //Error, free up memory block taken
        mail_box.free(log_d);
    }
}

void ExpansionBoard::getData(const void*){

    while(true){
        //Block on queue if no data is available
        osEvent event = mail_box.get();
        printf("MAIL_BOX.GET\n");
        
        if (event.status == osEventMail) {
            // Successful, store log_data
            log_data* temp = (log_data*) event.value.p;
            v->push_back(*temp);
            mail_box.free(temp);
            printf("GET value stored and freed up mail_box\n");
        }
    }
}

// Helper function for printing floats & doubles
char *ExpansionBoard::printDouble(char* str, double v, int decimalDigits) {
    int intPart, fractPart, len, i=1;
    char *ptr;
    for (; decimalDigits!=0; i*=10, decimalDigits--);
    intPart = (int)v;
    fractPart = (int)((v-(double)(int)v)*i);
    sprintf(str, "%i.", intPart);
    len = strlen(str);
    ptr = &str[len];
    for (i/=10; i>1; i/=10, ptr++) {
        if(fractPart >= i) break;
        *ptr = '0';
    }
    sprintf(ptr, "%i", fractPart);
    return str;
}

ExpansionBoard::ExpansionBoard(vector<log_data> vector, int num): T(15.0f), flag(false) {
    v = &vector;
    n = &num;
     /* Retrieve the composing elements of the expansion board */
    uint8_t id;
    printf("Initialising expansion board...\n\r");
    
    /*
    humidity_sensor->ReadID(&id);
    printf("HTS221  humidity & temperature    = 0x%X\r\n", id);
    pressure_sensor->ReadID(&id);
    printf("LPS25H  pressure & temperature    = 0x%X\r\n", id);
    magnetometer->ReadID(&id);
    printf("LIS3MDL magnetometer              = 0x%X\r\n", id);
    gyroscope->ReadID(&id);
    printf("LSM6DS0 accelerometer & gyroscope = 0x%X\r\n\n", id);     
    */
}