Skip to content

Commit

Permalink
Fix ADS1100
Browse files Browse the repository at this point in the history
  • Loading branch information
vickash committed Sep 26, 2024
1 parent 74ac6ea commit 527c9eb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
4 changes: 2 additions & 2 deletions examples/analog_io/ads1100.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def print_reading(name, raw, voltage)
end

board = Denko::Board.new(Denko::Connection::Serial.new)
bus = Denko::I2C::Bus.new(board: board, pin: :SDA)
bus = Denko::I2C::Bus.new(board: board)

# Unlike the ADS1115/1118, full-scale voltage depends on Vdd. Give during setup.
# This works for my M5Stack ADC unit (0-12V) when checked against a multimeter.
Expand All @@ -25,7 +25,7 @@ def print_reading(name, raw, voltage)
gain: 2
# address: 0x48 default

#
#
# Configure gain and sample rate. See datasheet for more info.
# Valid values:
# Gain: 1 (default), 2, 4, 8
Expand Down
40 changes: 23 additions & 17 deletions lib/denko/analog_io/ads1100.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ADS1100
4_096, # 0b00 12 128
16_383, # 0b01 14 32
32_767, # 0b10 15 16
65_535, # 0b00 16 8
65_535, # 0b11 16 8
]

# Wait times need to be slightly longer than the actual sample times.
Expand Down Expand Up @@ -50,17 +50,18 @@ class ADS1100
after_initialize do
# Validate user gave full scale voltage.
raise ArgumentError "full-scale voltage not a Numeric or not given for ADS1100" unless full_scale_voltage.is_a?(Numeric)
state

i2c_write(config_register)
sleep WAIT_TIMES[sample_rate]
sleep WAIT_TIMES[sample_rate_mask]
end

def _read
# Set bit 7 of the config register and write it to start conversion.
i2c_write(config_register | (1<<7))

# Sleep the right amount of time for conversion, based on sample rate bits.
sleep WAIT_TIMES[sample_rate]
sleep WAIT_TIMES[sample_rate_mask]

# Read the result, triggering callbacks.
i2c_read(2)
Expand All @@ -79,40 +80,45 @@ def pre_callback_filter(bytes)

# Default to single conversion.
def config_register
@config_register ||= CONFIG_STARTUP.dup
@config_register ||= CONFIG_STARTUP
end

def sample_rate=(rate)
raise Argument Error "wrong sample_rate: #{sample_rate.inspect} given for ADS1100" unless SAMPLE_RATES.include?(rate)
self.sample_rate_mask = SAMPLE_RATES.index(rate)
config_register = (config_register & SAMPLE_RATE_CLEAR) | (sample_rate_mask << 2)
@sample_rate = rate
end

def sample_rate_mask
@sample_rate_mask ||= SAMPLE_RATES.index(sample_rate)
end

def sample_rate
@sample_rate ||= 8
end

attr_writer :sample_rate_mask

def gain=(gain)
raise ArgumentError "wrong gain: #{gain.inspect} given for ADS1100" unless GAINS.include?(gain)
config_register = (config_register & GAIN_CLEAR) | GAINS.index(gain)
@gain = GAINS.index(gain)
end

def sample_rate=(rate)
raise Argument Error "wrong sample_rate: #{sample_rate.inspect} given for ADS1100" unless SAMPLE_RATES.include?(rate)
config_register = (config_register & SAMPLE_RATE_CLEAR) | (SAMPLE_RATES.index(rate) << 2)
@sample_rate = SAMPLE_RATES.index(rate)
end

def gain
# Default gain is 1.
self.gain = 1 unless @gain
GAINS[@gain]
end

def sample_rate
# Default sample rate is 8.
self.sample_rate = 8 unless @sample_rate
SAMPLE_RATES[@sample_rate]
end

# Unlike some ADS parts, full-scale voltage depends on supply (Vdd). User must specify.
def full_scale_voltage
@full_scale_voltage ||= params[:full_scale_voltage]
end

def volts_per_bit
full_scale_voltage / (GAINS[gain] * BIT_RANGES[sample_rate]).to_f
full_scale_voltage / (GAINS[gain] * BIT_RANGES[sample_rate_mask]).to_f
end
end
end
Expand Down

0 comments on commit 527c9eb

Please sign in to comment.