Class: DeviceControl::MovingAverage
- Inherits:
-
Object
- Object
- DeviceControl::MovingAverage
- Includes:
- Updateable
- Defined in:
- lib/device_control.rb
Instance Attribute Summary collapse
-
#idx ⇒ Object
readonly
Returns the value of attribute idx.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#storage ⇒ Object
readonly
Returns the value of attribute storage.
Instance Method Summary collapse
-
#initialize(size = 2) ⇒ MovingAverage
constructor
A new instance of MovingAverage.
-
#input=(val) ⇒ Object
never grow @storage; just use modmath to track what to replace.
- #output ⇒ Object
Methods included from Updateable
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
#idx ⇒ Object (readonly)
Returns the value of attribute idx.
280 281 282 |
# File 'lib/device_control.rb', line 280 def idx @idx end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
280 281 282 |
# File 'lib/device_control.rb', line 280 def size @size end |
#storage ⇒ Object (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 |
#output ⇒ Object
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 |