example

Dependencies:   sgam-lib

main.cpp

Committer:
AndersonIctus
Date:
2019-06-03
Revision:
15:acf6c9c6b15d
Parent:
12:04b0ccb40acc
Child:
17:a5e2d3e6a634

File content as of revision 15:acf6c9c6b15d:

#include "mbed.h"
#include "Temperature.h"
// #include "Gyroscope.h"

Serial pc(USBTX, USBRX);        // SAIDA SERIAL PADRÃO

Temperature temp (A1); // Instancia de Temperatura

// I2C    i2c(I2C_SDA, I2C_SCL);       // PORTA I2C
// Gyroscope giro(i2c); // Instancia do Giroscopio

// 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)

// FILAS PARA EVENTOS
EventQueue eventQueue1;
EventQueue eventQueue2;

// --------------------------------------------------------------------------------- //
void fazLeituraTemperatura() {
    pc.printf("** Temperatura => %02.02f\r\n", temp.getValue() );
}

// void fazLeituraGyroscopio() {
//     GyroscopeData* data = giro.getValue();
//     printf("*** (g_x = %03d; g_y = %03d; g_z = %03d)       ***\r\n", data->gx, data->gy, data->gz);
// }

// --------------------------------------------------------------------------------- //
int main() {
    printf("!!!INICIO!!!\r\n");

    //////////////////
    // 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);

    // 2 - Verificar se houve alguma mudança no Giroscópio
    // Thread eventThread_2(osPriorityNormal);
    // eventThread_2.start( callback(&eventQueue2, &EventQueue::dispatch_forever) );

    // fazLeituraTemperatura();
    printf("!!!FIM DE INICIALIZACAO!!!\r\n");
    wait(osWaitForever);
}

/*
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() {
    int count = 0;
    while(count ++ < 10) {
        led1 = 1;
        wait(0.4);
        led1 = 0;
        // wait(0.2);
    }

    led1 = 0;
}

void buss_ligth() {
    led_dig = 1;
    wait(0.3);
    led_dig = 0;    
}

int main () {
    printf("!!!INICIO!!!\r\n");    

    mpu.initialize();

    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;
    }

    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);
    }

    printf("!!!FIM DE LEITURA!!!\r\n");
    return 1;
}
// */