Class: Denko::AnalogIO::Input
- Inherits:
-
Object
- Object
- Denko::AnalogIO::Input
- Defined in:
- lib/denko/analog_io/input.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#gain ⇒ Object
readonly
Returns the value of attribute gain.
-
#negative_pin ⇒ Object
readonly
Returns the value of attribute negative_pin.
-
#sample_rate ⇒ Object
readonly
Returns the value of attribute sample_rate.
-
#smoothing ⇒ Object
Smoothing features.
-
#volts_per_bit ⇒ Object
ADCs can set this based on gain, so exact voltages can be calculated.
Attributes included from Behaviors::Listener
Attributes included from Behaviors::Callbacks
Attributes included from Behaviors::Threaded
Attributes included from Behaviors::SinglePin
Attributes included from Behaviors::Component
Instance Method Summary collapse
- #_listen(divider = nil) ⇒ Object
- #_read ⇒ Object
- #after_initialize(options = {}) ⇒ Object
- #before_initialize(options = {}) ⇒ Object
-
#on_change(&block) ⇒ Object
Attach a callback that only fires when state changes.
-
#pre_callback_filter(value) ⇒ Object
Convert data to integer, or pass it through smoothing if enabled.
- #smooth_input(value) ⇒ Object
Methods included from Behaviors::Listener
Methods included from Behaviors::Callbacks
#add_callback, #callbacks, #initialize, #remove_callback, #update
Methods included from Behaviors::State
Methods included from Behaviors::Poller
Methods included from Behaviors::Threaded
#enable_interrupts, included, #stop, #stop_thread, #threaded, #threaded_loop
Methods included from Behaviors::Reader
#read, #read_using, #wait_for_read
Methods included from Behaviors::InputPin
Methods included from Behaviors::Component
Instance Attribute Details
#gain ⇒ Object (readonly)
Returns the value of attribute gain.
35 36 37 |
# File 'lib/denko/analog_io/input.rb', line 35 def gain @gain end |
#negative_pin ⇒ Object (readonly)
Returns the value of attribute negative_pin.
35 36 37 |
# File 'lib/denko/analog_io/input.rb', line 35 def negative_pin @negative_pin end |
#sample_rate ⇒ Object (readonly)
Returns the value of attribute sample_rate.
35 36 37 |
# File 'lib/denko/analog_io/input.rb', line 35 def sample_rate @sample_rate end |
#smoothing ⇒ Object
Smoothing features. Does a moving average of the last 8 readings.
60 61 62 |
# File 'lib/denko/analog_io/input.rb', line 60 def smoothing @smoothing end |
#volts_per_bit ⇒ Object
ADCs can set this based on gain, so exact voltages can be calculated.
38 39 40 |
# File 'lib/denko/analog_io/input.rb', line 38 def volts_per_bit @volts_per_bit end |
Instance Method Details
#_listen(divider = nil) ⇒ Object
44 45 46 47 |
# File 'lib/denko/analog_io/input.rb', line 44 def _listen(divider=nil) @divider = divider || @divider board.analog_listen(pin, @divider) end |
#_read ⇒ Object
40 41 42 |
# File 'lib/denko/analog_io/input.rb', line 40 def _read board.analog_read(pin, negative_pin, gain, sample_rate) end |
#after_initialize(options = {}) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/denko/analog_io/input.rb', line 15 def after_initialize(={}) super() # Default 16ms listener for analog inputs connected to a Board. @divider = 16 # If using a negative input on a supported ADC, store the pin. @negative_pin = [:negative_pin] # If the ADC has a programmable amplifier, pass through its setting. @gain = [:gain] # If using a non-default sampling rate, store it. @sample_rate = [:sample_rate] # Default to smoothing disabled. @smoothing = false @smoothing_set ||= [] end |
#before_initialize(options = {}) ⇒ Object
9 10 11 12 13 |
# File 'lib/denko/analog_io/input.rb', line 9 def before_initialize(={}) [:board] = [:adc] if [:adc] [:adc] = nil super() end |
#on_change(&block) ⇒ Object
Attach a callback that only fires when state changes.
50 51 52 53 54 |
# File 'lib/denko/analog_io/input.rb', line 50 def on_change(&block) add_callback(:on_change) do |new_state| block.call(new_state) if new_state != self.state end end |
#pre_callback_filter(value) ⇒ Object
Convert data to integer, or pass it through smoothing if enabled.
74 75 76 |
# File 'lib/denko/analog_io/input.rb', line 74 def pre_callback_filter(value) smoothing ? smooth_input(value.to_i) : value.to_i end |
#smooth_input(value) ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/denko/analog_io/input.rb', line 62 def smooth_input(value) # Add new value, but limit to the 8 latest values. @smoothing_set << value @smoothing_set.shift if @smoothing_set.length > 8 average = @smoothing_set.reduce(:+) / @smoothing_set.length.to_f # Round up or down based on previous state to reduce fluctuations. state && (state > average) ? average.ceil : average.floor end |