Class: Denko::AnalogIO::ADS1100
- Inherits:
-
Object
- Object
- Denko::AnalogIO::ADS1100
- Includes:
- InputHelper, Behaviors::Lifecycle, Behaviors::Poller, I2C::Peripheral
- Defined in:
- lib/denko/analog_io/ads1100.rb
Constant Summary collapse
- I2C_ADDRESS =
0x48- I2C_FREQUENCY =
400_000- SAMPLE_RATES =
Convert sample rates in samples-per-seconds to their bit representation.
[ # Bitmask 128, # 0b00 32, # 0b01 16, # 0b10 8 # 0b11 (default) ]
- BIT_RANGES =
Faster sampling rates have lower resolution.
[ # Bitmask Bits SPS 4_096, # 0b00 12 128 16_383, # 0b01 14 32 32_767, # 0b10 15 16 65_535, # 0b11 16 8 ]
- WAIT_TIMES =
Wait times need to be slightly longer than the actual sample times.
SAMPLE_RATES.map { |rate| (1 / rate.to_f) + 0.0005 }
- GAINS =
Bitmask Full scale voltage
[ # Bitmask Full scale voltage 1, # 0b00 Vdd 2, # 0b01 Vdd / 2 4, # 0b10 Vdd / 4 8, # 0b11 Vdd / 8 ]
- CONFIG_STARTUP =
Default config register:
Bit 7 : Start conversion (write 1) | Conversion in progress (read 1) Bit 6-5 : Reserved, must be 00 Bit 4 : Conversion mode. 0 = continuous (datasheet default). 1 = single (our default). Bit 3-2 : Sample Rate (see array above) Bit 1-2 : PGA setting (see array above) 0b00011100- GAIN_CLEAR =
Masks
0b11111100- SAMPLE_RATE_CLEAR =
0b11110011
Constants included from Behaviors::Reader
Behaviors::Reader::READ_WAIT_TIME
Constants included from Behaviors::Lifecycle
Behaviors::Lifecycle::CALLBACK_METHODS
Constants included from I2C::Peripheral
I2C::Peripheral::I2C_REPEATED_START
Instance Attribute Summary collapse
Attributes included from InputHelper
Attributes included from Behaviors::Threaded
Attributes included from Behaviors::State
Attributes included from I2C::Peripheral
#i2c_frequency, #i2c_repeated_start
Attributes included from Behaviors::Component
Instance Method Summary collapse
- #_read ⇒ Object
-
#config_register ⇒ Object
Default to single conversion.
-
#full_scale_voltage ⇒ Object
Unlike some ADS parts, full-scale voltage depends on supply (Vdd).
- #gain ⇒ Object
- #gain=(gain) ⇒ Object
- #gain_mask ⇒ Object
- #listen(pin, divider = nil) ⇒ Object
- #pre_callback_filter(bytes) ⇒ Object
- #sample_rate ⇒ Object
- #sample_rate=(rate) ⇒ Object
- #volts_per_bit ⇒ Object
Methods included from InputHelper
#on_change, #smooth_input, #smoothing_set
Methods included from Behaviors::Poller
Methods included from Behaviors::Threaded
#enable_interrupts, included, #mruby_thread_check, #stop, #stop_thread, #threaded, #threaded_loop
Methods included from Behaviors::Reader
#read, #read_busy?, #read_nb, #read_raw, #read_using, #update
Methods included from Behaviors::Callbacks
#add_callback, #callbacks, #remove_callback, #update
Methods included from Behaviors::State
Methods included from Behaviors::Lifecycle
Methods included from I2C::Peripheral
#address, #i2c_default, #i2c_read, #i2c_read_raw, #i2c_write
Methods included from Behaviors::BusPeripheralAddressed
Methods included from Behaviors::BusPeripheral
Methods included from Behaviors::Component
Instance Attribute Details
#sample_rate_mask ⇒ Object
92 93 94 |
# File 'lib/denko/analog_io/ads1100.rb', line 92 def sample_rate_mask @sample_rate_mask ||= SAMPLE_RATES.index(sample_rate) end |
Instance Method Details
#_read ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/denko/analog_io/ads1100.rb', line 58 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_mask] # Read the result, triggering callbacks. i2c_read(2) end |
#config_register ⇒ Object
Default to single conversion.
81 82 83 |
# File 'lib/denko/analog_io/ads1100.rb', line 81 def config_register @config_register ||= CONFIG_STARTUP end |
#full_scale_voltage ⇒ Object
Unlike some ADS parts, full-scale voltage depends on supply (Vdd). User must specify.
118 119 120 |
# File 'lib/denko/analog_io/ads1100.rb', line 118 def full_scale_voltage @full_scale_voltage ||= params[:full_scale_voltage] end |
#gain ⇒ Object
113 114 115 |
# File 'lib/denko/analog_io/ads1100.rb', line 113 def gain @gain ||= params[:gain] || 1 end |
#gain=(gain) ⇒ Object
102 103 104 105 106 107 |
# File 'lib/denko/analog_io/ads1100.rb', line 102 def gain=(gain) raise ArgumentError "wrong gain: #{gain.inspect} given for ADS1100" unless GAINS.include?(gain) @gain_mask = GAINS.index(gain) config_register = (config_register & GAIN_CLEAR) | gain_mask @gain = gain end |
#gain_mask ⇒ Object
109 110 111 |
# File 'lib/denko/analog_io/ads1100.rb', line 109 def gain_mask @gain_mask ||= GAINS.index(gain) end |
#listen(pin, divider = nil) ⇒ Object
69 70 71 |
# File 'lib/denko/analog_io/ads1100.rb', line 69 def listen(pin, divider=nil) raise StandardError, "ADS1100 does not implement #listen. Use #read or #poll instead" end |
#pre_callback_filter(bytes) ⇒ Object
73 74 75 76 77 78 |
# File 'lib/denko/analog_io/ads1100.rb', line 73 def pre_callback_filter(bytes) # Readings are 16-bits, signed, big-endian. value = bytes.pack("C*").unpack("s>")[0] # Let InputHelper module handle smooothing. super(value) end |
#sample_rate ⇒ Object
96 97 98 |
# File 'lib/denko/analog_io/ads1100.rb', line 96 def sample_rate @sample_rate ||= params[:sample_rate] || 8 end |
#sample_rate=(rate) ⇒ Object
85 86 87 88 89 90 |
# File 'lib/denko/analog_io/ads1100.rb', line 85 def sample_rate=(rate) raise Argument Error "wrong sample_rate: #{sample_rate.inspect} given for ADS1100" unless SAMPLE_RATES.include?(rate) @sample_rate_mask = SAMPLE_RATES.index(rate) config_register = (config_register & SAMPLE_RATE_CLEAR) | (sample_rate_mask << 2) @sample_rate = rate end |
#volts_per_bit ⇒ Object
122 123 124 |
# File 'lib/denko/analog_io/ads1100.rb', line 122 def volts_per_bit full_scale_voltage / (GAINS[gain_mask] * BIT_RANGES[sample_rate_mask]).to_f end |