summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/include_lua/timer.lua
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/lua/include_lua/timer.lua')
-rw-r--r--apps/plugins/lua/include_lua/timer.lua114
1 files changed, 114 insertions, 0 deletions
diff --git a/apps/plugins/lua/include_lua/timer.lua b/apps/plugins/lua/include_lua/timer.lua
new file mode 100644
index 0000000000..6797874329
--- /dev/null
+++ b/apps/plugins/lua/include_lua/timer.lua
@@ -0,0 +1,114 @@
1--[[ Lua Timer functions
2/***************************************************************************
3 * __________ __ ___.
4 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
5 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
6 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
7 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
8 * \/ \/ \/ \/ \/
9 * $Id$
10 *
11 * Copyright (C) 2017 William Wilgus
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ****************************************************************************/
22]]
23
24--[[ Exposed Functions
25
26 _timer.active
27 _timer.check
28 _timer.split
29 _timer.start
30 _timer.stop
31
32]]
33
34local _timer = {} do
35
36 --internal constants
37 local _NIL = nil -- _NIL placeholder
38
39 -- newer versions of lua use table.unpack
40 local unpack = unpack or table.unpack
41
42 --stores time elapsed at call to split; only vaid for unique timers
43 local function split(index)
44 if type(index) ~= "table" then return end
45 index[#index + 1] = rb.current_tick() - _timer[index]
46 end
47
48 -- starts a new timer, if index is not specified a unique index is returned
49 -- numeric or string indices are valid to use directly for permanent timers
50 -- in this case its up to you to make sure you keep the index unique
51 local function start(index)
52 if index == _NIL then
53 ---if you have _timer.start create timer it returns a unique Id which
54 -- then has the same methods of _timer :start :stop :check :split
55 index = setmetatable({}, {__index = _timer})
56 end
57 if _timer[index] == _NIL then
58 _timer[index] = rb.current_tick()
59 end
60 return index
61 end
62
63 -- returns time elapsed in centiseconds, assigning bCheckonly keeps timer active
64 local function stop(index, bCheckonly)
65
66 local time_end = rb.current_tick()
67 index = index or 0
68 if not _timer[index] then
69 return 0
70 else
71 local time_start = _timer[index]
72 if not bCheckonly then _timer[index] = _NIL end -- destroy timer
73 if type(index) ~= "table" then
74 return time_end - time_start
75 else
76 return time_end - time_start, unpack(index)
77 end
78 end
79 end
80
81 -- returns time elapsed in centiseconds, assigning to bUpdate.. updates timer
82 local function check(index, bUpdate)
83 local elapsed = stop(index, true)
84 if bUpdate ~= _NIL and index then
85 _timer[index] = rb.current_tick()
86 end
87 return elapsed
88 end
89
90 -- returns table of active timers
91 local function active()
92 local t_active = {}
93 local n = 0
94 for k,v in pairs(_timer) do
95 if type(_timer[k]) ~= "function" then
96 n = n + 1
97 t_active[n]=(k)
98 end
99 end
100 return n, t_active
101 end
102
103 -- expose functions to the outside through _timer table
104 _timer.active = active
105 _timer.check = check
106 _timer.split = split
107 _timer.start = start
108 _timer.stop = stop
109
110 -- allows a call to _timer.start() by just calling _timer()
111 _timer = setmetatable(_timer,{__call = function(t, i) return start(i) end})
112end -- timer functions
113
114return _timer