Extract config file saving code to a function; Do not write the NUL character to...
[kugel-rb.git] / apps / plugins / vu_meter.c
blob077a6e92f41e0040fccb3984ffd75ead41222b18
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2003 Lee Pilgrim
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
19 **************************************************************************/
20 #include "plugin.h"
21 #include "lib/fixedpoint.h"
22 #include "lib/playback_control.h"
26 /* variable button definitions */
27 #if CONFIG_KEYPAD == RECORDER_PAD
28 #define VUMETER_QUIT BUTTON_OFF
29 #define VUMETER_HELP BUTTON_ON
30 #define VUMETER_MENU BUTTON_F1
31 #define VUMETER_UP BUTTON_UP
32 #define VUMETER_DOWN BUTTON_DOWN
33 #define LABEL_HELP "ON"
34 #define LABEL_QUIT "OFF"
35 #define LABEL_MENU "F1"
36 #define LABEL_VOLUME "UP/DOWN"
38 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
39 #define VUMETER_QUIT BUTTON_OFF
40 #define VUMETER_HELP BUTTON_ON
41 #define VUMETER_MENU BUTTON_F1
42 #define VUMETER_UP BUTTON_UP
43 #define VUMETER_DOWN BUTTON_DOWN
44 #define LABEL_HELP "ON"
45 #define LABEL_QUIT "OFF"
46 #define LABEL_MENU "F1"
47 #define LABEL_VOLUME "UP/DOWN"
49 #elif CONFIG_KEYPAD == ONDIO_PAD
50 #define VUMETER_QUIT BUTTON_OFF
51 #define VUMETER_HELP_PRE BUTTON_MENU
52 #define VUMETER_HELP (BUTTON_MENU | BUTTON_REL)
53 #define VUMETER_MENU_PRE BUTTON_MENU
54 #define VUMETER_MENU (BUTTON_MENU | BUTTON_REPEAT)
55 #define VUMETER_UP BUTTON_UP
56 #define VUMETER_DOWN BUTTON_DOWN
57 #define LABEL_HELP "MODE"
58 #define LABEL_QUIT "OFF"
59 #define LABEL_MENU "MODE.."
60 #define LABEL_VOLUME "UP/DOWN"
62 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
63 (CONFIG_KEYPAD == IRIVER_H300_PAD)
64 #define VUMETER_QUIT BUTTON_OFF
65 #define VUMETER_HELP BUTTON_ON
66 #define VUMETER_MENU BUTTON_SELECT
67 #define VUMETER_MENU2 BUTTON_MODE
68 #define VUMETER_UP BUTTON_UP
69 #define VUMETER_DOWN BUTTON_DOWN
70 #define LABEL_HELP "PLAY"
71 #define LABEL_QUIT "STOP"
72 #define LABEL_MENU "SELECT,MODE"
73 #define LABEL_VOLUME "UP/DOWN"
75 #define VUMETER_RC_QUIT BUTTON_RC_STOP
77 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
78 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
79 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
80 #define VUMETER_QUIT BUTTON_MENU
81 #define VUMETER_HELP BUTTON_PLAY
82 #define VUMETER_MENU BUTTON_SELECT
83 #define VUMETER_UP BUTTON_SCROLL_FWD
84 #define VUMETER_DOWN BUTTON_SCROLL_BACK
85 #define LABEL_HELP "PLAY"
86 #define LABEL_QUIT "MENU"
87 #define LABEL_MENU "SELECT"
88 #define LABEL_VOLUME "Wheel"
90 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
91 #define VUMETER_QUIT BUTTON_POWER
92 #define VUMETER_HELP BUTTON_A
93 #define VUMETER_MENU BUTTON_MENU
94 #define VUMETER_UP BUTTON_UP
95 #define VUMETER_DOWN BUTTON_DOWN
96 #define LABEL_HELP "A"
97 #define LABEL_QUIT "POWER"
98 #define LABEL_MENU "MENU"
99 #define LABEL_VOLUME "UP/DOWN"
101 #elif (CONFIG_KEYPAD == SANSA_E200_PAD)
102 #define VUMETER_QUIT BUTTON_POWER
103 #define VUMETER_HELP BUTTON_REC
104 #define VUMETER_MENU BUTTON_SELECT
105 #define VUMETER_UP BUTTON_SCROLL_FWD
106 #define VUMETER_DOWN BUTTON_SCROLL_BACK
107 #define LABEL_HELP "REC"
108 #define LABEL_QUIT "POWER"
109 #define LABEL_MENU "SELECT"
110 #define LABEL_VOLUME "Wheel"
112 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
113 #define VUMETER_QUIT (BUTTON_HOME|BUTTON_REPEAT)
114 #define VUMETER_HELP BUTTON_SELECT|BUTTON_REPEAT
115 #define VUMETER_MENU BUTTON_SELECT|BUTTON_REL
116 #define VUMETER_UP BUTTON_SCROLL_FWD
117 #define VUMETER_DOWN BUTTON_SCROLL_BACK
118 #define LABEL_HELP "Hold Select"
119 #define LABEL_QUIT "HOME"
120 #define LABEL_MENU "Select"
121 #define LABEL_VOLUME "Scrollwheel"
123 #elif (CONFIG_KEYPAD == SANSA_C200_PAD)
124 #define VUMETER_QUIT BUTTON_POWER
125 #define VUMETER_HELP BUTTON_REC
126 #define VUMETER_MENU BUTTON_SELECT
127 #define VUMETER_UP BUTTON_VOL_UP
128 #define VUMETER_DOWN BUTTON_VOL_DOWN
129 #define LABEL_HELP "REC"
130 #define LABEL_QUIT "POWER"
131 #define LABEL_MENU "SELECT"
132 #define LABEL_VOLUME "VOL UP/DN"
134 #elif (CONFIG_KEYPAD == SANSA_CLIP_PAD)
135 #define VUMETER_QUIT BUTTON_POWER
136 #define VUMETER_HELP BUTTON_HOME
137 #define VUMETER_MENU BUTTON_SELECT
138 #define VUMETER_UP BUTTON_VOL_UP
139 #define VUMETER_DOWN BUTTON_VOL_DOWN
140 #define LABEL_HELP "HOME"
141 #define LABEL_QUIT "POWER"
142 #define LABEL_MENU "SELECT"
143 #define LABEL_VOLUME "VOL UP/DN"
145 #elif (CONFIG_KEYPAD == SANSA_M200_PAD)
146 #define VUMETER_QUIT BUTTON_POWER
147 #define VUMETER_HELP (BUTTON_SELECT | BUTTON_UP)
148 #define VUMETER_MENU (BUTTON_SELECT | BUTTON_REL)
149 #define VUMETER_UP BUTTON_VOL_UP
150 #define VUMETER_DOWN BUTTON_VOL_DOWN
151 #define LABEL_HELP "SELECT + UP"
152 #define LABEL_QUIT "POWER"
153 #define LABEL_MENU "SELECT"
154 #define LABEL_VOLUME "VOL UP/DN"
156 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
157 #define VUMETER_QUIT BUTTON_POWER
158 #define VUMETER_HELP BUTTON_PLAY
159 #define VUMETER_MENU BUTTON_SELECT
160 #define VUMETER_UP BUTTON_UP
161 #define VUMETER_DOWN BUTTON_DOWN
162 #define LABEL_HELP "PLAY"
163 #define LABEL_QUIT "POWER"
164 #define LABEL_MENU "SELECT"
165 #define LABEL_VOLUME "UP/DOWN"
167 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
168 #define VUMETER_QUIT BUTTON_POWER
169 #define VUMETER_HELP BUTTON_PLAY
170 #define VUMETER_MENU BUTTON_REW
171 #define VUMETER_UP BUTTON_SCROLL_UP
172 #define VUMETER_DOWN BUTTON_SCROLL_DOWN
173 #define LABEL_HELP "PLAY"
174 #define LABEL_QUIT "POWER"
175 #define LABEL_MENU "REW"
176 #define LABEL_VOLUME "Scroller"
178 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
179 #define VUMETER_QUIT BUTTON_BACK
180 #define VUMETER_HELP BUTTON_NEXT
181 #define VUMETER_MENU BUTTON_MENU
182 #define VUMETER_UP BUTTON_UP
183 #define VUMETER_DOWN BUTTON_DOWN
184 #define LABEL_HELP "NEXT"
185 #define LABEL_QUIT "BACK"
186 #define LABEL_MENU "MENU"
187 #define LABEL_VOLUME "UP/DOWN"
189 #elif (CONFIG_KEYPAD == MROBE100_PAD)
190 #define VUMETER_QUIT BUTTON_POWER
191 #define VUMETER_HELP BUTTON_DISPLAY
192 #define VUMETER_MENU BUTTON_MENU
193 #define VUMETER_UP BUTTON_UP
194 #define VUMETER_DOWN BUTTON_DOWN
195 #define LABEL_HELP "DISPLAY"
196 #define LABEL_QUIT "POWER"
197 #define LABEL_MENU "MENU"
198 #define LABEL_VOLUME "UP/DOWN"
200 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
201 #define VUMETER_QUIT BUTTON_RC_REC
202 #define VUMETER_HELP BUTTON_RC_MODE
203 #define VUMETER_MENU BUTTON_RC_MENU
204 #define VUMETER_UP BUTTON_RC_VOL_UP
205 #define VUMETER_DOWN BUTTON_RC_VOL_DOWN
206 #define LABEL_HELP "MODE"
207 #define LABEL_QUIT "REC"
208 #define LABEL_MENU "MENU"
209 #define LABEL_VOLUME "VOL UP/DN"
211 #elif CONFIG_KEYPAD == COWON_D2_PAD
212 #define VUMETER_QUIT BUTTON_POWER
213 #define VUMETER_MENU BUTTON_MENU
214 #define LABEL_QUIT "POWER"
215 #define LABEL_MENU "MENU"
217 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
218 #define VUMETER_QUIT BUTTON_BACK
219 #define VUMETER_HELP BUTTON_SELECT
220 #define VUMETER_MENU BUTTON_MENU
221 #define VUMETER_UP BUTTON_UP
222 #define VUMETER_DOWN BUTTON_DOWN
223 #define LABEL_HELP "MIDDLE"
224 #define LABEL_QUIT "BACK"
225 #define LABEL_MENU "MENU"
226 #define LABEL_VOLUME "UP/DOWN"
228 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
229 #define VUMETER_QUIT BUTTON_POWER
230 #define VUMETER_HELP BUTTON_VIEW
231 #define VUMETER_MENU BUTTON_MENU
232 #define VUMETER_UP BUTTON_UP
233 #define VUMETER_DOWN BUTTON_DOWN
234 #define LABEL_HELP "VIEW"
235 #define LABEL_QUIT "POWER"
236 #define LABEL_MENU "MENU"
237 #define LABEL_VOLUME "UP/DOWN"
239 #elif CONFIG_KEYPAD == PHILIPS_HDD6330_PAD
240 #define VUMETER_QUIT BUTTON_POWER
241 #define VUMETER_HELP BUTTON_NEXT
242 #define VUMETER_MENU BUTTON_MENU
243 #define VUMETER_UP BUTTON_UP
244 #define VUMETER_DOWN BUTTON_DOWN
245 #define LABEL_HELP "NEXT"
246 #define LABEL_QUIT "QUIT"
247 #define LABEL_MENU "MENU"
248 #define LABEL_VOLUME "UP/DOWN"
250 #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
251 #define VUMETER_QUIT BUTTON_POWER
252 #define VUMETER_HELP BUTTON_RIGHT
253 #define VUMETER_MENU BUTTON_MENU
254 #define VUMETER_UP BUTTON_UP
255 #define VUMETER_DOWN BUTTON_DOWN
256 #define LABEL_HELP "RIGHT"
257 #define LABEL_QUIT "POWER"
258 #define LABEL_MENU "MENU"
259 #define LABEL_VOLUME "UP/DOWN"
261 #elif CONFIG_KEYPAD == ONDAVX747_PAD
262 #define VUMETER_QUIT BUTTON_POWER
263 #define VUMETER_MENU BUTTON_MENU
264 #define LABEL_QUIT "POWER"
265 #define LABEL_MENU "MENU"
267 #elif CONFIG_KEYPAD == ONDAVX777_PAD
268 #define VUMETER_QUIT BUTTON_POWER
269 #define LABEL_QUIT "POWER"
271 #elif CONFIG_KEYPAD == MROBE500_PAD
272 #define VUMETER_QUIT BUTTON_POWER
273 #define LABEL_QUIT "POWER"
275 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
276 #define VUMETER_QUIT BUTTON_REC
277 #define VUMETER_HELP BUTTON_PLAY
278 #define VUMETER_MENU BUTTON_LEFT
279 #define VUMETER_UP BUTTON_UP
280 #define VUMETER_DOWN BUTTON_DOWN
281 #define LABEL_HELP "PLAY"
282 #define LABEL_QUIT "REC"
283 #define LABEL_MENU "LEFT"
284 #define LABEL_VOLUME "UP/DOWN"
286 #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
287 #define VUMETER_QUIT BUTTON_REC
288 #define VUMETER_HELP BUTTON_PLAY
289 #define VUMETER_MENU BUTTON_MENU
290 #define VUMETER_UP BUTTON_UP
291 #define VUMETER_DOWN BUTTON_DOWN
292 #define LABEL_HELP "PLAY"
293 #define LABEL_QUIT "REC"
294 #define LABEL_MENU "MENU"
295 #define LABEL_VOLUME "UP/DOWN"
297 #elif CONFIG_KEYPAD == MPIO_HD200_PAD
298 #define VUMETER_QUIT (BUTTON_REC|BUTTON_PLAY)
299 #define VUMETER_HELP BUTTON_PLAY
300 #define VUMETER_MENU BUTTON_FUNC
301 #define VUMETER_UP BUTTON_VOL_UP
302 #define VUMETER_DOWN BUTTON_VOL_DOWN
303 #define LABEL_HELP "PLAY"
304 #define LABEL_QUIT "MENU"
305 #define LABEL_MENU "FUNC"
306 #define LABEL_VOLUME "UP/DOWN"
308 #elif CONFIG_KEYPAD == MPIO_HD300_PAD
309 #define VUMETER_QUIT (BUTTON_REC|BUTTON_REPEAT)
310 #define VUMETER_HELP BUTTON_PLAY
311 #define VUMETER_MENU BUTTON_MENU
312 #define VUMETER_UP BUTTON_UP
313 #define VUMETER_DOWN BUTTON_DOWN
314 #define LABEL_HELP "PLAY"
315 #define LABEL_QUIT "REC..."
316 #define LABEL_MENU "MENU"
317 #define LABEL_VOLUME "UP/DOWN"
319 #else
320 #error No keymap defined!
321 #endif
323 #ifdef HAVE_TOUCHSCREEN
324 #ifndef VUMETER_QUIT
325 #define VUMETER_QUIT BUTTON_TOPLEFT
326 #define LABEL_QUIT "TOPLEFT"
327 #endif
328 #ifndef VUMETER_HELP
329 #define VUMETER_HELP BUTTON_CENTER
330 #define LABEL_HELP "CENTRE"
331 #endif
332 #ifndef VUMETER_MENU
333 #define VUMETER_MENU BUTTON_TOPRIGHT
334 #define LABEL_MENU "TOPRIGHT"
335 #endif
336 #ifndef VUMETER_UP
337 #define VUMETER_UP BUTTON_TOPMIDDLE
338 #endif
339 #ifndef VUMETER_DOWN
340 #define VUMETER_DOWN BUTTON_BOTTOMMIDDLE
341 #endif
342 #ifndef LABEL_VOLUME
343 #define LABEL_VOLUME "UP/DOWN"
344 #endif
345 #endif
347 #if defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
348 #define mas_codec_readreg(x) rand()%MAX_PEAK
349 #endif
351 /* Defines x positions on a logarithmic (dBfs) scale. */
352 unsigned char analog_db_scale[LCD_WIDTH/2];
354 /* Define's y positions, to make the needle arch, like a real needle would. */
355 unsigned char y_values[LCD_WIDTH/2];
357 const unsigned char digital_db_scale[] =
358 {0,2,3,5,5,6,6,6,7,7,7,7,8,8,8,8,8,9,9,9,9,9,9,9,9,10,10,
359 10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11,11};
361 const unsigned char needle_cover[] =
362 {0x18, 0x1c, 0x1c, 0x1e, 0x1e, 0x1f, 0x1f, 0x1f, 0x1e, 0x1e, 0x1c, 0x1c, 0x18};
364 const unsigned char sound_speaker[] = {0x18,0x24,0x42,0xFF};
365 const unsigned char sound_low_level[] = {0x24,0x18};
366 const unsigned char sound_med_level[] = {0x42,0x3C};
367 const unsigned char sound_high_level[] = {0x81,0x7E};
368 const unsigned char sound_max_level[] = {0x0E,0xDF,0x0E};
370 const int half_width = LCD_WIDTH / 2;
371 const int quarter_width = LCD_WIDTH / 4;
372 const int half_height = LCD_HEIGHT / 2;
374 /* approx ratio of the previous hard coded values */
375 const int analog_mini_1 = (LCD_WIDTH / 2)*0.1;
376 const int analog_mini_2 = (LCD_WIDTH / 2)*0.25;
377 const int analog_mini_3 = (LCD_WIDTH / 2)*0.4;
378 const int analog_mini_4 = (LCD_WIDTH / 2)*0.75;
380 const int digital_block_width = LCD_WIDTH / 11;
381 const int digital_block_gap = (int)(LCD_WIDTH / 11) / 10;
382 /* ammount to lead in on left so 11x blocks are centered - is often 0 */
383 const int digital_lead = (LCD_WIDTH - (((int)(LCD_WIDTH / 11))*11) ) / 2;
385 const int digital_block_height = (LCD_HEIGHT - 54) / 2 ;
387 #define ANALOG 0 /* The two meter types */
388 #define DIGITAL 1
390 int left_needle_top_y;
391 int left_needle_top_x;
392 int last_left_needle_top_x;
393 int right_needle_top_y;
394 int right_needle_top_x;
395 int last_right_needle_top_x = LCD_WIDTH / 2;
397 int num_left_leds;
398 int num_right_leds;
399 int last_num_left_leds;
400 int last_num_right_leds;
402 int i;
403 #ifdef HAVE_LCD_COLOR
404 int screen_foreground;
405 #endif
407 #define MAX_PEAK 0x8000
409 /* gap at the top for left/right etc */
410 #define NEEDLE_TOP 25
412 /* pow(M_E, 5) * 65536 */
413 #define E_POW_5 9726404
415 struct saved_settings {
416 int meter_type;
417 bool analog_use_db_scale;
418 bool digital_use_db_scale;
419 bool analog_minimeters;
420 bool digital_minimeters;
421 int analog_decay;
422 int digital_decay;
423 } vumeter_settings;
425 void reset_settings(void) {
426 vumeter_settings.meter_type=ANALOG;
427 vumeter_settings.analog_use_db_scale=true;
428 vumeter_settings.digital_use_db_scale=true;
429 vumeter_settings.analog_minimeters=true;
430 vumeter_settings.digital_minimeters=false;
431 vumeter_settings.analog_decay=3;
432 vumeter_settings.digital_decay=0;
435 void calc_scales(void)
437 unsigned int fx_log_factor = E_POW_5/half_width;
438 unsigned int y,z;
440 long j;
441 long k;
442 int nh = LCD_HEIGHT - NEEDLE_TOP;
443 long nh2 = nh*nh;
445 for (i=1; i <= half_width; i++)
447 /* analog scale */
448 y = (half_width/5)*fp16_log(i*fx_log_factor);
450 /* better way of checking for negative values? */
451 z = y>>16;
452 if (z > LCD_WIDTH)
453 z = 0;
455 analog_db_scale[i-1] = z;
456 /* play nice */
457 rb->yield();
459 /* y values (analog needle co-ords) */
460 j = i - (int)(half_width/2);
461 k = nh2 - ( j * j );
463 /* fsqrt+1 seems to give a closer approximation */
464 y_values[i-1] = LCD_HEIGHT - (fp_sqrt(k, 16)>>8) - 1;
465 rb->yield();
469 void load_settings(void) {
470 int fp = rb->open(PLUGIN_DEMOS_DIR "/.vu_meter", O_RDONLY);
471 if(fp>=0) {
472 rb->read(fp, &vumeter_settings, sizeof(struct saved_settings));
473 rb->close(fp);
475 else {
476 reset_settings();
477 rb->splash(HZ, "Press " LABEL_HELP " for help");
481 void save_settings(void) {
482 int fp = rb->creat(PLUGIN_DEMOS_DIR "/.vu_meter", 0666);
483 if(fp >= 0) {
484 rb->write (fp, &vumeter_settings, sizeof(struct saved_settings));
485 rb->close(fp);
489 void change_volume(int delta) {
490 int minvol = rb->sound_min(SOUND_VOLUME);
491 int maxvol = rb->sound_max(SOUND_VOLUME);
492 int vol = rb->global_settings->volume + delta;
494 if (vol > maxvol) vol = maxvol;
495 else if (vol < minvol) vol = minvol;
496 if (vol != rb->global_settings->volume) {
497 rb->sound_set(SOUND_VOLUME, vol);
498 rb->global_settings->volume = vol;
499 rb->lcd_putsxyf(0,0, "%d", vol);
500 rb->lcd_update();
501 rb->sleep(HZ/12);
505 static bool vu_meter_menu(void)
507 int selection;
508 bool menu_quit = false;
509 bool exit = false;
511 MENUITEM_STRINGLIST(menu,"VU Meter Menu",NULL,"Meter Type","Scale",
512 "Minimeters","Decay Speed","Playback Control",
513 "Quit");
515 static const struct opt_items meter_type_option[2] = {
516 { "Analog", -1 },
517 { "Digital", -1 },
520 static const struct opt_items decay_speed_option[7] = {
521 { "No Decay", -1 },
522 { "Very Fast", -1 },
523 { "Fast", -1 },
524 { "Medium", -1 },
525 { "Medium-Slow", -1 },
526 { "Slow", -1 },
527 { "Very Slow", -1 },
530 while (!menu_quit) {
531 switch(rb->do_menu(&menu, &selection, NULL, false))
533 case 0:
534 rb->set_option("Meter Type", &vumeter_settings.meter_type, INT,
535 meter_type_option, 2, NULL);
536 break;
538 case 1:
539 if(vumeter_settings.meter_type==ANALOG)
541 rb->set_bool_options("Scale", &vumeter_settings.analog_use_db_scale,
542 "dBfs", -1, "Linear", -1, NULL);
544 else
546 rb->set_bool_options("Scale", &vumeter_settings.digital_use_db_scale,
547 "dBfs", -1, "Linear", -1, NULL);
549 break;
551 case 2:
552 if(vumeter_settings.meter_type==ANALOG)
554 rb->set_bool("Enable Minimeters",
555 &vumeter_settings.analog_minimeters);
557 else
559 rb->set_bool("Enable Minimeters",
560 &vumeter_settings.digital_minimeters);
562 break;
564 case 3:
565 if(vumeter_settings.meter_type==ANALOG)
567 rb->set_option("Decay Speed", &vumeter_settings.analog_decay, INT,
568 decay_speed_option, 7, NULL);
570 else
572 rb->set_option("Decay Speed", &vumeter_settings.digital_decay, INT,
573 decay_speed_option, 7, NULL);
575 break;
577 case 4:
578 playback_control(NULL);
579 break;
581 case 5:
582 exit = true;
583 /* fall through to exit the menu */
584 default:
585 menu_quit = true;
586 break;
589 /* the menu uses the userfont, set it back to sysfont */
590 rb->lcd_setfont(FONT_SYSFIXED);
591 return exit;
594 void draw_analog_minimeters(void) {
595 rb->lcd_mono_bitmap(sound_speaker, quarter_width-28, 12, 4, 8);
596 rb->lcd_set_drawmode(DRMODE_FG);
597 if(analog_mini_1<left_needle_top_x)
598 rb->lcd_mono_bitmap(sound_low_level, quarter_width-23, 12, 2, 8);
599 if(analog_mini_2<left_needle_top_x)
600 rb->lcd_mono_bitmap(sound_med_level, quarter_width-21, 12, 2, 8);
601 if(analog_mini_3<left_needle_top_x)
602 rb->lcd_mono_bitmap(sound_high_level, quarter_width-19, 12, 2, 8);
603 if(analog_mini_4<left_needle_top_x)
604 rb->lcd_mono_bitmap(sound_max_level, quarter_width-16, 12, 3, 8);
606 rb->lcd_set_drawmode(DRMODE_SOLID);
607 rb->lcd_mono_bitmap(sound_speaker, quarter_width+half_width-30, 12, 4, 8);
608 rb->lcd_set_drawmode(DRMODE_FG);
609 if(analog_mini_1<(right_needle_top_x-half_width))
610 rb->lcd_mono_bitmap(sound_low_level, quarter_width+half_width-25, 12, 2, 8);
611 if(analog_mini_2<(right_needle_top_x-half_width))
612 rb->lcd_mono_bitmap(sound_med_level, quarter_width+half_width-23, 12, 2, 8);
613 if(analog_mini_3<(right_needle_top_x-half_width))
614 rb->lcd_mono_bitmap(sound_high_level, quarter_width+half_width-21, 12, 2, 8);
615 if(analog_mini_4<(right_needle_top_x-half_width))
616 rb->lcd_mono_bitmap(sound_max_level, quarter_width+half_width-18, 12, 3, 8);
617 rb->lcd_set_drawmode(DRMODE_SOLID);
620 void draw_digital_minimeters(void) {
621 #ifdef HAVE_LCD_COLOR
622 rb->lcd_set_foreground(LCD_RGBPACK(255, 255 - 23 * num_left_leds, 0));
623 #endif
624 rb->lcd_mono_bitmap(sound_speaker, 34, half_height-8, 4, 8);
625 rb->lcd_set_drawmode(DRMODE_FG);
626 if(1<num_left_leds)
627 rb->lcd_mono_bitmap(sound_low_level, 39, half_height-8, 2, 8);
628 if(2<num_left_leds)
629 rb->lcd_mono_bitmap(sound_med_level, 41, half_height-8, 2, 8);
630 if(5<num_left_leds)
631 rb->lcd_mono_bitmap(sound_high_level, 43, half_height-8, 2, 8);
632 if(8<num_left_leds)
633 rb->lcd_mono_bitmap(sound_max_level, 46, half_height-8, 3, 8);
635 #ifdef HAVE_LCD_COLOR
636 rb->lcd_set_foreground(LCD_RGBPACK(255, 255 - 23 * num_right_leds, 0));
637 #endif
638 rb->lcd_set_drawmode(DRMODE_SOLID);
639 rb->lcd_mono_bitmap(sound_speaker, 34, half_height+8, 4, 8);
640 rb->lcd_set_drawmode(DRMODE_FG);
641 if(1<(num_right_leds))
642 rb->lcd_mono_bitmap(sound_low_level, 39, half_height+8, 2, 8);
643 if(2<(num_right_leds))
644 rb->lcd_mono_bitmap(sound_med_level, 41, half_height+8, 2, 8);
645 if(5<(num_right_leds))
646 rb->lcd_mono_bitmap(sound_high_level, 43, half_height+8, 2, 8);
647 if(8<(num_right_leds))
648 rb->lcd_mono_bitmap(sound_max_level, 46, half_height+8, 3, 8);
649 rb->lcd_set_drawmode(DRMODE_SOLID);
651 #ifdef HAVE_LCD_COLOR
652 rb->lcd_set_foreground(screen_foreground);
653 #endif
656 void analog_meter(void) {
658 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
659 int left_peak = rb->mas_codec_readreg(0xC);
660 int right_peak = rb->mas_codec_readreg(0xD);
661 #elif (CONFIG_CODEC == SWCODEC)
662 int left_peak, right_peak;
663 rb->pcm_calculate_peaks(&left_peak, &right_peak);
664 #endif
666 if(vumeter_settings.analog_use_db_scale) {
667 left_needle_top_x = analog_db_scale[left_peak * half_width / MAX_PEAK];
668 right_needle_top_x = analog_db_scale[right_peak * half_width / MAX_PEAK] + half_width;
670 else {
671 left_needle_top_x = left_peak * half_width / MAX_PEAK;
672 right_needle_top_x = right_peak * half_width / MAX_PEAK + half_width;
675 /* Makes a decay on the needle */
676 left_needle_top_x = (left_needle_top_x+last_left_needle_top_x*vumeter_settings.analog_decay)
677 /(vumeter_settings.analog_decay+1);
678 right_needle_top_x = (right_needle_top_x+last_right_needle_top_x*vumeter_settings.analog_decay)
679 /(vumeter_settings.analog_decay+1);
681 last_left_needle_top_x = left_needle_top_x;
682 last_right_needle_top_x = right_needle_top_x;
684 left_needle_top_y = y_values[left_needle_top_x];
685 right_needle_top_y = y_values[right_needle_top_x-half_width];
687 /* Needles */
688 rb->lcd_drawline(quarter_width, LCD_HEIGHT-1, left_needle_top_x, left_needle_top_y);
689 rb->lcd_drawline((quarter_width+half_width), LCD_HEIGHT-1, right_needle_top_x, right_needle_top_y);
691 if(vumeter_settings.analog_minimeters)
692 draw_analog_minimeters();
694 /* Needle covers */
695 rb->lcd_set_drawmode(DRMODE_FG);
696 rb->lcd_mono_bitmap(needle_cover, quarter_width-6, LCD_HEIGHT-5, 13, 5);
697 rb->lcd_mono_bitmap(needle_cover, half_width+quarter_width-6, LCD_HEIGHT-5, 13, 5);
698 rb->lcd_set_drawmode(DRMODE_SOLID);
700 /* Show Left/Right */
701 rb->lcd_putsxy(quarter_width-12, 12, "Left");
702 rb->lcd_putsxy(half_width+quarter_width-12, 12, "Right");
704 /* Line above/below the Left/Right text */
705 rb->lcd_hline(0,LCD_WIDTH-1,9);
706 rb->lcd_hline(0,LCD_WIDTH-1,21);
708 for(i=0; i<half_width; i++) {
709 rb->lcd_drawpixel(i, (y_values[i])-2);
710 rb->lcd_drawpixel(i+half_width, (y_values[i])-2);
714 void digital_meter(void) {
715 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
716 int left_peak = rb->mas_codec_readreg(0xC);
717 int right_peak = rb->mas_codec_readreg(0xD);
718 #elif (CONFIG_CODEC == SWCODEC)
719 int left_peak, right_peak;
720 rb->pcm_calculate_peaks(&left_peak, &right_peak);
721 #endif
723 if(vumeter_settings.digital_use_db_scale) {
724 num_left_leds = digital_db_scale[left_peak * 44 / MAX_PEAK];
725 num_right_leds = digital_db_scale[right_peak * 44 / MAX_PEAK];
727 else {
728 num_left_leds = left_peak * 11 / MAX_PEAK;
729 num_right_leds = right_peak * 11 / MAX_PEAK;
732 num_left_leds = (num_left_leds+last_num_left_leds*vumeter_settings.digital_decay)
733 /(vumeter_settings.digital_decay+1);
734 num_right_leds = (num_right_leds+last_num_right_leds*vumeter_settings.digital_decay)
735 /(vumeter_settings.digital_decay+1);
737 last_num_left_leds = num_left_leds;
738 last_num_right_leds = num_right_leds;
740 rb->lcd_set_drawmode(DRMODE_FG);
741 /* LEDS */
742 for(i=0; i<num_left_leds; i++) {
743 #ifdef HAVE_LCD_COLOR
744 rb->lcd_set_foreground(LCD_RGBPACK(255, 255 - 23 * i, 0));
745 #endif
746 rb->lcd_fillrect((digital_lead + (i*digital_block_width)),
747 14, digital_block_width - digital_block_gap, digital_block_height);
750 for(i=0; i<num_right_leds; i++) {
751 #ifdef HAVE_LCD_COLOR
752 rb->lcd_set_foreground(LCD_RGBPACK(255, 255 - 23 * i, 0));
753 #endif
754 rb->lcd_fillrect((digital_lead + (i*digital_block_width)),
755 (half_height + 20), digital_block_width - digital_block_gap,
756 digital_block_height);
759 #ifdef HAVE_LCD_COLOR
760 rb->lcd_set_foreground(screen_foreground);
761 #endif
762 rb->lcd_set_drawmode(DRMODE_SOLID);
764 if(vumeter_settings.digital_minimeters)
765 draw_digital_minimeters();
767 /* Lines above/below where the LEDS are */
768 rb->lcd_hline(0,LCD_WIDTH-1,12);
769 rb->lcd_hline(0,LCD_WIDTH-1,half_height-12);
771 rb->lcd_hline(0,LCD_WIDTH-1,half_height+18);
772 rb->lcd_hline(0,LCD_WIDTH-1,LCD_HEIGHT-6);
774 /* Show Left/Right */
775 rb->lcd_putsxy(2, half_height-8, "Left");
776 rb->lcd_putsxy(2, half_height+8, "Right");
778 /* Line in the middle */
779 rb->lcd_hline(0,LCD_WIDTH-1,half_height+3);
782 enum plugin_status plugin_start(const void* parameter) {
783 int button;
784 int lastbutton = BUTTON_NONE;
786 (void) parameter;
788 calc_scales();
790 load_settings();
791 rb->lcd_setfont(FONT_SYSFIXED);
792 #ifdef HAVE_LCD_COLOR
793 screen_foreground = rb->lcd_get_foreground();
794 #endif
796 while (1)
798 rb->lcd_clear_display();
800 rb->lcd_putsxy(half_width-23, 0, "VU Meter");
802 if(vumeter_settings.meter_type==ANALOG)
803 analog_meter();
804 else
805 digital_meter();
807 rb->lcd_update();
809 button = rb->button_get_w_tmo(1);
810 switch (button)
812 #ifdef VUMETER_RC_QUIT
813 case VUMETER_RC_QUIT:
814 #endif
815 case VUMETER_QUIT:
816 save_settings();
817 return PLUGIN_OK;
818 break;
820 case VUMETER_HELP:
821 #ifdef VUMETER_HELP_PRE
822 if (lastbutton != VUMETER_HELP_PRE)
823 break;
824 #endif
825 rb->lcd_clear_display();
826 rb->lcd_puts(0, 0, LABEL_QUIT ": Exit");
827 rb->lcd_puts(0, 1, LABEL_MENU ": Settings");
828 rb->lcd_puts(0, 2, LABEL_VOLUME ": Volume");
829 rb->lcd_update();
830 rb->sleep(HZ*3);
831 break;
833 case VUMETER_MENU:
835 #ifdef VUMETER_MENU2
836 case VUMETER_MENU2:
837 #endif
839 #ifdef VUMETER_MENU_PRE
840 if (lastbutton != VUMETER_MENU_PRE)
841 break;
842 #endif
843 if(vu_meter_menu())
844 return PLUGIN_OK;
845 break;
847 case VUMETER_UP:
848 case VUMETER_UP | BUTTON_REPEAT:
849 change_volume(1);
850 break;
852 case VUMETER_DOWN:
853 case VUMETER_DOWN | BUTTON_REPEAT:
854 change_volume(-1);
855 break;
857 default:
858 if(rb->default_event_handler(button) == SYS_USB_CONNECTED)
859 return PLUGIN_USB_CONNECTED;
860 break;
862 if (button != BUTTON_NONE)
863 lastbutton = button;