Module: Beaglebone::GPIO

Defined in:
lib/beaglebone/GPIO/GPIO.rb,
lib/beaglebone/GPIO/pins.rb

Overview

General Purpose I/O

Constant Summary collapse

DIRECTION =
{ :IN => 'in', :OUT => 'out', :LOW => 'in', :HIGH => 'out' }
VALUE =
{ :LOW => 0, :HIGH => 1, 0 => 0, 1 => 1 }
PINS =
{
  :P9_11 => 30, :P9_12 => 60,
  :P9_13 => 31, :P9_14 => 50,
  :P9_15 => 48, :P9_16 => 51,
  :P9_17 => 5, :P9_18 => 4,

  :P9_21 => 3, :P9_22 => 2,
  :P9_23 => 49, :P9_24 => 15,
  :P9_25 => 117, :P9_26 => 14,
  :P9_27 => 115, :P9_28 => 113,
  :P9_29 => 111, :P9_30 => 112,
  :P9_31 => 110,

  :P9_41 => 20, :P9_42 => 7,

  :P8_3 => 38, :P8_4 => 39,
  :P8_5 => 34, :P8_6 => 35,
  :P8_7 => 66, :P8_8 => 67,
  :P8_9 => 69, :P8_10 => 68,
  :P8_11 => 45, :P8_12 => 44,
  :P8_13 => 23, :P8_14 => 26,
  :P8_15 => 47, :P8_16 => 46,
  :P8_17 => 27, :P8_18 => 65,
  :P8_19 => 22, :P8_20 => 63,
  :P8_21 => 62, :P8_22 => 37,
  :P8_23 => 36, :P8_24 => 33,
  :P8_25 => 32, :P8_26 => 61,
  :P8_27 => 86, :P8_28 => 88,
  :P8_29 => 87, :P8_30 => 89,
  :P8_31 => 10, :P8_32 => 11,
  :P8_33 => 9, :P8_34 => 81,
  :P8_35 => 8, :P8_36 => 80,
  :P8_37 => 78, :P8_38 => 79,
  :P8_39 => 76, :P8_40 => 77,
  :P8_41 => 74, :P8_42 => 75,
  :P8_43 => 72, :P8_44 => 73,
  :P8_45 => 70, :P8_46 => 71
}

Class Method Summary collapse

Class Method Details

.disable_pin(pin) ⇒ Boolean

Disables a GPIO pin

Examples:

Beaglebone::GPIO.disable_pin(:P9_12)

Parameters:

  • pin (Object)

    Specifies a value from GPIO::PINS

Returns:

  • (Boolean)

    Returns true for success, false otherwise



55
56
57
58
59
60
61
# File 'lib/beaglebone/GPIO/GPIO.rb', line 55

def self.disable_pin(pin)
  pin = PINS[pin]
  unexport = File.open('/sys/class/gpio/unexport', 'w')
  return false if pin.nil? or unexport.nil?
  unexport.puts(pin)
  return true
end

.enable_pin(pin) ⇒ Boolean

Enables a pin for GPIO

Examples:

Beaglebone::GPIO.enable_pin(:P9_12)

Parameters:

  • pin (Object)

    Specifies a value from GPIO::PINS to enable

Returns:

  • (Boolean)

    Returns true for success, false otherwise



38
39
40
41
42
43
44
# File 'lib/beaglebone/GPIO/GPIO.rb', line 38

def self.enable_pin(pin)
  pin = PINS[pin]
  export = File.open('/sys/class/gpio/export', 'w')
  return false if pin.nil? or export.nil?
  export.puts(pin)
  return true
end

.list(list_all = false) ⇒ Array

Returns an array of active GPIO pins

Examples:

Return all active GPIO pins

Beaglebone::GPIO.list #=> ["gpiochip32", "gpiochip64", "gpiochip96", "gpiochip0"]

Parameters:

  • list_all (Bool) (defaults to: false)

    optional List all active pins including export and unexport utilites

Returns:

  • (Array)

    String array of active gpio pins



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/beaglebone/GPIO/GPIO.rb', line 16

def self.list(list_all = false)
  gpio_array = Dir.entries('/sys/class/gpio')
  gpio_array.delete('.')
  gpio_array.delete('..')

  unless list_all == true
    gpio_array.delete('export')
    gpio_array.delete('unexport')
  end

  gpio_array
end

.set_direction(pin, direction) ⇒ Boolean

Set pin direction

Examples:

Beaglebone::GPIO.set_direction(:P9_12, :OUT)

Parameters:

  • pin (Object)

    Specifies a value from GPIO::PINS

  • direction (Object)

    Specifies a value from GPIO::DIRECTION

Returns:

  • (Boolean)

    Returns true for success, false otherwise



73
74
75
76
77
78
# File 'lib/beaglebone/GPIO/GPIO.rb', line 73

def self.set_direction(pin, direction)
  dir_file = File.open("/sys/class/gpio/gpio#{PINS[pin]}/direction", 'w')
  return false if dir_file.nil?
  dir_file.puts(DIRECTION[direction])
  return true
end

.set_value(pin, value) ⇒ Boolean

Set pin value

Examples:

Beaglebone::GPIO.set_value(:P9_12, :HIGH)

Parameters:

  • pin (Object)

    Specifies a value from GPIO::PINS

  • value (Object)

    Specifies a value from GPIO::VALUE

Returns:

  • (Boolean)

    Returns true for success, false otherwise



90
91
92
93
94
95
# File 'lib/beaglebone/GPIO/GPIO.rb', line 90

def self.set_value(pin, value)
  mode = File.open("/sys/class/gpio/gpio#{PINS[pin]/value}", 'w')
  return false if mode.nil?
  mode.puts(VALUE[value])
  return true
end