* removed some more unused stuff in the simulator makefile
[kugel-rb.git] / apps / plugins / metronome.c
blob1647854acd0fdbc622a26612bde99cd8809e13bc
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2004 Matthias Wientapper
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include "plugin.h"
21 #ifndef SIMULATOR
23 static struct plugin_api* rb;
25 static int bpm = 120;
26 static int period = 0;
27 static int minitick = 0;
29 static bool sound_active = false;
30 static bool sound_paused = true;
32 static char buffer[30];
34 /*tick sound from a metronome*/
35 static unsigned char sound[]={
36 255,251, 80,196, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
37 0, 73,110,102,111, 0, 0, 0, 15, 0, 0, 0, 4, 0, 0, 4, 19, 0, 64, 64,
38 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
39 64, 64, 64,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,
40 128,128,128,128,128,128,128,128,192,192,192,192,192,192,192,192,192,192,192,192,
41 192,192,192,192,192,192,192,192,192,192,192,192,192,255,255,255,255,255,255,255,
42 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, 0, 0, 0,
43 58, 76, 65, 77, 69, 51, 46, 57, 50, 32, 1,137, 0, 0, 0, 0, 0, 0, 0, 0,
44 2, 64, 36, 5,191, 65, 0, 0, 0, 0, 0, 4, 19,168,187,153, 93, 0, 0, 0,
45 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
46 0, 0, 0, 0, 0, 0, 0, 0,255,251, 80,196, 0, 0, 10, 81, 57, 65,184,120,
47 128, 1, 95,159,239,191,144, 96, 0, 0,128, 0, 0, 0, 0, 0, 0, 48, 24, 12,
48 0, 0, 15,175,185,222, 39, 7,126, 46, 50,191,243,197,255,247, 62, 92,111,252,
49 115, 15, 27,140,153,159,247,242,124, 44, 92,164,177, 57,175,255,236, 79,164, 98,
50 68,205, 69,160, 64, 15,251,127,128,168, 38, 92,138, 17, 67,196, 80,184,223,255,
51 255,161,117, 33, 55, 83,191,255,193,240, 33, 57,114, 17, 0, 2, 0, 0, 0, 10,
52 191, 10, 8,196,209, 75, 55,236,177,115,238,223,203, 61,176, 89, 78,101,219,118,
53 118, 65,154,126,187,239,241,127,247,245, 59, 18,219, 94,105,245,221,161,173, 91,
54 191, 27,255,247, 18,124, 71,117,139,133,170,221,227,251,135,110,236,255,183,219,
55 100, 78,184,125,212,131, 65, 95,212,245,145, 67, 77, 10,153,250,132,195, 4,138,
56 224, 0, 0, 0,158,107, 42, 65,227,185, 90,158, 86, 6,155, 77,255,251, 82,196,
57 8, 0, 10,136,185,103, 4, 61, 48, 65,118,173, 44, 60,147, 10,152,107, 49, 74,
58 18,208, 91, 23,178,196,220, 25, 64,233,230, 36,170, 0,210,219,131, 40,165, 22,
59 86,195, 23,145, 33, 16,138, 99,109,117,245, 71,162, 69, 42,220,164, 91, 72,165,
60 26, 69,146,150,199,222, 73,102,148, 29, 19, 5, 65, 86, 6,150,120,180, 26, 88,
61 75, 18,191,255,250,171, 4, 8,132,140, 0, 0, 20,186,242,162, 66,100, 62, 86,
62 206, 86,255, 26,203,201,255,255,141,128,166,196,175,133, 9, 70,188,146,175,133,
63 60, 17,217,229, 90, 27, 87, 93, 34, 76,113,185, 84,213, 56, 17,170, 83, 75,180,
64 203,116,101, 93, 90, 99, 35,174,230, 42,125, 12,128, 79, 93, 81,234, 67, 66,137,
65 105,157,157, 23, 71, 53, 40,250,204,105, 89,203,107,250, 63,174, 89,133, 81,140,
66 0, 52, 17, 16, 0, 0, 18,111, 20, 92, 32,213, 48,179, 50, 26,244,179, 27,230,
67 154, 34,133,177, 88,255,251, 82,196, 13, 0, 10,125, 65, 85,244, 85, 0, 1,210,
68 172,231, 7, 31, 32, 0, 37, 11, 68, 69, 13, 37, 48,210, 23, 69, 36,122,143,148,
69 211,141, 44, 76,135, 72,148,195,135,174, 84,149,167, 30,198,183,255,213,111, 61,
70 27,155,191,177,207,246,163,255,230, 30,137,219,255,182,186,185,239, 52,148,171,
71 75, 63,167, 44, 0, 2, 81,190, 41,253,248,182,180, 23, 43,127,217,161, 41,191,
72 36,199, 40,153,108, 66,224,131,192,217, 12, 67,217, 6,218,240,108,112, 63, 33,
73 93, 12, 8, 43, 66, 42, 74, 12,215,135,100,172, 33, 65,172, 29, 98, 4, 83, 34,
74 40, 17,111,133,255, 18,145, 60, 59,136,145, 92,106,144, 34,185, 58, 76,154,254,
75 146,144,115, 4, 84,180,142,178, 38, 79,253, 34,237,105, 31,106, 70, 73, 36,138,
76 73,164,181,127,253,182, 82,217, 26,245,117,163,255,219, 50,115,223,245, 92,198,
77 165,173, 0, 15,249,132, 50, 19,157, 85,121,153,245, 51,255,251, 82,196, 6,131,
78 74, 96,134,156, 92, 19, 0, 8, 0, 0, 52,128, 0, 0, 0, 60,226, 73, 57, 26,
79 249,253,122,211,128, 65, 86, 10, 1, 9,195,128, 36, 72,225,196,146,115, 73, 18,
80 75, 65, 77, 9, 5,200, 43, 16,163,129, 77, 9, 5,200,110, 39,127,255,255,255,
81 255,255,255,255,252, 83, 66,142,140, 21,136, 46, 64,166,133, 28, 12, 21,136, 46,
82 64,166,133, 21, 6, 76, 65, 77, 69, 51, 46, 57, 50, 85, 85, 85, 85, 85, 85, 85,
83 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85,
84 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85,
85 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85,
86 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85,
87 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85,
88 85, 85, 85};
92 void led(bool on){
93 if(on)
94 or_b(0x40, &PBDRL);
95 else
96 and_b(~0x40, &PBDRL);
99 void calc_period(void){
100 period = 61440/bpm-1; // (60*1024)/bpm;
103 void callback(unsigned char** start, int* size){
104 (void)start; /* unused parameter, avoid warning */
105 *size = NULL; /* end of data */
106 sound_active = false;
107 led(0);
110 void play_tock(void){
111 sound_active = true;
112 led(1);
113 rb->mp3_play_data(sound, sizeof(sound), callback);
114 rb->mp3_play_pause(true); /* kickoff audio */
118 void draw_display(void){
119 rb->lcd_clear_display();
121 #ifdef HAVE_LCD_BITMAP
122 rb->lcd_setfont(FONT_SYSFIXED);
123 rb->lcd_putsxy(1, 1, "Metronome");
124 #endif
126 rb->snprintf(buffer, sizeof(buffer), "BPM: %d ",bpm);
127 #ifdef HAVE_LCD_BITMAP
128 rb->lcd_puts(0,7, buffer);
129 #else
130 rb->lcd_puts(0,0, buffer);
131 #endif
132 rb->snprintf(buffer, sizeof(buffer), "Vol: %d",
133 rb->global_settings->volume);
134 #ifdef HAVE_LCD_BITMAP
135 rb->lcd_puts(10, 7, buffer);
136 #else
137 rb->lcd_puts(0,1, buffer);
138 #endif
140 #ifdef HAVE_LCD_BITMAP
141 rb->lcd_drawline(0, 12, 111, 12);
142 if(sound_paused)
143 rb->lcd_puts(0,2,"press play");
144 else
145 rb->lcd_puts(0,2,"press pause");
146 rb->lcd_update();
147 #endif
150 /* helper function to change the volume by a certain amount, +/-
151 ripped from video.c */
152 void change_volume(int delta){
153 int vol = rb->global_settings->volume + delta;
154 char buffer[30];
155 if (vol > 100) vol = 100;
156 else if (vol < 0) vol = 0;
157 if (vol != rb->global_settings->volume) {
158 rb->mpeg_sound_set(SOUND_VOLUME, vol);
159 rb->global_settings->volume = vol;
160 rb->snprintf(buffer, sizeof(buffer), "Vol: %d ", vol);
161 #ifdef HAVE_LCD_BITMAP
162 rb->lcd_puts(10,7, buffer);
163 rb->lcd_update();
164 #else
165 rb->lcd_puts(0,1, buffer);
166 #endif
170 void timer_callback(void){
171 if(minitick>=period){
172 minitick = 0;
173 if(!sound_active && !sound_paused){
174 play_tock();
177 else {
178 minitick++;
183 enum plugin_status plugin_start(struct plugin_api* api, void* parameter){
185 TEST_PLUGIN_API(api);
186 (void)parameter;
187 rb = api;
189 rb->bitswap(sound, sizeof(sound));
191 if (rb->mp3_is_playing())
192 rb->mp3_play_stop(); // stop audio ISR
194 calc_period();
195 rb->plugin_register_timer((FREQ/1024), 1, timer_callback);
197 draw_display();
199 /* main loop */
200 while (true){
202 switch (rb->button_get(false)) {
203 #ifdef HAVE_RECORDER_KEYPAD
204 case BUTTON_OFF:
205 #else
206 case BUTTON_STOP:
207 #endif
208 /* get out of here */
209 rb->mp3_play_stop(); /* stop audio ISR */
210 rb->plugin_unregister_timer();
211 led(0);
212 return PLUGIN_OK;
214 case BUTTON_PLAY:
215 if(sound_paused)
216 sound_paused = false;
217 else
218 sound_paused = true;
219 calc_period();
220 draw_display();
221 break;
223 #ifdef HAVE_RECORDER_KEYPAD
224 case BUTTON_UP:
225 case BUTTON_UP | BUTTON_REPEAT:
226 #else
227 case BUTTON_ON | BUTTON_RIGHT:
228 case BUTTON_ON | BUTTON_RIGHT | BUTTON_REPEAT:
229 #endif
230 change_volume(1);
231 calc_period();
232 break;
234 #ifdef HAVE_RECORDER_KEYPAD
235 case BUTTON_DOWN:
236 case BUTTON_DOWN | BUTTON_REPEAT:
237 #else
238 case BUTTON_ON | BUTTON_LEFT:
239 case BUTTON_ON | BUTTON_LEFT | BUTTON_REPEAT:
240 #endif
241 change_volume(-1);
242 calc_period();
243 break;
245 case BUTTON_LEFT:
246 if (bpm > 1)
247 bpm--;
248 calc_period();
249 draw_display();
250 break;
252 case BUTTON_LEFT | BUTTON_REPEAT:
253 if (bpm > 10)
254 bpm=bpm-10;
255 calc_period();
256 draw_display();
257 break;
259 case BUTTON_RIGHT:
260 if(bpm < 400)
261 bpm++;
262 calc_period();
263 draw_display();
264 break;
266 case BUTTON_RIGHT | BUTTON_REPEAT:
267 if (bpm < 400)
268 bpm=bpm+10;
269 calc_period();
270 draw_display();
271 break;
273 case SYS_USB_CONNECTED:
274 rb->plugin_unregister_timer();
275 rb->usb_screen();
276 return PLUGIN_USB_CONNECTED;
280 #endif /* #ifndef SIMULATOR */