A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita
audio
rust
zig
deno
mpris
rockbox
mpd
1/***************************************************************************
2* __________ __ ___.
3* Open \______ \ ____ ____ | | _\_ |__ _______ ___
4* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7* \/ \/ \/ \/ \/
8* $Id$
9*
10* Plasma demo plugin
11*
12* My crack at making a 80's style retro plasma effect for the fantastic
13* rockbox!
14* Okay, I could've hard-coded the sine wave values, I just wanted the
15* challange of calculating them! silly: maybe, fun: yes!
16*
17* This program is free software; you can redistribute it and/or
18* modify it under the terms of the GNU General Public License
19* as published by the Free Software Foundation; either version 2
20* of the License, or (at your option) any later version.
21*
22* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
23* KIND, either express or implied.
24*
25****************************************************************************/
26
27#include "plugin.h"
28#include "fixedpoint.h"
29#include "lib/helper.h"
30#include "lib/pluginlib_actions.h"
31#include "lib/pluginlib_exit.h"
32
33#ifndef HAVE_LCD_COLOR
34#include "lib/grey.h"
35#endif
36
37
38/******************************* Globals ***********************************/
39static fb_data *lcd_fb;
40
41
42static unsigned char wave_array[256]; /* Pre calculated wave array */
43#ifdef HAVE_LCD_COLOR
44static fb_data colours[256]; /* Smooth transition of shades */
45static int redfactor = 1, greenfactor = 2, bluefactor = 3;
46static int redphase = 0, greenphase = 50, bluephase = 100;
47 /* lower chance of gray at regular intervals */
48#else
49GREY_INFO_STRUCT
50static unsigned char colours[256]; /* Smooth transition of shades */
51static unsigned char greybuffer[LCD_HEIGHT*LCD_WIDTH]; /* off screen buffer */
52static unsigned char *gbuf;
53static size_t gbuf_size = 0;
54#endif
55static unsigned char sp1, sp2, sp3, sp4; /* Speed of plasma */
56static int plasma_frequency;
57#ifdef HAVE_ADJUSTABLE_CPU_FREQ
58static bool boosted = false;
59#endif
60
61static const struct button_mapping* plugin_contexts[]= {
62 pla_main_ctx,
63#if defined(HAVE_REMOTE_LCD)
64 pla_remote_ctx,
65#endif
66};
67
68#define WAV_AMP 90
69
70/*
71 * Main wave function so we don't have to re-calc the sine
72 * curve every time. Mess around WAV_AMP and FREQ to make slighlty
73 * weirder looking plasmas!
74 */
75
76static void wave_table_generate(void)
77{
78 int i;
79 for (i=0;i<256;++i)
80 {
81 wave_array[i] = (unsigned char)((WAV_AMP
82 * (fp14_sin((i * 360 * plasma_frequency) / 256))) / 16384);
83 }
84}
85
86#ifdef HAVE_LCD_COLOR
87/* Make a smooth colour cycle. */
88static void shades_generate(int time)
89{
90 int i;
91 unsigned red, green, blue;
92 unsigned r = time * redfactor + redphase;
93 unsigned g = time * greenfactor + greenphase;
94 unsigned b = time * bluefactor + bluephase;
95
96 for(i=0; i < 256; ++i)
97 {
98 r &= 0xFF; g &= 0xFF; b &= 0xFF;
99
100 red = 2 * r;
101 if (red > 255)
102 red = 510 - red;
103 green = 2 * g;
104 if (green > 255)
105 green = 510 - green;
106 blue = 2 * b;
107 if (blue > 255)
108 blue= 510 - blue;
109
110 colours[i] = FB_RGBPACK(red, green, blue);
111
112 r++; g++; b++;
113 }
114#if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
115 rb->lcd_pal256_update_pal(colours);
116#endif
117}
118#else
119/* Make a smooth shade from black into white and back into black again. */
120static void shades_generate(void)
121{
122 int i, y;
123
124 for(i=0; i < 256; ++i)
125 {
126 y = 2 * i;
127 if (y > 255)
128 y = 510 - y;
129 colours[i] = y;
130 }
131}
132#endif
133
134static void cleanup(void)
135{
136#ifdef HAVE_ADJUSTABLE_CPU_FREQ
137 if (boosted)
138 rb->cpu_boost(false);
139#endif
140#ifndef HAVE_LCD_COLOR
141 grey_release();
142#endif
143
144 /* Turn on backlight timeout (revert to settings) */
145 backlight_use_settings();
146
147#if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
148 rb->lcd_set_mode(LCD_MODE_RGB565);
149#endif
150}
151
152/*
153 * Main function that also contain the main plasma
154 * algorithm.
155 */
156
157int main(void)
158{
159 plasma_frequency = 1;
160 int action, x, y;
161 unsigned char p1,p2,p3,p4,t1,t2,t3,t4, z,z0;
162#ifdef HAVE_ADJUSTABLE_CPU_FREQ
163 long last_tick = *rb->current_tick;
164 int delay;
165 int cumulated_lag = 0;
166#endif
167#ifdef HAVE_LCD_COLOR
168#if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
169 unsigned char *ptr;
170#else
171 fb_data *ptr;
172#endif
173 int time=0;
174#else
175 unsigned char *ptr;
176#endif
177
178 /*Generate the neccesary pre calced stuff*/
179 wave_table_generate();
180
181#ifndef HAVE_LCD_COLOR
182 shades_generate(); /* statically */
183
184 /* get the remainder of the plugin buffer */
185 gbuf = (unsigned char *) rb->plugin_get_buffer(&gbuf_size);
186
187 if (!grey_init(gbuf, gbuf_size, GREY_ON_COP, LCD_WIDTH, LCD_HEIGHT, NULL))
188 {
189 rb->splash(HZ, "Couldn't init greyscale display");
190 return PLUGIN_ERROR;
191 }
192 /* switch on greyscale overlay */
193 grey_show(true);
194#endif
195 atexit(cleanup);
196 sp1 = 4;
197 sp2 = 2;
198 sp3 = 4;
199 sp4 = 2;
200 p1=p2=p3=p4=0;
201 while (true)
202 {
203#ifdef HAVE_LCD_COLOR
204 shades_generate(time++); /* dynamically */
205#if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
206 ptr = (unsigned char*)lcd_fb;
207#else
208 ptr = lcd_fb;
209#endif
210
211#else
212 ptr = greybuffer;
213#endif
214 t1=p1;
215 t2=p2;
216 for(y = 0; y < LCD_HEIGHT; ++y)
217 {
218 t3=p3;
219 t4=p4;
220 z0 = wave_array[t1] + wave_array[t2];
221 for(x = 0; x < LCD_WIDTH; ++x)
222 {
223 z = z0 + wave_array[t3] + wave_array[t4];
224#if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
225 *ptr++ = z;
226#else
227 *ptr++ = colours[z];
228#endif
229 t3+=1;
230 t4+=2;
231 }
232 t1+=2;
233 t2+=1;
234 rb->yield();
235 }
236 p1+=sp1;
237 p2-=sp2;
238 p3+=sp3;
239 p4-=sp4;
240#ifdef HAVE_LCD_COLOR
241#if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
242 rb->lcd_blit_pal256( (unsigned char*)lcd_fb,
243 0,0,0,0,LCD_WIDTH,LCD_HEIGHT);
244#else
245 rb->lcd_update();
246#endif
247#else
248 grey_ub_gray_bitmap(greybuffer, 0, 0, LCD_WIDTH, LCD_HEIGHT);
249#endif
250
251#ifdef HAVE_ADJUSTABLE_CPU_FREQ
252 delay = last_tick - *rb->current_tick + HZ/33;
253 if (!boosted && delay < 0)
254 {
255 cumulated_lag -= delay; /* proportional increase */
256 if (cumulated_lag >= HZ)
257 rb->cpu_boost(boosted = true);
258 }
259 else if (boosted && delay > 1) /* account for jitter */
260 {
261 if (--cumulated_lag <= 0) /* slow decrease */
262 rb->cpu_boost(boosted = false);
263 }
264 last_tick = *rb->current_tick;
265#endif
266 action = pluginlib_getaction(0, plugin_contexts,
267 ARRAYLEN(plugin_contexts));
268
269 switch(action)
270 {
271#if (CONFIG_KEYPAD == IPOD_1G2G_PAD) \
272 || (CONFIG_KEYPAD == IPOD_3G_PAD) \
273 || (CONFIG_KEYPAD == IPOD_4G_PAD)
274 case PLA_UP:
275#endif
276 case PLA_EXIT:
277 case PLA_CANCEL:
278 return PLUGIN_OK;
279 break;
280
281#ifdef HAVE_SCROLLWHEEL
282 case PLA_SCROLL_FWD:
283 case PLA_SCROLL_FWD_REPEAT:
284#endif
285#if (CONFIG_KEYPAD != IPOD_1G2G_PAD) \
286 && (CONFIG_KEYPAD != IPOD_3G_PAD) \
287 && (CONFIG_KEYPAD != IPOD_4G_PAD)
288 case PLA_UP:
289#endif
290 case PLA_UP_REPEAT:
291 ++plasma_frequency;
292 wave_table_generate();
293 break;
294
295#ifdef HAVE_SCROLLWHEEL
296 case PLA_SCROLL_BACK:
297 case PLA_SCROLL_BACK_REPEAT:
298#endif
299 case PLA_DOWN:
300 case PLA_DOWN_REPEAT:
301 if(plasma_frequency>1)
302 {
303 --plasma_frequency;
304 wave_table_generate();
305 }
306 break;
307#ifdef HAVE_LCD_COLOR
308 case PLA_SELECT:
309 redfactor=rb->rand()%4;
310 greenfactor=rb->rand()%4;
311 bluefactor=rb->rand()%4;
312 redphase=rb->rand()%256;
313 greenphase=rb->rand()%256;
314 bluephase=rb->rand()%256;
315 break;
316#endif
317
318 default:
319 exit_on_usb(action);
320 break;
321 }
322 }
323}
324
325/*************************** Plugin entry point ****************************/
326
327enum plugin_status plugin_start(const void* parameter)
328{
329 (void)parameter;
330#if LCD_DEPTH > 1
331 rb->lcd_set_backdrop(NULL);
332#endif
333
334 /* Turn off backlight timeout */
335 backlight_ignore_timeout();
336
337#if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
338 rb->lcd_set_mode(LCD_MODE_PAL256);
339#endif
340 struct viewport *vp_main = rb->lcd_set_viewport(NULL);
341 lcd_fb = vp_main->buffer->fb_ptr;
342
343 return main();
344}