Package jpigpio

Class Pigpio

java.lang.Object
All Implemented Interfaces:
JPigpio

public class Pigpio
extends CommonPigpio
Pigpiod wrapper class using native C++ methods. This class can be used only when executing your application directly at Raspberry Pi device. See class PigpioSocket for socket based implementation allowing you to develop/debug/run your over network.

See interface JPigpio for full documentation of specific methods.

Methods which are not implemented yet has "Not implemented" in their description.
All other methods you can consider implemented.

NOTE: if method you are looking for is not implemented, check class PigpioSocket - maybe it is implemented there.
  • Constructor Details

  • Method Details

    • gpioInitialize

      public void gpioInitialize() throws PigpioException
      Throws:
      PigpioException
    • gpioTerminate

      public void gpioTerminate() throws PigpioException
      Description copied from interface: JPigpio
      Terminate the usage of the pigpio interfaces.
      Throws:
      PigpioException - on error
    • reconnect

      public void reconnect() throws PigpioException
      Not implemented
      Throws:
      PigpioException
    • gpioSetMode

      public void gpioSetMode​(int pin, int mode) throws PigpioException
      Description copied from interface: JPigpio
      Set the mode of the gpio
      Parameters:
      pin - The gpio pin to set
      mode - The mode of the pin. One of PI_INPUT or PI_OUTPUT
      Throws:
      PigpioException - on pigpiod error
    • gpioGetMode

      public int gpioGetMode​(int pin) throws PigpioException
      Description copied from interface: JPigpio
      Retrieve the mode of the given gpio
      Parameters:
      pin - The gpio to retrieve the mode
      Returns:
      The mode of the gpio
      Throws:
      PigpioException - on pigpiod error
    • gpioSetPullUpDown

      public void gpioSetPullUpDown​(int pin, int pud) throws PigpioException
      Throws:
      PigpioException
    • gpioRead

      public boolean gpioRead​(int pin) throws PigpioException
      Description copied from interface: JPigpio
      Retrieve the state of the gpio
      Parameters:
      pin - The gpio pin to retrieve
      Returns:
      The state of the gpio, one of PI_HIGH or PI_LOW (or equivalents)
      Throws:
      PigpioException - on pigpiod error
    • gpioWrite

      public void gpioWrite​(int pin, boolean value) throws PigpioException
      Description copied from interface: JPigpio
      Set the state of the gpio
      Parameters:
      pin - The gpio pin to set
      value - The desired value of the new pin state. One of PI_HIGH or PI_LOW (or equivalents)
      Throws:
      PigpioException - on pigpiod error
    • notifyOpen

      public int notifyOpen() throws PigpioException
      Not implemented
      Returns:
      notification handle
      Throws:
      PigpioException - on pigpiod error
    • notifyBegin

      public void notifyBegin​(int handle, int bits) throws PigpioException
      Not implemented
      Parameters:
      handle - >=0 (as returned by a prior call to [*notify_open*])
      bits - a 32 bit mask indicating the GPIO to be notified.
      Throws:
      PigpioException - on pigpiod error
    • notifyPause

      public void notifyPause​(int handle) throws PigpioException
      Not implemented
      Parameters:
      handle - >=0 (as returned by a prior call to [*notify_open*])
      Throws:
      PigpioException - on pigpiod error
    • notifyClose

      public void notifyClose​(int handle) throws PigpioException
      Not implemented
      Parameters:
      handle - >=0 (as returned by a prior call to [*notify_open*])
      Throws:
      PigpioException - on pigpiod error
    • setWatchdog

      public void setWatchdog​(int userGpio, int timeout) throws PigpioException
      Not implemented
      Parameters:
      userGpio - 0-31
      timeout - 0-60000
      Throws:
      PigpioException - on pigpiod error
    • waveClear

      public void waveClear() throws PigpioException
      Not implemented
      Throws:
      PigpioException - on pigpiod error
    • waveAddGeneric

      public int waveAddGeneric​(java.util.ArrayList<Pulse> pulses) throws PigpioException
      Not implemented
      Parameters:
      pulses - list of pulses to add to the waveform.
      Returns:
      Returns the new total number of pulses in the current waveform.
      Throws:
      PigpioException - on pigpiod error
    • waveAddSerial

      public int waveAddSerial​(int userGpio, int baud, byte[] data, int offset, int bbBits, int bbStop) throws PigpioException
      Not implemented
      Parameters:
      userGpio - GPIO to transmit data. You must set the GPIO mode to output.
      baud - 50-1000000 bits per second.
      data - the bytes to write.
      offset - number of microseconds from the start of the waveform, default 0.
      bbBits - number of data bits, default 8.
      bbStop - number of stop half bits, default 2.
      Returns:
      Returns the new total number of pulses in the current waveform.
      Throws:
      PigpioException - on pigpiod error
    • waveAddNew

      public void waveAddNew() throws PigpioException
      Not implemented
      Throws:
      PigpioException - on pigpiod error
    • waveTxBusy

      public boolean waveTxBusy() throws PigpioException
      Not implemented
      Returns:
      The return code from wave_tx_busy.
      Throws:
      PigpioException - on pigpiod error
    • waveTxStop

      public int waveTxStop() throws PigpioException
      Not implemented
      Returns:
      The return code from wave_tx_stop.
      Throws:
      PigpioException - on pigpiod error
    • waveCreate

      public int waveCreate() throws PigpioException
      Not implemented
      Returns:
      wave id (>=0) if OK, otherwise PI_EMPTY_WAVEFORM, PI_TOO_MANY_CBS, PI_TOO_MANY_OOL, or PI_NO_WAVEFORM_ID.
      Throws:
      PigpioException - on pigpiod error
    • waveDelete

      public void waveDelete​(int waveId) throws PigpioException
      Not implemented
      Parameters:
      waveId - >=0 (as returned by a prior call to [*wave_create*]).
      Throws:
      PigpioException - on pigpiod error
    • waveSendOnce

      public int waveSendOnce​(int waveId) throws PigpioException
      Not implemented
      Parameters:
      waveId - >=0 (as returned by a prior call to [*wave_create*]).
      Returns:
      Returns the number of DMA control blocks used in the waveform.
      Throws:
      PigpioException - on pigpiod error
    • waveSendRepeat

      public int waveSendRepeat​(int waveId) throws PigpioException
      Not implemented
      Parameters:
      waveId - >=0 (as returned by a prior call to [*wave_create*]).
      Returns:
      Returns the number of DMA control blocks used in the waveform.
      Throws:
      PigpioException - on pigpiod error
    • i2cOpen

      public int i2cOpen​(int i2cBus, int i2cAddr) throws PigpioException
      Description copied from interface: JPigpio
      Returns a handle (>=0) for the device at the I2C bus address.

      i2c_flags:= 0, no flags are currently defined.

      Normally you would only use the [*i2c_**] functions if you are or will be connecting to the Pi over a network. If you will always run on the local Pi use the standard SMBus module instead.

      For the SMBus commands the low level transactions are shown at the end of the function description. The following abbreviations are used.
      
       . .
       S     (1 bit) : Start bit
       P     (1 bit) : Stop bit
       Rd/Wr (1 bit) : Read/Write bit. Rd equals 1, Wr equals 0.
       A, NA (1 bit) : Accept and not accept bit.
       Addr  (7 bits): I2C 7 bit address.
       reg   (8 bits): Command byte, which often selects a register.
       Data  (8 bits): A data byte.
       Count (8 bits): A byte defining the length of a block operation.
      
       [..]: Data sent by the device.
       . .
      
       ...
       h = pi.i2c_open(1, 0x53) # open device at address 0x53 on bus 1
       ...
      Parameters:
      i2cBus - 0-1.
      i2cAddr - 0x00-0x7F.
      Returns:
      Returns a handle (>=0) for the device at the I2C bus address.
      Throws:
      PigpioException - on pigpiod error
    • i2cClose

      public void i2cClose​(int handle) throws PigpioException
      Description copied from interface: JPigpio
      Closes the I2C device associated with handle.
      
       ...
       pi.i2c_close(h)
       ...
      Parameters:
      handle - >=0 (as returned by a prior call to [*i2c_open*]).
      Throws:
      PigpioException - on pigpiod error
    • i2cReadDevice

      public int i2cReadDevice​(int handle, byte[] data) throws PigpioException
      Description copied from interface: JPigpio
      Returns count bytes read from the raw device associated with handle.
       . .
       S Addr Rd [A] [Data] A [Data] A ... A [Data] NA P
       . .
       
      The returned value is a tuple of the number of bytes read and a bytearray containing the bytes. If there was an error the number of bytes read will be less than zero (and will contain the error code).
      
       ...
       (count, data) = pi.i2c_read_device(h, 12)
       ...
      Parameters:
      handle - >=0 (as returned by a prior call to [*i2c_open*]).
      data - >0, the number of bytes to read.
      Returns:
      Returns count bytes read from the raw device associated with handle.
      Throws:
      PigpioException - on pigpiod error
    • i2cWriteDevice

      public void i2cWriteDevice​(int handle, byte[] data) throws PigpioException
      Description copied from interface: JPigpio
      Writes the data bytes to the raw device associated with handle.
       . .
       S Addr Wr [A] data0 [A] data1 [A] ... [A] datan [A] P
       . .
      
       
       ...
       pi.i2c_write_device(h, b"\\x12\\x34\\xA8")
      
       pi.i2c_write_device(h, b"help")
      
       pi.i2c_write_device(h, 'help')
      
       pi.i2c_write_device(h, [23, 56, 231])
       ...
      Parameters:
      handle - >=0 (as returned by a prior call to [*i2c_open*]).
      data - the bytes to write.
      Throws:
      PigpioException - on pigpiod error
    • gpioDelay

      public void gpioDelay​(long delay) throws PigpioException
      Description copied from interface: JPigpio
      Delay for the specified number of microseconds
      Parameters:
      delay - The number of microseconds for which to block
      Throws:
      PigpioException - on pigpiod error
    • gpioTick

      public long gpioTick() throws PigpioException
      Throws:
      PigpioException
    • getCurrentTick

      public long getCurrentTick() throws PigpioException
      Throws:
      PigpioException
    • gpioServo

      public void gpioServo​(int gpio, int pulseWidth) throws PigpioException
      Description copied from interface: JPigpio
      Starts (500-2500) or stops (0) servo pulses on the GPIO.
      The selected pulsewidth will continue to be transmitted until changed by a subsequent call to set_servo_pulsewidth.

      The pulsewidths supported by servos varies and should probably be determined by experiment. A value of 1500 should always be safe and represents the mid-point of rotation.

      You can DAMAGE a servo if you command it to move beyond its limits.

       
       ...
       pi.setServoPulsewidth(17, 0);    // off
       pi.setServoPulsewidth(17, 1000); // safe anti-clockwise
       pi.setServoPulsewidth(17, 1500); // centre
       pi.setServoPulsewidth(17, 2000); // safe clockwise
       ...
       
      Parameters:
      gpio - GPIO to control the servo. 0-31
      pulseWidth - The pulse width of the pulse (500-2500).
      0 = off
      500 = most anti-clockwise
      2500 = most clock-wise
      Throws:
      PigpioException - on pigpiod error
    • setServoPulseWidth

      public void setServoPulseWidth​(int gpio, int pulseWidth) throws PigpioException
      Throws:
      PigpioException
    • getServoPulseWidth

      public int getServoPulseWidth​(int gpio) throws PigpioException
      Not implemented
      Parameters:
      gpio - user gpio 0-31
      Returns:
      Returns the servo pulsewidth.
      Throws:
      PigpioException - on pigpiod error
    • setPWMDutycycle

      public void setPWMDutycycle​(int gpio, int dutycycle) throws PigpioException
      Description copied from interface: JPigpio
      Starts (non-zero dutycycle) or stops (0) PWM pulses on the GPIO.

      The [*setPWMRange*] function can change the default range of 255.
      
       ...
       pi.setPWMDutycycle(4,   0) # PWM off
       pi.setPWMDutycycle(4,  64) # PWM 1/4 on
       pi.setPWMDutycycle(4, 128) # PWM 1/2 on
       pi.setPWMDutycycle(4, 192) # PWM 3/4 on
       pi.setPWMDutycycle(4, 255) # PWM full on
       ...
      Parameters:
      gpio - user gpio 0-31
      dutycycle - 0-range (range defaults to 255).
      Throws:
      PigpioException - on pigpiod error
    • getPWMDutycycle

      public int getPWMDutycycle​(int gpio) throws PigpioException
      Not implemented
      Parameters:
      gpio - user gpio 0-31
      Returns:
      PWM dutycycle
      Throws:
      PigpioException - on pigpiod error
    • setPWMRange

      public void setPWMRange​(int gpio, int range) throws PigpioException
      Not implemented
      Parameters:
      gpio - user gpio 0-31
      range - range 25-40000
      Throws:
      PigpioException - on pigpiod error
    • getPWMRange

      public int getPWMRange​(int gpio) throws PigpioException
      Not implemented
      Parameters:
      gpio - user gpio 0-31
      Returns:
      range of PWM values
      Throws:
      PigpioException - on pigpiod error
    • getPWMRealRange

      public int getPWMRealRange​(int gpio) throws PigpioException
      Not implemented
      Parameters:
      gpio - user gpio 0-31
      Returns:
      the real (underlying) range of PWM values
      Throws:
      PigpioException - on pigpiod error
    • setPWMFrequency

      public int setPWMFrequency​(int gpio, int frequency) throws PigpioException
      Not implemented
      Parameters:
      gpio - user gpio 0-31
      frequency - >= 0 Hz
      Returns:
      Returns the frequency actually set.
      Throws:
      PigpioException - on pigpiod error
    • getPWMFrequency

      public int getPWMFrequency​(int gpio) throws PigpioException
      Not implemented
      Parameters:
      gpio - user gpio 0-31
      Returns:
      frequency (in Hz) used for the GPIO.
      Throws:
      PigpioException - on pigpiod error
    • serialOpen

      public int serialOpen​(java.lang.String tty, int baudRate, int flags) throws PigpioException
      Not implemented
      Parameters:
      tty - serial device to open
      baudRate - baud rate in bits per second, see below.
      flags - connection flags. No flags are currently defined.
      Returns:
      handle for opened device
      Throws:
      PigpioException - on pigpiod error
    • serialClose

      public void serialClose​(int handle) throws PigpioException
      Not implemented
      Parameters:
      handle - >=0 (as returned by a prior call to [*serial_open*]).
      Throws:
      PigpioException - on pigpiod error
    • serialReadByte

      public byte serialReadByte​(int handle) throws PigpioException
      Not implemented
      Parameters:
      handle - >=0 (as returned by a prior call to [*serial_open*]).
      Returns:
      single byte from opened device
      Throws:
      PigpioException - on pigpiod error
    • serialWriteByte

      public void serialWriteByte​(int handle, byte data) throws PigpioException
      Not implemented
      Parameters:
      handle - >=0 (as returned by a prior call to [*serial_open*]).
      data - 0-255, the value to write.
      Throws:
      PigpioException - on pigpiod error
    • serialRead

      public byte[] serialRead​(int handle, int count) throws PigpioException
      Not implemented
      Parameters:
      handle - >=0 (as returned by a prior call to [*serial_open*]).
      count - >0, the number of bytes to read.
      Returns:
      read bytes
      Throws:
      PigpioException - on pigpiod error
    • serialWrite

      public void serialWrite​(int handle, byte[] data) throws PigpioException
      Not implemented
      Parameters:
      handle - >=0 (as returned by a prior call to [*serial_open*]).
      data - the bytes to write.
      Throws:
      PigpioException - on pigpiod error
    • serialDataAvailable

      public int serialDataAvailable​(int handle) throws PigpioException
      Not implemented
      Parameters:
      handle - >=0 (as returned by a prior call to [*serial_open*]).
      Returns:
      number of bytes available to be read
      Throws:
      PigpioException - on pigpiod error
    • gpioSetAlertFunc

      public void gpioSetAlertFunc​(int pin, Alert alert) throws PigpioException
      Throws:
      PigpioException
    • gpioTrigger

      public void gpioTrigger​(int gpio, long pulseLen, boolean level) throws PigpioException
      Description copied from interface: JPigpio
      Set the specified gpio to the level specified by level for the duration (in micro-sec) specified by pulseLen and then set the gpio back to not-level.
      Parameters:
      gpio - The GPIO pin to pulse.
      pulseLen - The duration in microseconds to hold the pulse.
      level - The level to target the pulse.
      Throws:
      PigpioException - on pigpiod error
    • spiOpen

      public int spiOpen​(int channel, int baudRate, int flags) throws PigpioException
      Description copied from interface: JPigpio
      Returns a handle for the SPI device on channel. Data will be transferred at baud bits per second. The flags may be used to modify the default behaviour of 4-wire operation, mode 0, active low chip select.

      An auxiliary SPI device is available on all models but the A and B and may be selected by setting the A bit in the flags. The auxiliary device has 3 chip selects and a selectable word size in bits.

      Normally you would only use the [*spi_**] functions if you are or will be connecting to the Pi over a network. If you will always run on the local Pi use the standard SPI module instead.

      spi_flags consists of the least significant 22 bits.
       . .
       21 20 19 18 17 16 15 14 13 12 11 10  9  8  7  6  5  4  3  2  1  0
        b  b  b  b  b  b  R  T  n  n  n  n  W  A u2 u1 u0 p2 p1 p0  m  m
       . .
      
       mm defines the SPI mode.
      
       WARNING: modes 1 and 3 do not appear to work on the auxiliary device.
      
       . .
       Mode POL PHA
        0    0   0
        1    0   1
        2    1   0
        3    1   1
       . .
       
      px is 0 if CEx is active low (default) and 1 for active high.

      ux is 0 if the CEx GPIO is reserved for SPI (default) and 1 otherwise.

      A is 0 for the standard SPI device, 1 for the auxiliary SPI.

      W is 0 if the device is not 3-wire, 1 if the device is 3-wire. Standard SPI device only.

      nnnn defines the number of bytes (0-15) to write before switching the MOSI line to MISO to read data. This field is ignored if W is not set. Standard SPI device only.

      T is 1 if the least significant bit is transmitted on MOSI first, the default (0) shifts the most significant bit out first. Auxiliary SPI device only.

      R is 1 if the least significant bit is received on MISO first, the default (0) receives the most significant bit first. Auxiliary SPI device only.

      bbbbbb defines the word size in bits (0-32). The default (0) sets 8 bits per word. Auxiliary SPI device only.

      The [*spi_read*], [*spi_write*], and [*spi_xfer*] functions transfer data packed into 1, 2, or 4 bytes according to the word size in bits.

      For bits 1-8 there will be one byte per character.
      For bits 9-16 there will be two bytes per character.
      For bits 17-32 there will be four bytes per character.

      E.g. 32 12-bit words will be transferred in 64 bytes.

      The other bits in flags should be set to zero.
      
       ...
       # open SPI device on channel 1 in mode 3 at 50000 bits per second
      
       h = pi.spi_open(1, 50000, 3)
       ...
      Parameters:
      channel - The channel to open.
      0-1 (0-2 for the auxiliary SPI device).
      baudRate - The baud rate for transmission and reception. Some constants are provided:
      • PI_SPI_BAUD_125KHZ
      • PI_SPI_BAUD_250KHZ
      • PI_SPI_BAUD_500KHZ
      • PI_SPI_BAUD_1MHZ
      • PI_SPI_BAUD_2MHZ
      • PI_SPI_BAUD_4MHZ
      • PI_SPI_BAUD_8MHZ
      flags - Control flags. The flags can include:
      Returns:
      A handle used in subsequent SPI API calls
      Throws:
      PigpioException - on pigpiod error
    • spiClose

      public void spiClose​(int handle) throws PigpioException
      Description copied from interface: JPigpio
      Closes the SPI device associated with handle.
      
       ...
       pi.spi_close(h)
       ...
      Parameters:
      handle - The handle to be closed.
      >=0 (as returned by a prior call to [*spi_open*]).
      Throws:
      PigpioException - on pigpiod error
    • spiRead

      public int spiRead​(int handle, byte[] data) throws PigpioException
      Description copied from interface: JPigpio
      Reads count bytes from the SPI device associated with handle.

      The returned value is a tuple of the number of bytes read and a bytearray containing the bytes. If there was an error the number of bytes read will be less than zero (and will contain the error code).
      
       ...
       (b, d) = pi.spi_read(h, 60) # read 60 bytes from device h
       if b == 60:
       # process read data
       else:
       # error path
       ...
      Parameters:
      handle - The handle from which to read. >=0 (as returned by a prior call to [*spi_open*]).
      data - An array into which to read data.
      Returns:
      The number of bytes actually read.
      Throws:
      PigpioException - on pigpiod error
    • spiWrite

      public int spiWrite​(int handle, byte[] data) throws PigpioException
      Description copied from interface: JPigpio
      Writes the data bytes to the SPI device associated with handle.
      
       ...
       pi.spi_write(0, b'\\x02\\xc0\\x80') # write 3 bytes to device 0
      
       pi.spi_write(0, b'defgh')        # write 5 bytes to device 0
      
       pi.spi_write(0, "def")           # write 3 bytes to device 0
      
       pi.spi_write(1, [2, 192, 128])   # write 3 bytes to device 1
       ...
      Parameters:
      handle - The handle into which to write. >=0 (as returned by a prior call to [*spi_open*]).
      data - An array of data to write to SPI.
      Returns:
      The number of bytes actually written
      Throws:
      PigpioException - on pigpiod error
    • spiXfer

      public int spiXfer​(int handle, byte[] txData, byte[] rxData) throws PigpioException
      Description copied from interface: JPigpio
      Writes the data bytes to the SPI device associated with handle, returning the data bytes read from the device.

      The returned value is a tuple of the number of bytes read and a bytearray containing the bytes. If there was an error the number of bytes read will be less than zero (and will contain the error code).
      
       ...
       (count, rx_data) = pi.spi_xfer(h, b'\\x01\\x80\\x00')
      
       (count, rx_data) = pi.spi_xfer(h, [1, 128, 0])
      
       (count, rx_data) = pi.spi_xfer(h, b"hello")
      
       (count, rx_data) = pi.spi_xfer(h, "hello")
       ...
      Parameters:
      handle - The handle into which to write. >=0 (as returned by a prior call to [*spi_open*]).
      txData - An array of data to write.
      rxData - An array of data to read.
      Returns:
      The number of bytes actually transferred.
      Throws:
      PigpioException - on pigpiod error
    • setDebug

      public void setDebug​(boolean flag)
    • gpioxPulseAndWait

      public long gpioxPulseAndWait​(int outGpio, int inGpio, long waitDuration, long pulseHoldDuration, boolean pulseLow) throws PigpioException
      Description copied from interface: JPigpio
      Pulse a named pin and then wait for a response on a different pin. The output pin should already have a PI_OUTPUT mode and the input pin should already have a PI_INPUT mode. The wait duration is in microseconds. The pulse hold duration is how long (in microseconds) the pulse should be held for. The default is to pulse the output high and then return low however if the pulseLow flag is set the inverse will happen (pulse the output low and then return high).

      The return is how long we waited for the pulse measured in microseconds. If no response is received, we return -1 to indicate a timeout.
      Parameters:
      outGpio - The pin on which the output pulse will occur.
      inGpio - The pin on which the input pulse will be sought.
      waitDuration - The maximum time to wait in microseconds.
      pulseHoldDuration - The time to hold the output pulse in microseconds.
      pulseLow - True if the pulse should be a low pulse otherwise a high pulse will be sent.
      Returns:
      The time in microseconds waiting for a pulse or -1 to signfify a timeout.
      Throws:
      PigpioException - on pigpiod error
    • addCallback

      public void addCallback​(GPIOListener gpioListener) throws PigpioException
      Not implemented
      Parameters:
      gpioListener - user supplied callback object.
      Throws:
      PigpioException
    • removeCallback

      public void removeCallback​(GPIOListener cgpioListener) throws PigpioException
      Not implemented
      Parameters:
      cgpioListener - GPIOListener object
      Throws:
      PigpioException - on pigpiod error