Class: BBB::Pins::IO::PinMapper
- Inherits:
-
Object
- Object
- BBB::Pins::IO::PinMapper
- 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.("../../../../../resources/pin_mappings.json", __FILE__)
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Class Method Summary collapse
-
.convert(json_filename) ⇒ Object
Factor a new JSONPinMapper based on a json_file that get’s parsed to a hash.
-
.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.
Instance Method Summary collapse
-
#convert ⇒ Struct
Convert from a the data hash to ruby objects.
-
#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.
-
#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.
-
#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.
-
#initialize(hash) ⇒ PinMapper
constructor
Initialize a JSONPinMapper.
Constructor Details
#initialize(hash) ⇒ PinMapper
Initialize a JSONPinMapper
32 33 34 |
# File 'lib/BBB/pins/io/pin_mapper.rb', line 32 def initialize(hash) @data = hash end |
Instance Attribute Details
#data ⇒ Object (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.
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.
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
#convert ⇒ Struct
Convert from a the data hash to ruby objects
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"
}
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"
]
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"
}
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 |