Add platform file for Iaudio M5.
[Rockbox.git] / apps / plugins / grayscale.c
blob61231af39b4f748e336ad3e0718d884484226134
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"
24 #if defined(HAVE_LCD_BITMAP) && (LCD_DEPTH < 4)
25 #include "gray.h"
27 PLUGIN_HEADER
29 /* variable button definitions */
30 #if CONFIG_KEYPAD == RECORDER_PAD
31 #define GRAYSCALE_SHIFT BUTTON_ON
32 #define GRAYSCALE_UP BUTTON_UP
33 #define GRAYSCALE_DOWN BUTTON_DOWN
34 #define GRAYSCALE_LEFT BUTTON_LEFT
35 #define GRAYSCALE_RIGHT BUTTON_RIGHT
36 #define GRAYSCALE_OFF BUTTON_OFF
38 #elif CONFIG_KEYPAD == ONDIO_PAD
39 #define GRAYSCALE_SHIFT BUTTON_MENU
40 #define GRAYSCALE_UP BUTTON_UP
41 #define GRAYSCALE_DOWN BUTTON_DOWN
42 #define GRAYSCALE_LEFT BUTTON_LEFT
43 #define GRAYSCALE_RIGHT BUTTON_RIGHT
44 #define GRAYSCALE_OFF BUTTON_OFF
46 #elif CONFIG_KEYPAD == IRIVER_H100_PAD
47 #define GRAYSCALE_SHIFT BUTTON_ON
48 #define GRAYSCALE_UP BUTTON_UP
49 #define GRAYSCALE_DOWN BUTTON_DOWN
50 #define GRAYSCALE_LEFT BUTTON_LEFT
51 #define GRAYSCALE_RIGHT BUTTON_RIGHT
52 #define GRAYSCALE_OFF BUTTON_OFF
54 #define GRAYSCALE_RC_OFF BUTTON_RC_STOP
56 #elif (CONFIG_KEYPAD == IPOD_3G_PAD) || (CONFIG_KEYPAD == IPOD_4G_PAD)
57 #define GRAYSCALE_SHIFT BUTTON_SELECT
58 #define GRAYSCALE_UP BUTTON_SCROLL_BACK
59 #define GRAYSCALE_DOWN BUTTON_SCROLL_FWD
60 #define GRAYSCALE_LEFT BUTTON_LEFT
61 #define GRAYSCALE_RIGHT BUTTON_RIGHT
62 #define GRAYSCALE_OFF BUTTON_MENU
63 #endif
65 #define GFX_HEIGHT (LCD_HEIGHT-8)
66 #if LCD_WIDTH < 160
67 #define GFX_GRAYTONE_WIDTH 86
68 #define GFX_GRAYTONE_STEP 3
69 #else
70 #define GFX_GRAYTONE_WIDTH 128
71 #define GFX_GRAYTONE_STEP 2
72 #endif
73 /******************************* Globals ***********************************/
75 static struct plugin_api* rb; /* global api struct pointer */
76 static char pbuf[32]; /* global printf buffer */
77 static unsigned char *gbuf;
78 static unsigned int gbuf_size = 0;
80 /**************************** main function ********************************/
82 void cleanup(void *parameter)
84 (void)parameter;
86 gray_release(); /* switch off overlay and deinitialize */
87 /* restore normal backlight setting */
88 rb->backlight_set_timeout(rb->global_settings->backlight_timeout);
91 /* this is only a demo of what the framework can do */
92 int main(void)
94 int shades, time;
95 int x, y, i;
96 int button, scroll_amount;
97 bool black_border = false;
99 static const unsigned char rockbox[] = {
100 /* ...........................................
101 * .####...###...###..#...#.####...###..#...#.
102 * .#...#.#...#.#...#.#..#..#...#.#...#..#.#..
103 * .####..#...#.#.....###...####..#...#...#...
104 * .#..#..#...#.#...#.#..#..#...#.#...#..#.#..
105 * .#...#..###...###..#...#.####...###..#...#.
106 * ...........................................
107 * 43 x 7 pixel, 1 bpp
109 0x00, 0x3E, 0x0A, 0x0A, 0x1A, 0x24, 0x00, 0x1C, 0x22, 0x22,
110 0x22, 0x1C, 0x00, 0x1C, 0x22, 0x22, 0x22, 0x14, 0x00, 0x3E,
111 0x08, 0x08, 0x14, 0x22, 0x00, 0x3E, 0x2A, 0x2A, 0x2A, 0x14,
112 0x00, 0x1C, 0x22, 0x22, 0x22, 0x1C, 0x00, 0x22, 0x14, 0x08,
113 0x14, 0x22, 0x00
116 static const unsigned char showing[] = {
117 /* .......................................
118 * ..####.#...#..###..#...#.#.#...#..####.
119 * .#.....#...#.#...#.#...#.#.##..#.#.....
120 * ..###..#####.#...#.#.#.#.#.#.#.#.#..##.
121 * .....#.#...#.#...#.#.#.#.#.#..##.#...#.
122 * .####..#...#..###...#.#..#.#...#..####.
123 * .......................................
124 * 39 x 7 pixel, 1 bpp
126 0x00, 0x24, 0x2A, 0x2A, 0x2A, 0x12, 0x00, 0x3E, 0x08, 0x08,
127 0x08, 0x3E, 0x00, 0x1C, 0x22, 0x22, 0x22, 0x1C, 0x00, 0x1E,
128 0x20, 0x18, 0x20, 0x1E, 0x00, 0x3E, 0x00, 0x3E, 0x04, 0x08,
129 0x10, 0x3E, 0x00, 0x1C, 0x22, 0x22, 0x2A, 0x3A, 0x00
132 static const unsigned char grayscale_gray[] = {
133 /* .......................................................
134 * ..####.####...###..#...#..####..###...###..#.....#####.
135 * .#.....#...#.#...#.#...#.#.....#...#.#...#.#.....#.....
136 * .#..##.####..#####..#.#...###..#.....#####.#.....####..
137 * .#...#.#..#..#...#...#.......#.#...#.#...#.#.....#.....
138 * ..####.#...#.#...#...#...####...###..#...#.#####.#####.
139 * .......................................................
140 * 55 x 7 pixel, 8 bpp
142 110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,
143 110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,
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,
146 120,120, 20, 20, 20, 20,120,222,222,222,222,120,120,120, 24, 24,
147 24,120,120,226,120,120,120,226,120,120, 28, 28, 28, 28,120,120,
148 230,230,230,120,120,120, 32, 32, 32,120,120,234,120,120,120,120,
149 120, 36, 36, 36, 36, 36,120,
150 130, 20,130,130,130,130,130,222,130,130,130,222,130, 24,130,130,
151 130, 24,130,226,130,130,130,226,130, 28,130,130,130,130,130,230,
152 130,130,130,230,130, 32,130,130,130, 32,130,234,130,130,130,130,
153 130, 36,130,130,130,130,130,
154 140, 20,140,140, 20, 20,140,222,222,222,222,140,140, 24, 24, 24,
155 24, 24,140,140,226,140,226,140,140,140, 28, 28, 28,140,140,230,
156 140,140,140,140,140, 32, 32, 32, 32, 32,140,234,140,140,140,140,
157 140, 36, 36, 36, 36,140,140,
158 130, 20,130,130,130, 20,130,222,130,130,222,130,130, 24,130,130,
159 130, 24,130,130,130,226,130,130,130,130,130,130,130, 28,130,230,
160 130,130,130,230,130, 32,130,130,130, 32,130,234,130,130,130,130,
161 130, 36,130,130,130,130,130,
162 120,120, 20, 20, 20, 20,120,222,120,120,120,222,120, 24,120,120,
163 120, 24,120,120,120,226,120,120,120, 28, 28, 28, 28,120,120,120,
164 230,230,230,120,120, 32,120,120,120, 32,120,234,234,234,234,234,
165 120, 36, 36, 36, 36, 36,120,
166 110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,
167 110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,
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
172 if (rb->global_settings->backlight_timeout > 0)
173 rb->backlight_set_timeout(1); /* keep the light on */
175 rb->lcd_setfont(FONT_SYSFIXED); /* select default font */
177 /* get the remainder of the plugin buffer */
178 gbuf = (unsigned char *) rb->plugin_get_buffer(&gbuf_size);
180 /* initialize the greyscale buffer:
181 Archos: 112 pixels wide, 7 rows (56 pixels) high, (try to) reserve
182 32 bitplanes for 33 shades of grey.
183 H1x0: 160 pixels wide, 30 rows (120 pixels) high, (try to) reserve
184 32 bitplanes for 33 shades of grey. */
185 shades = gray_init(rb, gbuf, gbuf_size, true, LCD_WIDTH, GFX_HEIGHT,
186 32, 0, NULL) + 1;
188 /* place greyscale overlay 1 row down */
189 gray_set_position(0, 8);
191 rb->snprintf(pbuf, sizeof(pbuf), "Shades: %d", shades);
192 rb->lcd_puts(0, 0, pbuf);
193 rb->lcd_update();
195 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
196 rb->cpu_boost(true);
197 #endif
198 gray_show(true); /* switch on greyscale overlay */
200 time = *rb->current_tick; /* start time measurement */
202 gray_set_background(150);
203 gray_clear_display(); /* fill everything with grey 150 */
205 /* draw a dark grey line star background */
206 gray_set_foreground(80);
207 for (y = 0; y < GFX_HEIGHT; y += 8) /* horizontal part */
209 gray_drawline(0, y, (LCD_WIDTH-1), (GFX_HEIGHT-1) - y); /*grey lines */
211 for (x = 10; x <= LCD_WIDTH; x += 10) /* vertical part */
213 gray_drawline(x, 0, (LCD_WIDTH-1) - x, (GFX_HEIGHT-1)); /*grey lines */
216 gray_set_foreground(0);
217 gray_drawrect(0, 0, LCD_WIDTH, GFX_HEIGHT); /* black border */
219 /* draw grey tones */
220 for (i = 0; i < GFX_GRAYTONE_WIDTH; i++)
222 x = ((LCD_WIDTH-GFX_GRAYTONE_WIDTH)/2) + i;
223 gray_set_foreground(GFX_GRAYTONE_STEP * i);
224 /* vertical lines */
225 gray_vline(x, (GFX_HEIGHT/8), (GFX_HEIGHT-GFX_HEIGHT/8-1));
228 gray_set_drawmode(DRMODE_COMPLEMENT);
229 /* invert rectangle (lower half) */
230 gray_fillrect((LCD_WIDTH-GFX_GRAYTONE_WIDTH)/2, (GFX_HEIGHT/2+1),
231 GFX_GRAYTONE_WIDTH, (GFX_HEIGHT/2-GFX_HEIGHT/8-1));
232 /* invert a line */
233 gray_hline((LCD_WIDTH-GFX_GRAYTONE_WIDTH)/2,
234 (LCD_WIDTH+GFX_GRAYTONE_WIDTH)/2, (GFX_HEIGHT/2-1));
236 /* show bitmaps (1 bit and 8 bit) */
237 /* opaque */
238 gray_set_drawinfo(DRMODE_SOLID, 255, 100);
239 gray_mono_bitmap(rockbox,
240 MAX((LCD_WIDTH/2-47), ((LCD_WIDTH-GFX_GRAYTONE_WIDTH)/2)),
241 (5*GFX_HEIGHT/16-4), 43, 7);
242 /* transparent */
243 gray_set_drawinfo(DRMODE_FG, 0, 100);
244 gray_mono_bitmap(showing, (LCD_WIDTH/2+4) , (5*GFX_HEIGHT/16-4), 39, 7);
245 /* greyscale */
246 gray_gray_bitmap(grayscale_gray, ((LCD_WIDTH-55)/2), (11*GFX_HEIGHT/16-4),
247 55, 7);
249 gray_update();
251 time = *rb->current_tick - time; /* end time measurement */
253 rb->snprintf(pbuf, sizeof(pbuf), "Shades: %d, %d.%02ds", shades,
254 time / 100, time % 100);
255 rb->lcd_puts(0, 0, pbuf);
256 gray_deferred_lcd_update(); /* schedule an lcd_update() */
257 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
258 rb->cpu_boost(false);
259 #endif
261 /* drawing is now finished, play around with scrolling
262 * until you press OFF or connect USB
264 gray_set_background(255);
265 while (true)
267 scroll_amount = 1;
269 button = rb->button_get(true);
271 if (rb->default_event_handler_ex(button, cleanup, NULL)
272 == SYS_USB_CONNECTED)
273 return PLUGIN_USB_CONNECTED;
275 if (button & GRAYSCALE_SHIFT)
277 if (!black_border)
279 gray_set_background(0);
280 black_border = true;
283 else
285 if (black_border)
287 gray_set_background(255);
288 black_border = false;
292 if (button & BUTTON_REPEAT)
293 scroll_amount = 4;
295 switch (button & ~(GRAYSCALE_SHIFT | BUTTON_REPEAT))
297 case GRAYSCALE_LEFT:
299 gray_scroll_left(scroll_amount); /* scroll left */
300 gray_update();
301 break;
303 case GRAYSCALE_RIGHT:
305 gray_scroll_right(scroll_amount); /* scroll right */
306 gray_update();
307 break;
309 case GRAYSCALE_UP:
311 gray_scroll_up(scroll_amount); /* scroll up */
312 gray_update();
313 break;
315 case GRAYSCALE_DOWN:
317 gray_scroll_down(scroll_amount); /* scroll down */
318 gray_update();
319 break;
320 #ifdef GRAYSCALE_RC_OFF
321 case GRAYSCALE_RC_OFF:
322 #endif
323 case GRAYSCALE_OFF:
325 cleanup(NULL);
326 return PLUGIN_OK;
331 /*************************** Plugin entry point ****************************/
333 enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
335 rb = api; /* copy to global api pointer */
336 (void)parameter;
338 return main();
341 #endif /* #ifdef HAVE_LCD_BITMAP */