Class: Denko::SPI::BitBang

Inherits:
Object
  • Object
show all
Includes:
Behaviors::BusController, Behaviors::MultiPin, Behaviors::Reader
Defined in:
lib/denko/spi/bit_bang.rb

Instance Attribute Summary

Attributes included from Behaviors::Callbacks

#callback_mutex

Attributes included from Behaviors::MultiPin

#pin, #pins, #proxies

Attributes included from Behaviors::Component

#board

Instance Method Summary collapse

Methods included from Behaviors::Reader

#_read, #read, #read_using, #wait_for_read

Methods included from Behaviors::Callbacks

#add_callback, #callbacks, #initialize, #pre_callback_filter, #remove_callback, #update

Methods included from Behaviors::State

#initialize, #state

Methods included from Behaviors::BusController

#mutex

Methods included from Behaviors::Subcomponents

#components, #single_pin_components

Methods included from Behaviors::MultiPin

#before_initialize, #convert_pins, #proxy_pin, #proxy_states, #require_pin, #require_pins

Methods included from Behaviors::Component

#initialize, #micro_delay

Instance Method Details

#add_component(component) ⇒ Object

Add peripheral to self and the board. It gets callbacks directly from the board.



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

def add_component(component)
  # Treat pin 255 as the component having no select pin. Mostly for APA102.
  return if component.pin == 255

  pins = components.map { |c| c.pin }
  if pins.include? component.pin
    raise ArgumentError, "duplicate select pin for #{component}"
  end

  components << component
  board.add_component(component)
end

#convert_pin(pin) ⇒ Object



49
50
51
# File 'lib/denko/spi/bit_bang.rb', line 49

def convert_pin(pin)
  board.convert_pin(pin)
end

#initialize_pins(options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/denko/spi/bit_bang.rb', line 8

def initialize_pins(options={})
  # Allow pin aliases.
  pins[:input]  = pins[:input]  || pins[:poci] || pins[:miso]
  pins[:output] = pins[:output] || pins[:pico] || pins[:mosi]
  pins[:clock]  = pins[:clock]  || pins[:sck]  || pins[:clk]

  # Clean up the pins hash.
  [:poci, :miso, :pico, :mosi, :sck, :clk].each { |key| pins.delete(key) }

  # Validate input or output pin.
  unless pins[:input] || pins[:output]
    raise ArgumentError, "no input or output pin given. Require either or both"
  end

  # If only output or input, set the other 255 for a one-directional bus.
  pins[:input]  = 255 if pins[:output] && !pins[:input]
  pins[:output] = 255 if pins[:input]  && !pins[:output]

  require_pins(:clock, :input, :output)
end

#listen(select_pin, read: 0, frequency: nil, mode: 0, bit_order: :msbfirst) ⇒ Object



34
35
36
37
# File 'lib/denko/spi/bit_bang.rb', line 34

def listen(select_pin, read: 0, frequency: nil, mode: 0, bit_order: :msbfirst)
  board.spi_bb_listen select_pin, clock: pins[:clock], input: pins[:input],
                                  read: read, mode: mode, bit_order: bit_order
end

#remove_component(component) ⇒ Object

Remove peripheral from self and the board.



68
69
70
71
# File 'lib/denko/spi/bit_bang.rb', line 68

def remove_component(component)
  components.delete(component)
  board.remove_component(component)
end

#set_pin_mode(*args) ⇒ Object

Delegate this to board so peripherals can initialize their select pins.



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

def set_pin_mode(*args)
  board.set_pin_mode(*args)
end

#stop(pin) ⇒ Object

Uses regular Board#spi_stop since listeners are shared.



40
41
42
# File 'lib/denko/spi/bit_bang.rb', line 40

def stop(pin)
  board.spi_stop(pin)
end

#transfer(select_pin, write: [], read: 0, frequency: nil, mode: 0, bit_order: :msbfirst) ⇒ Object



29
30
31
32
# File 'lib/denko/spi/bit_bang.rb', line 29

def transfer(select_pin, write: [], read: 0, frequency: nil, mode: 0, bit_order: :msbfirst)
  board.spi_bb_transfer select_pin, clock: pins[:clock], output: pins[:output], input: pins[:input],
                                    write: write, read: read, mode: mode, bit_order: bit_order
end