Class: Beaglebone::GPIOPin

Inherits:
Object
  • Object
show all
Defined in:
lib/beaglebone/gpio.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(pin, mode, pullmode = nil, slewrate = nil) ⇒ GPIOPin

Initialize a GPIO pin Return’s a GPIOPin object, setting the pin mode on initialization

Examples:

p9_12 = GPIOPin.new(:P9_12, :OUT)
p9_11 = GPIOPin.new(:P9_11, :IN)

Parameters:

  • mode

    should specify the mode of the pin, either :IN or :OUT

  • pullmode (optional) (defaults to: nil)

    should specify the pull mode, :PULLUP, :PULLDOWN, or :NONE

  • slewrate (optional) (defaults to: nil)

    should specify the slew rate, :FAST or :SLOW



612
613
614
615
616
# 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

Instance Method Details

#digital_readSymbol

Reads a pin’s input state and return that value

Examples:

p9_11 = GPIOPin.new(:P9_12, :OUT)
p9_11.digital_read => :HIGH

Returns:

  • (Symbol)

    :HIGH or :LOW



637
638
639
# File 'lib/beaglebone/gpio.rb', line 637

def digital_read
  GPIO::digital_read(@pin)
end

#digital_write(state) ⇒ Object

Sets a pin’s output state

Examples:

p9_12 = GPIOPin.new(:P9_12, :OUT)
p9_12.digital_write(:HIGH)
p9_12.digital_write(:LOW)

Parameters:

  • state

    should be a symbol representin the state, :HIGH or :LOW



626
627
628
# File 'lib/beaglebone/gpio.rb', line 626

def digital_write(state)
  GPIO::digital_write(@pin, state)
end

#disable_gpio_pinObject

Disable GPIO pin



726
727
728
# File 'lib/beaglebone/gpio.rb', line 726

def disable_gpio_pin
  GPIO::disable_gpio_pin(@pin)
end

#get_gpio_edgeSymbol

Returns the GPIO edge trigger type

Returns:

  • (Symbol)

    :NONE, :RISING, :FALLING, or :BOTH



698
699
700
# File 'lib/beaglebone/gpio.rb', line 698

def get_gpio_edge
  GPIO::get_gpio_edge(@pin)
end

#get_gpio_modeSymbol

Returns mode from pin, reads mode if unknown

Returns:

  • (Symbol)

    :IN or :OUT



692
693
694
# File 'lib/beaglebone/gpio.rb', line 692

def get_gpio_mode
  GPIO::get_gpio_mode(@pin)
end

#get_gpio_stateSymbol

Returns last known state from pin, reads state if unknown

Returns:

  • (Symbol)

    :HIGH or :LOW



686
687
688
# 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) ⇒ Object

Runs a callback on an edge trigger event. This creates a new thread that runs in the background

Examples:

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)

Parameters:

  • callback

    A method to call when the edge trigger is detected. This method should take 3 arguments, the pin, the edge, and the counter

  • edge

    should be a symbol representing the trigger type, e.g. :RISING, :FALLING, :BOTH

  • timeout (defaults to: nil)

    is optional and specifies a time window to wait

  • repeats (defaults to: nil)

    is optional and specifies the number of times the callback will be run



652
653
654
# 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) ⇒ Object

Runs a callback one time on an edge trigger event. this is a convenience method for run_on_edge

See Also:



659
660
661
# 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) ⇒ Object

Set GPIO edge trigger type

Examples:

p9_11.set_gpio_edge(:RISING)

Parameters:

  • edge

    should be a symbol representing the trigger type, e.g. :RISING, :FALLING, :BOTH

  • force (defaults to: nil)

    is optional, if set will set the mode even if already set



721
722
723
# 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) ⇒ Object

Set GPIO mode on an initialized pin

Examples:

p9_12.set_gpio_mode(:OUT)
p9_11.set_gpio_mode(:IN)

Parameters:

  • mode

    should specify the mode of the pin, either :IN or :OUT



710
711
712
# File 'lib/beaglebone/gpio.rb', line 710

def set_gpio_mode(mode)
  GPIO::set_gpio_mode(@pin, mode)
end

#stop_edge_waitObject

Stops any threads waiting for data on this pin



665
666
667
# File 'lib/beaglebone/gpio.rb', line 665

def stop_edge_wait
  GPIO::stop_edge_wait(@pin)
end

#wait_for_edge(edge, timeout = nil) ⇒ Symbol

Wait for an edge trigger. Returns the type that triggered the event, e.g. :RISING, :FALLING, :BOTH

Examples:

p9_11 = GPIOPin.new(:P9_11, :IN)
p9_11.wait_for_edge(:RISING, 30) => :RISING

Parameters:

  • edge

    should be a symbol representing the trigger type, e.g. :RISING, :FALLING, :BOTH

  • timeout (defaults to: nil)

    is optional and specifies a time window to wait

Returns:

  • (Symbol)

    :RISING, :FALLING, or :BOTH



680
681
682
# File 'lib/beaglebone/gpio.rb', line 680

def wait_for_edge(edge, timeout=nil)
  GPIO::wait_for_edge(@pin, edge, timeout)
end