RosettaCodeData/Task/Active-object/Lingo/active-object-1.lingo

42 lines
882 B
Plaintext

property _sum
property _func
property _timeLast
property _valueLast
property _ms0
property _updateTimer
on new (me, func)
if voidP(func) then func = "0.0"
me._sum = 0.0
-- update frequency: 100/sec (arbitrary)
me._updateTimer = timeout().new("update", 10, #_update, me)
me.input(func)
return me
end
on stop (me)
me._updateTimer.period = 0 -- deactivates timer
end
-- func is a term (as string) that might contain "t" and is evaluated at runtime
on input (me, func)
me._func = func
me._ms0 = _system.milliseconds
me._timeLast = 0.0
t = 0.0
me._valueLast = value(me._func)
end
on output (me)
return me._sum
end
on _update (me)
now = _system.milliseconds - me._ms0
t = now/1000.0
val = value(me._func)
me._sum = me._sum + (me._valueLast+val)*(t - me._timeLast)/2
me._timeLast = t
me._valueLast = val
end