Class: Denko::SPI::OutputRegister

Inherits:
BaseRegister show all
Includes:
Behaviors::Threaded
Defined in:
lib/denko/spi/output_register.rb

Instance Attribute Summary

Attributes included from Behaviors::Threaded

#interrupts_enabled, #thread

Attributes inherited from BaseRegister

#bytes

Attributes included from Peripheral

#spi_bit_order, #spi_frequency, #spi_mode

Attributes included from Behaviors::BusPeripheral

#address

Attributes included from Behaviors::Component

#board

Attributes included from Behaviors::Callbacks

#callback_mutex

Attributes included from Behaviors::SinglePin

#mode, #pin

Instance Method Summary collapse

Methods included from Behaviors::Threaded

#enable_interrupts, included, #stop, #stop_thread, #threaded, #threaded_loop

Methods included from Behaviors::BoardProxy

#convert_pin, #high, #low, #set_pin_mode, #start_read

Methods included from Behaviors::Subcomponents

#add_component, #components, #remove_component, #single_pin_components

Methods included from Peripheral

#spi_listen, #spi_read, #spi_stop, #spi_transfer, #spi_write

Methods included from Behaviors::BusPeripheral

#atomically

Methods included from Behaviors::Component

#initialize, #micro_delay

Methods included from Behaviors::State

#initialize, #state

Methods included from Behaviors::Callbacks

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

Instance Method Details

#after_initialize(options = {}) ⇒ Object



17
18
19
# File 'lib/denko/spi/output_register.rb', line 17

def after_initialize(options={})
  write
end

#before_initialize(options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/denko/spi/output_register.rb', line 5

def before_initialize(options={})
  super(options)
  #
  # When used as a board proxy, only write sate if @write_delay seconds
  # have passed since this object last got input. Better for things like SSDs
  # where many bits change in sequence, but not at exactly the same time.
  #
  @buffer_writes = true
  @buffer_writes = false if options[:buffer_writes] == false
  @write_delay = options[:write_delay] || 0.001
end

#digital_read(pin) ⇒ Object



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

def digital_read(pin)
  state[pin]
end

#digital_write(pin, value) ⇒ Object

BoardProxy interface



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

def digital_write(pin, value)
  state[pin] = value  # Might not be atomic?
  @buffer_writes ? write_buffered(state) : write
end

#writeObject

Overrides Peripheral#write to always write @state. Convert bit state to array of 0-255 integers (bytes) first.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/denko/spi/output_register.rb', line 25

def write
  bytes = []
  @state.each_slice(8) do |slice|
    # Convert nils in the slice to zero.
    zeroed = slice.map { |bit| bit.to_i }
    
    # Each slice is 8 bits of a byte, with the lowest on the left.
    # Reverse to reading order (lowest right) then join into string, and convert to integer.
    byte = zeroed.reverse.join.to_i(2)
    
    # Pack bytes in reverse order.
    bytes.unshift byte
  end
  spi_write(bytes)
end

#write_buffered(old_state) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/denko/spi/output_register.rb', line 58

def write_buffered(old_state)
  threaded do
    sleep @write_delay
    # Keep delaying if state has changed.
    write if (old_state == state)
  end
end