summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/include_lua/lcd.lua
diff options
context:
space:
mode:
authorWilliam Wilgus <me.theuser@yahoo.com>2018-05-28 17:56:06 +0200
committerWilliam Wilgus <me.theuser@yahoo.com>2018-07-22 18:05:02 +0200
commit2daec3d3c3d84e7176a22bc073ca5530e8e44c6d (patch)
tree0f7e64ed5abd305fe80a7fbfabf79d2a9374ed4f /apps/plugins/lua/include_lua/lcd.lua
parent19b2964d78b2ad6624a0e7cddd0ac6a49082cca4 (diff)
downloadrockbox-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/lcd.lua')
-rw-r--r--apps/plugins/lua/include_lua/lcd.lua153
1 files changed, 153 insertions, 0 deletions
diff --git a/apps/plugins/lua/include_lua/lcd.lua b/apps/plugins/lua/include_lua/lcd.lua
new file mode 100644
index 0000000000..62fa988ec1
--- /dev/null
+++ b/apps/plugins/lua/include_lua/lcd.lua
@@ -0,0 +1,153 @@
1--[[ Lua LCD Wrapper 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 _lcd.clear
27 _lcd.duplicate
28 _lcd.image
29 _lcd.set_viewport
30 _lcd.splashf
31 _lcd.text_extent
32 _lcd.update
33 _lcd.update_rect
34
35-- Exposed Constants
36 _lcd.CX
37 _lcd.CY
38 _lcd.DEPTH
39 _lcd.W
40 _lcd.H
41
42 _lcd
43 _LCD
44
45]]
46if not rb.lcd_framebuffer then rb.splash(rb.HZ, "No Support!") return nil end
47
48_LCD = rb.lcd_framebuffer()
49
50local _lcd = {} do
51
52 --internal constants
53 local _NIL = nil -- _NIL placeholder
54 local LCD_W, LCD_H = rb.LCD_WIDTH, rb.LCD_HEIGHT
55
56 -- clamps value to >= min and <= max
57 local function clamp(val, min, max)
58 -- Warning doesn't check if min < max
59 if val < min then
60 return min
61 elseif val < max then
62 return val
63 end
64 return max
65 end
66
67 -- return a copy of lcd screen
68 local function duplicate(t, screen_img)
69 screen_img = screen_img or rb.new_image()
70 screen_img:copy(rb.lcd_framebuffer())
71 return screen_img
72 end
73
74 -- updates screen in specified rectangle
75 local function update_rect(t, x, y, w, h)
76 rb.lcd_update_rect(x - 1, y - 1,
77 clamp(x + w, 1, LCD_W) - 1,
78 clamp(y + h, 1, LCD_H) - 1)
79 end
80
81 -- clears lcd, optional.. ([color, x1, y1, x2, y2, clip])
82 local function clear(t, clr, ...)
83 if clr == _NIL and ... == _NIL then
84 rb.lcd_clear_display()
85 else
86 rb.lcd_scroll_stop() --rb really doesn't like bg change while scroll
87 _LCD:clear(clr, ...)
88 end
89 end
90
91 -- loads an image to the screen
92 local function image(t, src, x, y)
93 if not src then --make sure an image was passed, otherwise bail
94 rb.splash(rb.HZ, "No Image!")
95 return _NIL
96 end
97 _LCD:copy(src,x,y,1,1)
98 end
99
100 -- Formattable version of splash
101 local function splashf(t, timeout, ...)
102 rb.splash(timeout, string.format(...))
103 end
104
105 -- Gets size of text
106 local function text_extent(t, msg, font)
107 font = font or rb.FONT_UI
108
109 return rb.font_getstringsize(msg, font)
110 end
111
112 -- Sets viewport size
113 local function set_viewport(t, vp)
114 if not vp then rb.set_viewport() return end
115 if rb.LCD_DEPTH == 2 then -- invert 2-bit screens
116 --vp.drawmode = bit.bxor(vp.drawmode, 4)
117 vp.fg_pattern = 3 - vp.fg_pattern
118 vp.bg_pattern = 3 - vp.bg_pattern
119 end
120 rb.set_viewport(vp)
121 end
122
123 -- allows the use of _lcd() as a identifier for the screen
124 local function index(k, v)
125 return function(x, ...)
126 _LCD[v](_LCD, ...)
127 end
128 end
129
130 -- allows the use of _lcd() as a identifier for the screen
131 local function call()
132 return rb.lcd_framebuffer()
133 end
134
135 --expose functions to the outside through _lcd table
136 _lcd.text_extent = text_extent
137 _lcd.set_viewport = set_viewport
138 _lcd.duplicate = duplicate
139 _lcd.update = rb.lcd_update
140 _lcd.update_rect = update_rect
141 _lcd.clear = clear
142 _lcd.splashf = splashf
143 _lcd.image = image
144 _lcd.DEPTH = rb.LCD_DEPTH
145 _lcd.W = rb.LCD_WIDTH
146 _lcd.H = rb.LCD_HEIGHT
147 _lcd.CX = (rb.LCD_WIDTH / 2)
148 _lcd.CY = (rb.LCD_HEIGHT / 2)
149 _lcd = setmetatable(_lcd,{__index = index, __call = call})
150
151end -- _lcd functions
152
153return _lcd