Anderson Cunha / Mbed OS example_smart-grid

Dependencies:   sgam-lib

Files at this revision

API Documentation at this revision

Comitter:
AndersonIctus
Date:
Thu Jun 06 23:55:57 2019 -0300
Parent:
16:22ed771ebe4c
Child:
18:422bbe7a7e47
Commit message:
Uso da lib sgam para fazer chamada assincronas a partir de call back !!

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
sgam-lib.lib Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue Jun 04 02:36:14 2019 +0000
+++ b/main.cpp	Thu Jun 06 23:55:57 2019 -0300
@@ -1,140 +1,173 @@
 #include "mbed.h"
 #include "Temperature.h"
-// #include "Gyroscope.h"
+#include "Gyroscope.h"
+
+#define ONE_SEC         1000
 
 Serial pc(USBTX, USBRX);        // SAIDA SERIAL PADRÃO
 
-Temperature temp (A1); // Instancia de Temperatura
+Temperature temp(A1); // Instancia de Temperatura
 
-// I2C    i2c(I2C_SDA, I2C_SCL);       // PORTA I2C
-// Gyroscope giro(i2c); // Instancia do Giroscopio
+I2C    i2c(I2C_SDA, I2C_SCL);       // PORTA I2C
+Gyroscope giro(i2c);                // Instancia do Giroscopio
+GyroscopeData* data_anterior; //Guarda a ultima leitura apra ter uma base das mudanças !!
+int mudancas = 0;
 
 // LEDs para serem acesas 
-// DigitalOut led1 (LED1);         // LED 1 de 3 DO BOARD
-// DigitalOut led_dig(D6);         // LED 1 da porta Digital 6 (A placa tem D6 e D8)
+DigitalOut led1 (LED1);         // LED 1 de 3 DO BOARD
+DigitalOut led2 (LED2);         // LED 2 de 3 DO BOARD
+DigitalOut led3 (LED3);         // LED 3 de 3 DO BOARD
+DigitalOut led_dig(D6);         // LED 1 da porta Digital 6 (A placa tem D6 e D8)
 
-// FILAS PARA EVENTOS
-EventQueue eventQueue1;
-EventQueue eventQueue2;
+#define LOG(args...)    pc.printf(args)
 
 // --------------------------------------------------------------------------------- //
-void fazLeituraTemperatura() {
-    pc.printf("** Temperatura => %02.02f\r\n", temp.getValue() );
-}
+// HEADERS  das funcoes !!
+void sem_movimento_leds();
+void sinaliza_led(int num_led);
+bool comparaPosicao(int16_t data, int16_t data_ant, int16_t dif);
 
-// void fazLeituraGyroscopio() {
-//     GyroscopeData* data = giro.getValue();
-//     printf("*** (g_x = %03d; g_y = %03d; g_z = %03d)       ***\r\n", data->gx, data->gy, data->gz);
-// }
+void recebeTemperatura(float* temperatura);
+void fazLeituraGyroscopio(GyroscopeData* data);
 
+// METODO PRINCIPAL !!
 // --------------------------------------------------------------------------------- //
 int main() {
-    printf("!!!INICIO!!!\r\n");
+    LOG("!!!INICIO!!!\r\n");
+    sem_movimento_leds();
 
     //////////////////
     // 1 -> Fazer um temporizador que fica lendo a temperatura a cada 5 segundos !!
-    Thread eventThread(osPriorityNormal);
-    eventThread.start( callback(&eventQueue1, &EventQueue::dispatch_forever) );
- 
-    Ticker ledTicker;
-    ledTicker.attach(eventQueue1.event(&fazLeituraTemperatura), 3.0f);
+    // Chama o callback a cada 4 SEGUNDOS !
+    temp.setCallbackReadOcurred(&recebeTemperatura, 4*ONE_SEC);
 
+    //////////////////
     // 2 - Verificar se houve alguma mudança no Giroscópio
-    // Thread eventThread_2(osPriorityNormal);
-    // eventThread_2.start( callback(&eventQueue2, &EventQueue::dispatch_forever) );
+    giro.setCallbackReadOcurred(&fazLeituraGyroscopio, ONE_SEC); // leituras a cada um segundo !
+
+    //////////////////
+    // 3 - INICIALIZA AS TASKS ASSINCRONAS !!
+    giro.initializeTask();
+    temp.initializeTask();
 
-    // fazLeituraTemperatura();
-    printf("!!!FIM DE INICIALIZACAO!!!\r\n");
-    wait(osWaitForever);
+    LOG("!!!FIM DE INICIALIZACAO!!!\r\n");
+    int count = 0;
+    while(count++ < 6) wait(10); // WAIT ONE MINUTE
+
+    temp.~Temperature();
+    giro.~Gyroscope();
+    pc.printf("!!! ----------- TEMINADO -----------!!!\r\n");
+
+    // APAGANDO LEDS !
+    led_dig = 0;
+    sem_movimento_leds();
 }
 
