Class: Denko::AnalogIO::ADS1118

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

Constant Summary collapse

CONFIG_STARTUP =

Config register values on startup. MSB-first. Matches datasheet. Same as: [0x05, 0x8B] or [5, 139]

[0b00000101, 0b10001011]
BASE_MSB =

Base config bytes to mask settings into. Not same as default. MSB bits 0 and 7 set to enable single-shot mode. LSB bit 3 set to enable pullup resistor on MISO pin. LSB bit 1 set to flag valid data in the NOP bits.

0b10000001
BASE_LSB =
0b00001010

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 SPI::Peripheral

#spi_bit_order, #spi_frequency, #spi_mode

Attributes included from Behaviors::BusPeripheral

#address

Attributes included from Behaviors::Component

#board

Attributes included from Behaviors::SinglePin

#mode, #pin

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 SPI::Peripheral

#before_initialize, #spi_listen, #spi_read, #spi_stop, #spi_transfer, #spi_write

Methods included from Behaviors::BusPeripheral

#atomically, #before_initialize

Methods included from Behaviors::Component

#initialize, #micro_delay

Instance Method Details

#_read(config) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/denko/analog_io/ads1118.rb', line 37

def _read(config)
  # Write config register to start reading.
  spi_write(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.
  spi_read(2)
end

#_temperature_readObject



54
55
56
57
58
59
# File 'lib/denko/analog_io/ads1118.rb', line 54

def _temperature_read
  # Wrap in mutex to not interfere with other reads.
  @mutex.synchronize do
    _read([0b10000001, 0b10011011])
  end
end

#after_initialize(options = {}) ⇒ Object



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

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

  # SPI mode 1 recommended.
  @spi_mode = options[:spi_mode] || 1

  # 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
  spi_write(@config_register)

  # Enable BoardProxy callbacks.
  enable_proxy
end

#pre_callback_filter(message) ⇒ Object

Pack the 2 bytes back into a string, then unpack as big-endian signed int16.



49
50
51
52
# File 'lib/denko/analog_io/ads1118.rb', line 49

def pre_callback_filter(message)
  bytes = message.split(",").map { |b| b.to_i }
  bytes.pack("C*").unpack("s>")[0]
end

#temperature_read(&block) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/denko/analog_io/ads1118.rb', line 61

def temperature_read(&block)
  reading = read_using -> { _temperature_read }
  
  # Temperature is shifted 2 bits left, and is 0.03125 degrees C per bit.
  temperature = (reading / 4) * 0.03125

  block.call(temperature) if block_given?
  return temperature
end