A bit of adapting.
[Rockbox.git] / apps / plugins / grayscale.c
blob56e1ce0e31acf6f7b86e0c06f0b551e27a2e88e8
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
66 #elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD
67 #define GRAYSCALE_SHIFT BUTTON_PLAY
68 #define GRAYSCALE_UP BUTTON_UP
69 #define GRAYSCALE_DOWN BUTTON_DOWN
70 #define GRAYSCALE_LEFT BUTTON_LEFT
71 #define GRAYSCALE_RIGHT BUTTON_RIGHT
72 #define GRAYSCALE_OFF BUTTON_EQ
73 #endif
75 #define GFX_HEIGHT (LCD_HEIGHT-8)
76 #if LCD_WIDTH < 160
77 #define GFX_GRAYTONE_WIDTH 86
78 #define GFX_GRAYTONE_STEP 3
79 #else
80 #define GFX_GRAYTONE_WIDTH 128
81 #define GFX_GRAYTONE_STEP 2
82 #endif
83 /******************************* Globals ***********************************/
85 static struct plugin_api* rb; /* global api struct pointer */
86 static char pbuf[32]; /* global printf buffer */
87 static unsigned char *gbuf;
88 static size_t gbuf_size = 0;
90 /**************************** main function ********************************/
92 void cleanup(void *parameter)
94 (void)parameter;
96 gray_release(); /* switch off overlay and deinitialize */
97 /* Turn on backlight timeout (revert to settings) */
98 backlight_use_settings(rb); /* backlight control in lib/helper.c */
101 /* this is only a demo of what the framework can do */
102 int main(void)
104 int shades, time;
105 int x, y, i;
106 int button, scroll_amount;
107 bool black_border = false;
109 static const unsigned char rockbox[] = {
110 /* ...........................................
111 * .####...###...###..#...#.####...###..#...#.
112 * .#...#.#...#.#...#.#..#..#...#.#...#..#.#..
113 * .####..#...#.#.....###...####..#...#...#...
114 * .#..#..#...#.#...#.#..#..#...#.#...#..#.#..
115 * .#...#..###...###..#...#.####...###..#...#.
116 * ...........................................
117 * 43 x 7 pixel, 1 bpp
119 0x00, 0x3E, 0x0A, 0x0A, 0x1A, 0x24, 0x00, 0x1C, 0x22, 0x22,
120 0x22, 0x1C, 0x00, 0x1C, 0x22, 0x22, 0x22, 0x14, 0x00, 0x3E,
121 0x08, 0x08, 0x14, 0x22, 0x00, 0x3E, 0x2A, 0x2A, 0x2A, 0x14,
122 0x00, 0x1C, 0x22, 0x22, 0x22, 0x1C, 0x00, 0x22, 0x14, 0x08,
123 0x14, 0x22, 0x00
126 static const unsigned char showing[] = {
127 /* .......................................
128 * ..####.#...#..###..#...#.#.#...#..####.
129 * .#.....#...#.#...#.#...#.#.##..#.#.....
130 * ..###..#####.#...#.#.#.#.#.#.#.#.#..##.
131 * .....#.#...#.#...#.#.#.#.#.#..##.#...#.
132 * .####..#...#..###...#.#..#.#...#..####.
133 * .......................................
134 * 39 x 7 pixel, 1 bpp
136 0x00, 0x24, 0x2A, 0x2A, 0x2A, 0x12, 0x00, 0x3E, 0x08, 0x08,
137 0x08, 0x3E, 0x00, 0x1C, 0x22, 0x22, 0x22, 0x1C, 0x00, 0x1E,
138 0x20, 0x18, 0x20, 0x1E, 0x00, 0x3E, 0x00, 0x3E, 0x04, 0x08,
139 0x10, 0x3E, 0x00, 0x1C, 0x22, 0x22, 0x2A, 0x3A, 0x00
142 static const unsigned char grayscale_gray[] = {
143 /* .......................................................
144 * ..####.####...###..#...#..####..###...###..#.....#####.
145 * .#.....#...#.#...#.#...#.#.....#...#.#...#.#.....#.....
146 * .#..##.####..#####..#.#...###..#.....#####.#.....####..
147 * .#...#.#..#..#...#...#.......#.#...#.#...#.#.....#.....
148 * ..####.#...#.#...#...#...####...###..#...#.#####.#####.
149 * .......................................................
150 * 55 x 7 pixel, 8 bpp
152 110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,
153 110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,
154 110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,
155 110,110,110,110,110,110,110,
156 120,120, 20, 20, 20, 20,120,222,222,222,222,120,120,120, 24, 24,
157 24,120,120,226,120,120,120,226,120,120, 28, 28, 28, 28,120,120,
158 230,230,230,120,120,120, 32, 32, 32,120,120,234,120,120,120,120,
159 120, 36, 36, 36, 36, 36,120,
160 130, 20,130,130,130,130,130,222,130,130,130,222,130, 24,130,130,
161 130, 24,130,226,130,130,130,226,130, 28,130,130,130,130,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 140, 20,140,140, 20, 20,140,222,222,222,222,140,140, 24, 24, 24,
165 24, 24,140,140,226,140,226,140,140,140, 28, 28, 28,140,140,230,
166 140,140,140,140,140, 32, 32, 32, 32, 32,140,234,140,140,140,140,
167 140, 36, 36, 36, 36,140,140,
168 130, 20,130,130,130, 20,130,222,130,130,222,130,130, 24,130,130,
169 130, 24,130,130,130,226,130,130,130,130,130,130,130, 28,130,230,
170 130,130,130,230,130, 32,130,130,130, 32,130,234,130,130,130,130,
171 130, 36,130,130,130,130,130,
172 120,120, 20, 20, 20, 20,120,222,120,120,120,222,120, 24,120,120,
173 120, 24,120,120,120,226,120,120,120, 28, 28, 28, 28,120,120,120,
174 230,230,230,120,120, 32,120,120,120, 32,120,234,234,234,234,234,
175 120, 36, 36, 36, 36, 36,120,
176 110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,
177 110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,
178 110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,
179 110,110,110,110,110,110,110
182 /* Turn off backlight timeout */
183 backlight_force_on(rb); /* backlight control in lib/helper.c */
185 rb->lcd_setfont(FONT_SYSFIXED); /* select default font */
187 /* get the remainder of the plugin buffer */
188 gbuf = (unsigned char *) rb->plugin_get_buffer(&gbuf_size);
190 /* initialize the greyscale buffer:
191 Archos: 112 pixels wide, 7 rows (56 pixels) high, (try to) reserve
192 32 bitplanes for 33 shades of grey.
193 H1x0: 160 pixels wide, 30 rows (120 pixels) high, (try to) reserve
194 32 bitplanes for 33 shades of grey. */
195 shades = gray_init(rb, gbuf, gbuf_size, true, LCD_WIDTH, GFX_HEIGHT,
196 32, 0, NULL) + 1;
198 /* place greyscale overlay 1 row down */
199 gray_set_position(0, 8);
201 rb->snprintf(pbuf, sizeof(pbuf), "Shades: %d", shades);
202 rb->lcd_puts(0, 0, pbuf);
203 rb->lcd_update();
205 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
206 rb->cpu_boost(true);
207 #endif
208 gray_show(true); /* switch on greyscale overlay */
210 time = *rb->current_tick; /* start time measurement */
212 gray_set_background(150);
213 gray_clear_display(); /* fill everything with grey 150 */
215 /* draw a dark grey line star background */
216 gray_set_foreground(80);
217 for (y = 0; y < GFX_HEIGHT; y += 8) /* horizontal part */
219 gray_drawline(0, y, (LCD_WIDTH-1), (GFX_HEIGHT-1) - y); /*grey lines */
221 for (x = 10; x <= LCD_WIDTH; x += 10) /* vertical part */
223 gray_drawline(x, 0, (LCD_WIDTH-1) - x, (GFX_HEIGHT-1)); /*grey lines */
226 gray_set_foreground(0);
227 gray_drawrect(0, 0, LCD_WIDTH, GFX_HEIGHT); /* black border */
229 /* draw grey tones */
230 for (i = 0; i < GFX_GRAYTONE_WIDTH; i++)
232 x = ((LCD_WIDTH-GFX_GRAYTONE_WIDTH)/2) + i;
233 gray_set_foreground(GFX_GRAYTONE_STEP * i);
234 /* vertical lines */
235 gray_vline(x, (GFX_HEIGHT/8), (GFX_HEIGHT-GFX_HEIGHT/8-1));
238 gray_set_drawmode(DRMODE_COMPLEMENT);
239 /* invert rectangle (lower half) */
240 gray_fillrect((LCD_WIDTH-GFX_GRAYTONE_WIDTH)/2, (GFX_HEIGHT/2+1),
241 GFX_GRAYTONE_WIDTH, (GFX_HEIGHT/2-GFX_HEIGHT/8-1));
242 /* invert a line */
243 gray_hline((LCD_WIDTH-GFX_GRAYTONE_WIDTH)/2,
244 (LCD_WIDTH+GFX_GRAYTONE_WIDTH)/2, (GFX_HEIGHT/2-1));
246 /* show bitmaps (1 bit and 8 bit) */
247 /* opaque */
248 gray_set_drawinfo(DRMODE_SOLID, 255, 100);
249 gray_mono_bitmap(rockbox,
250 MAX((LCD_WIDTH/2-47), ((LCD_WIDTH-GFX_GRAYTONE_WIDTH)/2)),
251 (5*GFX_HEIGHT/16-4), 43, 7);
252 /* transparent */
253 gray_set_drawinfo(DRMODE_FG, 0, 100);
254 gray_mono_bitmap(showing, (LCD_WIDTH/2+4) , (5*GFX_HEIGHT/16-4), 39, 7);
255 /* greyscale */
256 gray_gray_bitmap(grayscale_gray, ((LCD_WIDTH-55)/2), (11*GFX_HEIGHT/16-4),
257 55, 7);
259 gray_update();
261 time = *rb->current_tick - time; /* end time measurement */
263 rb->snprintf(pbuf, sizeof(pbuf), "Shades: %d, %d.%02ds", shades,
264 time / 100, time % 100);
265 rb->lcd_puts(0, 0, pbuf);
266 gray_deferred_lcd_update(); /* schedule an lcd_update() */
267 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
268 rb->cpu_boost(false);
269 #endif
271 /* drawing is now finished, play around with scrolling
272 * until you press OFF or connect USB
274 gray_set_background(255);
275 while (true)
277 scroll_amount = 1;
279 button = rb->button_get(true);
281 if (rb->default_event_handler_ex(button, cleanup, NULL)
282 == SYS_USB_CONNECTED)
283 return PLUGIN_USB_CONNECTED;
285 if (button & GRAYSCALE_SHIFT)
287 if (!black_border)
289 gray_set_background(0);
290 black_border = true;
293 else
295 if (black_border)
297 gray_set_background(255);
298 black_border = false;
302 if (button & BUTTON_REPEAT)
303 scroll_amount = 4;
305 switch (button & ~(GRAYSCALE_SHIFT | BUTTON_REPEAT))
307 case GRAYSCALE_LEFT:
309 gray_scroll_left(scroll_amount); /* scroll left */
310 gray_update();
311 break;
313 case GRAYSCALE_RIGHT:
315 gray_scroll_right(scroll_amount); /* scroll right */
316 gray_update();
317 break;
319 case GRAYSCALE_UP:
321 gray_scroll_up(scroll_amount); /* scroll up */
322 gray_update();
323 break;
325 case GRAYSCALE_DOWN:
327 gray_scroll_down(scroll_amount); /* scroll down */
328 gray_update();
329 break;
330 #ifdef GRAYSCALE_RC_OFF
331 case GRAYSCALE_RC_OFF:
332 #endif
333 case GRAYSCALE_OFF:
335 cleanup(NULL);
336 return PLUGIN_OK;
341 /*************************** Plugin entry point ****************************/
343 enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
345 rb = api; /* copy to global api pointer */
346 (void)parameter;
348 return main();
351 #endif /* #ifdef HAVE_LCD_BITMAP */