Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: X_NUCLEO_IKS01A1-f255a2c75ecb mbed-rtos mbed
sensor.cpp
- Committer:
- nlsantos
- Date:
- 2016-05-17
- Revision:
- 25:2197b8bb930c
- Parent:
- 24:c924342f7c76
- Child:
- 27:1216b002cbf3
File content as of revision 25:2197b8bb930c:
#include "mbed.h"
#include "x_nucleo_iks01a1.h"
#include <vector>
#include "sensor.h"
#include "userMethods.h"
// Mail box
Mail<log_data, QUEUESIZE> ExpansionBoard::mail_box;
vector<log_data> * ExpansionBoard::v;
int * ExpansionBoard::n;
bool ExpansionBoard::flag;
//This is the producer
void ExpansionBoard::sampleData()
{
if(flag) printf("\nAbout to alloc\n");
log_data* log_d = (log_data*)mail_box.alloc();
if (log_d == NULL) {
mail_box.get();
if(flag) printf("Out of memory, last sample deleted\n");
log_data* log_d = (log_data*)mail_box.alloc();
if(flag) printf("Allocated after out of memory\n");
}
if(flag) 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;
if(flag) printf("All values STORED\n");
//Send pointer to sample to the queue
osStatus stat = mail_box.put(log_d);
//v->push_back(*log_d);
if(flag) printf("MAIL_BOX.PUT run\n");
// Check for resource error
if (stat == osErrorResource) {
if(flag) printf("mail_box->put() Error %4Xh\n", stat);
//Error, free up memory block taken
mail_box.free(log_d);
}
}
// This is the consumer
/*
void ExpansionBoard::getData(const void*)
{
while(true) {
//Block on queue if no data is available
osEvent event = mail_box.get();
if(flag) {
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);
if(flag) {
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;
}
// Constructor for the class
ExpansionBoard::ExpansionBoard(vector<log_data> vector, int num, bool lFlag): T(15.0f)
{
v = &vector;
n = #
flag = &lFlag;
/* Retrieve the composing elements of the expansion board */
printf("Initialising expansion board...\n\r");
}
