Class: Ecu::Signal

Inherits:
Object
  • Object
show all
Defined in:
lib/ecu/signals/signal.rb,
lib/ecu/interfaces/lab/signal.rb

Constant Summary collapse

DEFAULT_SIGNAL_SIZE =
32
DEFAULT_PERIOD =
10e-3

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, task: "Undefined", description: nil) ⇒ Signal

Returns a new instance of Signal.



11
12
13
14
15
# File 'lib/ecu/signals/signal.rb', line 11

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

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



9
10
11
# File 'lib/ecu/signals/signal.rb', line 9

def description
  @description
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/ecu/signals/signal.rb', line 9

def name
  @name
end

#taskObject

Returns the value of attribute task.



9
10
11
# File 'lib/ecu/signals/signal.rb', line 9

def task
  @task
end

Class Method Details

.from_lab(line) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/ecu/interfaces/lab/signal.rb', line 3

def self.from_lab(line)
  name, tasks, comment = line.strip.split(";")
  maintask             = tasks.split("&").first rescue nil
  new(name: name,
      task: maintask,
      description: comment&.strip)
end

Instance Method Details

#<=>(other) ⇒ Object



17
18
19
20
# File 'lib/ecu/signals/signal.rb', line 17

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

#==(other) ⇒ Object Also known as: eql?



22
23
24
# File 'lib/ecu/signals/signal.rb', line 22

def ==(other)
  name == other.name
end

#bandwidthObject



35
36
37
# File 'lib/ecu/signals/signal.rb', line 35

def bandwidth
  DEFAULT_SIGNAL_SIZE / period
end

#basenameObject



51
52
53
# File 'lib/ecu/signals/signal.rb', line 51

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

#grep(regexp) ⇒ Object



31
32
33
# File 'lib/ecu/signals/signal.rb', line 31

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

#hashObject



27
28
29
# File 'lib/ecu/signals/signal.rb', line 27

def hash
  [name].hash
end

#periodObject



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ecu/signals/signal.rb', line 39

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

#to_hObject



55
56
57
58
59
# File 'lib/ecu/signals/signal.rb', line 55

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

#to_labObject



11
12
13
# File 'lib/ecu/interfaces/lab/signal.rb', line 11

def to_lab
  "#{name};#{task};#{description}"
end

#to_sObject



61
62
63
64
65
# File 'lib/ecu/signals/signal.rb', line 61

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

#with(hsh = {}) ⇒ Object



67
68
69
70
# File 'lib/ecu/signals/signal.rb', line 67

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