class LadderDrive::LadderDriveConfig

Public Class Methods

default() click to toggle source
# File lib/ladder_drive/config.rb, line 41
def default
  @config ||= begin
    load File.join("config", "plc.yml")
  end
end
load(path) click to toggle source
# File lib/ladder_drive/config.rb, line 47
def load path
  h = {}
  if File.exist?(path)
    erb = ERB.new File.read(path)
    h = YAML.load(erb.result(binding))
    h = JSON.parse(h.to_json, symbolize_names: true)
  end
  new h || {}
end
new(options={}) click to toggle source
# File lib/ladder_drive/config.rb, line 59
    def initialize options={}
      default = {
        input: "asm/main.asm",
        output: "build/main.hex",
      }
      emulator_default = {
        host: "localhost",
        port: 5555,
        protocol: "emu_protocol",
        keep: [
          ["L0", "L1023"],
          ["H0", "H1023"],
        ],
      }
      raspberrypi_default = {
        host: "localhost",
        port: 5555,
        protocol: "emu_protocol",
        keep: [
          ["L0", "L1023"],
          ["H0", "H1023"],
        ],
        io: {
          inputs: {
            x0: 4,
            x1: 17,
            x2: 27,
            x3: 22,
            x4: 5,
            x5: 6,
            x6: 13,
            x7: 19,
            x8: 26,
          },
          outputs: {
=begin
            y0: 18,
            y1: 23,
            y2: 24,
            y3: 25,
            y4: 12,
            y5: 16,
            y6: 20,
            y7: 21,
=end
          }
        },
      }

      @config = default.merge options
      @config[:plc] ||= {}
      @config[:plc][:emulator] = @config[:plc][:emulator] ? emulator_default.merge(@config[:plc][:emulator]) : emulator_default
      @config[:plc][:raspberrypi] = @config[:plc][:raspberrypi] ? emulator_default.merge(@config[:plc][:raspberrypi]) : raspberrypi_default

      @config[:default] ||= {}

      @targets = {}
    end

Public Instance Methods

[](key) click to toggle source
# File lib/ladder_drive/config.rb, line 118
def [] key
  @config[key]
end
method_missing(name, *args) click to toggle source
# File lib/ladder_drive/config.rb, line 137
def method_missing(name, *args)
  name = name.to_s unless name.is_a? String
  case name.to_s
  when /(.*)=$/
    @config[$1.to_sym] = args.first
  else
    @config[name.to_sym]
  end
end
target(name=nil) click to toggle source
# File lib/ladder_drive/config.rb, line 122
def target name=nil
  name ||= (@config[:default][:target] || :emulator)
  name = name.to_sym if name.is_a? String
  target = @targets[name]
  unless target
    h = @config[:plc][name]
    unless h.nil? || h.empty?
      h = {name:name}.merge h
      target = LadderDriveConfigTarget.new h
      @targets[name] = target
    end
  end
  target
end