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

    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 <pigpin.h>
#include <libpigcore.h>


pigPin::pigPin( bcm_t bcmNo, io_t modeNo, pud_t pudType )
{
    init( bcmNo, modeNo, pudType );
}


pigPin::~pigPin()
{

}


void pigPin::init( bcm_t bcmNo, io_t modeNo, pud_t pudType )
{
    bcm  = bcmNo;
    set_io_mode( modeNo );
    set_pud_mode( pudType );
}


int pigPin::get_level()
{
    return  pigpio::get_level( bcm );
}


void pigPin::set_level( level_t level )
{
    pigpio::set_level( bcm, level );
}


int pigPin::trigger_level( unsigned level, unsigned pulseLen )
{
    return pigpio::trigger_level( bcm, pulseLen, level );
}


int pigPin::wait_for_edge( pigpio::edge_t edge, double timeout )
{
    return pigpio::wait_for_edge( bcm, edge, timeout );
}


// pin config
int pigPin::set_io_mode( io_t modType )
{
    mode = modType;
    return pigpio::set_io_mode( bcm, mode );
}


int pigPin::get_io_mode()
{
    return pigpio::get_io_mode( bcm );
}


int pigPin::set_pud_mode( pud_t pudType )
{
    pud = pudType;
    return pigpio::set_pud_mode( bcm, pud );
}


int pigPin::set_glitch_filter( uint32_t glitch )
{
    return pigpio::set_glitch_filter( bcm, glitch );
}


int pigPin::set_noise_filter( uint32_t steady, uint32_t active )
{
    return pigpio::set_noise_filter( bcm, steady, active );
}

// reality interface
int pigPin::set_callback( pigCall* fn, edge_t edge )
{
    return pigpio::set_callback( bcm, fn, edge);
}

int pigPin::cancel_callback( int cbId )
{
    return pigpio::cancel_callback( cbId );
}


void pigPin::dump()
{
    std::cout << "\nPin:  " << (uint32_t) bcm << " " << std::endl;
    std::cout << "Mode: " << (uint32_t) mode << " " << std::endl;
    std::cout << "Pud:  " << (uint32_t) pud << " " << std::endl;   
}

/// ---


pigPinIn::pigPinIn()
{
    mode = pigpio::input;
}


pigPinIn::pigPinIn( pigpio::bcm_t bcmNo, pigpio::pud_t pudType )
    : pigPin{ bcmNo, pigpio::input, pudType }
{


}


pigPinIn::~pigPinIn()
{
    if(  cbId > -1 )
        pigpio::cancel_callback( cbId );
}

void pigPinIn::init( bcm_t bcmNo, pud_t pudType )
{
    pigPin::init( bcmNo, pigpio::input, pudType );
}


/// ---


pigPinOut::pigPinOut()
{
    mode = pigpio::output;
}


pigPinOut::pigPinOut( pigpio::bcm_t bcmNo, pigpio::pud_t pudType )
    : pigPin{ bcmNo, pigpio::output, pudType }
{

}


pigPinOut::~pigPinOut()
{

}

void pigPinOut::init( bcm_t bcmNo, pud_t pudType )
{
    pigPin::init( bcmNo, pigpio::output, pudType );
}


