Module: Denko::SPI::Peripheral

Constant Summary

Constants included from Behaviors::Lifecycle

Behaviors::Lifecycle::CALLBACK_METHODS

Instance Attribute Summary collapse

Attributes included from Behaviors::State

#state

Attributes included from Behaviors::Component

#board, #params

Attributes included from Behaviors::MultiPin

#pin, #pins, #proxies

Instance Method Summary collapse

Methods included from Behaviors::Lifecycle

included

Methods included from Behaviors::Callbacks

#add_callback, #callbacks, #pre_callback_filter, #remove_callback

Methods included from Behaviors::State

#update_state

Methods included from Behaviors::BusPeripheral

#atomically

Methods included from Behaviors::Component

#initialize, #micro_delay

Methods included from Behaviors::MultiPin

#convert_pins, #proxy_states, #require_pin, #require_pins

Instance Attribute Details

#spi_bit_orderObject



80
81
82
# File 'lib/denko/spi/peripheral.rb', line 80

def spi_bit_order
  @spi_bit_order ||= params[:spi_bit_order] || :msbfirst
end

#spi_frequencyObject

SPI Properties



72
73
74
# File 'lib/denko/spi/peripheral.rb', line 72

def spi_frequency
  @spi_frequency ||= params[:spi_frequency] || 1_000_000
end

#spi_modeObject



76
77
78
# File 'lib/denko/spi/peripheral.rb', line 76

def spi_mode
  @spi_mode ||= params[:spi_mode] || 0
end

Instance Method Details

#ensure_byte_array(message) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/denko/spi/peripheral.rb', line 55

def ensure_byte_array(message)
  if message.class == Array
    # Byte array coming from PiBoard.
    return message
  else
    # Split up comma delimited bytes coming from a microcontroller.
    return message.split(",").map { |b| b.to_i }
  end
end

#initialize_pins(params = {}) ⇒ Object

Chip select pin is always treated as a subcomponent



44
45
46
47
# File 'lib/denko/spi/peripheral.rb', line 44

def initialize_pins(params={})
  super(params)
  proxy_pin :select, ChipSelect, mode: :output
end

#proxy_pin(*args, **kwargs) ⇒ Object

We have the SPI bus set as our board, but the select pin, and any other pins, need to attach to the underlying board.



30
31
32
33
# File 'lib/denko/spi/peripheral.rb', line 30

def proxy_pin(*args, **kwargs)
  kwargs[:board] = bus.board
  super(*args, **kwargs)
end

#spi_listen(num_bytes) ⇒ Object



101
102
103
# File 'lib/denko/spi/peripheral.rb', line 101

def spi_listen(num_bytes)
  bus.listen(select.pin, read: num_bytes, frequency: spi_frequency, mode: spi_mode, bit_order: spi_bit_order)
end

#spi_read(num_bytes) ⇒ Object



97
98
99
# File 'lib/denko/spi/peripheral.rb', line 97

def spi_read(num_bytes)
  bus.transfer(select.pin, read: num_bytes, frequency: spi_frequency, mode: spi_mode, bit_order: spi_bit_order)
end

#spi_stopObject



105
106
107
# File 'lib/denko/spi/peripheral.rb', line 105

def spi_stop
  bus.stop(select.pin)
end

#spi_transfer(write: [], read: 0) ⇒ Object

Delegate methods to the bus.



89
90
91
# File 'lib/denko/spi/peripheral.rb', line 89

def spi_transfer(write: [], read: 0)
  bus.transfer(select.pin, write: write, read: read, frequency: spi_frequency, mode: spi_mode, bit_order: spi_bit_order)
end

#spi_write(byte_array) ⇒ Object



93
94
95
# File 'lib/denko/spi/peripheral.rb', line 93

def spi_write(byte_array)
  bus.transfer(select.pin, write: byte_array, frequency: spi_frequency, mode: spi_mode, bit_order: spi_bit_order)
end

#update(message) ⇒ Object



65
66
67
# File 'lib/denko/spi/peripheral.rb', line 65

def update(message)
  super(ensure_byte_array(message))
end