Nelson Santos / Mbed 2 deprecated Coursework_copy

Dependencies:   X_NUCLEO_IKS01A1 mbed-rtos mbed

Fork of HelloWorld_IKS01A1 by ST

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers expansionBoard.cpp Source File

expansionBoard.cpp

00001 #include "mbed.h"
00002 #include "x_nucleo_iks01a1.h"
00003 #include "mailBox.cpp"
00004 
00005 using namespace std;
00006 
00007 
00008 /* Instantiate the expansion board */
00009 static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(D14, D15);
00010 
00011 static GyroSensor *gyroscope = mems_expansion_board->GetGyroscope();
00012 static MotionSensor *accelerometer = mems_expansion_board->GetAccelerometer();
00013 static MagneticSensor *magnetometer = mems_expansion_board->magnetometer;
00014 static HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor;
00015 static PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor;
00016 static TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor;
00017 static TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor;
00018 
00019 static Ticker ticker;
00020 void readData();
00021 
00022 class ExpansionBoard {
00023 public:
00024     float T;
00025     int count;
00026     
00027 private:
00028     /* Helper function for printing floats & doubles */
00029     static char *printDouble(char* str, double v, int decimalDigits=2) {
00030         int i = 1;
00031         int intPart, fractPart;
00032         int len;
00033         char *ptr;
00034         /* prepare decimal digits multiplicator */
00035         for (; decimalDigits!=0; i*=10, decimalDigits--);
00036         /* calculate integer & fractinal parts */
00037         intPart = (int)v;
00038         fractPart = (int)((v-(double)(int)v)*i);
00039         /* fill in integer part */
00040         sprintf(str, "%i.", intPart);
00041         /* prepare fill in of fractional part */
00042         len = strlen(str);
00043         ptr = &str[len];
00044         /* fill in leading fractional zeros */
00045         for (i/=10; i>1; i/=10, ptr++) {
00046             if(fractPart >= i) break;
00047             *ptr = '0'; }
00048         /* fill in (rest of) fractional part */
00049         sprintf(ptr, "%i", fractPart);
00050         return str;
00051     }
00052     
00053     //This is the producer
00054     void readData() {
00055         float value1, value2;
00056         char buffer1[32], buffer2[32];
00057         int32_t axes[3];
00058         
00059         log_data *log_d = (log_data*)mail_box.alloc();
00060         //TODO Too small!?
00061         log_d->id = rand() % 255;
00062         
00063         //TODO Out of memory, specs say to delete oldest sample
00064         if (log_d == NULL) return;
00065         
00066         temp_sensor1->GetTemperature(&value1);
00067         humidity_sensor->GetHumidity(&value2);
00068         printf("HTS221: [temp] %7s°C,   [hum] %s%%\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2));
00069         log_d->tempCelsius = value1;
00070         log_d->humidity = value2;
00071         
00072         temp_sensor2->GetFahrenheit(&value1);
00073         pressure_sensor->GetPressure(&value2);
00074         printf("LPS25H: [temp] %7s°F, [press] %smbar\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2));
00075         log_d->tempFarenheit = value1;
00076         log_d->pressure = value2;
00077         printf("---\r\n");
00078 
00079         magnetometer->Get_M_Axes(axes);
00080         printf("LIS3MDL [mag/mgauss]:  %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
00081         memcpy(&log_d->magnetometer, &axes, sizeof(axes));
00082         
00083         accelerometer->Get_X_Axes(axes);
00084         printf("LSM6DS0 [acc/mg]:      %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
00085         memcpy(&log_d->accelerometer, &axes, sizeof(axes));
00086         
00087         gyroscope->Get_G_Axes(axes);
00088         printf("LSM6DS0 [gyro/mdps]:   %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
00089         memcpy (&log_d->accelerometer, &axes, sizeof(axes));
00090         
00091         //TODO Not sure about this one, maybe use time()
00092         //log_d->date = asctime(localtime());
00093         
00094         //Send pointer to the queue
00095         osStatus *stat = (osStatus*)mail_box.put(log_d);
00096         count++;
00097         
00098         // Check for resource error
00099         if (stat == osErrorResource.osStatus) {
00100             printf("mail_box->put() Error %4Xh", stat);
00101             //Error, free up memory block
00102             mail_box.free(log_d);
00103             count--;
00104             return;
00105         }
00106     }
00107     
00108 public:
00109     ExpansionBoard() : count(0) {
00110         /* Retrieve the composing elements of the expansion board */
00111         uint8_t id;
00112         humidity_sensor->ReadID(&id);
00113         printf("HTS221  humidity & temperature    = 0x%X\r\n", id);
00114         pressure_sensor->ReadID(&id);
00115         printf("LPS25H  pressure & temperature    = 0x%X\r\n", id);
00116         magnetometer->ReadID(&id);
00117         printf("LIS3MDL magnetometer              = 0x%X\r\n", id);
00118         gyroscope->ReadID(&id);
00119         printf("LSM6DS0 accelerometer & gyroscope = 0x%X\r\n", id);
00120     }
00121     void startSampling() {
00122         //TODO For now it just spits out the data
00123         //but data must be passed to the FIFO
00124         ticker.attach(this, &ExpansionBoard::readData, T);
00125     }
00126     void stopSampling() { ticker.detach(); }
00127 };