Class: AVR::Clock

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/avr/clock.rb

Direct Known Subclasses

Oscillator

Defined Under Namespace

Classes: Sink

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil) ⇒ Clock

Returns a new instance of Clock.



52
53
54
55
56
57
58
59
60
# File 'lib/avr/clock.rb', line 52

def initialize(name = nil)
  @name = name
  @sinks = T.let([], T::Array[Sink])
  @watches = T.let({}, T::Hash[Integer, T.untyped])
  @ticks = T.let(0, Integer)
  @count = T.let(0, Integer)
  @scale = T.let(1, Integer)
  @last_tick = T.let(0, Integer)
end

Instance Attribute Details

#countObject

Returns the value of attribute count.



43
44
45
# File 'lib/avr/clock.rb', line 43

def count
  @count
end

#nameObject (readonly)

Returns the value of attribute name.



34
35
36
# File 'lib/avr/clock.rb', line 34

def name
  @name
end

#scaleObject

Returns the value of attribute scale.



49
50
51
# File 'lib/avr/clock.rb', line 49

def scale
  @scale
end

#sinksObject (readonly)

Returns the value of attribute sinks.



37
38
39
# File 'lib/avr/clock.rb', line 37

def sinks
  @sinks
end

#ticksObject

Returns the value of attribute ticks.



46
47
48
# File 'lib/avr/clock.rb', line 46

def ticks
  @ticks
end

#watchesObject (readonly)

Returns the value of attribute watches.



40
41
42
# File 'lib/avr/clock.rb', line 40

def watches
  @watches
end

Instance Method Details

#clear_sinksObject



73
74
75
# File 'lib/avr/clock.rb', line 73

def clear_sinks
  @sinks = []
end

#notify_at_tick(tick, sink) ⇒ Object



78
79
80
81
# File 'lib/avr/clock.rb', line 78

def notify_at_tick(tick, sink)
  @watches[tick] ||= []
  @watches[tick] << sink
end

#push_sink(sink) ⇒ Object



68
69
70
# File 'lib/avr/clock.rb', line 68

def push_sink(sink)
  sinks.push(sink)
end

#tick(_source = nil, _ticks = nil) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/avr/clock.rb', line 84

def tick(_source = nil, _ticks = nil)
  @count += 1
  if (@count % @scale).zero?
    @last_tick = @ticks
    sinks.each do |sink|
      sink.tick(self, @ticks)
    end
    watches[@ticks]&.each do |watch|
      watch.tick(self, @ticks)
    end
    @ticks += 1
  end
  @last_tick
end

#unshift_sink(sink) ⇒ Object



63
64
65
# File 'lib/avr/clock.rb', line 63

def unshift_sink(sink)
  sinks.unshift(sink)
end