class Beaglebone::AINPin

Object Oriented AIN Implementation. This treats the pin as an object.

Public Class Methods

new(pin) click to toggle source

Initialize a Analog pin Return’s an AINPin object

@example

p9_33 = AINPin.new(:P9_33)
# File lib/beaglebone/ain.rb, line 362
def initialize(pin)
  @pin = pin
end

Public Instance Methods

disable_analog_pin() click to toggle source

Disable analog pin

# File lib/beaglebone/ain.rb, line 473
def disable_analog_pin
  AIN::disable_analog_pin(@pin)
end
read() click to toggle source

Read from an analog pin

@return [Integer] value in millivolts

@example

p9_33 = AINPin.new(:P9_33)
p9_33.read => 1799
# File lib/beaglebone/ain.rb, line 373
def read
  AIN::read(@pin)
end
run_on_change(callback, mv_change=10, interval=0.01, repeats=nil) click to toggle source

Runs a callback after voltage changes by specified amount. This creates a new thread that runs in the background and polls at specified interval.

@param callback A method to call when the change is detected This method should take 4 arguments: the pin, the previous voltage, the current voltage, and the counter @param mv_change an integer specifying the required change in mv @param interval a number representing the wait time between polling @param repeats is optional and specifies the number of times the callback will be run

@example

# This polls every 0.1 seconds and will run after a 10mv change is detected
callback = lambda { |pin, mv_last, mv, count| puts "[#{count}] #{pin} #{mv_last} -> #{mv}" }
p9_33 = AINPin.new(:P9_33)
p9_33.run_on_change(callback, 10, 0.1)
# File lib/beaglebone/ain.rb, line 391
def run_on_change(callback, mv_change=10, interval=0.01, repeats=nil)
  AIN::run_on_change(callback, @pin, mv_change, interval, repeats)
end
run_on_threshold(callback, mv_lower, mv_upper, mv_reset=10, interval=0.01, repeats=nil) click to toggle source

Runs a callback after voltage changes beyond a certain threshold. This creates a new thread that runs in the background and polls at specified interval. When the voltage crosses the specified thresholds the callback is run.

@param callback A method to call when the change is detected. This method should take 6 arguments: the pin, the previous voltage, the current voltage, the previous state, the current state, and the counter @param mv_lower an integer specifying the lower threshold voltage @param mv_upper an integer specifying the upper threshold voltage @param mv_reset an integer specifying the range in mv required to reset the threshold trigger @param interval a number representing the wait time between polling @param repeats is optional and specifies the number of times the callback will be run

@example

# This polls every 0.01 seconds and will run after a the voltage crosses 400mv or 1200mv.
# Voltage will have to cross a range by at least 5mv to prevent rapidly triggering events
callback = lambda { |pin, mv_last, mv, state_last, state, count|
  puts "[#{count}] #{pin} #{state_last} -> #{state}     #{mv_last} -> #{mv}"
}
p9_33 = AINPin.new(:P9_33)
p9_33.run_on_threshold(callback, 400, 1200, 5, 0.01)
# File lib/beaglebone/ain.rb, line 421
def run_on_threshold(callback, mv_lower, mv_upper, mv_reset=10, interval=0.01, repeats=nil)
  AIN::run_on_threshold(callback, @pin, mv_lower, mv_upper, mv_reset, interval, repeats)
end
run_once_on_change(callback, mv_change=10, interval=0.01) click to toggle source

Runs a callback once after specified change in voltage detected Convenience method for run_on_change

# File lib/beaglebone/ain.rb, line 397
def run_once_on_change(callback, mv_change=10, interval=0.01)
  AIN::run_once_on_change(callback, @pin, mv_change, interval)
end
run_once_on_threshold(callback, mv_lower, mv_upper, mv_reset=10, interval=0.01) click to toggle source

Runs a callback once after voltage crosses a specified threshold. Convenience method for run_on_threshold

# File lib/beaglebone/ain.rb, line 428
def run_once_on_threshold(callback, mv_lower, mv_upper, mv_reset=10, interval=0.01)
  AIN::run_once_on_threshold(callback, @pin, mv_lower, mv_upper, mv_reset, interval)
end
stop_wait() click to toggle source

Stops any threads waiting for data on this pin

# File lib/beaglebone/ain.rb, line 468
def stop_wait
  AIN::stop_wait(@pin)
end
wait_for_change(mv_change, interval, mv_last=nil) click to toggle source

Returns when voltage changes by specified amount

@param mv_change an integer specifying the required change in mv @param interval a number representing the wait time between polling @param mv_last is optional and specifies the voltage to use as the initial point to measure change

@example

# This will poll every P9_33 every 0.01 seconds until 10mv of change is detected
# This method will return the initial reading, final reading, and how many times it polled
p9_33 = AINPin.new(:P9_33)
p9_33.wait_for_change(10, 0.01) => [ 1200, 1210, 4]
# File lib/beaglebone/ain.rb, line 463
def wait_for_change(mv_change, interval, mv_last=nil)
  AIN::wait_for_change(@pin, mv_change, interval, mv_last)
end
wait_for_threshold(mv_lower, mv_upper, mv_reset=10, interval=0.01, mv_last=nil, state_last=nil) click to toggle source

Returns when voltage changes by specified amount @param mv_lower an integer specifying the lower threshold voltage @param mv_upper an integer specifying the upper threshold voltage @param mv_reset an integer specifying the range in mv required to reset the threshold trigger @param interval a number representing the wait time between polling @param mv_last is optional and specifies the voltage to use as the initial point to measure change @param state_last is optional and specifies the state to use as the initial state to watch change

@example

# This polls every 0.01 seconds and will run after a the voltage crosses 400mv or 1200mv.
# Voltage will have to cross a range by at least 5mv to prevent rapidly triggering events
callback = lambda { |pin, mv_last, mv, state_last, state, count|
  puts "[#{count}] #{pin} #{state_last} -> #{state}     #{mv_last} -> #{mv}"
}
p9_33 = AINPin.new(:P9_33)
p9_33.wait_for_threshold(400, 1200, 5, 0.01)
# File lib/beaglebone/ain.rb, line 448
def wait_for_threshold(mv_lower, mv_upper, mv_reset=10, interval=0.01, mv_last=nil, state_last=nil)
  AIN::wait_for_threshold(@pin, mv_lower, mv_upper, mv_reset, interval, mv_last, state_last)
end