Class: BBB::Pins::IO::PinMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/BBB/pins/io/pin_mapper.rb

Overview

This class provides a convenient way of mapping a JSON representation of the pins, taken from the bonescript sourcecode, into Ruby objects. After converting the json to ruby objects the “normal” PinMapper will take the object structure and provide the actual mapping from the pin header positions, like p8_3 to the correct GPIO, I2C or WPM endpoints.

This class should not be used directly to provide these mappings it’s simply a helper class to get from JSON to Ruby.

Defined Under Namespace

Classes: I2C, Pin, PinMap, UART

Constant Summary collapse

PIN_MAP_FILE =
File.expand_path("../../../../../resources/pin_mappings.json", __FILE__)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ PinMapper

Initialize a JSONPinMapper

Parameters:

  • hash (Hash)

    Hash that was converted from json with the keys pinIndex, uart and i2c.



32
33
34
# File 'lib/BBB/pins/io/pin_mapper.rb', line 32

def initialize(hash)
  @data = hash
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



24
25
26
# File 'lib/BBB/pins/io/pin_mapper.rb', line 24

def data
  @data
end

Class Method Details

.convert(json_filename) ⇒ Object

Factor a new JSONPinMapper based on a json_file that get’s parsed to a hash.

Parameters:

  • json_filename (String)

    The file that contains the JSON object with pin information

Returns:

  • JSONPinMapper instance



45
46
47
48
49
# File 'lib/BBB/pins/io/pin_mapper.rb', line 45

def self.convert(json_filename)
  file = File.open(json_filename, "r")
  hash = JSON.parse(file.read)
  new(hash).convert
end

.map(pin_symbol) ⇒ Object

Map a pin symbol and a type to a pin_map or raise an error when a pin can’t be found.

Parameters:

  • pin_symbol (Symbol)


58
59
60
61
62
63
64
65
# File 'lib/BBB/pins/io/pin_mapper.rb', line 58

def self.map(pin_symbol)
  @map ||= convert(PIN_MAP_FILE)
  begin
    @map.pins.fetch(pin_symbol.upcase.to_sym)
  rescue Exception => e
    raise UnknownPinException, "Pin #{pin_symbol} could not be mapped"
  end
end

Instance Method Details

#convertStruct

Convert from a the data hash to ruby objects

Returns:

  • (Struct)

    with pins, uart and i2c as keys. pins has a



73
74
75
76
77
78
79
# File 'lib/BBB/pins/io/pin_mapper.rb', line 73

def convert
  pins = convert_pins(data["pinIndex"])
  uart = convert_uart(data["uarts"])
  i2c  = convert_i2c(data["i2c"])

  PinMap.new(pins, uart, i2c)
end

#convert_i2c(data_hash) ⇒ Hash

Convert a hash of I2Cs to a hash with the I2C folder as the key and a i2c

object as the value

Here is an example of an individual I2C hash:

"/dev/i2c-1": {
    "devicetree": "BB-I2C1",
    "path": "/dev/i2c-2",
    "sda": "P9_18",
    "scl": "P9_17"
}

Parameters:

  • data_hash (Hash)

    with strings as keys and hashes as values

Returns:

  • (Hash)

    with strings of I2C folders as keys and I2C objects as values



163
164
165
# File 'lib/BBB/pins/io/pin_mapper.rb', line 163

def convert_i2c(data_hash)
  convert_hash_with_hashes(data_hash, I2C)
end

#convert_pins(array) ⇒ Hash

Converts an array of pins into a hash with the pin key as a key and a Pin

Struct as a value.

Here is an example of a piece of the JSON code containing pin info:

"name": "USR0",
"gpio": 53,
"led": "usr0",
"mux": "gpmc_a5",
"key": "USR0",
"muxRegOffset": "0x054",
"options": [
    "gpmc_a5",
    "gmii2_txd0",
    "rgmii2_td0",
    "rmii2_txd0",
    "gpmc_a21",
    "pr1_mii1_rxd3",
    "eqep1b_in",
    "gpio1_21"
]

Parameters:

  • array (Array<Hash>)

    An array of Pin hashes

Returns:

  • (Hash)

    a hash with pin keys as keys and pin objects as values.



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/BBB/pins/io/pin_mapper.rb', line 110

def convert_pins(array)
  hash = {}

  array.each do |pin_hash|
    pin = Pin.new
    pin_hash.each_pair do |key, value|
      pin[underscore(key).to_sym] = value
    end
    hash[pin.key.upcase.to_sym] = pin
  end

  return hash
end

#convert_uart(data_hash) ⇒ Hash

Convert a hash of uarts to a hash with the uart folder (e.g. /dev/tty00)

as the key and an uart object as the value.

Here is an example of an individual UART hash:

"/dev/ttyO1": {
    "devicetree": "BB-UART1",
    "rx": "P9_26",
    "tx": "P9_24"
}

Parameters:

  • data_hash (Hash)

    with strings as keys and hashes as values

Returns:

  • (Hash)

    with strings of uart positions (e.g. /dev/tty00) as keys and uart objects as values



141
142
143
# File 'lib/BBB/pins/io/pin_mapper.rb', line 141

def convert_uart(data_hash)
  convert_hash_with_hashes(data_hash, UART)
end