trabalho

Dependencies:   X_NUCLEO_IKS01A1 mbed-rtos mbed

Fork of HelloWorld_IKS01A1 by ST

Files at this revision

API Documentation at this revision

Comitter:
stwykd
Date:
Sun May 08 14:49:34 2016 +0000
Parent:
27:0bbc1d575986
Child:
29:f96590e6058d
Commit message:
Implement consumer, producer with mail box. Buffer can now be deleted

Changed in this revision

expansionBoard.cpp Show annotated file Show diff for this revision Revisions of this file
mailBox.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/expansionBoard.cpp	Sun May 08 09:18:37 2016 +0000
+++ b/expansionBoard.cpp	Sun May 08 14:49:34 2016 +0000
@@ -1,6 +1,6 @@
 #include "mbed.h"
 #include "x_nucleo_iks01a1.h"
-#include "buffer.cpp"
+#include "mailBox.cpp"
 
 using namespace std;
 
@@ -46,39 +46,57 @@
         return str;
     }
     
+    //This is the producer
     void readData() {
         float value1, value2;
         char buffer1[32], buffer2[32];
         int32_t axes[3];
-        log_data log_d;
-
+        
+        log_data* log_d = mail_box.alloc();
+        //TODO Too small!?
+        log_d->id = rand() % 255;
+        
+        //TODO Out of memory, specs say to delete oldest sample
+        if (log_d == NULL) return;
+        
         temp_sensor1->GetTemperature(&value1);
         humidity_sensor->GetHumidity(&value2);
         printf("HTS221: [temp] %7s°C,   [hum] %s%%\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2));
-        log_d.tempCelsius = value1;
-        log_d.humidity = value2;
+        log_d->tempCelsius = value1;
+        log_d->humidity = value2;
         
         temp_sensor2->GetFahrenheit(&value1);
         pressure_sensor->GetPressure(&value2);
         printf("LPS25H: [temp] %7s°F, [press] %smbar\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2));
-        log_d.tempFarenheit = value1;
-        log_d.pressure = value2;
+        log_d->tempFarenheit = value1;
+        log_d->pressure = value2;
         printf("---\r\n");
 
         magnetometer->Get_M_Axes(axes);
         printf("LIS3MDL [mag/mgauss]:  %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
-        memcpy(&log_d.magnetometer, &axes, sizeof(axes));
+        memcpy(&log_d->magnetometer, &axes, sizeof(axes));
         
         accelerometer->Get_X_Axes(axes);
         printf("LSM6DS0 [acc/mg]:      %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
-        memcpy(&log_d.accelerometer, &axes, sizeof(axes));
+        memcpy(&log_d->accelerometer, &axes, sizeof(axes));
         
         gyroscope->Get_G_Axes(axes);
         printf("LSM6DS0 [gyro/mdps]:   %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
-        memcpy (&log_d.accelerometer, &axes, sizeof(axes));
+        memcpy (&log_d->accelerometer, &axes, sizeof(axes));
         
         //TODO Not sure about this one, maybe use time()
-        //log_d.date = asctime(localtime());
+        //log_d->date = asctime(localtime());
+        
+        //Send pointer to the queue
+        osStatus stat = mail_box.put(log_d);
+        
+        // Check for resource error
+        if (stat == osErrorResource) {
+            printf("mail_box->put() Error %4Xh", stat);
+            //Error, free up memory block
+            mail_box.free(log_d);
+            return;
+        }
     }
     
 public:
--- a/mailBox.cpp	Sun May 08 09:18:37 2016 +0000
+++ b/mailBox.cpp	Sun May 08 14:49:34 2016 +0000
@@ -1,45 +1,18 @@
 #include "mbed.h"
 #include "rtos.h"
-#include <time.h>
-//#include <main.cpp>
- 
- class MailBox{
- 
-    public:
-     typedef struct {
+
+#define QUEUESIZE 100
+
+typedef struct {
         uint8_t   id;
-        float    tempCelcius;
+        float    tempCelsius;
         float    tempFarenheit;
         float    humidity;
         float    pressure;
-        int      accelerometer;
-        int      gyroscope;
-        int      magnetometer;
+        int32_t  accelerometer[3];
+        int32_t  gyroscope[3];
+        int32_t  magnetometer[3];
         char*    date;
     } log_data;
-
-    Mail<log_data, 120> mail_box;
-
-    void send_thread (log_data newLog) {
-        while (true) {
-            log_data *log = mail_box.alloc();
-            log->tempCelcius = newLog.tempCelcius;
-            log->tempFarenheit = newLog.tempFarenheit;
-            log->humidity = newLog.humidity;
-            log->pressure = newLog.pressure;
-            log->accelerometer = newLog.accelerometer;
-            log->gyroscope = newLog.gyroscope;
-            log->magnetometer = newLog.magnetometer;
-            
-            //TODO I get a weird error because of include <main>
-            //You need that to include t
-            //Not solved it yet, but you can do it by moving t or something
-            //For the date represetation, that statement does it
-            //log->date = asctime(&t);
-
-            mail_box.put(log);
-            Thread::wait(1000);
-        }
-    }
-};
- 
+    
+Mail<log_data, QUEUESIZE> mail_box;
\ No newline at end of file
--- a/main.cpp	Sun May 08 09:18:37 2016 +0000
+++ b/main.cpp	Sun May 08 14:49:34 2016 +0000
@@ -17,6 +17,25 @@
 #include <stdexcept>
 
 ExpansionBoard e;
+thread *t;
+
+void getData(const void*){
+    while(true){
+        //Block on queuefor at most 5s if no data is available
+        osEvent event = mail_box.get(5000);
+        
+        if(event.status == osEventTimeout) {
+            printf("mail_box.get %02x, Timeout error", event.status);
+            return;
+        }
+        else if (event.status == osEventMail) {
+            // Successful, store log_data
+            //TODO Store it somewhere
+            log_data *log_d = (log_d) event.value.p;
+            mail_box.free(log_d);
+        }
+    }
+}
 
 int main() {
 
@@ -116,14 +135,11 @@
     }
     else if(strcmp("LOGGING", command)==0) {
         scanf("%s", arg);
-        if (strcmp("ON", arg)==0) {
-            //startLogging();
-            printf("LOGGING ON");
-        }
-        else if (strcmp("OFF", arg)==0) {
-            //stopLogging();
+        if (strcmp("ON", arg)==0)
+            startLogging();
+        else if (strcmp("OFF", arg)==0)
+            stopLogging();
             printf("LOGGING OFF");
-        }
         else printf("The argument is invalid");
     }
     else printf("There is no command matching. Please try again");