rockbox/apps/plugins/lua/include_lua/timer.lua
William Wilgus af35d19916 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

Fixed handling for 24-bit devices (32?)

-------------------------------------------------------------------------
Example lua script to follow on forums
-------------------------------------------------------------------------

Change-Id: I8a9ff0ff72aacf4b1662767ccb2b312fc355239c
2018-07-23 05:13:32 +02:00

115 lines
3.7 KiB
Lua

--[[ Lua Timer functions
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2017 William Wilgus
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
]]
--[[ Exposed Functions
_timer.active
_timer.check
_timer.split
_timer.start
_timer.stop
]]
local _timer = {} do
--internal constants
local _NIL = nil -- _NIL placeholder
-- newer versions of lua use table.unpack
local unpack = unpack or table.unpack
--stores time elapsed at call to split; only vaid for unique timers
local function split(index)
if type(index) ~= "table" then return end
index[#index + 1] = rb.current_tick() - _timer[index]
end
-- starts a new timer, if index is not specified a unique index is returned
-- numeric or string indices are valid to use directly for permanent timers
-- in this case its up to you to make sure you keep the index unique
local function start(index)
if index == _NIL then
---if you have _timer.start create timer it returns a unique Id which
-- then has the same methods of _timer :start :stop :check :split
index = setmetatable({}, {__index = _timer})
end
if _timer[index] == _NIL then
_timer[index] = rb.current_tick()
end
return index
end
-- returns time elapsed in centiseconds, assigning bCheckonly keeps timer active
local function stop(index, bCheckonly)
local time_end = rb.current_tick()
index = index or 0
if not _timer[index] then
return 0
else
local time_start = _timer[index]
if not bCheckonly then _timer[index] = _NIL end -- destroy timer
if type(index) ~= "table" then
return time_end - time_start
else
return time_end - time_start, unpack(index)
end
end
end
-- returns time elapsed in centiseconds, assigning to bUpdate.. updates timer
local function check(index, bUpdate)
local elapsed = stop(index, true)
if bUpdate ~= _NIL and index then
_timer[index] = rb.current_tick()
end
return elapsed
end
-- returns table of active timers
local function active()
local t_active = {}
local n = 0
for k,v in pairs(_timer) do
if type(_timer[k]) ~= "function" then
n = n + 1
t_active[n]=(k)
end
end
return n, t_active
end
-- expose functions to the outside through _timer table
_timer.active = active
_timer.check = check
_timer.split = split
_timer.start = start
_timer.stop = stop
-- allows a call to _timer.start() by just calling _timer()
_timer = setmetatable(_timer,{__call = function(t, i) return start(i) end})
end -- timer functions
return _timer