Module: PiGPIO

Extended by:
FFI::Library
Defined in:
lib/pigpio_ffi.rb,
lib/pigpio_ffi/i2c.rb,
lib/pigpio_ffi/pwm.rb,
lib/pigpio_ffi/spi.rb,
lib/pigpio_ffi/basic.rb,
lib/pigpio_ffi/servo.rb,
lib/pigpio_ffi/waves.rb,
lib/pigpio_ffi/version.rb,
lib/pigpio_ffi/advanced.rb,
lib/pigpio_ffi/essential.rb,
lib/pigpio_ffi/utilities.rb,
lib/pigpio_ffi/intermediate.rb,
lib/pigpio_ffi/configuration.rb

Overview

Additional convenience and helper methods added here.

Defined Under Namespace

Classes: WavePulse

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.gpioSetAlertFunc(gpio, &block) ⇒ Object

Call this one with a block.



40
41
42
# File 'lib/pigpio_ffi/intermediate.rb', line 40

def self.gpioSetAlertFunc(gpio, &block)
	self._gpioSetAlertFunc(gpio, &block)
end

.gpioSetTimerFunc(timer, millis, &block) ⇒ Object

Call this one with a block.



57
58
59
# File 'lib/pigpio_ffi/intermediate.rb', line 57

def self.gpioSetTimerFunc(timer, millis, &block)
	self._gpioSetTimerFunc(gpio, millis, &block)
end

.gpioTicksSince(old_time) ⇒ Object

Helper to handle systick integer rollover calculation.



32
33
34
35
36
37
38
39
# File 'lib/pigpio_ffi.rb', line 32

def self.gpioTicksSince(old_time)
  current_time = gpioTick
  if current_time < old_time
    (0xFFFF - old_time) + current_time
  else
    current_time - old_time
  end
end

.gpioWaveAddGeneric(pulses = []) ⇒ Object

Call this instead. No length arg like C function. Args: array of pulse hashes.



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pigpio_ffi/waves.rb', line 23

def self.gpioWaveAddGeneric(pulses=[])
	pulses_pointer = FFI::MemoryPointer.new(WavePulse, pulses.length)
	pulse_structs = pulses.length.times.collect do |i|
		WavePulse.new(pulses_pointer + i * WavePulse.size)
	end
	pulses.each_with_index do |hash, i|
		pulse_structs[i][:gpioOn]  = hash[:gpioOn]
		pulse_structs[i][:gpioOff] = hash[:gpioOff]
		pulse_structs[i][:usDelay] = hash[:usDelay] 
	end
  self._gpioWaveAddGeneric(pulses.length, pulses_pointer)
end

.gpioWaveChain(wave_id_array = []) ⇒ Object

Call this instead. No length arg like C function. Args: array of wave_ids to send in order.



55
56
57
58
59
60
61
62
# File 'lib/pigpio_ffi/waves.rb', line 55

def self.gpioWaveChain(wave_id_array=[])
   # NOTE: Should add validation here? Each array element should be a byte.
   # Maximum number of bytes should be 600.

   # Just pack the byte array into a string. Gets mapped to char * in C.
   string = wave_id_array.pack('C*')
  self._gpioWaveChain(string, wave_id_array.length)
end

.i2cReadI2CBlockData(handle, start_register, length) ⇒ Object

Call this instead.

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
# File 'lib/pigpio_ffi/i2c.rb', line 42

def self.i2cReadI2CBlockData(handle, start_register, length)
  raise ArgumentError, "Cannot read more than 32 I2C bytes" if length > 32
  buffer_pointer = FFI::MemoryPointer.new(:uint8, length)

  # Read it and convert it to a Ruby array.
  self._i2cReadI2CBlockData(handle, start_register, buffer_pointer)
  bytes = buffer_pointer.read_array_of_type(:uint8, length)
end

.i2cWriteI2CBlockData(handle, start_register, byte_array) ⇒ Object

Call this instead.

Raises:

  • (ArgumentError)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/pigpio_ffi/i2c.rb', line 58

def self.i2cWriteI2CBlockData(handle, start_register, byte_array)
  # Do some validation.
  raise ArgumentError, "Cannot write more than 32 I2C bytes" if byte_array > 32
  byte_array.each do |element|
    raise ArgumentError, "Byte values must be within 0x00 and 0xFF" if (element > 0xFF || element < 0x00)
  end

  # Copy the array to the pointer location.
  buffer_pointer = FFI::MemoryPointer.new(:uint8, byte_array.length)
  buffer_pointer.write_array_of_type(:uint8, byte_array)

  # Write it.
  self._i2cReadI2CBlockData(handle, start_register, buffer_pointer)
end

.spiRead(handle, length) ⇒ Object

Call this instead.



28
29
30
31
32
33
34
35
# File 'lib/pigpio_ffi/spi.rb', line 28

def self.spiRead(handle, length)
# Limited to word sizes of 8 bits.
  buffer_pointer = FFI::MemoryPointer.new(:uint8, length)

  # Read it and convert it to a Ruby array.
  self._spiRead(handle, buffer_pointer, length)
  bytes = buffer_pointer.read_array_of_type(:uint8, length)
end

.spiWrite(handle, byte_array) ⇒ Object

Call this instead.



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pigpio_ffi/spi.rb', line 44

def self.spiWrite(handle, byte_array)
# Do some validation.
  byte_array.each do |element|
    raise ArgumentError, "Byte values must be within 0x00 and 0xFF" if (element > 0xFF || element < 0x00)
  end

  # Copy the array to the pointer location.
  buffer_pointer = FFI::MemoryPointer.new(:uint8, byte_array.length)
  buffer_pointer.write_array_of_type(:uint8, byte_array)

  # Write it.
  self._spiWrite(handle, buffer_pointer, buffer_pointer.length)
end

.spiXfer(handle, tx_array) ⇒ Object

Call this instead.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/pigpio_ffi/spi.rb', line 65

def self.spiXfer(handle, tx_array)
tx_array.each do |element|
	raise ArgumentError, "Byte values must be within 0x00 and 0xFF" if (element > 0xFF || element < 0x00)
end

# Copy the tx array to the pointer location.
tx_pointer = FFI::MemoryPointer.new(:uint8, tx_array.length)
  tx_pointer.write_array_of_type(:uint8, tx_array)

# Make pointer for the receive buffer.
rx_pointer = FFI::MemoryPointer.new(:uint8, tx_array.length)

# Transfer.
self._spiWrite(handle, buffer_pointer, buffer_pointer.length)

  # Read bytes from the rx pointer.
  rx_bytes = rx_pointer.read_array_of_type(:uint8, tx_array.length)
end