Class: Pigpio::SPI

Inherits:
Object
  • Object
show all
Defined in:
lib/pigpio/spi.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pi, spi_channel = 0, enable_cex = 1, baud = 500000, bits_per_word: 8, first_MISO: false, first_MOSI: false, idol_bytes: 0, is_3wire: false, active_low_cex: 0, spi_mode: 0) ⇒ SPI

Returns a new instance of SPI.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/pigpio/spi.rb', line 4

def initialize(pi, spi_channel = 0, enable_cex = 1, baud = 500000, bits_per_word: 8, first_MISO: false, first_MOSI: false, idol_bytes: 0, is_3wire: false, active_low_cex: 0, spi_mode: 0)
  is_aux = (spi_channel != 0)
  flg = ((spi_mode.to_i & 0x03) |
     ((active_low_cex.to_i & 0x07) << 2) |
     ((enable_cex.to_i & 0x07) << 5) |
     ((is_aux ? 1 : 0) << 8) |
     ((is_3wire ? 1 : 0) << 9) |
     ((idol_bytes.to_i & 0x0f) << 10) |
     ((first_MOSI ? 1 : 0) << 14) |
     ((first_MISO ? 1 : 0) << 15) |
     ((bits_per_word.to_i & 0x3f) << 16)
        )
  @pi = pi
  @byte_per_word = (bits_per_word / 8.0).ceil
  @handle = IF.spi_open(@pi, spi_channel, baud, flg)
end

Instance Attribute Details

#handleObject (readonly)

Returns the value of attribute handle.



3
4
5
# File 'lib/pigpio/spi.rb', line 3

def handle
  @handle
end

#piObject (readonly)

Returns the value of attribute pi.



3
4
5
# File 'lib/pigpio/spi.rb', line 3

def pi
  @pi
end

Instance Method Details

#closeObject



21
22
23
# File 'lib/pigpio/spi.rb', line 21

def close
  IF.spi_close(@pi, @handle)
end

#read(words = 1) ⇒ Object



25
26
27
# File 'lib/pigpio/spi.rb', line 25

def read(words = 1)
  IF.spi_read(@pi, @handle, words * @byte_per_word)
end

#write(buf) ⇒ Object



29
30
31
# File 'lib/pigpio/spi.rb', line 29

def write(buf)
  IF.spi_write(@pi, @handle, buf)
end

#xfer(buf) ⇒ Object



33
34
35
# File 'lib/pigpio/spi.rb', line 33

def xfer(buf)
  IF.spi_xfer(@pi, @handle, buf)
end