Class: DeviceControl::MovingAverage

Inherits:
Object
  • Object
show all
Includes:
Updateable
Defined in:
lib/device_control.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Updateable

#update

Constructor Details

#initialize(size = 2) ⇒ MovingAverage

Returns a new instance of MovingAverage.



282
283
284
285
286
# File 'lib/device_control.rb', line 282

def initialize(size = 2)
  @size = size
  @idx = 0
  @storage = Array.new(@size, 0)
end

Instance Attribute Details

#idxObject (readonly)

Returns the value of attribute idx.



280
281
282
# File 'lib/device_control.rb', line 280

def idx
  @idx
end

#sizeObject (readonly)

Returns the value of attribute size.



280
281
282
# File 'lib/device_control.rb', line 280

def size
  @size
end

#storageObject (readonly)

Returns the value of attribute storage.



280
281
282
# File 'lib/device_control.rb', line 280

def storage
  @storage
end

Instance Method Details

#input=(val) ⇒ Object

never grow @storage; just use modmath to track what to replace



289
290
291
292
# File 'lib/device_control.rb', line 289

def input=(val)
  @storage[@idx % @size] = val
  @idx += 1
end

#outputObject



294
295
296
297
# File 'lib/device_control.rb', line 294

def output
  return 0 if @idx == 0
  @storage.sum / (@idx > @size ? @size : @idx).to_f
end