Remove leftover backslash from macro conversion in FRACTMUL_SHL
[maemo-rb.git] / apps / plugins / test_grey.c
blobefceeff2c312264d852f396f4a30c90f53b3aa4d
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2008 Jens Arnold
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "plugin.h"
22 #include "lib/grey.h"
23 #include "lib/helper.h"
27 #if (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) \
28 || (CONFIG_KEYPAD == IPOD_1G2G_PAD)
29 #define GREY_QUIT BUTTON_MENU
30 #define GREY_OK BUTTON_SELECT
31 #define GREY_PREV BUTTON_LEFT
32 #define GREY_NEXT BUTTON_RIGHT
33 #define GREY_UP BUTTON_SCROLL_FWD
34 #define GREY_DOWN BUTTON_SCROLL_BACK
36 #elif CONFIG_KEYPAD == IRIVER_H100_PAD
37 #define GREY_QUIT BUTTON_OFF
38 #define GREY_OK BUTTON_SELECT
39 #define GREY_PREV BUTTON_LEFT
40 #define GREY_NEXT BUTTON_RIGHT
41 #define GREY_UP BUTTON_UP
42 #define GREY_DOWN BUTTON_DOWN
44 #elif CONFIG_KEYPAD == RECORDER_PAD
45 #define GREY_QUIT BUTTON_OFF
46 #define GREY_OK BUTTON_PLAY
47 #define GREY_PREV BUTTON_LEFT
48 #define GREY_NEXT BUTTON_RIGHT
49 #define GREY_UP BUTTON_UP
50 #define GREY_DOWN BUTTON_DOWN
52 #elif CONFIG_KEYPAD == ONDIO_PAD
53 #define GREY_QUIT BUTTON_OFF
54 #define GREY_OK BUTTON_MENU
55 #define GREY_PREV BUTTON_LEFT
56 #define GREY_NEXT BUTTON_RIGHT
57 #define GREY_UP BUTTON_UP
58 #define GREY_DOWN BUTTON_DOWN
60 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD) \
61 || (CONFIG_KEYPAD == MROBE100_PAD) \
62 || (CONFIG_KEYPAD == SANSA_CLIP_PAD)
63 #define GREY_QUIT BUTTON_POWER
64 #define GREY_OK BUTTON_SELECT
65 #define GREY_PREV BUTTON_LEFT
66 #define GREY_NEXT BUTTON_RIGHT
67 #define GREY_UP BUTTON_UP
68 #define GREY_DOWN BUTTON_DOWN
70 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
71 #define GREY_QUIT BUTTON_RC_REC
72 #define GREY_OK BUTTON_RC_PLAY
73 #define GREY_PREV BUTTON_RC_REW
74 #define GREY_NEXT BUTTON_RC_FF
75 #define GREY_UP BUTTON_RC_VOL_UP
76 #define GREY_DOWN BUTTON_RC_VOL_DOWN
78 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
79 #define GREY_QUIT BUTTON_REC
80 #define GREY_OK BUTTON_PLAY
81 #define GREY_PREV BUTTON_LEFT
82 #define GREY_NEXT BUTTON_RIGHT
83 #define GREY_UP BUTTON_UP
84 #define GREY_DOWN BUTTON_DOWN
86 #elif CONFIG_KEYPAD == MPIO_HD200_PAD
87 #define GREY_QUIT (BUTTON_REC|BUTTON_PLAY)
88 #define GREY_OK BUTTON_PLAY
89 #define GREY_PREV BUTTON_REW
90 #define GREY_NEXT BUTTON_FF
91 #define GREY_UP BUTTON_VOL_UP
92 #define GREY_DOWN BUTTON_VOL_DOWN
94 #elif CONFIG_KEYPAD == MPIO_HD300_PAD
95 #define GREY_QUIT (BUTTON_REC|BUTTON_REPEAT)
96 #define GREY_OK BUTTON_PLAY
97 #define GREY_PREV BUTTON_REW
98 #define GREY_NEXT BUTTON_FF
99 #define GREY_UP BUTTON_UP
100 #define GREY_DOWN BUTTON_DOWN
102 #else
103 #error unsupported keypad
104 #endif
106 #define BLOCK_WIDTH (LCD_WIDTH/8)
107 #define BLOCK_HEIGHT (LCD_HEIGHT/8)
109 #define STEPS 16
111 GREY_INFO_STRUCT
113 static const unsigned char dither_matrix[16][16] = {
114 { 0,192, 48,240, 12,204, 60,252, 3,195, 51,243, 15,207, 63,255 },
115 { 128, 64,176,112,140, 76,188,124,131, 67,179,115,143, 79,191,127 },
116 { 32,224, 16,208, 44,236, 28,220, 35,227, 19,211, 47,239, 31,223 },
117 { 160, 96,144, 80,172,108,156, 92,163, 99,147, 83,175,111,159, 95 },
118 { 8,200, 56,248, 4,196, 52,244, 11,203, 59,251, 7,199, 55,247 },
119 { 136, 72,184,120,132, 68,180,116,139, 75,187,123,135, 71,183,119 },
120 { 40,232, 24,216, 36,228, 20,212, 43,235, 27,219, 39,231, 23,215 },
121 { 168,104,152, 88,164,100,148, 84,171,107,155, 91,167,103,151, 87 },
122 { 2,194, 50,242, 14,206, 62,254, 1,193, 49,241, 13,205, 61,253 },
123 { 130, 66,178,114,142, 78,190,126,129, 65,177,113,141, 77,189,125 },
124 { 34,226, 18,210, 46,238, 30,222, 33,225, 17,209, 45,237, 29,221 },
125 { 162, 98,146, 82,174,110,158, 94,161, 97,145, 81,173,109,157, 93 },
126 { 10,202, 58,250, 6,198, 54,246, 9,201, 57,249, 5,197, 53,245 },
127 { 138, 74,186,122,134, 70,182,118,137, 73,185,121,133, 69,181,117 },
128 { 42,234, 26,218, 38,230, 22,214, 41,233, 25,217, 37,229, 21,213 },
129 { 170,106,154, 90,166,102,150, 86,169,105,153, 89,165,101,149, 85 }
132 static unsigned char input_levels[STEPS+1];
133 static unsigned char lcd_levels[STEPS+1];
135 static unsigned char *gbuf;
136 static size_t gbuf_size = 0;
138 static void fill_rastered(int bx, int by, int bw, int bh, int step)
140 int x, xmax, y, ymax;
141 int level;
143 if (step < 0)
144 step = 0;
145 else if (step > STEPS)
146 step = STEPS;
148 level = input_levels[step];
149 level += (level-1) >> 7;
151 for (y = (LCD_HEIGHT/2) + by * BLOCK_HEIGHT, ymax = y + bh * BLOCK_HEIGHT;
152 y < ymax; y++)
154 for (x = (LCD_WIDTH/2) + bx * BLOCK_WIDTH, xmax = x + bw * BLOCK_WIDTH;
155 x < xmax; x++)
157 grey_set_foreground((level > dither_matrix[y & 0xf][x & 0xf])
158 ? 255 : 0);
159 grey_drawpixel(x, y);
164 /* plugin entry point */
165 enum plugin_status plugin_start(const void* parameter)
167 bool done = false;
168 int cur_step = 1;
169 int button, i, j, l, fd;
170 unsigned char filename[MAX_PATH];
172 /* standard stuff */
173 (void)parameter;
175 gbuf = (unsigned char *) rb->plugin_get_buffer(&gbuf_size);
177 if (!grey_init(gbuf, gbuf_size,
178 GREY_BUFFERED|GREY_RAWMAPPED|GREY_ON_COP,
179 LCD_WIDTH, LCD_HEIGHT, NULL))
181 rb->splash(HZ, "Not enough memory.");
182 return PLUGIN_ERROR;
184 for (i = 0; i <= STEPS; i++)
185 input_levels[i] = lcd_levels[i] = (255 * i + (STEPS/2)) / STEPS;
187 backlight_ignore_timeout();
189 grey_set_background(0); /* set background to black */
190 grey_clear_display();
191 grey_show(true);
193 rb->lcd_setfont(FONT_SYSFIXED);
195 while (!done)
197 fill_rastered(-3, -3, 2, 2, cur_step - 1);
198 fill_rastered(-1, -3, 2, 2, cur_step);
199 fill_rastered(1, -3, 2, 2, cur_step + 1);
200 fill_rastered(-3, -1, 2, 2, cur_step);
201 grey_set_foreground(lcd_levels[cur_step]);
202 grey_fillrect(LCD_WIDTH/2-BLOCK_WIDTH, LCD_HEIGHT/2-BLOCK_HEIGHT,
203 2*BLOCK_WIDTH, 2*BLOCK_HEIGHT);
204 fill_rastered(1, -1, 2, 2, cur_step);
205 fill_rastered(-3, 1, 2, 2, cur_step + 1);
206 fill_rastered(-1, 1, 2, 2, cur_step);
207 fill_rastered(1, 1, 2, 2, cur_step - 1);
208 grey_update();
210 button = rb->button_get(true);
211 switch (button)
213 case GREY_PREV:
214 if (cur_step > 0)
215 cur_step--;
216 break;
218 case GREY_NEXT:
219 if (cur_step < STEPS)
220 cur_step++;
221 break;
223 case GREY_UP:
224 case GREY_UP|BUTTON_REPEAT:
225 l = lcd_levels[cur_step];
226 if (l < 255)
228 l++;
229 for (i = cur_step; i <= STEPS; i++)
230 if (lcd_levels[i] < l)
231 lcd_levels[i] = l;
233 break;
235 case GREY_DOWN:
236 case GREY_DOWN|BUTTON_REPEAT:
237 l = lcd_levels[cur_step];
238 if (l > 0)
240 l--;
241 for (i = cur_step; i >= 0; i--)
242 if (lcd_levels[i] > l)
243 lcd_levels[i] = l;
245 break;
247 case GREY_OK:
249 /* dump result in form suitable for lcdlinear[] */
250 rb->create_numbered_filename(filename, "/", "test_grey_",
251 ".txt", 2 IF_CNFN_NUM_(, NULL));
252 fd = rb->open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666);
253 if (fd >= 0)
255 rb->fdprintf(fd, "Adjusted grey shades\n");
256 for (i = 0; i <= STEPS; i++)
257 rb->fdprintf(fd, "%3d: %3d\n", input_levels[i],
258 lcd_levels[i]);
259 rb->fdprintf(fd,"\n\nInterpolated lcdlinear matrix\n");
261 for (i = 1; i <= STEPS; i++)
263 for (j=0; j < STEPS; j++)
265 rb->fdprintf(fd, "%3d, ",
266 lcd_levels[i-1] +
267 ((lcd_levels[i] - lcd_levels[i-1])*j)/STEPS);
269 rb->fdprintf(fd, "\n");
271 rb->close(fd);
273 /* fall through */
275 case GREY_QUIT:
276 done = true;
277 break;
281 grey_release();
282 backlight_use_settings();
283 return PLUGIN_OK;