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) ⇒ 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



479
480
481
482
483
# File 'lib/beaglebone/gpio.rb', line 479

def initialize(pin, mode)
  @pin = pin

  GPIO::pin_mode(@pin, mode)
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



504
505
506
# File 'lib/beaglebone/gpio.rb', line 504

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



493
494
495
# File 'lib/beaglebone/gpio.rb', line 493

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

#disable_gpio_pinObject

Disable GPIO pin



593
594
595
# File 'lib/beaglebone/gpio.rb', line 593

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



565
566
567
# File 'lib/beaglebone/gpio.rb', line 565

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



559
560
561
# File 'lib/beaglebone/gpio.rb', line 559

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



553
554
555
# File 'lib/beaglebone/gpio.rb', line 553

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



519
520
521
# File 'lib/beaglebone/gpio.rb', line 519

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:



526
527
528
# File 'lib/beaglebone/gpio.rb', line 526

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



588
589
590
# File 'lib/beaglebone/gpio.rb', line 588

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



577
578
579
# File 'lib/beaglebone/gpio.rb', line 577

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

#stop_edge_waitObject

Stops any threads waiting for data on this pin



532
533
534
# File 'lib/beaglebone/gpio.rb', line 532

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



547
548
549
# File 'lib/beaglebone/gpio.rb', line 547

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