class Beaglebone::GPIOPin
Object Oriented GPIO
Implementation. This treats the pin as an object.
Public Class Methods
Initialize a GPIO
pin Return’s a GPIOPin
object, setting the pin mode on initialization
@param mode should specify the mode of the pin, either :IN or :OUT @param pullmode (optional) should specify the pull mode, :PULLUP, :PULLDOWN, or :NONE @param slewrate (optional) should specify the slew rate, :FAST or :SLOW
@return [GPIOPin]
@example
p9_12 = GPIOPin.new(:P9_12, :OUT) p9_11 = GPIOPin.new(:P9_11, :IN)
# File lib/beaglebone/gpio.rb, line 612 def initialize(pin, mode, pullmode = nil, slewrate = nil) @pin = pin GPIO::pin_mode(@pin, mode, pullmode, slewrate) end
Public Instance Methods
Reads a pin’s input state and return that value
@return [Symbol] :HIGH or :LOW
@example
p9_11 = GPIOPin.new(:P9_12, :OUT) p9_11.digital_read => :HIGH
# File lib/beaglebone/gpio.rb, line 637 def digital_read GPIO::digital_read(@pin) end
Sets a pin’s output state
@param state should be a symbol representin the state, :HIGH or :LOW
@example
p9_12 = GPIOPin.new(:P9_12, :OUT) p9_12.digital_write(:HIGH) p9_12.digital_write(:LOW)
# File lib/beaglebone/gpio.rb, line 626 def digital_write(state) GPIO::digital_write(@pin, state) end
Disable GPIO
pin
# File lib/beaglebone/gpio.rb, line 726 def disable_gpio_pin GPIO::disable_gpio_pin(@pin) end
Returns the GPIO
edge trigger type @return [Symbol] :NONE, :RISING, :FALLING, or :BOTH
# File lib/beaglebone/gpio.rb, line 698 def get_gpio_edge GPIO::get_gpio_edge(@pin) end
Returns mode from pin, reads mode if unknown @return [Symbol] :IN or :OUT
# File lib/beaglebone/gpio.rb, line 692 def get_gpio_mode GPIO::get_gpio_mode(@pin) end
Returns last known state from pin
, reads state if unknown @return [Symbol] :HIGH or :LOW
# File lib/beaglebone/gpio.rb, line 686 def get_gpio_state GPIO::get_gpio_state(@pin) end
Runs a callback on an edge trigger event. This creates a new thread that runs in the background
@param callback A method to call when the edge trigger is detected. This method should take 3 arguments, the pin, the edge, and the counter @param edge should be a symbol representing the trigger type, e.g. :RISING, :FALLING, :BOTH @param timeout is optional and specifies a time window to wait @param repeats is optional and specifies the number of times the callback will be run
@example
p9_11 = GPIOPin.new(:P9_11, :IN) p9_11.run_on_edge(lambda { |pin,edge,count| puts "[#{count}] #{pin} -- #{edge}" }, :P9_11, :RISING) def run_on_edge(callback, edge, timeout=nil, repeats=nil)
# File lib/beaglebone/gpio.rb, line 652 def run_on_edge(callback, edge, timeout=nil, repeats=nil) GPIO::run_on_edge(callback, @pin, edge, timeout, repeats) end
Runs a callback one time on an edge trigger event. this is a convenience method for run_on_edge
@see run_on_edge
# File lib/beaglebone/gpio.rb, line 659 def run_once_on_edge(callback, edge, timeout=nil) GPIO::run_once_on_edge(callback, @pin, edge, timeout) end
Set GPIO
edge trigger type
@param edge should be a symbol representing the trigger type, e.g. :RISING, :FALLING, :BOTH @param force is optional, if set will set the mode even if already set
@example
p9_11.set_gpio_edge(:RISING)
# File lib/beaglebone/gpio.rb, line 721 def set_gpio_edge(edge, force=nil) GPIO::set_gpio_edge(@pin, edge, force) end
Set GPIO
mode on an initialized pin
@param mode should specify the mode of the pin, either :IN or :OUT
@example
p9_12.set_gpio_mode(:OUT) p9_11.set_gpio_mode(:IN)
# File lib/beaglebone/gpio.rb, line 710 def set_gpio_mode(mode) GPIO::set_gpio_mode(@pin, mode) end
Stops any threads waiting for data on this pin
# File lib/beaglebone/gpio.rb, line 665 def stop_edge_wait GPIO::stop_edge_wait(@pin) end
Wait for an edge trigger. Returns the type that triggered the event, e.g. :RISING, :FALLING, :BOTH
@return [Symbol] :RISING, :FALLING, or :BOTH
@param edge should be a symbol representing the trigger type, e.g. :RISING, :FALLING, :BOTH @param timeout is optional and specifies a time window to wait
@example
p9_11 = GPIOPin.new(:P9_11, :IN) p9_11.wait_for_edge(:RISING, 30) => :RISING
# File lib/beaglebone/gpio.rb, line 680 def wait_for_edge(edge, timeout=nil) GPIO::wait_for_edge(@pin, edge, timeout) end