Module: Denko::I2C::Peripheral

Constant Summary collapse

I2C_ADDRESS =

Set I2C defaults for including classes by defining these constants in them.

nil
I2C_FREQUENCY =
100_000
I2C_REPEATED_START =
false

Constants included from Behaviors::Lifecycle

Behaviors::Lifecycle::CALLBACK_METHODS

Constants included from Behaviors::Reader

Behaviors::Reader::READ_WAIT_TIME

Instance Attribute Summary collapse

Attributes included from Behaviors::State

#state

Attributes included from Behaviors::Component

#board, #params

Instance Method Summary collapse

Methods included from Behaviors::Lifecycle

included

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

#update_state

Methods included from Behaviors::BusPeripheral

#atomically

Methods included from Behaviors::Component

#initialize, #micro_delay

Instance Attribute Details

#i2c_frequencyObject



24
25
26
# File 'lib/denko/i2c/peripheral.rb', line 24

def i2c_frequency
  @i2c_frequency ||= params[:i2c_frequency] || params[:frequency] || i2c_default(:frequency)
end

#i2c_repeated_startObject



28
29
30
31
32
33
34
# File 'lib/denko/i2c/peripheral.rb', line 28

def i2c_repeated_start
  return @i2c_repeated_start unless @i2c_repeated_start.nil?
  @i2c_repeated_start = params[:i2c_repeated_start]   if @i2c_repeated_start.nil?
  @i2c_repeated_start = params[:repeated_start]       if @i2c_repeated_start.nil?
  @i2c_repeated_start = i2c_default(:repeated_start)  if @i2c_repeated_start.nil?
  @i2c_repeated_start
end

Instance Method Details

#addressObject Also known as: i2c_address

Use @address instead of @i2c_address for default BusPeripheral behavior.



19
20
21
# File 'lib/denko/i2c/peripheral.rb', line 19

def address
  @address ||= params[:i2c_address] || params[:address] || i2c_default(:address)
end

#i2c_default(sym) ⇒ Object



13
14
15
16
# File 'lib/denko/i2c/peripheral.rb', line 13

def i2c_default(sym)
  const_sym = "I2C_#{sym}".upcase.to_sym
  self.class.const_get(const_sym) if self.class.const_defined?(const_sym)
end

#i2c_read(num_bytes, register: nil) ⇒ Object



42
43
44
# File 'lib/denko/i2c/peripheral.rb', line 42

def i2c_read(num_bytes, register: nil)
  bus.read_nb(i2c_address, register, num_bytes, i2c_frequency, i2c_repeated_start)
end

#i2c_read_raw(num_bytes, register: nil) ⇒ Object



46
47
48
# File 'lib/denko/i2c/peripheral.rb', line 46

def i2c_read_raw(num_bytes, register: nil)
  read_raw -> { bus.read_nb(i2c_address, register, num_bytes, i2c_frequency, i2c_repeated_start)}
end

#i2c_write(bytes = []) ⇒ Object



38
39
40
# File 'lib/denko/i2c/peripheral.rb', line 38

def i2c_write(bytes=[])
  bus.write(i2c_address, bytes, i2c_frequency, i2c_repeated_start)
end