Class: Denko::DigitalIO::PCF8574
- Inherits:
-
Object
- Object
- Denko::DigitalIO::PCF8574
- Includes:
- Behaviors::BoardProxy, Behaviors::Lifecycle, I2C::Peripheral
- Defined in:
- lib/denko/digital_io/pcf8574.rb
Constant Summary collapse
- I2C_ADDRESS =
Default I2C address. Override with i2c_address: key in initialize hash.
0x3F- I2C_FREQUENCY =
400_000
Constants included from Behaviors::Lifecycle
Behaviors::Lifecycle::CALLBACK_METHODS
Constants included from I2C::Peripheral
I2C::Peripheral::I2C_REPEATED_START
Constants included from Behaviors::Reader
Behaviors::Reader::READ_WAIT_TIME
Instance Attribute Summary collapse
-
#bytes ⇒ Object
Default registers to 1 byte, or 8 pins when used as Board Proxy.
Attributes included from I2C::Peripheral
#i2c_frequency, #i2c_repeated_start
Attributes included from Behaviors::Component
Instance Method Summary collapse
- #bit_set(pin, value) ⇒ Object
- #digital_read(pin) ⇒ Object
-
#digital_write(pin, value) ⇒ Object
Taken from SPI::Output Register.
- #is_a_register? ⇒ Boolean
- #pin_is_pwm?(pin) ⇒ Boolean
-
#platform ⇒ Object
BoardProxy interface.
- #read_state ⇒ Object
- #set_pin_mode(pin, mode, options = {}) ⇒ Object
-
#state ⇒ Object
When used as BoardProxy, store the state of each register pin as a 0 or 1 in an array that is (@bytes * 8) long.
- #write ⇒ Object
Methods included from Behaviors::BoardProxy
#analog_read_high, #analog_write_high, #convert_pin, #high, #low, #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 Behaviors::Lifecycle
Methods included from I2C::Peripheral
#address, #i2c_default, #i2c_read, #i2c_read_raw, #i2c_write
Methods included from Behaviors::Reader
#_read, #read, #read_busy?, #read_nb, #read_raw, #read_using, #update
Methods included from Behaviors::Callbacks
#add_callback, #callbacks, #pre_callback_filter, #remove_callback, #update
Methods included from Behaviors::State
Methods included from Behaviors::BusPeripheralAddressed
Methods included from Behaviors::BusPeripheral
Methods included from Behaviors::Component
Instance Attribute Details
#bytes ⇒ Object
Default registers to 1 byte, or 8 pins when used as Board Proxy. Can be ignored if reading / writing the register directly.
82 83 84 |
# File 'lib/denko/digital_io/pcf8574.rb', line 82 def bytes @bytes = params[:bytes] || 1 end |
Instance Method Details
#bit_set(pin, value) ⇒ Object
102 103 104 105 106 107 |
# File 'lib/denko/digital_io/pcf8574.rb', line 102 def bit_set(pin, value) @state_mutex.lock @state[pin] = value @state_mutex.unlock value end |
#digital_read(pin) ⇒ Object
67 68 69 |
# File 'lib/denko/digital_io/pcf8574.rb', line 67 def digital_read(pin) state[pin] end |
#digital_write(pin, value) ⇒ Object
Taken from SPI::Output Register.
97 98 99 100 |
# File 'lib/denko/digital_io/pcf8574.rb', line 97 def digital_write(pin, value) bit_set(pin, value) write end |
#is_a_register? ⇒ Boolean
71 72 73 |
# File 'lib/denko/digital_io/pcf8574.rb', line 71 def is_a_register? true end |
#pin_is_pwm?(pin) ⇒ Boolean
109 110 111 |
# File 'lib/denko/digital_io/pcf8574.rb', line 109 def pin_is_pwm?(pin) false end |
#platform ⇒ Object
BoardProxy interface. Refactor maybe?
55 56 57 |
# File 'lib/denko/digital_io/pcf8574.rb', line 55 def platform :pcf8574 end |
#read_state ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/denko/digital_io/pcf8574.rb', line 42 def read_state @state_mutex.lock byte = i2c_read_raw(1)[0] 8.times do |i| @state[i] = (byte >> i) & 0b1 end @state_mutex.unlock @state end |
#set_pin_mode(pin, mode, options = {}) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/denko/digital_io/pcf8574.rb', line 59 def set_pin_mode(pin, mode, ={}) if mode == :output digital_write(pin, 0) else digital_write(pin, 1) end end |
#state ⇒ Object
When used as BoardProxy, store the state of each register pin as a 0 or 1 in an array that is (@bytes * 8) long.
90 91 92 |
# File 'lib/denko/digital_io/pcf8574.rb', line 90 def state @state ||= Array.new(bytes*8) { 1 } end |
#write ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/denko/digital_io/pcf8574.rb', line 22 def write bytes = [] @state_mutex.lock if @state != @previous_state @state.each_slice(8) do |slice| byte = 0 slice.each_with_index do |bit, index| next unless bit byte |= (bit << index) end bytes.unshift byte end i2c_write bytes @previous_state = @state.dup end @state_mutex.unlock @state end |