55 lines
1.4 KiB
Lua
55 lines
1.4 KiB
Lua
local Pro2Timer = {}
|
|
Pro2Timer.__index = Pro2Timer
|
|
|
|
function Pro2Timer.new(tmrID, maxTime, led, cb, timerPeriod, toggleInt)
|
|
local self = setmetatable({}, Pro2Timer)
|
|
self.timer = tmr.attach(tmrID, timerPeriod or 10000, function() self:timer_callback() end)
|
|
self.maxTime = maxTime
|
|
self.curTime = self.maxTime
|
|
self.maxLedToggleInterval = toggleInt or 3000
|
|
self.ledToggleInterval = self.maxLedToggleInterval
|
|
self.lastToggleTime = os.clock()
|
|
self.led = led
|
|
self.cb = cb or function() end
|
|
self.timer:start()
|
|
return self
|
|
end
|
|
|
|
function Pro2Timer:reload()
|
|
self.timer:stop()
|
|
self.led.off()
|
|
self.curTime = self.maxTime
|
|
self.ledToggleInterval = self.maxLedToggleInterval
|
|
self.lastToggleTime = os.clock()
|
|
self.timer:start()
|
|
end
|
|
|
|
function Pro2Timer:start()
|
|
self.timer:start()
|
|
end
|
|
|
|
function Pro2Timer:stop()
|
|
self.timer:stop()
|
|
end
|
|
|
|
function Pro2Timer:timer_callback()
|
|
self.curTime = self.curTime - 0.01
|
|
if self.curTime <= 0 then
|
|
self.curTime = 0
|
|
self.led.off()
|
|
self.timer:stop()
|
|
self.cb()
|
|
return
|
|
end
|
|
|
|
if (os.clock() - self.lastToggleTime) * 1000 >= self.ledToggleInterval then
|
|
self.led.inv()
|
|
self.lastToggleTime = os.clock()
|
|
|
|
self.ledToggleInterval = 1000 * (self.curTime / self.maxTime)
|
|
if self.ledToggleInterval < 10 then self.ledToggleInterval = 10 end
|
|
end
|
|
end
|
|
|
|
return Pro2Timer
|