Class: Denko::SPI::OutputRegister
- Inherits:
-
BaseRegister
- Object
- BaseRegister
- Denko::SPI::OutputRegister
- 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
Attributes included from Peripheral
#spi_bit_order, #spi_frequency, #spi_mode
Attributes included from Behaviors::State
Attributes included from Behaviors::Component
Attributes included from Behaviors::MultiPin
Instance Method Summary collapse
- #bit_set(pin, value) ⇒ Object
- #digital_read(pin) ⇒ Object
-
#digital_write(pin, value) ⇒ Object
BoardProxy interface.
- #pin_is_pwm?(pin) ⇒ Boolean
-
#write ⇒ Object
Overrides Peripheral#write to always write state.
Methods included from Behaviors::Lifecycle
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
Methods included from Behaviors::BusPeripheral
Methods included from Behaviors::Component
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
56 57 58 |
# File 'lib/denko/spi/output_register.rb', line 56 def pin_is_pwm?(pin) false end |
#write ⇒ Object
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 |