Files
libpigpio/libpiglinux.cpp
2025-08-05 22:33:23 +02:00

408 lines
9.5 KiB
C++

#if defined(unix) || defined(__unix__) || defined(__unix)
#include <iostream>
#include <unistd.h>
#include <pigpiod_if2.h>
#include <libpiglinux.h>
#include <pigi2c.h>
using namespace pigpio;
pigLinux::pigLinux() noexcept
{
//::gpioInitialise();
/// Connect to Pi.
constexpr char* optHost = nullptr;
constexpr char* optPort = nullptr;
_Pi = ::pigpio_start( optHost, optPort );
if( _Pi < 0 )
std::cout << "-- pigLinux(): FAIL" << std::endl;
}
pigLinux::~pigLinux()
{
//::gpioTerminate();
::pigpio_stop( _Pi );
}
int pigLinux::init( const std::string& host, const std::string& port )
{
//::gpioInitialise();
/// Connect to Pi.
_Pi = ::pigpio_start( host.c_str(), port.c_str() );
if( _Pi < 0 )
std::cout << "-- pigLinux(): FAIL" << std::endl;
return _Pi;
}
///
/// pin config
///
int pigLinux::set_io_mode( bcm_t bcm, io_t mode )
{
int result = ::set_mode( _Pi, (unsigned) bcm, (unsigned) mode );
if( result < 0 )
return show_error( result );
return result;
}
int pigLinux::get_io_mode( bcm_t bcm )
{
int result = ::get_mode( _Pi, (unsigned) bcm );
if( result < 0 )
return show_error( result );
return result;
}
int pigLinux::set_pud_mode( bcm_t bcm, pud_t pud )
{
int result = ::set_pull_up_down( _Pi, (unsigned) bcm, (unsigned) pud );
if( result < 0 )
return show_error( result );
return result;
}
int pigLinux::set_glitch_filter( bcm_t bcm , uint32_t glitch )
{
int result = ::set_glitch_filter( _Pi, (unsigned) bcm, glitch );
if( result < 0 )
return show_error( result );
return result;
}
//Sets a noise filter on a GPIO.
int pigLinux::set_noise_filter( bcm_t bcm , unsigned steady, unsigned active )
{
int result = ::set_noise_filter( _Pi, (unsigned) bcm, steady, active );
if( result < 0 )
return show_error( result );
return result;
}
///
/// pin operations
///
int pigLinux::get_level( bcm_t bcm )
{
return ::gpio_read( _Pi, (unsigned) bcm );
}
int pigLinux::set_level( bcm_t bcm , level_t level )
{
int result = ::gpio_write( _Pi, (unsigned) bcm, level ); ;
if( result < 0 )
return show_error( result );
return result;
}
//This function sends a trigger pulse to a GPIO. The GPIO is set to level for pulseLen microseconds and then reset to not level.
int pigLinux::trigger_level( bcm_t bcm , level_t level, uint32_t pulseLen )
{
//int trigger_level(int pi, unsigned user_gpio, unsigned pulseLen, unsigned level)
int result = ::gpio_trigger( _Pi, (unsigned) bcm, pulseLen, level ); ;
if( result < 0 )
return show_error( result );
return result;
}
int pigLinux::wait_for_edge( bcm_t bcm , edge_t edge, double tout )
{
//std::cout << " --pigLinux::wait_for_edge: " << (uint32_t) bcm << " : " << (uint32_t) edge << std::endl;
int result = ::wait_for_edge( _Pi, (unsigned) bcm, (unsigned) edge, tout );
if( result < 0 )
return show_error( result );
return result;
}
int pigLinux::set_callback( bcm_t bcm, pigCall* fn, edge_t edge )
{
//std::cout << " --pigLinux::set_callback: " << (uint32_t) bcm << " : " << (uint32_t) edge << std::endl;
/// typedef int (*CBFuncEx_t)(int pi, unsigned user_gpio, unsigned level, uint32_t tick, int * userdata );
///int callback_ex(int pi, unsigned user_gpio, unsigned edge, CBFuncEx_t f, int *userdata)
//int result = cbId = ::callback_ex( _Pi, (unsigned) bcm, (unsigned) edge, trigger, widget );
//int result = ::callback_ex( _Pi, (unsigned) bcm, (unsigned) edge, trigger, widget );
int result = ::callback_ex( _Pi, (unsigned) bcm, (unsigned) edge, trigger, fn );
if( result < 0 )
return show_error( result );
return result;
}
int pigLinux::cancel_callback( int cbId )
{
int result = ::callback_cancel( cbId );
if( result < 0 )
return show_error( result );
return result;
}
///
/// ic2
///
int pigLinux::i2c_open( pigI2C& i2c )
{
int result = i2c.handle = ::i2c_open( _Pi, i2c.bus, i2c.addr, 0 );
if( result < 0 )
return show_error( result );
return result;
}
int pigLinux::i2c_close( pigI2C& i2c )
{
int result = ::i2c_close( _Pi, i2c.handle );
if( result < 0 )
return show_error( result );
return result;
}
/*
int pigLinux::i2c_write_quick( pigI2C& i2c, unsigned bit )
{
int result = ::i2c_write_quick( _Pi, i2c.handle, bit );
if( result < 0 )
return show_error( result );
return result;
}
int pigLinux::i2c_write_byte( pigI2C& i2c, unsigned bVal )
{
int result = ::i2c_write_byte( _Pi, i2c.handle, bVal );
if( result < 0 )
return show_error( result );
return result;
}
int pigLinux::i2c_write_byte_data( pigI2C& i2c, unsigned reg, unsigned val )
{
int result = ::i2c_write_byte_data( _Pi, i2c.handle, reg, val );
if( result < 0 )
return show_error( result );
return result;
}
int pigLinux::i2c_write_word_data( pigI2C& i2c, unsigned reg, unsigned val )
{
int result = ::i2c_write_word_data( _Pi, i2c.handle, reg, val );
if( result < 0 )
return show_error( result );
return result;
}
int pigLinux::i2c_write_block_data( pigI2C& i2c, unsigned reg, char* buf, unsigned count )
{
int result = ::i2c_write_block_data( _Pi, i2c.handle, reg, buf, count );
if( result < 0 )
return show_error( result );
return result;
}
int pigLinux::i2c_write_i2c_block_data( pigI2C& i2c, unsigned reg, char* buf, unsigned count )
{
int result = ::i2c_write_i2c_block_data( _Pi, i2c.handle, reg, buf, count );
if( result < 0 )
return show_error( result );
return result;
}
*/
int pigLinux::i2c_write_device( pigI2C& i2c, char* buf, unsigned count )
{
int result = ::i2c_write_device( _Pi, i2c.handle, buf, count );
if( result < 0 )
return show_error( result );
return result;
}
/*
int pigLinux::i2c_read_byte( pigI2C& i2c )
{
int result = ::i2c_read_byte( _Pi, i2c.handle );
if( result < 0 )
return show_error( result );
return result;
}
int pigLinux::i2c_read_byte_data( pigI2C& i2c, unsigned reg )
{
int result = ::i2c_read_byte_data( _Pi, i2c.handle, reg );
if( result < 0 )
return show_error( result );
return result;
}
int pigLinux::i2c_read_word_data( pigI2C& i2c, unsigned reg )
{
int result = ::i2c_read_word_data( _Pi, i2c.handle, reg );
if( result < 0 )
return show_error( result );
return result;
}
int pigLinux::i2c_read_block_data( pigI2C& i2c, unsigned reg, char* buf )
{
int result = ::i2c_read_block_data( _Pi, i2c.handle, reg, buf );
if( result < 0 )
return show_error( result );
return result;
}
int pigLinux::i2c_read_i2c_block_data( pigI2C& i2c, unsigned reg, char* buf, unsigned count )
{
int result = ::i2c_read_i2c_block_data( _Pi, i2c.handle, reg, buf, count );
if( result < 0 )
return show_error( result );
return result;
}
int pigLinux::i2c_read_device( pigI2C& i2c, char* buf, unsigned count )
{
int result = ::i2c_read_device( _Pi, i2c.handle, buf, count );
if( result < 0 )
return show_error( result );
return result;
}
int pigLinux::i2c_process_call( pigI2C& i2c, unsigned reg, unsigned wVal )
{
int result = ::i2c_process_call( _Pi, i2c.handle, reg, wVal );
if( result < 0 )
return show_error( result );
return result;
}
int pigLinux::i2c_block_process_call( pigI2C& i2c,unsigned reg, char* buf, unsigned count )
{
int result = ::i2c_block_process_call( _Pi, i2c.handle, reg, buf, count );
if( result < 0 )
return show_error( result );
return result;
}
int pigLinux::i2c_zip( pigI2C& i2c, char *inBuf, unsigned inLen, char *outBuf, unsigned outLen )
{
int result = ::i2c_zip( _Pi, i2c.handle, inBuf, inLen, outBuf, outLen );
if( result < 0 )
return show_error( result );
return result;
}
*/
///
/// advanced
///
//Starts hardware PWM on a GPIO at the specified frequency and dutycycle. Frequencies above 30MHz are unlikely to work.
int pigLinux::hardware_PWM( bcm_t bcm , unsigned PWMfreq, uint32_t PWMduty )
{
//int hardware_PWM(int pi, unsigned gpio, unsigned PWMfreq, uint32_t PWMduty)
int result = ::hardware_PWM( _Pi, (unsigned) bcm, PWMfreq, PWMduty );
if( result < 0 )
return show_error( result );
return result;
}
//Starts a hardware clock on a GPIO at the specified frequency. Frequencies above 30MHz are unlikely to work.
int pigLinux::hardware_clock( bcm_t bcm , unsigned clkfreq )
{
//int hardware_clock(int pi, unsigned gpio, unsigned clkfreq)
int result = ::hardware_clock( _Pi, (unsigned) bcm, clkfreq );
if( result < 0 )
return show_error( result );
return result;
}
uint32_t pigLinux::get_current_tick()
{
//int hardware_clock(int pi, unsigned gpio, unsigned clkfreq)
return ::get_current_tick( _Pi );
}
///
/// convenience
///
int pigLinux::show_error( int error )
{
std::cout << "Error: " << ::pigpio_error( error ) << std::endl;
return error;
}
/// ---
//Get the Pi's hardware revision number.
int pigLinux::get_hardware_revision()
{
int result = ::get_hardware_revision( _Pi );
if( result < 0 )
return show_error( result );
return result;
}
//Returns the pigpio version.
int pigLinux::get_pigpio_version()
{
int result = ::get_pigpio_version( _Pi );
if( result < 0 )
return show_error( result );
return result;
}
//Return the pigpiod_if2 version.
unsigned pigLinux::pigpiod_if_version()
{
//unsigned pigpiod_if_version(void)
int result = ::pigpiod_if_version();
if( result < 0 )
return show_error( result );
return result;
}
#endif