Sha256: 526c3f02eefd0e2832360d58463f0a7312c4aefd2849902d8ba4887167496248

Contents?: true

Size: 1.4 KB

Versions: 10

Compression:

Stored size: 1.4 KB

Contents

require_relative "../interfaces/lab/signal"

class Ecu
  class Signal

    DEFAULT_SIGNAL_SIZE = 32
    DEFAULT_PERIOD      = 10e-3

    attr_accessor :name, :description, :task

    def initialize(name:, task: "Undefined", description: nil)
      @name        = name.chomp.strip
      @task        = task
      @description = description
    end

    def <=>(other)
      return nil unless other.is_a?(self.class)
      name.downcase <=> other.name.downcase
    end

    def ==(other)
      name == other.name
    end
    alias :eql? :==

    def hash
      [name].hash
    end

    def grep(regexp)
      name.match(regexp) || (!description.nil? && description.match(regexp))
    end

    def bandwidth
      DEFAULT_SIGNAL_SIZE / period
    end

    def period
      return DEFAULT_PERIOD if task.nil?
      case task
      when /100us/   then 100e-6
      when /10ms/    then 10e-3
      when /segment/ then 12e-3
      when /100ms/   then 100e-3
      when /1s/      then 1
      else DEFAULT_PERIOD
      end
    end

    def basename
      name.sub(/_\[\d+\]$/, "")
    end

    def to_h
      instance_variables.
        map { |v| [v.to_s[1..-1].to_sym, instance_variable_get(v)] }.
        to_h
    end

    def to_s
      str = "Signal: #{name}"
      str << " [#{task}]" unless task.nil?
      str
    end

    def with(hsh={})
      return self if hsh.empty?
      self.class.new(**to_h.merge(hsh))
    end

  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
automotive-ecu-0.1.9 lib/ecu/signals/signal.rb
automotive-ecu-0.1.8 lib/ecu/signals/signal.rb
automotive-ecu-0.1.7 lib/ecu/signals/signal.rb
automotive-ecu-0.1.6 lib/ecu/signals/signal.rb
automotive-ecu-0.1.5 lib/ecu/signals/signal.rb
automotive-ecu-0.1.4 lib/ecu/signals/signal.rb
automotive-ecu-0.1.3 lib/ecu/signals/signal.rb
automotive-ecu-0.1.2 lib/ecu/signals/signal.rb
automotive-ecu-0.1.1 lib/ecu/signals/signal.rb
automotive-ecu-0.1.0 lib/ecu/signals/signal.rb