Bring mpegplayer backlight fix to the other plugins, this also fixes some wrongly...
[Rockbox.git] / apps / plugins / grayscale.c
blobd28237da31fc9b128904974ce9c83e50782a808c
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Grayscale demo plugin
12 * Copyright (C) 2004 Jens Arnold
14 * All files in this archive are subject to the GNU General Public License.
15 * See the file COPYING in the source tree root for full license agreement.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "plugin.h"
23 #include "helper.h"
25 #if defined(HAVE_LCD_BITMAP) && (LCD_DEPTH < 4)
26 #include "gray.h"
28 PLUGIN_HEADER
30 /* variable button definitions */
31 #if CONFIG_KEYPAD == RECORDER_PAD
32 #define GRAYSCALE_SHIFT BUTTON_ON
33 #define GRAYSCALE_UP BUTTON_UP
34 #define GRAYSCALE_DOWN BUTTON_DOWN
35 #define GRAYSCALE_LEFT BUTTON_LEFT
36 #define GRAYSCALE_RIGHT BUTTON_RIGHT
37 #define GRAYSCALE_OFF BUTTON_OFF
39 #elif CONFIG_KEYPAD == ONDIO_PAD
40 #define GRAYSCALE_SHIFT BUTTON_MENU
41 #define GRAYSCALE_UP BUTTON_UP
42 #define GRAYSCALE_DOWN BUTTON_DOWN
43 #define GRAYSCALE_LEFT BUTTON_LEFT
44 #define GRAYSCALE_RIGHT BUTTON_RIGHT
45 #define GRAYSCALE_OFF BUTTON_OFF
47 #elif CONFIG_KEYPAD == IRIVER_H100_PAD
48 #define GRAYSCALE_SHIFT BUTTON_ON
49 #define GRAYSCALE_UP BUTTON_UP
50 #define GRAYSCALE_DOWN BUTTON_DOWN
51 #define GRAYSCALE_LEFT BUTTON_LEFT
52 #define GRAYSCALE_RIGHT BUTTON_RIGHT
53 #define GRAYSCALE_OFF BUTTON_OFF
55 #define GRAYSCALE_RC_OFF BUTTON_RC_STOP
57 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
58 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
59 #define GRAYSCALE_SHIFT BUTTON_SELECT
60 #define GRAYSCALE_UP BUTTON_SCROLL_BACK
61 #define GRAYSCALE_DOWN BUTTON_SCROLL_FWD
62 #define GRAYSCALE_LEFT BUTTON_LEFT
63 #define GRAYSCALE_RIGHT BUTTON_RIGHT
64 #define GRAYSCALE_OFF BUTTON_MENU
65 #endif
67 #define GFX_HEIGHT (LCD_HEIGHT-8)
68 #if LCD_WIDTH < 160
69 #define GFX_GRAYTONE_WIDTH 86
70 #define GFX_GRAYTONE_STEP 3
71 #else
72 #define GFX_GRAYTONE_WIDTH 128
73 #define GFX_GRAYTONE_STEP 2
74 #endif
75 /******************************* Globals ***********************************/
77 static struct plugin_api* rb; /* global api struct pointer */
78 static char pbuf[32]; /* global printf buffer */
79 static unsigned char *gbuf;
80 static size_t gbuf_size = 0;
82 /**************************** main function ********************************/
84 void cleanup(void *parameter)
86 (void)parameter;
88 gray_release(); /* switch off overlay and deinitialize */
89 /* Turn on backlight timeout (revert to settings) */
90 backlight_use_settings(); /* backlight control in lib/helper.c */
93 /* this is only a demo of what the framework can do */
94 int main(void)
96 int shades, time;
97 int x, y, i;
98 int button, scroll_amount;
99 bool black_border = false;
101 static const unsigned char rockbox[] = {
102 /* ...........................................
103 * .####...###...###..#...#.####...###..#...#.
104 * .#...#.#...#.#...#.#..#..#...#.#...#..#.#..
105 * .####..#...#.#.....###...####..#...#...#...
106 * .#..#..#...#.#...#.#..#..#...#.#...#..#.#..
107 * .#...#..###...###..#...#.####...###..#...#.
108 * ...........................................
109 * 43 x 7 pixel, 1 bpp
111 0x00, 0x3E, 0x0A, 0x0A, 0x1A, 0x24, 0x00, 0x1C, 0x22, 0x22,
112 0x22, 0x1C, 0x00, 0x1C, 0x22, 0x22, 0x22, 0x14, 0x00, 0x3E,
113 0x08, 0x08, 0x14, 0x22, 0x00, 0x3E, 0x2A, 0x2A, 0x2A, 0x14,
114 0x00, 0x1C, 0x22, 0x22, 0x22, 0x1C, 0x00, 0x22, 0x14, 0x08,
115 0x14, 0x22, 0x00
118 static const unsigned char showing[] = {
119 /* .......................................
120 * ..####.#...#..###..#...#.#.#...#..####.
121 * .#.....#...#.#...#.#...#.#.##..#.#.....
122 * ..###..#####.#...#.#.#.#.#.#.#.#.#..##.
123 * .....#.#...#.#...#.#.#.#.#.#..##.#...#.
124 * .####..#...#..###...#.#..#.#...#..####.
125 * .......................................
126 * 39 x 7 pixel, 1 bpp
128 0x00, 0x24, 0x2A, 0x2A, 0x2A, 0x12, 0x00, 0x3E, 0x08, 0x08,
129 0x08, 0x3E, 0x00, 0x1C, 0x22, 0x22, 0x22, 0x1C, 0x00, 0x1E,
130 0x20, 0x18, 0x20, 0x1E, 0x00, 0x3E, 0x00, 0x3E, 0x04, 0x08,
131 0x10, 0x3E, 0x00, 0x1C, 0x22, 0x22, 0x2A, 0x3A, 0x00
134 static const unsigned char grayscale_gray[] = {
135 /* .......................................................
136 * ..####.####...###..#...#..####..###...###..#.....#####.
137 * .#.....#...#.#...#.#...#.#.....#...#.#...#.#.....#.....
138 * .#..##.####..#####..#.#...###..#.....#####.#.....####..
139 * .#...#.#..#..#...#...#.......#.#...#.#...#.#.....#.....
140 * ..####.#...#.#...#...#...####...###..#...#.#####.#####.
141 * .......................................................
142 * 55 x 7 pixel, 8 bpp
144 110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,
145 110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,
146 110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,
147 110,110,110,110,110,110,110,
148 120,120, 20, 20, 20, 20,120,222,222,222,222,120,120,120, 24, 24,
149 24,120,120,226,120,120,120,226,120,120, 28, 28, 28, 28,120,120,
150 230,230,230,120,120,120, 32, 32, 32,120,120,234,120,120,120,120,
151 120, 36, 36, 36, 36, 36,120,
152 130, 20,130,130,130,130,130,222,130,130,130,222,130, 24,130,130,
153 130, 24,130,226,130,130,130,226,130, 28,130,130,130,130,130,230,
154 130,130,130,230,130, 32,130,130,130, 32,130,234,130,130,130,130,
155 130, 36,130,130,130,130,130,
156 140, 20,140,140, 20, 20,140,222,222,222,222,140,140, 24, 24, 24,
157 24, 24,140,140,226,140,226,140,140,140, 28, 28, 28,140,140,230,
158 140,140,140,140,140, 32, 32, 32, 32, 32,140,234,140,140,140,140,
159 140, 36, 36, 36, 36,140,140,
160 130, 20,130,130,130, 20,130,222,130,130,222,130,130, 24,130,130,
161 130, 24,130,130,130,226,130,130,130,130,130,130,130, 28,130,230,
162 130,130,130,230,130, 32,130,130,130, 32,130,234,130,130,130,130,
163 130, 36,130,130,130,130,130,
164 120,120, 20, 20, 20, 20,120,222,120,120,120,222,120, 24,120,120,
165 120, 24,120,120,120,226,120,120,120, 28, 28, 28, 28,120,120,120,
166 230,230,230,120,120, 32,120,120,120, 32,120,234,234,234,234,234,
167 120, 36, 36, 36, 36, 36,120,
168 110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,
169 110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,
170 110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,
171 110,110,110,110,110,110,110
174 /* Turn off backlight timeout */
175 backlight_force_on(); /* backlight control in lib/helper.c */
177 rb->lcd_setfont(FONT_SYSFIXED); /* select default font */
179 /* get the remainder of the plugin buffer */
180 gbuf = (unsigned char *) rb->plugin_get_buffer(&gbuf_size);
182 /* initialize the greyscale buffer:
183 Archos: 112 pixels wide, 7 rows (56 pixels) high, (try to) reserve
184 32 bitplanes for 33 shades of grey.
185 H1x0: 160 pixels wide, 30 rows (120 pixels) high, (try to) reserve
186 32 bitplanes for 33 shades of grey. */
187 shades = gray_init(rb, gbuf, gbuf_size, true, LCD_WIDTH, GFX_HEIGHT,
188 32, 0, NULL) + 1;
190 /* place greyscale overlay 1 row down */
191 gray_set_position(0, 8);
193 rb->snprintf(pbuf, sizeof(pbuf), "Shades: %d", shades);
194 rb->lcd_puts(0, 0, pbuf);
195 rb->lcd_update();
197 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
198 rb->cpu_boost(true);
199 #endif
200 gray_show(true); /* switch on greyscale overlay */
202 time = *rb->current_tick; /* start time measurement */
204 gray_set_background(150);
205 gray_clear_display(); /* fill everything with grey 150 */
207 /* draw a dark grey line star background */
208 gray_set_foreground(80);
209 for (y = 0; y < GFX_HEIGHT; y += 8) /* horizontal part */
211 gray_drawline(0, y, (LCD_WIDTH-1), (GFX_HEIGHT-1) - y); /*grey lines */
213 for (x = 10; x <= LCD_WIDTH; x += 10) /* vertical part */
215 gray_drawline(x, 0, (LCD_WIDTH-1) - x, (GFX_HEIGHT-1)); /*grey lines */
218 gray_set_foreground(0);
219 gray_drawrect(0, 0, LCD_WIDTH, GFX_HEIGHT); /* black border */
221 /* draw grey tones */
222 for (i = 0; i < GFX_GRAYTONE_WIDTH; i++)
224 x = ((LCD_WIDTH-GFX_GRAYTONE_WIDTH)/2) + i;
225 gray_set_foreground(GFX_GRAYTONE_STEP * i);
226 /* vertical lines */
227 gray_vline(x, (GFX_HEIGHT/8), (GFX_HEIGHT-GFX_HEIGHT/8-1));
230 gray_set_drawmode(DRMODE_COMPLEMENT);
231 /* invert rectangle (lower half) */
232 gray_fillrect((LCD_WIDTH-GFX_GRAYTONE_WIDTH)/2, (GFX_HEIGHT/2+1),
233 GFX_GRAYTONE_WIDTH, (GFX_HEIGHT/2-GFX_HEIGHT/8-1));
234 /* invert a line */
235 gray_hline((LCD_WIDTH-GFX_GRAYTONE_WIDTH)/2,
236 (LCD_WIDTH+GFX_GRAYTONE_WIDTH)/2, (GFX_HEIGHT/2-1));
238 /* show bitmaps (1 bit and 8 bit) */
239 /* opaque */
240 gray_set_drawinfo(DRMODE_SOLID, 255, 100);
241 gray_mono_bitmap(rockbox,
242 MAX((LCD_WIDTH/2-47), ((LCD_WIDTH-GFX_GRAYTONE_WIDTH)/2)),
243 (5*GFX_HEIGHT/16-4), 43, 7);
244 /* transparent */
245 gray_set_drawinfo(DRMODE_FG, 0, 100);
246 gray_mono_bitmap(showing, (LCD_WIDTH/2+4) , (5*GFX_HEIGHT/16-4), 39, 7);
247 /* greyscale */
248 gray_gray_bitmap(grayscale_gray, ((LCD_WIDTH-55)/2), (11*GFX_HEIGHT/16-4),
249 55, 7);
251 gray_update();
253 time = *rb->current_tick - time; /* end time measurement */
255 rb->snprintf(pbuf, sizeof(pbuf), "Shades: %d, %d.%02ds", shades,
256 time / 100, time % 100);
257 rb->lcd_puts(0, 0, pbuf);
258 gray_deferred_lcd_update(); /* schedule an lcd_update() */
259 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
260 rb->cpu_boost(false);
261 #endif
263 /* drawing is now finished, play around with scrolling
264 * until you press OFF or connect USB
266 gray_set_background(255);
267 while (true)
269 scroll_amount = 1;
271 button = rb->button_get(true);
273 if (rb->default_event_handler_ex(button, cleanup, NULL)
274 == SYS_USB_CONNECTED)
275 return PLUGIN_USB_CONNECTED;
277 if (button & GRAYSCALE_SHIFT)
279 if (!black_border)
281 gray_set_background(0);
282 black_border = true;
285 else
287 if (black_border)
289 gray_set_background(255);
290 black_border = false;
294 if (button & BUTTON_REPEAT)
295 scroll_amount = 4;
297 switch (button & ~(GRAYSCALE_SHIFT | BUTTON_REPEAT))
299 case GRAYSCALE_LEFT:
301 gray_scroll_left(scroll_amount); /* scroll left */
302 gray_update();
303 break;
305 case GRAYSCALE_RIGHT:
307 gray_scroll_right(scroll_amount); /* scroll right */
308 gray_update();
309 break;
311 case GRAYSCALE_UP:
313 gray_scroll_up(scroll_amount); /* scroll up */
314 gray_update();
315 break;
317 case GRAYSCALE_DOWN:
319 gray_scroll_down(scroll_amount); /* scroll down */
320 gray_update();
321 break;
322 #ifdef GRAYSCALE_RC_OFF
323 case GRAYSCALE_RC_OFF:
324 #endif
325 case GRAYSCALE_OFF:
327 cleanup(NULL);
328 return PLUGIN_OK;
333 /*************************** Plugin entry point ****************************/
335 enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
337 rb = api; /* copy to global api pointer */
338 (void)parameter;
340 return main();
343 #endif /* #ifdef HAVE_LCD_BITMAP */