Class: Denko::AnalogIO::ADS1115

Inherits:
Object
  • Object
show all
Includes:
ADS111X, I2C::Peripheral
Defined in:
lib/denko/analog_io/ads1115.rb

Constant Summary collapse

CONFIG_STARTUP =

Config register values on startup. MSB-first. Matches datasheet, except MSB bit 7 unset to avoid conversion start. Same as: [0x05, 0x83] or [5, 131]

[0b00000101, 0b10000011]
BASE_MSB =

Base config bytes to mask settings into. Not same as startup config. MSB bits 0 and 7 set to enable single-shot mode. LSB bits 0 and 1 set to disable comparator.

0b10000001
BASE_LSB =
0b00000011
CONFIG_ADDRESS =

Register addresses.

0b01
CONVERSION_ADDRESS =
0b00

Constants included from ADS111X

Denko::AnalogIO::ADS111X::MUX_SETTINGS, Denko::AnalogIO::ADS111X::PGA_RANGE, Denko::AnalogIO::ADS111X::PGA_SETTINGS, Denko::AnalogIO::ADS111X::SAMPLE_RATE_RANGE, Denko::AnalogIO::ADS111X::SAMPLE_TIMES, Denko::AnalogIO::ADS111X::WAIT_TIMES

Instance Attribute Summary

Attributes included from Behaviors::Callbacks

#callback_mutex

Attributes included from I2C::Peripheral

#i2c_frequency, #i2c_repeated_start

Attributes included from Behaviors::BusPeripheral

#address

Attributes included from Behaviors::Component

#board

Instance Method Summary collapse

Methods included from ADS111X

#analog_listen, #analog_read, #enable_proxy, #pins_to_mux_bits, #stop_listener

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 Behaviors::Reader

#read, #read_using, #wait_for_read

Methods included from Behaviors::Callbacks

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

Methods included from Behaviors::State

#initialize, #state

Methods included from I2C::Peripheral

#i2c_read, #i2c_write

Methods included from Behaviors::BusPeripheral

#atomically

Methods included from Behaviors::Component

#initialize, #micro_delay

Instance Method Details

#_read(config) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/denko/analog_io/ads1115.rb', line 44

def _read(config)
  # Write config register to start reading.
  i2c_write [CONFIG_ADDRESS] + config
  
  # Sleep the right amount of time for conversion, based on sample rate bits.
  sleep WAIT_TIMES[config[1] >> 5]

  # Read the result, triggering callbacks.
  i2c_read(CONVERSION_ADDRESS, 2)
end

#after_initialize(options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/denko/analog_io/ads1115.rb', line 28

def after_initialize(options={})
  super(options)

  # Mutex and variables for BoardProxy behavior.
  @mutex        = Mutex.new
  @active_pin   = nil
  @active_gain  = nil

  # Set register bytes to default and write to device.
  @config_register = CONFIG_STARTUP.dup
  i2c_write [CONFIG_ADDRESS] + @config_register

  # Enable BoardProxy callbacks.
  enable_proxy
end

#before_initialize(options = {}) ⇒ Object



22
23
24
25
26
# File 'lib/denko/analog_io/ads1115.rb', line 22

def before_initialize(options={})
  @i2c_address   = 0x48
  @i2c_frequency = 400_000
  super(options)
end

#pre_callback_filter(bytes) ⇒ Object

Readings are 2 bytes big-endian.



56
57
58
# File 'lib/denko/analog_io/ads1115.rb', line 56

def pre_callback_filter(bytes)
  bytes.pack("C*").unpack("s>")[0]
end