// NUClight-V1-BORAD
// TEST 13.02.2019
// Diese Software testet die verschiedenen Funktionen des M0 Boards
// BULME Graz,     by F. Wolf   04.02.2019
/*
                             PIN-OUT-NUClight
                                NUCLEO-L432KC
                                 |-------|
                                 |       |
                                 |       |
                                 |       |
                        LED1 <-D2|       |
                        LED2 <-D3|       |
SDA (I2C)  (MPU6050 gyro) <- D4  |       |
SCL (I2C)  (MPU6050 gyro) <- D5  |       |
                        LED3 <-D6|       | A3 -> Taster
                                 |       | A2 -> Taster
                                 |       | A1 -> Taster
                                 |       | A0 -> DS18B20
                     RGB-g <- D10|       |
                     RGB-b <- D11|       |
                     RGB-r <- D12|-------|

 RGB LED aktiv hight (1)
 */

#include "mbed.h"
#include "MPU6050.h"
#include "DS1820.h"

#define DATA_PIN        A0  // DS18B20
#define MAX_PROBES      2

// ********   Definitionen  **********
//Serial pc(USBTX, USBRX);
Serial pc(SERIAL_TX,SERIAL_RX);

// Definition der Taster (Switches)
/*
InterruptIn  sw1(A1); //SW1
InterruptIn  sw2(A2); //SW2 -> sein IO Pin ist laut Schaltplan der P0_15
InterruptIn  sw3(A3); //SW3
*/



InterruptIn sw1(A1);
InterruptIn sw2(A2);
InterruptIn sw3(A3);


// Definition der 3 LED's
DigitalOut led1(D2);
DigitalOut led2(D3);
DigitalOut led3(D6);

DigitalOut led4(D13);  // on Board LED

// RGB LED
DigitalOut RGBb(D10);  // blaue LED
DigitalOut RGBg(D11);  // grĂ¼ne LED
DigitalOut RGBr(D12);  // rote  LED

int a;
int num_devices = 0;

//mpu6050
//creating onject of MPU6050 class
MPU6050 ark(D4,D5); // MPU6050(PinName sda, PinName scl);

DS1820* probe[MAX_PROBES];

// ********** Deklarationen **************
void RGBtest();
void LEDtest();
void LEDonoff();

// **********  Hauptprogramm **************
int main()
{
    pc.printf("Welcome to NUClight TEST V1 \r\n"); //HTerm Welcome Message


    while(1)
    {
    // attach the address of the flip function to the rising edge
    sw1.fall(&RGBtest);      //RGB-Test Unterprogramm aufrufen
    sw2.fall(&LEDonoff);     //LED ON OFF - Test Unterprogramm aufrufen
    sw3.fall(&LEDtest);      //LED-Test Unterprogramm aufrufen



          /*************** MPU6050 (gyro) TEST *******************************/

        //reading Temprature
        float temp = ark.getTemp();
        pc.printf("MPU6050-temprature = %0.2f ^C\r\n",temp);

        //reading Grometer readings
        float gyro[3];
        ark.getGyro(gyro);
        pc.printf("Gyro0=%0.3f,\tGyro1=%0.3f,\tGyro2=%0.3f\r\n",gyro[0],gyro[1],gyro[2]);

        //reading Acclerometer readings
        float acce[3];
        ark.getAccelero(acce);
        pc.printf("Acce0=%0.3f,Acce1=%0.3f,Acce2=%0.3f\r\n",acce[0],acce[1],acce[2]);

        wait(1); //wait 1000ms

//DS18B20

       // define MULTIPLE_PROBES


        DS1820* probe[MAX_PROBES];


            // Initialize the probe array to DS1820 objects
           // int num_devices = 0;
            while(DS1820::unassignedProbe(DATA_PIN)) {
                probe[num_devices] = new DS1820(DATA_PIN);
                num_devices++;
                if (num_devices == MAX_PROBES)
                    break;
            }

            printf("Found %d device(s)\r\n\n", num_devices);

                probe[0]->convertTemperature(true, DS1820::all_devices);         //Start temperature conversion, wait until ready
                for (int i = 0; itemperature());
                printf("\r\n");
                wait(1);

            wait(1);
            }
}
/**************************************************** Unterprogramme****************************************************/
/***********************************************************************************************************************/

/************************** RGB ****************************/
/***********************************************************/
void RGBtest()
{
    pc.printf("RGBTEST WEIS-ROT-MAGENTA-BLAU-TURKIS-GRUN-GELB\r\n"); //HTerm Ausgabe

        //Weiss
        RGBr = 1;   // Alle RGB LED EIN
        RGBb = 1;
        RGBg = 1;
        wait(0.2);

        //Rot
        RGBb = 0; // Blaue LED ausschalten
        RGBg = 0; // Grune LED ausschalten
        wait(0.2);

        //Purple
        RGBb = 1; // Blaue LED einschalten
        wait(0.2);

        //Blaue
        RGBr = 0; // Rote LED ausschalten
        wait(0.2);

        //Turkis
        RGBg = 1; // Grune LED einschalten
        wait(0.2);

        //Grun
        RGBb = 0; // Blaue LED ausschalten
        wait(0.2);

        //Gelb
        RGBr = 1; // Rote LED einschalten
        wait(0.2);

        //Alle ausschalten
        RGBr = 0;
        RGBb = 0;
        RGBg = 0;
}

/************************** LED ****************************/
/***********************************************************/
void LEDtest()
{
    pc.printf("LAUFLICHT UEBER 3 LED (5-mal) \r\n"); //HTerm Ausgabe
    a=0;

    RGBtest();


    while (a<5) {
        led1=1;     //Led1 einschalten
        wait(0.2);
        led1=0;     //Led1 ausschalten

        led2=1;     //Led2 einschalten
        wait(0.2);
        led2=0;     //Led2 ausschalten

        led3=1;     //Led3 einschalten
        wait(0.2);
        led3=0;     //Led3 ausschalten

        led4=!led4;
        a++;
        }
}

/************************ Sensoren *************************/
/***********************************************************/
void LEDonoff()
{
    /*
    led1=!led1;     //Led1 einschalten
    led2=!led2;     //Led1 einschalten
    led3=!led3;     //Led1 einschalten
*/

  led4=!led4;     //Led1 einschalten
}

/***********/
/********************   ENDE  ***********************/