Class: Denko::AnalogIO::Input

Inherits:
Object
  • Object
show all
Includes:
Behaviors::InputPin, Behaviors::Listener, Behaviors::Poller, Behaviors::Reader
Defined in:
lib/denko/analog_io/input.rb

Direct Known Subclasses

Potentiometer, Sensor

Instance Attribute Summary collapse

Attributes included from Behaviors::Listener

#divider

Attributes included from Behaviors::Callbacks

#callback_mutex

Attributes included from Behaviors::Threaded

#interrupts_enabled, #thread

Attributes included from Behaviors::SinglePin

#mode, #pin

Attributes included from Behaviors::Component

#board

Instance Method Summary collapse

Methods included from Behaviors::Listener

#listen, #stop

Methods included from Behaviors::Callbacks

#add_callback, #callbacks, #initialize, #remove_callback, #update

Methods included from Behaviors::State

#initialize, #state

Methods included from Behaviors::Poller

#poll, #poll_using, #stop

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

#_stop_listener

Methods included from Behaviors::Component

#initialize, #micro_delay

Instance Attribute Details

#gainObject (readonly)

Returns the value of attribute gain.



35
36
37
# File 'lib/denko/analog_io/input.rb', line 35

def gain
  @gain
end

#negative_pinObject (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_rateObject (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

#smoothingObject

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_bitObject

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

#_readObject



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(options={})
  super(options)

  # 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 = options[:negative_pin]

  # If the ADC has a programmable amplifier, pass through its setting.
  @gain = options[:gain]

  # If using a non-default sampling rate, store it.
  @sample_rate = options[: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(options={})        
  options[:board] = options[:adc] if options[:adc]
  options[:adc] = nil
  super(options)
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