* removed some more unused stuff in the simulator makefile
[kugel-rb.git] / apps / plugins / mandelbrot.c
blob106d4b69f7580a8108ffdf00dcea42ee6f2bfcfc
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2004 Matthias Wientapper
13 * All files in this archive are subject to the GNU General Public License.
14 * See the file COPYING in the source tree root for full license agreement.
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
19 ****************************************************************************/
20 #ifndef SIMULATOR
21 #include "plugin.h"
23 #ifdef HAVE_LCD_BITMAP // this is not fun on the player
24 # include "gray.h"
26 static struct plugin_api* rb;
27 static char buff[32];
28 static int lcd_aspect_ratio;
29 static int x_min;
30 static int x_max;
31 static int y_min;
32 static int y_max;
33 static int delta;
34 static int max_iter;
35 static unsigned char *gbuf;
36 static unsigned int gbuf_size = 0;
39 void init_mandelbrot_set(void){
40 x_min = -5<<25; // -2.0<<26
41 x_max = 1<<26; // 1.0<<26
42 y_min = -1<<26; // -1.0<<26
43 y_max = 1<<26; // 1.0<<26
44 delta = (x_max - x_min) >> 3; // /8
45 max_iter = 25;
48 void calc_mandelbrot_set(void){
50 unsigned int start_tick;
51 int n_iter;
52 int x_pixel, y_pixel;
53 int x, x2, y, y2, a, b;
54 int x_fact, y_fact;
55 int brightness;
57 start_tick = *rb->current_tick;
59 // rb->lcd_clear_display();
60 // rb->lcd_update();
62 gray_clear_display();
64 x_fact = (x_max - x_min) / LCD_WIDTH;
65 y_fact = (y_max - y_min) / LCD_HEIGHT;
67 for(y_pixel = LCD_HEIGHT-1; y_pixel>=0; y_pixel--){
68 b = (y_pixel * y_fact) + y_min;
69 for (x_pixel = LCD_WIDTH-1; x_pixel>=0; x_pixel--){
70 a = (x_pixel * x_fact) + x_min;
71 x = 0;
72 y = 0;
73 n_iter = 0;
75 while (++n_iter<=max_iter) {
76 x >>= 13;
77 y >>= 13;
78 x2 = x * x;
79 y2 = y * y;
81 if (x2 + y2 > (4<<26)) break;
83 y = 2 * x * y + b;
84 x = x2 - y2 + a;
87 // "coloring"
88 brightness = 0;
89 if (n_iter > max_iter){
90 brightness = 0; // black
91 } else {
92 brightness = 255 - (31 * (n_iter & 7));
95 gray_drawpixel( x_pixel, y_pixel, brightness);
101 enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
103 int grayscales;
104 bool redraw = true;
106 TEST_PLUGIN_API(api);
107 rb = api;
108 (void)parameter;
110 /* This plugin uses the grayscale framework, so initialize */
111 gray_init(api);
113 /* get the remainder of the plugin buffer */
114 gbuf = (unsigned char *) rb->plugin_get_buffer(&gbuf_size);
116 /* initialize the grayscale buffer:
117 * 112 pixels wide, 8 rows (64 pixels) high, (try to) reserve
118 * 16 bitplanes for 17 shades of gray.*/
119 grayscales = gray_init_buffer(gbuf, gbuf_size, 112, 8, 16, NULL) + 1;
120 if (grayscales != 17){
121 rb->snprintf(buff, sizeof(buff), "%d", grayscales);
122 rb->lcd_puts(0, 1, buff);
123 rb->lcd_update();
124 rb->sleep(HZ*2);
125 return(0);
128 gray_show_display(true); /* switch on grayscale overlay */
130 init_mandelbrot_set();
131 lcd_aspect_ratio = ((LCD_WIDTH<<13) / LCD_HEIGHT)<<13;
133 /* main loop */
134 while (true){
135 if(redraw)
136 calc_mandelbrot_set();
138 redraw = false;
140 switch (rb->button_get(true)) {
141 case BUTTON_OFF:
142 gray_release_buffer();
143 return PLUGIN_OK;
145 case BUTTON_ON:
146 x_min -= ((delta>>13)*(lcd_aspect_ratio>>13));
147 x_max += ((delta>>13)*(lcd_aspect_ratio>>13));
148 y_min -= delta;
149 y_max += delta;
150 delta = (x_max - x_min) >> 3;
151 redraw = true;
152 break;
155 case BUTTON_PLAY:
156 x_min += ((delta>>13)*(lcd_aspect_ratio>>13));
157 x_max -= ((delta>>13)*(lcd_aspect_ratio>>13));
158 y_min += delta;
159 y_max -= delta;
160 delta = (x_max - x_min) >> 3;
161 redraw = true;
162 break;
164 case BUTTON_UP:
165 y_min -= delta;
166 y_max -= delta;
167 redraw = true;
168 break;
170 case BUTTON_DOWN:
171 y_min += delta;
172 y_max += delta;
173 redraw = true;
174 break;
176 case BUTTON_LEFT:
177 x_min -= delta;
178 x_max -= delta;
179 redraw = true;
180 break;
182 case BUTTON_RIGHT:
183 x_min += delta;
184 x_max += delta;
185 redraw = true;
186 break;
188 case BUTTON_F1:
189 if (max_iter>5){
190 max_iter -= 5;
191 redraw = true;
193 break;
195 case BUTTON_F2:
196 if (max_iter < 195){
197 max_iter += 5;
198 redraw = true;
200 break;
202 case BUTTON_F3:
203 init_mandelbrot_set();
204 redraw = true;
205 break;
207 case SYS_USB_CONNECTED:
208 gray_release_buffer();
209 rb->usb_screen();
210 return PLUGIN_USB_CONNECTED;
213 gray_release_buffer();
214 return false;
216 #endif
217 #endif