Module: Denko::AnalogIO::ADS111X
- Includes:
- Behaviors::BoardProxy, Behaviors::Reader
- Defined in:
- lib/denko/analog_io/ads111x.rb
Constant Summary collapse
- PGA_SETTINGS =
Bitmask Full scale voltage
[ # Bitmask Full scale voltage 0.0001875, # 0b000 6.144 V 0.000125, # 0b001 4.095 V 0.0000625, # 0b010 2.048 V (default) 0.00003125, # 0b011 1.024 V 0.000015625, # 0b100 0.512 V 0.0000078125, # 0b101 0.256 V 0.0000078125, # 0b110 0.256 V 0.0000078125, # 0b111 0.256 V ]
- PGA_RANGE =
(0..7).to_a
- SAMPLE_TIMES =
Sample rate bitmask maps to sample time in seconds.
[ # Bitmask 0.125, # 0b000 0.0625, # 0b001 0.03125, # 0b010 0.015625, # 0b011 0.0078125, # 0b100 (default) 0.004, # 0b101 0.002105263, # 0b110 0.00116279, # 0b111 ]
- SAMPLE_RATE_RANGE =
0b111
(0..7).to_a
- WAIT_TIMES =
Wait times need to be slightly longer than the actual sample times.
SAMPLE_TIMES.map { |time| time + 0.0005 }
- MUX_SETTINGS =
Mux bits map to array of form [positive input, negative input].
{ 0b000 => [0, 1], 0b001 => [0, 3], 0b010 => [1, 3], 0b011 => [2, 3], 0b100 => [0, nil], 0b101 => [1, nil], 0b110 => [2, nil], 0b111 => [3, nil], }
Constants included from Behaviors::Reader
Behaviors::Reader::READ_WAIT_TIME
Constants included from Behaviors::Lifecycle
Behaviors::Lifecycle::CALLBACK_METHODS
Instance Attribute Summary collapse
-
#active_gain ⇒ Object
Returns the value of attribute active_gain.
-
#active_pin ⇒ Object
Returns the value of attribute active_pin.
-
#config_register ⇒ Object
Returns the value of attribute config_register.
Attributes included from Behaviors::State
Instance Method Summary collapse
- #analog_listen(pin, divider = nil) ⇒ Object
- #analog_read(pin, negative_pin = nil, gain = nil, sample_rate = nil) ⇒ Object
-
#enable_proxy ⇒ Object
Mimic Board#update, but inside a callback, wrapped by #update.
- #mutex ⇒ Object
- #pins_to_mux_bits(pin, negative_pin) ⇒ Object
- #pre_callback_filter(bytes) ⇒ Object
- #stop_listener(pin) ⇒ Object
Methods included from Behaviors::BoardProxy
#analog_read_high, #analog_write_high, #convert_pin, #high, #low, #set_pin_mode, #start_read
Methods included from Behaviors::Subcomponents
#add_component, #add_hw_i2c, #add_hw_spi, #add_single_pin, #components, #hw_i2c_comps, #hw_spi_comps, #remove_component, #remove_hw_i2c, #remove_hw_spi, #remove_single_pin, #single_pin_components
Methods included from Behaviors::Reader
#_read, #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
Instance Attribute Details
#active_gain ⇒ Object
Returns the value of attribute active_gain.
8 9 10 |
# File 'lib/denko/analog_io/ads111x.rb', line 8 def active_gain @active_gain end |
#active_pin ⇒ Object
Returns the value of attribute active_pin.
8 9 10 |
# File 'lib/denko/analog_io/ads111x.rb', line 8 def active_pin @active_pin end |
#config_register ⇒ Object
Returns the value of attribute config_register.
8 9 10 |
# File 'lib/denko/analog_io/ads111x.rb', line 8 def config_register @config_register end |
Instance Method Details
#analog_listen(pin, divider = nil) ⇒ Object
124 125 126 |
# File 'lib/denko/analog_io/ads111x.rb', line 124 def analog_listen(pin, divider=nil) raise StandardError, "ADS111X does not implement #listen for subcomponents. Use #read or #poll instead" end |
#analog_read(pin, negative_pin = nil, gain = nil, sample_rate = nil) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/denko/analog_io/ads111x.rb', line 72 def analog_read(pin, negative_pin=nil, gain=nil, sample_rate=nil) # Wrap in mutex so calls and callbacks are atomic. mutex.lock # Default gain and sample rate. gain ||= 0b010 sample_rate ||= 0b100 # Set these for callbacks. self.active_pin = pin self.active_gain = gain # Set gain in upper config register. raise ArgumentError "wrong gain: #{gain.inspect} given for ADS111X" unless PGA_RANGE.include?(gain) config_register[0] = self.class::BASE_MSB | (gain << 1) # Set mux bits in upper config register. mux_bits = pins_to_mux_bits(pin, negative_pin) config_register[0] = config_register[0] | (mux_bits << 4) # Set sample rate in lower config_register. raise ArgumentError "wrong sample_rate: #{sample_rate.inspect} given for ADS111X" unless SAMPLE_RATE_RANGE.include?(sample_rate) config_register[1] = self.class::BASE_LSB | (sample_rate << 5) result = read(config_register) mutex.unlock result end |
#enable_proxy ⇒ Object
Mimic Board#update, but inside a callback, wrapped by #update.
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/denko/analog_io/ads111x.rb', line 56 def enable_proxy self.add_callback(:board_proxy) do |value| components.each do |component| if active_pin == component.pin component.volts_per_bit = PGA_SETTINGS[active_gain] component.update(value) end end end end |
#mutex ⇒ Object
131 132 133 134 |
# File 'lib/denko/analog_io/ads111x.rb', line 131 def mutex # mruby doesn't have Thread or Mutex, so only stub there. @mutex ||= Denko.mruby? ? Denko::MutexStub.new : Mutex.new end |
#pins_to_mux_bits(pin, negative_pin) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/denko/analog_io/ads111x.rb', line 102 def pins_to_mux_bits(pin, negative_pin) # Pin 1 is negative input. Only pin 0 can be read. if negative_pin == 1 raise ArgumentError, "given pin: #{pin.inspect} cannot be used when pin 1 is negative input, only 0" unless pin == 0 return 0b000 end # Pin 3 is negative input. Pins 0..2 can be read. if negative_pin == 3 raise ArgumentError, "given pin: #{pin.inspect} cannot be used when pin 3 is negative input, only 0..2" unless [0,1,2].include? pin return 0b001 + pin end # No negative input. Any pin from 0 to 3 can be read. unless negative_pin raise ArgumentError, "given pin: #{pin.inspect} is out of range 0..3" unless [0,1,2,3].include? pin return (0b100 + pin) end raise ArgumentError, "only pins 1 and 3 can be used as negative input" end |
#pre_callback_filter(bytes) ⇒ Object
67 68 69 70 |
# File 'lib/denko/analog_io/ads111x.rb', line 67 def pre_callback_filter(bytes) # Pack the 2 bytes into a string, then unpack as big-endian int16. value = bytes.pack("C*").unpack("s>")[0] end |
#stop_listener(pin) ⇒ Object
128 129 |
# File 'lib/denko/analog_io/ads111x.rb', line 128 def stop_listener(pin) end |