module Beaglebone

This is the main module for this gem. You generally do not want to call these methods directly.

Constants

I2CS

I2C device hash

PINS

Hash of pins and their uses

SPIS

SPI device hash

TREES

Generic device trees

UARTS

UART device hash

Attributes

loaded_dtbs[RW]
pinmutex[RW]
pinstatus[RW]

Public Class Methods

check_valid_pin(pin, type = nil) click to toggle source

check if a pin of given type is valid

# File lib/beaglebone/beaglebone.rb, line 227
def check_valid_pin(pin, type = nil)
  #check to see if pin exists
  raise ArgumentError, "No such PIN: #{pin.to_s}" unless PINS[pin]

  if type
    raise StandardError, "Pin does not support #{type}: #{pin.to_s}" unless PINS[pin][type]
  end
end
cleanup() click to toggle source

cleanup all the things

# File lib/beaglebone/beaglebone.rb, line 275
def cleanup
  Beaglebone::AIN.cleanup
  Beaglebone::PWM.cleanup
  Beaglebone::GPIO.cleanup
  Beaglebone::UART.cleanup
  Beaglebone::I2C.cleanup
  Beaglebone::SPI.cleanup
end
delete_pin_status(pin, key = nil) click to toggle source

@private delete pin’s hash entry

# File lib/beaglebone/beaglebone.rb, line 199
def delete_pin_status(pin, key = nil)
  pinmutex.synchronize do
    if key.nil?
      pinstatus.delete(pin)
    else
      pinstatus[pin].delete(key) if pinstatus[pin]
    end
  end
end
device_tree_load(name, delay=0.25) click to toggle source

load a device tree

# File lib/beaglebone/beaglebone.rb, line 247
def device_tree_load(name, delay=0.25)
  return true if loaded_dtbs.include?(name)

  if device_tree_loaded?(name)
    loaded_dtbs << name
    return true
  end

  File.open("#{get_capemgr_dir}/slots", 'w') { |f| f.write(name) }
  sleep delay
  raise StandardError, "Unable to load device tree: #{name}" unless device_tree_loaded?(name)
  true
end
device_tree_loaded?(name) click to toggle source

check if device tree is loaded

# File lib/beaglebone/beaglebone.rb, line 242
def device_tree_loaded?(name)
  !!File.open("#{get_capemgr_dir}/slots").read.match(/,#{name}$/)
end
device_tree_unload(name) click to toggle source

unload a device tree, return false if not loaded, return true if it unloads

# File lib/beaglebone/beaglebone.rb, line 262
def device_tree_unload(name)
  return false unless device_tree_loaded?(name)

  dtb_id = File.open("#{get_capemgr_dir}/slots", 'r').read.scan(/^ ?(\d+): .*?,#{name}/).flatten.first

  File.open("#{get_capemgr_dir}/slots", 'w') { |f| f.write("-#{dtb_id}") }

  raise StandardError, "Unable to unload device tree: #{name}" if device_tree_loaded?(name)

  true
end
disable_pin(pin) click to toggle source

disable pin

# File lib/beaglebone/beaglebone.rb, line 210
def disable_pin(pin)
  status = get_pin_status(pin)

  if status
    case status[:type]
      when :gpio
        Beaglebone::GPIO.disable_gpio_pin(pin)
      when :pwm
        Beaglebone::PWM.disable_pwm_pin(pin)
      else
        #we can't disable any other pin types at this time
        raise StandardError, "Cannot disable pin: #{pin} in #{status[:type]} mode"
    end
  end
end
get_capemgr_dir() click to toggle source

return capemgr directory

# File lib/beaglebone/beaglebone.rb, line 237
def get_capemgr_dir
  Dir.glob([ '/sys/devices/bone_capemgr.*', '/sys/devices/platform/bone_capemgr' ]).first
end
get_pin_status(pin, key = nil) click to toggle source

@private get hash entry for pin

# File lib/beaglebone/beaglebone.rb, line 178
def get_pin_status(pin, key = nil)
  pinmutex.synchronize do
    if key
      pinstatus[pin] ? pinstatus[pin][key] : nil
    else
      pinstatus[pin]
    end
  end
end
set_pin_status(pin, key, value) click to toggle source

@private set hash entry for pin

# File lib/beaglebone/beaglebone.rb, line 190
def set_pin_status(pin, key, value)
  pinmutex.synchronize do
    pinstatus[pin]    ||= {}
    pinstatus[pin][key] = value
  end
end