lib para o framework sgam

Dependencies:   MPU6050 Grove_temperature

Dependents:   example_smart-grid

Files at this revision

API Documentation at this revision

Comitter:
AndersonIctus
Date:
Thu Jun 06 23:54:07 2019 -0300
Parent:
4:f21aab30658a
Commit message:
- Mudancas para por a chamada Assincrona na LIB !!

Changed in this revision

peripheral/Sensor.h Show annotated file Show diff for this revision Revisions of this file
peripheral/impl/Gyroscope.cpp Show annotated file Show diff for this revision Revisions of this file
peripheral/impl/Gyroscope.h Show annotated file Show diff for this revision Revisions of this file
peripheral/impl/Temperature.cpp Show annotated file Show diff for this revision Revisions of this file
peripheral/impl/Temperature.h Show annotated file Show diff for this revision Revisions of this file
diff -r f21aab30658a -r caecc2426bbb peripheral/Sensor.h
--- a/peripheral/Sensor.h	Mon Jun 03 23:33:00 2019 -0300
+++ b/peripheral/Sensor.h	Thu Jun 06 23:54:07 2019 -0300
@@ -10,13 +10,14 @@
     ~Sensor(){ };
 
     virtual T getValue() = 0;
-    virtual void setCallbackReadOcurred( void* (*callback)(T* value), float timeout ) = 0;
+    virtual void setCallbackReadOcurred( void (*callback_sensor)(T* value), uint32_t timeout ) = 0;
+    virtual void initializeTask() = 0;
+    virtual void finalizeTask() = 0;
 
 protected:
     T* value;
-    void* (*callback)(T* value); // callback que deve ser configurado e chamado posteriormente !
+    void (*callback_sensor)(T* value); // callback que deve ser configurado e chamado posteriormente !
 
-    virtual void run(float timeout) = 0;
     virtual void acceptDataEvent(T* data) = 0;
 };
 
diff -r f21aab30658a -r caecc2426bbb peripheral/impl/Gyroscope.cpp
--- a/peripheral/impl/Gyroscope.cpp	Mon Jun 03 23:33:00 2019 -0300
+++ b/peripheral/impl/Gyroscope.cpp	Thu Jun 06 23:54:07 2019 -0300
@@ -5,18 +5,22 @@
 
 #define LOG(args...)   // printf(args)     
 
