// Diese Software testet die verschiedenen Funktionen des NUClight V3 Boards  
// BULME Graz,     
// by F. Wolf   01.11.2019
/*
                             PIN-OUT-NUClight
                                NUCLEO-L432KC
                     RGB-rot   D1|-------| VIn
                     RGB-gruen D0|       | GND
                             NRST|       | RST
                              GND|       | 5V0
                        LED1 <-D2|       | A7
                        LED2 <-D3|       | A6 -> LED7
SDA (I2C)  (MPU6050 gyro)   <- D4|       | A5
SCL (I2C)  (MPU6050 gyro)   <- D5|       | A4
                        LED3 <-D6|       | A3 -> POTI
                            nc D7|       | A2 -> Taster
                            nc D8|       | A1 -> Taster
                         LED4  D9|       | A0 -> DS18B20
                  RGB-blau <- D10|       | ARF
                      LED5 <- D11|       | 3V0
                      LED6 <- D12|-------| D13 -> LED8
        
 RGB LED aktiv hight (1)       
 */   
#include "mbed.h"

//****** Definitionen **********************
Serial pc(SERIAL_TX,SERIAL_RX);  //nucleo

// Definition der Taster (Switches)
InterruptIn sw1(A1); 
AnalogIn   ain(A3);

// Definition der 8 LED's
DigitalOut led1(D2);
DigitalOut led2(D3);
DigitalOut led3(D6);
DigitalOut led4(D9);
DigitalOut led5(D11);
DigitalOut led6(D12);
DigitalOut led7(A6);
DigitalOut led8(D13);   // on Board LED

// RGB LED
PwmOut RGBr(D1);  // rote LED

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

// **********  Hauptprogramm ************** 
int main()
{
    pc.printf("******** TEST-SW *************\r\n"); //HTerm Welcome Message  
    pc.printf("TEST-AIN-PWM\r\n"); //HTerm Welcome Message 
    pc.printf("*******************************\r\n"); //HTerm Welcome Message 
    while(1)
    {
        // attach the address of the flip function to the rising edge
        sw1.fall(&LEDonoff);     //LED ON OFF - Test Unterprogramm aufrufen
        pc.printf("percentage: %3.0f%%\r\n", ain.read()*100.0f);  
        RGBr.period_ms(20.0f);  // 20 mili second period
        RGBr.pulsewidth_ms(ain.read()*10.0f);  //  5 mili second pulse (on)
    }
}


// TEST-LED-on-off
//------------------------------------
void LEDonoff()
{
    led1=!led1;     //Led1 einschalten
    led2=!led2;     //Led1 einschalten
    led3=!led3;     //Led1 einschalten
    led4=!led4;     //Led1 einschalten
    led5=!led5;     //Led1 einschalten
    led6=!led6;     //Led1 einschalten
    led7=!led7;     //Led1 einschalten
    led8=!led8;     //Led1 einschalten
}
/********************   ENDE  ***********************/