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:
- 22:23d5bb18d6a0
- Parent:
- 20:014a808b3ea2
- Child:
- 23:36a76a98c438
File content as of revision 22:23d5bb18d6a0:
#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("\nAbout to alloc\n");
log_data* log_d = (log_data*)mail_box.alloc();
printf("Pressure: %f",log_d->pressure);
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\n");
}
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);
osEvent ev = mail_box.get();
log_data *ld = (log_data*)ev.value.p;
printf("Pressure: %f",ld->pressure);
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("gotcha: %f",temp->pressure);
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, bool lFlag): T(15.0f) {
v = &vector;
n = #
flag = &lFlag;
/* Retrieve the composing elements of the expansion board */
printf("Initialising expansion board...\n\r");
}