-/*
-DigitalOut led1 (LED1);         // LED 1 de 3 DO BOARD
-DigitalOut led_dig(D6);         // LED 1 da porta Digital 6 (A placa tem D6 e D8)
-
-Grove_temperature temp(A1);     // Deve ser ligado na Porta ANALOGICA 1 
-Serial pc(USBTX, USBRX);        // SAIDA SERIAL PADRÃO
-
-// Sensor de movimento !!!
-I2C    i2c(I2C_SDA, I2C_SCL);       // PORTA I2C
-MPU6050 mpu(i2c, MPU6050_ADDRESS_AD0_LOW);
-
-// Constantes usadas no sensor de movimento !
-int16_t ax, ay, az;
-int16_t gx, gy, gz;
-
-void rodarLed() {
+// Outros metodos !!
+// --------------------------------------------------------------------------------- //
+// Pisca os led para informar q está sem movimento !!
+void sem_movimento_leds() {
     int count = 0;
-    while(count ++ < 10) {
-        led1 = 1;
-        wait(0.4);
+    while(count++ >= 2) {
         led1 = 0;
-        // wait(0.2);
+        led2 = 0;
+        led3 = 0;
+        ThisThread::sleep_for(1000);
+        led1 = 1;
+        led2 = 1;
+        led3 = 1;
+        ThisThread::sleep_for(1500);     
     }
-
     led1 = 0;
+    led2 = 0;
+    led3 = 0;
 }
 
-void buss_ligth() {
-    led_dig = 1;
-    wait(0.3);
-    led_dig = 0;    
+// Vai ligar o LED conforme o que tiver mudado
+void sinaliza_led(int num_led) {
+    led1 = 0;
+    led2 = 0;
+    led3 = 0;
+    switch(num_led) {
+        case 1: led1 = 1; break;
+        case 2: led2 = 1; break;
+        case 3: led3 = 1; break;
+        default:
+            break;
+    }
 }
 
-int main () {
-    printf("!!!INICIO!!!\r\n");    
+// faz a comparação para saber se estar dentro dos parâmetros esperados !!
+bool comparaPosicao(int16_t data, int16_t data_ant, int16_t dif) { 
+    float menor = 0;
+    float maior = 0;
+    if(data_ant < 0) {
+        maior = data_ant + (dif*-1);
+        menor = data_ant - (dif*-1);
+    } else {
+        maior = data_ant + dif;
+        menor = data_ant - dif;
+    }
 
-    mpu.initialize();
+    if( data > maior || data < menor ) {
+        return true; // Se a data estiver fora em algum dos limites percentuais
+    }
 
-    bool mpu6050TestResult = mpu.testConnection();
-    if(mpu6050TestResult) {
-        printf("MPU6050 test passed \r\n");
-    } else {
-        printf("MPU6050 test failed \r\n");
-        printf("FIM \r\n");
-        return 0;
+    return false;
+}
+
+void fazLeituraGyroscopio(GyroscopeData* data) {
+    int mud = 0;
+    if(data_anterior == NULL) {
+        data_anterior = data;
+        mud = 0;
+    } else if( comparaPosicao(data->gz, data_anterior->gz, 300) ) {
+        data_anterior = data;
+        mud = 3;
+    } else if( comparaPosicao(data->gy, data_anterior->gy, 300)) {
+        data_anterior = data;
+        mud = 2;
+    } else if( comparaPosicao(data->gx, data_anterior->gx, 800) ) {
+        data_anterior = data;
+        mud = 1;
+    }  else {
+        // SEM MOVIMENTACAO ...
     }
 
-    int count = 0;
-    float tempValue = 0;
-    char buffer_giro[100];
-    while(count ++ < 25) {
-        // ------------------------------------------------------ //
-        mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
-
-        //sabe calibrar o gyroscópio!  
-        if ( gz > 150 ) { 
-            sprintf(buffer_giro, "Movimentou Z .... gy_z = %d", gz);
-        } else if ( gy > 50 ){             
-            sprintf(buffer_giro, "Movimentou Y .... gy_y = %d",gy);
-        } else if ( gx < -370 ){            
-            sprintf(buffer_giro, "Movimentou X .... gy_x = %d",gx);
-        } else {
-            sprintf(buffer_giro, "Sem movimento!");
-        }
-        
-        // ------------------------------------------------------ //
-        tempValue = temp.getTemperature();        
-        // Acende o LED quando está acima do valor indicado !!
-        if(tempValue > 30.0) {
-            led_dig = 1;
-        } else {
-            led_dig = 0;
-        }
-        // ------------------------------------------------------ //
-
-        printf("******************* ROLL -> %02d ********************\r\n", count);
-        printf("*** Giro -> %-35s ***\r\n", buffer_giro);
-        printf("*** (gy_x = %03d; gy_y = %03d; gy_z = %03d)       ***\r\n", gx, gy, gz);
-        printf("*** Temperatura -> %02.02f                        ***\r\n", tempValue);
-        printf("***************************************************\r\n\r\n");
-
-        wait(2);
+    switch(mud) {
+        default:
+        case 0: // sem mudanças (imprime somente uma vez a cada 10 vezes sem mudanças consecutivas !!)
+            if( mudancas++ >= 10 ) {
+                LOG("Sem movimento! \r\n");
+                mudancas = 0;
+                sem_movimento_leds();
+            }
+            break;
+        case 1: // mudou eixo X
+            LOG("Movimentou X .... gy_x = %d\r\n", data->gx);            
+            break;
+        case 2: // Mudou eixo Y
+            LOG("Movimentou Y .... gy_y = %d\r\n", data->gy);
+            break;
+        case 3: // Mudou eixo Z
+            LOG("Movimentou Z .... gy_z = %d\r\n", data->gz);            
+            break;
     }
 
-    printf("!!!FIM DE LEITURA!!!\r\n");
-    return 1;
+    if(mud != 0)
+        mudancas = 0;
+
+    sinaliza_led(mud);
+
+    // LOG("*** GIRO -> (g_x = %03d; g_y = %03d; g_z = %03d)  ***\r\n", data->gx, data->gy, data->gz);
 }
-// */
 
-
+void recebeTemperatura(float* temperatura) {
+    LOG("** Temperatura RECEBIDA => %02.02f\r\n", *temperatura );
+    if(*temperatura > 30) { // ASCENDER LED !!
+        LOG("** Temperatura ACIMA DO PERMITIDO !!\r\n");
+        led_dig = 1;
+    } else {    // APAGA O LED 
+        led_dig = 0;
+    }
+}
--- a/sgam-lib.lib	Tue Jun 04 02:36:14 2019 +0000
+++ b/sgam-lib.lib	Thu Jun 06 23:55:57 2019 -0300
@@ -1,1 +1,1 @@
-https://os.mbed.com/users/AndersonIctus/code/sgam-lib/#f21aab30658a
+https://os.mbed.com/users/AndersonIctus/code/sgam-lib/#caecc2426bbb