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

    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.h
    by joan2937, pigpio @ abyz.me.uk
    https://abyz.me.uk/rpi/pigpio

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

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


#ifndef LIBPIGLINUX_H
#define LIBPIGLINUX_H

#if defined(unix) || defined(__unix__) || defined(__unix)

#include <libpigpio.h>
#include <libpigcore.h>

namespace pigpio
{
    class LIBPIGPIO_EXPORT pigLinux : public pigCore
    {

    friend pigCore& get_instance();

    public:

        pigLinux( const pigLinux& )       = delete;
        void operator=( const pigLinux& ) = delete;

        virtual ~pigLinux();

        int init( const std::string& host ="", const std::string& port="" ) override;

        // pin config
        int set_io_mode( bcm_t bcm, io_t mode ) override;
        int get_io_mode( bcm_t bcm ) override;
        int set_pud_mode( bcm_t bcm, pud_t pud ) override;
        int set_glitch_filter( bcm_t bcm, uint32_t glitch = GlitchDef ) override;
        int set_noise_filter( bcm_t bcm, uint32_t steady, uint32_t active ) override;

        // pin operations
        int get_level(  bcm_t bcm ) override;
        int set_level(  bcm_t bcm, level_t level ) override;
        int wait_for_edge( bcm_t bcm, edge_t edge, double timeout = TimeoutDef ) override;
        int trigger_level( bcm_t bcm, level_t level, uint32_t pulseLen )  override;

        // interface
        int set_callback( bcm_t bcm, pigCall* user, edge_t edge = either ) override;
        int cancel_callback( int cbId ) override;

        // software PWM
        /*
        set_PWM_dutycycle	Start/stop PWM pulses on a GPIO
        set_PWM_frequency	Configure PWM frequency for a GPIO
        set_PWM_range	Configure PWM range for a GPIO
        get_PWM_dutycycle	Get the PWM dutycycle in use on a GPIO
        get_PWM_frequency	Get configured PWM frequency for a GPIO
        get_PWM_range	Get configured PWM range for a GPIO
        get_PWM_real_range	Get underlying PWM range for a GPIO
        */

        
        // i2c
        int i2c_open( pigI2C& i2c ) override;
        int i2c_close( pigI2C& i2c ) override;
    /*
        int i2c_write_quick( pigI2C& i2c, unsigned bit ) override;
        int i2c_write_byte( pigI2C& i2c, unsigned bVal ) override;
        int i2c_write_byte_data( pigI2C& i2c, unsigned reg, unsigned val ) override;
        int i2c_write_word_data( pigI2C& i2c, unsigned reg, unsigned val ) override;
        int i2c_write_block_data( pigI2C& i2c, unsigned reg, char* buf, unsigned count ) override;
        int i2c_write_i2c_block_data( pigI2C& i2c, unsigned reg, char* buf, unsigned count ) override;
        */

        int i2c_write_device( pigI2C& i2c, char* buf, unsigned count ) override;
/*
        int i2c_read_byte( pigI2C& i2c ) override;
        int i2c_read_byte_data( pigI2C& i2c, unsigned reg ) override;
        int i2c_read_word_data( pigI2C& i2c, unsigned reg ) override;
        int i2c_read_block_data( pigI2C& i2c, unsigned reg, char* buf ) override;
        int i2c_read_i2c_block_data( pigI2C& i2c, unsigned reg, char* buf, unsigned count ) override;
        int i2c_read_device( pigI2C& i2c, char* buf, unsigned count ) override;

        int i2c_process_call( pigI2C& i2c, unsigned i2c_reg, unsigned wVal ) override;
        int i2c_block_process_call( pigI2C& i2c,unsigned i2c_reg, char* buf, unsigned count ) override;
        int i2c_zip( pigI2C& i2c, char* inBuf, unsigned inLen, char* outBuf, unsigned outLen ) override;
        */

        // advanced
        int hardware_PWM( bcm_t bcm, uint32_t PWMfreq, uint32_t PWMduty )  override;
        int hardware_clock( bcm_t bcm, uint32_t clkfreq ) override;
        uint32_t get_current_tick() override;

        // info
        int get_hardware_revision() override;
        int get_pigpio_version() override;
        unsigned pigpiod_if_version() override;

        int show_error( int errNo ) override;

    private:

       pigLinux() noexcept;

    };

} // namepace

#endif // def linux

#endif // LIBPIGLINUX_H
