class Beaglebone::GPIOPin

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

Public Class Methods

new(pin, mode, pullmode = nil, slewrate = nil) click to toggle source

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

digital_read() click to toggle source

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
digital_write(state) click to toggle source

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() click to toggle source

Disable GPIO pin

# File lib/beaglebone/gpio.rb, line 726
def disable_gpio_pin
  GPIO::disable_gpio_pin(@pin)
end
get_gpio_edge() click to toggle source

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
get_gpio_mode() click to toggle source

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
get_gpio_state() click to toggle source

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
run_on_edge(callback, edge, timeout=nil, repeats=nil) click to toggle source

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
run_once_on_edge(callback, edge, timeout=nil) click to toggle source

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(edge, force=nil) click to toggle source

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(mode) click to toggle source

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
stop_edge_wait() click to toggle source

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_edge(edge, timeout=nil) click to toggle source

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