/*****************************************************************************

    source::worx libPiGPio
    Copyright © 2022 c.holzheuer
    chris@sourceworx.org

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Uses:

    pigpiod_if2 by joan2937, pigpio @ abyz.me.uk
    https://abyz.me.uk/rpi/pigpio

    sigslot  by 2017 Pierre-Antoine Lacaze
    https://github.com/palacaze/sigslot

***************************************************************************/


#include <iostream>
#include <ctime>

#include <libpigpio.h>
#include <pigbutton.h>
#include <pigtimer.h>
#include <pigrotarydial.h>
#include <pigchrono.h>
#include <pigsonar.h>
#include <piglcd.h>
#include <pigbeeper.h>
#include <pigbuzzer.h>
#include <pigbutton.h>
#include <pigled.h>


#include <QApplication>
#include <QtGPIOMain.h>



using namespace std;
using namespace pigpio;



void on_level( level_t level )
{
    cout << "on_level:" << level << endl;
}



namespace qtgpio
{
    // LED
    [[maybe_unused]] static const char* const icnRed      = ":images/Red.png";
    [[maybe_unused]] static const char* const icnGreen    = ":images/Green.png";

    /// misc
    [[maybe_unused]] static const char* const version     = "Version 0.8.43, 19.10.2022  © 2022 chris@sourceworx.org";

}

//#define high true
[[maybe_unused]] constexpr static const char* mainVersion  = "libPiGPIO 0.2.11 / 19.10.2022 © 2022 chris@sourceworx.org";

void funktion( double state ) { std::cout << "free function: " << state << std::endl; }

int main( int argc, char** argv )
{
    Q_UNUSED(argc)
    Q_UNUSED(argv)

    //pigpio::init( "host", "port" );
    greet();

    // Blink is the “Hello World” of the microcontroller world.
    // It usually does nothing more than flash an LED or 2.
    //                                    (Gordon Henderson, WiringPi )

    //pigLCD lcd( 0x27 );
    //lcd.set_backlight( on );
    //lcd.write( 0, 0, "blink!" );

    /*
    on_level( 1 );
    on_level( true );
    on_level( pigpio::high );
    on_level( high );
    */

    /*
    // ein 'Drückschalter' an Pin GPIO 16
    pigButton button( bcm16 );
    // eine LED und ein Summer an Pin 12 und Pin 23
    pigLED LED( bcm12 );
    pigBuzzer buzzer( bcm23 );
    // Zustände verknüpfen
    button.connect_state( LED );
    button.connect_state( buzzer );
    // 'connect_state' ist die Kurzform von
    // sigState.connect( &T::change_state, &listener );
    */

    /*
    pigBeeper beeper( bcm25 );
    std::vector<double> tnv = { 262.0, 294.0, 330.0, 262.0 };
    std::vector<double> drv = { 0.5, 0.5, 0.5, 0.5 };
    beeper.play_tune( tnv, drv );
    beeper.play_tone( 295, 0.3 );
    */


    //pigRotaryDial dial( pigpio::bcm17, pigpio::bcm27, pigpio::bcm22 );
    //pigRotaryDial dial( board17, board27, board22 );

    // für die raDIYo-Verdrahtung:
    //pigRotaryDial dialA( bcm16, bcm20, bcm21 );
    pigRotaryDial dialB( bcm05, bcm13, bcm06 );

    //dialA.sigValue.connect( [&](int value){  cout << "Dial A: " << value << endl; } );
    dialB.sigValue.connect( [&](int value){  cout << "Dial B: " << value << endl; } );



    /*
    pigSonar sonar( bcm21, bcm20 );
    sonar.sigValue.connect( [&](double dist){  buzzer.buzz( 100, dist*50 ); } );
    sonar.start( 20 );

    while( loop )
    {
        double dist = sonar.distance();
        lcd.write( 0, 0, "Dist.: " + val2str( dist) + " cm" );

        if( dist < 20 )
        {
            buzzer.buzz( 100, dist*50 );
            LED.blink( dist*30 );
        }
        delay_millis( 200 );
    }

    bool loop = true;
    button.sigState.connect( [&](bool){ loop = false; } );

    while( loop )
    {
        lcd.write( 0,0,  format_time() );
        delay_seconds( 1 );
    }

    lcd.reset();

    */

    // don't return, just wait forever, handling callbacks
    wait_loop();

    // alternatively, show button as widgets
    QApplication application( argc, argv );
    QtGPIOMain window;
    window.show();
    return application.exec();



}
