diff options
author | William Wilgus <me.theuser@yahoo.com> | 2018-05-28 17:56:06 +0200 |
---|---|---|
committer | William Wilgus <me.theuser@yahoo.com> | 2018-07-22 18:05:02 +0200 |
commit | 2daec3d3c3d84e7176a22bc073ca5530e8e44c6d (patch) | |
tree | 0f7e64ed5abd305fe80a7fbfabf79d2a9374ed4f /apps/plugins/lua/include_lua/timer.lua | |
parent | 19b2964d78b2ad6624a0e7cddd0ac6a49082cca4 (diff) | |
download | rockbox-2daec3d3c3d84e7176a22bc073ca5530e8e44c6d.tar.gz rockbox-2daec3d3c3d84e7176a22bc073ca5530e8e44c6d.zip |
Rocklua -- Extend / Fix rliImage
Some devices(1-bit / 2-bit displays) have packed bit formats that
need to be unpacked in order to work on them at a pixel level.
This caused a few issues on 1 & 2-bit devices:
Greatly Oversized data arrays for bitmaps
Improper handling of native image data
Framebuffer data was near unusable without jumping through hoops
Conversion between native addressing and per pixel addressing
incurs extra overhead but it is much faster to do it
on the 'C' side rather than in lua.
Not to mention the advantage of a unified interface for the end programer
-------------------------------------------------------------------
Adds a sane way to access each pixel of image data
Adds:
--------------------------------------------------------------------
img:clear([color],[x1],[y1],[x2],[y2])
(set whole image or a portion to a particular value)
--------------------------------------------------------------------
img:invert([x1],[y1],[x2],[y2])
(inverts whole image or a portion)
--------------------------------------------------------------------
img:marshal([x1],[y1],[x2],[y2],[funct])
(calls funct for each point defined by rect of x1,y1 x2,y2
returns value and allows setting value of each point return
nil to terminate early)
--------------------------------------------------------------------
img:points([x1],[y1],[x2],[y2],[dx],[dy])
(returns iterator function that steps delta-x and delta-y pixels each call
returns value of pixel each call but doesn't allow setting to a new value
compare to lua pairs method)
--------------------------------------------------------------------
img:copy(src,[x1],[y1],[x2],[y2],[w],[h],[clip][operation][clr/funct])
(copies all or part of an image -- straight copy or special ops
optionally calls funct for each point defined by rect of
x1, y1, w, h and x2, y2, w, h for dest and src images
returns value of dst and src and allows setting value of
each point return nil to terminate early)
--------------------------------------------------------------------
img:line(x1, y1, x2, y2, color)
--------------------------------------------------------------------
img:ellipse(x1, y1, x2, y2, color, [fillcolor]
--------------------------------------------------------------------
Fixed handling of 2-bit vertical integrated screens
Added direct element access for saving / restoring native image etc.
Added more data to tostring() handler and a way to access individual items
Added equals method to see if two variables reference the same image address
(doesn't check if two separate images contain the same 'picture')
Optimized get and set routines
Fixed out of bound x coord access shifting to next line
Added lua include files to expose new functionality
Finished image saving routine
Static allocation of set_viewport struct faster + saves ram over dynamic
Cleaned up code
Fixed pixel get/set for 1/2 bit devices
-------------------------------------------------------------------------
Example lua script to follow on forums
-------------------------------------------------------------------------
Change-Id: I7b9c1fd699442fb683760f781021091786c18509
Diffstat (limited to 'apps/plugins/lua/include_lua/timer.lua')
-rw-r--r-- | apps/plugins/lua/include_lua/timer.lua | 114 |
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 | |||
34 | local _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}) | ||
112 | end -- timer functions | ||
113 | |||
114 | return _timer | ||