Class: Denko::SPI::OutputRegister

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

Constant Summary

Constants included from Behaviors::Lifecycle

Behaviors::Lifecycle::CALLBACK_METHODS

Instance Attribute Summary

Attributes inherited from BaseRegister

#bytes

Attributes included from Peripheral

#spi_bit_order, #spi_frequency, #spi_mode

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 inherited from BaseRegister

#is_a_register?, #platform, #state

Methods included from Behaviors::BoardProxy

#analog_read_high, #analog_write_high, #convert_pin, #high, #low, #set_pin_mode, #start_read

Methods included from Behaviors::Subcomponents

#add_component, #add_hw_i2c, #add_hw_spi, #add_single_pin, #components, #hw_i2c_comps, #hw_spi_comps, #remove_component, #remove_hw_i2c, #remove_hw_spi, #remove_single_pin, #single_pin_components

Methods included from Peripheral

#ensure_byte_array, #initialize_pins, #proxy_pin, #spi_listen, #spi_read, #spi_stop, #spi_transfer, #spi_write, #update

Methods included from Behaviors::Callbacks

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

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_pin, #proxy_states, #require_pin, #require_pins

Instance Method Details

#bit_set(pin, value) ⇒ Object



45
46
47
48
49
50
# File 'lib/denko/spi/output_register.rb', line 45

def bit_set(pin, value)
  @state_mutex.lock
  @state[pin] = value
  @state_mutex.unlock
  value
end

#digital_read(pin) ⇒ Object



52
53
54
# File 'lib/denko/spi/output_register.rb', line 52

def digital_read(pin)
  state[pin]
end

#digital_write(pin, value) ⇒ Object

BoardProxy interface



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

def digital_write(pin, value)
  bit_set(pin, value)
  write
end

#pin_is_pwm?(pin) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/denko/spi/output_register.rb', line 56

def pin_is_pwm?(pin)
  false
end

#writeObject

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



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/denko/spi/output_register.rb', line 15

def write
  bytes = []
  @state_mutex.lock
  if @state != @previous_state
    @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)
    @previous_state = @state.dup
  end
  @state_mutex.unlock
  @state
end