-Gyroscope::Gyroscope(I2C i2c): mpu(i2c, MPU6050_ADDRESS_AD0_LOW) { 
+Gyroscope::Gyroscope(I2C &i2c): mpu(i2c, MPU6050_ADDRESS_AD0_LOW), timeout(10*1000) { 
     mpu.initialize();
 
-    if(mpu.testConnection()) {
-        LOG("MPU6050 test passed !!\r\n");
+    if( mpu.testConnection() ) {
+        LOG("Giroscope Initialized !!\r\n");
     } else {
-        LOG("MPU6050 test failed !!\r\n");
+        LOG("There's an error trying initialize the Gyroscope !!\r\n");
     }
+
+    value = new GyroscopeData();
 }
 
 Gyroscope::~Gyroscope() {
-    // mpu.
+    finalizeTask();
+    value->~GyroscopeData();
+    // mpu.~MPU6050();
 }
 
 void Gyroscope::getMotion(GyroscopeData* data) {
@@ -27,19 +31,36 @@
 }
 
 GyroscopeData Gyroscope::getValue() {
-    getMotion(Gyroscope::value);
-    return *(Gyroscope::value);
+    getMotion(value);
+    return *(value);
+}
+
+void Gyroscope::setCallbackReadOcurred( void (*callback_sensor)(GyroscopeData* value), uint32_t timeout = 10*1000 ) {
+    Gyroscope::callback_sensor = callback_sensor;
+    Gyroscope::timeout = timeout;
 }
 
-void Gyroscope::setCallbackReadOcurred( void* (*callback)(GyroscopeData* value) ) {
-    Gyroscope::callback = callback;
-}
+void Gyroscope::run(void const *self_context) {
+    Gyroscope* self = (Gyroscope*)self_context;
 
-void Gyroscope::run(float timeout) {
-    // TODO: every timeout, calls acceptData !!
+    while(1) {
+        GyroscopeData val = self->getValue();
+        self->acceptDataEvent( &val );
+        ThisThread::sleep_for(self->timeout);
+    }
 }
 
 void Gyroscope::acceptDataEvent(GyroscopeData* data) {
-    if(Gyroscope::callback != NULL)
-        Gyroscope::callback(data);
+    if(callback_sensor != NULL)
+        callback_sensor(data);
 }
+
+void Gyroscope::initializeTask() { 
+    t = new Thread();
+    t->start(callback(run, this));
+}
+
+void Gyroscope::finalizeTask() {
+    if(t != NULL)
+        t->terminate();
+ }
diff -r f21aab30658a -r caecc2426bbb peripheral/impl/Gyroscope.h
--- a/peripheral/impl/Gyroscope.h	Mon Jun 03 23:33:00 2019 -0300
+++ b/peripheral/impl/Gyroscope.h	Thu Jun 06 23:54:07 2019 -0300
@@ -5,26 +5,34 @@
 #include "Sensor.h"
 #include "MPU6050.h"
 
-typedef struct {
+class GyroscopeData {
+public:
     int16_t ax, ay, az;
     int16_t gx, gy, gz;
-} GyroscopeData;
+
+    GyroscopeData(){}
+    ~GyroscopeData(){}
+};
 
 class Gyroscope: public Sensor<GyroscopeData> {
 public:
-    Gyroscope(I2C i2c);
+    Gyroscope(I2C &i2c);
     virtual ~Gyroscope();
 
     virtual GyroscopeData getValue();
-    virtual void setCallbackReadOcurred( void* (*callback)(GyroscopeData* value) );
+    virtual void setCallbackReadOcurred( void (*callback_sensor)(GyroscopeData* value), uint32_t timeout );
+    virtual void initializeTask();
+    virtual void finalizeTask();
 
 protected:
-    virtual void run(float timeout);
+    static void run(void const *self_context);
     virtual void acceptDataEvent(GyroscopeData* data);
 
 private:
     MPU6050 mpu;
-
+    Thread* t;
+    uint32_t timeout;
+    
     void getMotion(GyroscopeData* data);
 };
 
diff -r f21aab30658a -r caecc2426bbb peripheral/impl/Temperature.cpp
--- a/peripheral/impl/Temperature.cpp	Mon Jun 03 23:33:00 2019 -0300
+++ b/peripheral/impl/Temperature.cpp	Thu Jun 06 23:54:07 2019 -0300
@@ -1,13 +1,13 @@
 #include "mbed.h"
 #include "Temperature.h"
 
-Temperature::Temperature(PinName pin): temperature(pin) { 
-    // Temperature::value
+// using namespace rtos;
+
+Temperature::Temperature(PinName pin): temperature(pin), timeout(10*1000) { 
 }
 
 Temperature::~Temperature() {
-    // ~Sensor();
-
+    finalizeTask();
     temperature.~Grove_temperature();
 }
 
@@ -15,16 +15,32 @@
     return temperature.getTemperature();
 }
 
-void Temperature::setCallbackReadOcurred( void* (*callback)(float* value), float timeout ) {
-    Temperature::callback = callback;
-    // configure the run method !!
+void Temperature::setCallbackReadOcurred( void (*callback_sensor)(float* value), uint32_t timeout = 10*1000 ) {
+    Temperature::callback_sensor = callback_sensor;
+    Temperature::timeout = timeout;
 }
 
-void Temperature::run(float timeout) {
-    // TODO: Configure the readTimeOut !!
+void Temperature::run(void const *self_context) {
+    Temperature* self = (Temperature*)self_context;
+
+    while(1) {
+        float val = self->getValue();
+        self->acceptDataEvent( &val );
+        ThisThread::sleep_for(self->timeout);
+    }
 }
 
 void Temperature::acceptDataEvent(float* data) {
-    if(Temperature::callback != NULL)
-        Temperature::callback(data);
+    if(callback_sensor != NULL)
+        callback_sensor(data);
 }
+
+void Temperature::initializeTask() {
+    t = new Thread();
+    t->start(callback(run, this));
+}
+
+void Temperature::finalizeTask() {
+    if(t != NULL)
+        t->terminate();
+}
diff -r f21aab30658a -r caecc2426bbb peripheral/impl/Temperature.h
--- a/peripheral/impl/Temperature.h	Mon Jun 03 23:33:00 2019 -0300
+++ b/peripheral/impl/Temperature.h	Thu Jun 06 23:54:07 2019 -0300
@@ -11,14 +11,18 @@
     virtual ~Temperature();
 
     virtual float getValue();
-    virtual void setCallbackReadOcurred( void* (*callback)(float* value), float timeout );
+    virtual void setCallbackReadOcurred( void (*callback_sensor)(float* value), uint32_t timeout );
+    virtual void initializeTask();
+    virtual void finalizeTask();
 
 protected:
-    virtual void run(float timeout);
+    static void run(void const *self_context);
     virtual void acceptDataEvent(float* data);
 
 private:
     Grove_temperature temperature;
+    Thread* t;
+    uint32_t timeout;
 };
 
 #endif // SGAM_TEMPERATURE_H