Sha256: 309d8a1d351196d84b29148e4d3e4a0c50ceaa50d8830cf60bf16dd01628a368

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

require 'yaml'

module Denko
  class Board
    MAPS_FOLDER = File.join(Denko.root, "vendor/board-maps/yaml")

    attr_reader :map
    
    def load_map(board_name)
      if board_name
        map_path = File.join(MAPS_FOLDER, "#{board_name}.yml")
        @map = YAML.load_file(map_path)
      else
        @map = nil
      end
    rescue
      raise StandardError, "error loading board map from file for board name: '#{board_name}'"
    end
    
    def convert_pin(pin)
      # Convert non numerical strings to symbols.
      pin = pin.to_sym if (pin.class == String) && !(pin.match (/\A\d+\.*\d*/))

      # Handle symbols.
      if (pin.class == Symbol)
        if map && map[pin]
          return map[pin]
        elsif map
          raise ArgumentError, "error in pin: #{pin.inspect}. Make sure that pin is defined for this board by calling Board#map"
        else
          raise ArgumentError, "error in pin: #{pin.inspect}. Given a Symbol, but board has no map. Try using GPIO integer instead"
        end
      end

      # Handle integers.
      return pin if pin.class == Integer

      # Try #to_i on anyting else. Will catch numerical strings.
      begin
        return pin.to_i
      rescue
        raise ArgumentError, "error in pin: #{pin.inspect}"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
denko-0.13.1 lib/denko/board/map.rb
denko-0.13.0 lib/denko/board/map.rb