blob: 6e3b9515f23fa73a6cf45536acf1a092336905be [file] [log] [blame]
Linus Nielsen Feltzing1fcd8ce2004-03-12 08:21:26 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2004 Matthias Wientapper
11 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000012 * 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.
Linus Nielsen Feltzing1fcd8ce2004-03-12 08:21:26 +000016 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
Jonathan Gordon7b8b0ff2006-11-16 02:53:44 +000021#include "pluginlib_actions.h"
Kevin Ferrare81aac2a2007-07-29 03:43:37 +000022#include "metronome.h"
Linus Nielsen Feltzing1fcd8ce2004-03-12 08:21:26 +000023
Jens Arnolda36b1d42006-01-15 18:20:18 +000024PLUGIN_HEADER
Kevin Ferrare81aac2a2007-07-29 03:43:37 +000025#define METRONOME_QUIT PLA_QUIT
Kevin Ferrareebe5acf2008-01-23 08:25:04 +000026#define METRONOME_VOL_UP PLA_INC
27#define METRONOME_VOL_DOWN PLA_DEC
28#define METRONOME_VOL_UP_REP PLA_INC_REPEAT
29#define METRONOME_VOL_DOWN_REP PLA_DEC_REPEAT
Kevin Ferrare81aac2a2007-07-29 03:43:37 +000030#define METRONOME_LEFT PLA_LEFT
31#define METRONOME_RIGHT PLA_RIGHT
Jonathan Gordon10b00132006-11-17 00:50:35 +000032#define METRONOME_LEFT_REP PLA_LEFT_REPEAT
33#define METRONOME_RIGHT_REP PLA_RIGHT_REPEAT
Jonathan Gordon7b8b0ff2006-11-16 02:53:44 +000034enum {
35 METRONOME_PLAY_TAP = LAST_PLUGINLIB_ACTION+1,
Jonathan Gordonfee5ca22006-11-26 07:20:04 +000036#if CONFIG_KEYPAD == ONDIO_PAD
Jonathan Gordon7b8b0ff2006-11-16 02:53:44 +000037 METRONOME_PAUSE,
Kevin Ferrarea2b14a52007-07-29 05:30:04 +000038#endif /* ONDIO_PAD */
Jonathan Gordonfee5ca22006-11-26 07:20:04 +000039#if (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
Jonathan Gordon7b8b0ff2006-11-16 02:53:44 +000040 METRONOME_SYNC
Kevin Ferrarea2b14a52007-07-29 05:30:04 +000041#endif /* IRIVER_H100_PAD||IRIVER_H300_PAD */
Jonathan Gordon7b8b0ff2006-11-16 02:53:44 +000042};
Jens Arnoldc9e77262004-10-12 23:45:25 +000043
Jonathan Gordon7b8b0ff2006-11-16 02:53:44 +000044
45#if CONFIG_KEYPAD == ONDIO_PAD
Jonathan Gordonfee5ca22006-11-26 07:20:04 +000046#define METRONOME_TAP PLA_START
Jens Arnoldb8c9a0c2004-12-26 14:56:05 +000047#define METRONOME_MSG_START "start: mode"
48#define METRONOME_MSG_STOP "pause: hold mode"
Jonathan Gordon7b8b0ff2006-11-16 02:53:44 +000049static const struct button_mapping ondio_action[] =
50{
Jonathan Gordon10b00132006-11-17 00:50:35 +000051 {METRONOME_PLAY_TAP, BUTTON_MENU|BUTTON_REL, BUTTON_MENU },
Jonathan Gordonfee5ca22006-11-26 07:20:04 +000052 {METRONOME_PAUSE, BUTTON_MENU|BUTTON_REPEAT, BUTTON_NONE },
53 {CONTEXT_CUSTOM,BUTTON_NONE,BUTTON_NONE}
Jonathan Gordon7b8b0ff2006-11-16 02:53:44 +000054};
Kevin Ferrarea2b14a52007-07-29 05:30:04 +000055#else /* !ONDIO_PAD */
Jonathan Gordonfee5ca22006-11-26 07:20:04 +000056#define METRONOME_TAP PLA_FIRE
57#define METRONOME_PLAYPAUSE PLA_START
Daniel Stenberga00619f2005-02-04 10:02:42 +000058#define METRONOME_MSG_START "press play"
59#define METRONOME_MSG_STOP "press pause"
Dave Chapmanccf32832006-01-10 08:59:08 +000060
Jonathan Gordon7b8b0ff2006-11-16 02:53:44 +000061#if (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
Jonathan Gordon148c5532006-12-04 01:04:14 +000062#define MET_SYNC
Martin Scarratt16122752006-12-03 16:49:36 +000063static const struct button_mapping iriver_syncaction[] =
Jonathan Gordon7b8b0ff2006-11-16 02:53:44 +000064{
65 {METRONOME_SYNC, BUTTON_REC, BUTTON_NONE },
Jonathan Gordonfee5ca22006-11-26 07:20:04 +000066 {CONTEXT_CUSTOM,BUTTON_NONE,BUTTON_NONE}
Jonathan Gordon7b8b0ff2006-11-16 02:53:44 +000067};
Kevin Ferrarea2b14a52007-07-29 05:30:04 +000068#endif /* IRIVER_H100_PAD||IRIVER_H300_PAD */
Jonathan Gordon7b8b0ff2006-11-16 02:53:44 +000069#endif /* #if CONFIG_KEYPAD == ONDIO_PAD */
Linus Nielsen Feltzing81c9d582006-01-10 02:51:50 +000070
Kevin Ferrarea2b14a52007-07-29 05:30:04 +000071const struct button_mapping *plugin_contexts[]={
Kevin Ferrareebe5acf2008-01-23 08:25:04 +000072 generic_increase_decrease,
Kevin Ferrarea2b14a52007-07-29 05:30:04 +000073 generic_directions,
74#if CONFIG_KEYPAD == ONDIO_PAD
75 ondio_action,
76#elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
77 iriver_syncaction,
78#endif
79 generic_actions
80};
Kevin Ferrareebe5acf2008-01-23 08:25:04 +000081#define PLA_ARRAY_COUNT sizeof(plugin_contexts)/sizeof(plugin_contexts[0])
Linus Nielsen Feltzing81c9d582006-01-10 02:51:50 +000082
Steve Bavin65265772008-05-13 09:57:56 +000083static const struct plugin_api* rb;
Linus Nielsen Feltzing1fcd8ce2004-03-12 08:21:26 +000084
Nils Wallméniusc7f9ca42007-06-13 15:35:07 +000085MEM_FUNCTION_WRAPPERS(rb);
86
Linus Nielsen Feltzing4bd27022004-05-26 06:56:18 +000087static int bpm = 120;
88static int period = 0;
89static int minitick = 0;
Linus Nielsen Feltzing1fcd8ce2004-03-12 08:21:26 +000090
91static bool sound_active = false;
92static bool sound_paused = true;
Linus Nielsen Feltzingc5067bf2004-03-17 14:54:32 +000093
Linus Nielsen Feltzing4bd27022004-05-26 06:56:18 +000094static char buffer[30];
Linus Nielsen Feltzing1fcd8ce2004-03-12 08:21:26 +000095
Kevin Ferrarea2b14a52007-07-29 05:30:04 +000096static bool reset_tap = false;
Linus Nielsen Feltzing569b9982004-11-26 13:31:40 +000097static int tap_count = 0;
98static int tap_time = 0;
99static int tap_timeout = 0;
100
Linus Nielsen Feltzingc7c90692006-01-14 02:39:30 +0000101int bpm_step_counter = 0;
102
Linus Nielsen Feltzing81c9d582006-01-10 02:51:50 +0000103#if CONFIG_CODEC != SWCODEC
104
105#define MET_IS_PLAYING rb->mp3_is_playing()
106#define MET_PLAY_STOP rb->mp3_play_stop()
107
Nils Wallméniusc7f9ca42007-06-13 15:35:07 +0000108void callback(unsigned char** start, size_t* size){
Jörg Hohensohnfd5714b2004-03-19 22:15:52 +0000109 (void)start; /* unused parameter, avoid warning */
Marianne Arnold071c2ac2008-01-13 18:27:54 +0000110 *size = 0; /* end of data */
Linus Nielsen Feltzing1fcd8ce2004-03-12 08:21:26 +0000111 sound_active = false;
Kevin Ferrare81aac2a2007-07-29 03:43:37 +0000112 rb->led(0);
Linus Nielsen Feltzing1fcd8ce2004-03-12 08:21:26 +0000113}
114
115void play_tock(void){
116 sound_active = true;
Kevin Ferrare81aac2a2007-07-29 03:43:37 +0000117 rb->led(1);
Linus Nielsen Feltzing1fcd8ce2004-03-12 08:21:26 +0000118 rb->mp3_play_data(sound, sizeof(sound), callback);
119 rb->mp3_play_pause(true); /* kickoff audio */
120}
121
Kevin Ferrarea2b14a52007-07-29 05:30:04 +0000122#else /* CONFIG_CODEC == SWCODEC */
Linus Nielsen Feltzing1fcd8ce2004-03-12 08:21:26 +0000123
Linus Nielsen Feltzing81c9d582006-01-10 02:51:50 +0000124#define MET_IS_PLAYING rb->pcm_is_playing()
Linus Nielsen Feltzingc7c90692006-01-14 02:39:30 +0000125#define MET_PLAY_STOP rb->audio_stop()
Linus Nielsen Feltzing81c9d582006-01-10 02:51:50 +0000126
Linus Nielsen Feltzing81c9d582006-01-10 02:51:50 +0000127
128int tock;
Miika Pekkarinen0569fae2007-04-12 17:58:42 +0000129bool need_to_play = false;
Linus Nielsen Feltzing81c9d582006-01-10 02:51:50 +0000130
Linus Nielsen Feltzingbefd0ac2006-01-10 22:22:19 +0000131short sndbuf[sizeof(sound)*2];
132
133/* Convert the mono "tock" sample to interleaved stereo */
134void prepare_tock(void)
135{
136 int i;
137 for(i = 0;i < (int)sizeof(sound)/2;i++) {
138 sndbuf[i*2] = sound[i];
139 sndbuf[i*2+1] = sound[i];
140 }
141}
142
Linus Nielsen Feltzing81c9d582006-01-10 02:51:50 +0000143void play_tock(void) {
Brandon Low413da2a2006-02-07 20:38:55 +0000144 rb->pcm_play_data(NULL,(unsigned char *)sndbuf,sizeof(sndbuf));
Linus Nielsen Feltzing81c9d582006-01-10 02:51:50 +0000145 tock++;
146}
147
Kevin Ferrarea2b14a52007-07-29 05:30:04 +0000148#endif /* CONFIG_CODEC != SWCODEC */
Linus Nielsen Feltzing81c9d582006-01-10 02:51:50 +0000149
150void calc_period(void)
151{
Daniel Stenberg220fafd2006-03-03 08:14:44 +0000152 period = 61440/bpm-1; /* (60*1024)/bpm; */
Linus Nielsen Feltzing81c9d582006-01-10 02:51:50 +0000153}
154
155
Kevin Ferrare81aac2a2007-07-29 03:43:37 +0000156void metronome_draw(struct screen* display)
Linus Nielsen Feltzing81c9d582006-01-10 02:51:50 +0000157{
Kevin Ferrare81aac2a2007-07-29 03:43:37 +0000158 display->clear_display();
Linus Nielsen Feltzing1fcd8ce2004-03-12 08:21:26 +0000159
160#ifdef HAVE_LCD_BITMAP
Kevin Ferrare81aac2a2007-07-29 03:43:37 +0000161 display->setfont(FONT_SYSFIXED);
162 display->puts(0, 0, "Metronome");
163 if(display->screen_type==SCREEN_MAIN)
164 {
165 display->puts(0, 5, "Select to TAP");
166 display->puts(0, 6, "Rec to SYNC");
167 }
Linus Nielsen Feltzing81c9d582006-01-10 02:51:50 +0000168#ifdef HAVE_REMOTE_LCD
Kevin Ferrare81aac2a2007-07-29 03:43:37 +0000169 else
170 {
171 display->puts(0, 5, "Rec to TAP");
172 display->puts(0, 6, "Mode to SYNC");
173 }
174#endif
Kevin Ferrarea2b14a52007-07-29 05:30:04 +0000175#endif /* HAVE_LCD_BITMAP */
Linus Nielsen Feltzingc5067bf2004-03-17 14:54:32 +0000176
Linus Nielsen Feltzing1fcd8ce2004-03-12 08:21:26 +0000177 rb->snprintf(buffer, sizeof(buffer), "BPM: %d ",bpm);
178#ifdef HAVE_LCD_BITMAP
Kevin Ferrare81aac2a2007-07-29 03:43:37 +0000179 display->puts(0,3, buffer);
Linus Nielsen Feltzing1fcd8ce2004-03-12 08:21:26 +0000180#else
Kevin Ferrare81aac2a2007-07-29 03:43:37 +0000181 display->puts(0,0, buffer);
Kevin Ferrarea2b14a52007-07-29 05:30:04 +0000182#endif /* HAVE_LCD_BITMAP */
Linus Nielsen Feltzing81c9d582006-01-10 02:51:50 +0000183
Linus Nielsen Feltzing1fcd8ce2004-03-12 08:21:26 +0000184 rb->snprintf(buffer, sizeof(buffer), "Vol: %d",
185 rb->global_settings->volume);
186#ifdef HAVE_LCD_BITMAP
Kevin Ferrare81aac2a2007-07-29 03:43:37 +0000187 display->puts(10, 3, buffer);
Linus Nielsen Feltzing1fcd8ce2004-03-12 08:21:26 +0000188#else
Kevin Ferraref3114f32007-07-29 04:01:46 +0000189 display->puts(0,1, buffer);
Kevin Ferrarea2b14a52007-07-29 05:30:04 +0000190#endif /* HAVE_LCD_BITMAP */
Linus Nielsen Feltzing1fcd8ce2004-03-12 08:21:26 +0000191
192#ifdef HAVE_LCD_BITMAP
Jens Arnoldfef82552008-04-12 09:51:16 +0000193 display->hline(0, 111, 12);
Linus Nielsen Feltzing1fcd8ce2004-03-12 08:21:26 +0000194 if(sound_paused)
Kevin Ferrare81aac2a2007-07-29 03:43:37 +0000195 display->puts(0,2,METRONOME_MSG_START);
Linus Nielsen Feltzing1fcd8ce2004-03-12 08:21:26 +0000196 else
Kevin Ferrare81aac2a2007-07-29 03:43:37 +0000197 display->puts(0,2,METRONOME_MSG_STOP);
Jonathan Gordon44047242008-03-09 02:36:17 +0000198 display->setfont(FONT_UI);
Kevin Ferrarea2b14a52007-07-29 05:30:04 +0000199#endif /* HAVE_LCD_BITMAP */
Kevin Ferrare81aac2a2007-07-29 03:43:37 +0000200 display->update();
Linus Nielsen Feltzing1fcd8ce2004-03-12 08:21:26 +0000201}
202
Kevin Ferrare4f084832007-07-29 04:16:12 +0000203void draw_display(void)
Kevin Ferrare81aac2a2007-07-29 03:43:37 +0000204{
205 int i;
206 FOR_NB_SCREENS(i)
207 metronome_draw(rb->screens[i]);
208}
Kevin Ferrarea2b14a52007-07-29 05:30:04 +0000209
Linus Nielsen Feltzingc5067bf2004-03-17 14:54:32 +0000210/* helper function to change the volume by a certain amount, +/-
211 ripped from video.c */
Linus Nielsen Feltzing1fcd8ce2004-03-12 08:21:26 +0000212void change_volume(int delta){
Jens Arnoldc80e0c12005-12-17 21:13:30 +0000213 int minvol = rb->sound_min(SOUND_VOLUME);
214 int maxvol = rb->sound_max(SOUND_VOLUME);
Linus Nielsen Feltzing1fcd8ce2004-03-12 08:21:26 +0000215 int vol = rb->global_settings->volume + delta;
Jens Arnoldc80e0c12005-12-17 21:13:30 +0000216
217 if (vol > maxvol) vol = maxvol;
218 else if (vol < minvol) vol = minvol;
Linus Nielsen Feltzing1fcd8ce2004-03-12 08:21:26 +0000219 if (vol != rb->global_settings->volume) {
Linus Nielsen Feltzing674eaca2005-04-01 13:41:03 +0000220 rb->sound_set(SOUND_VOLUME, vol);
Linus Nielsen Feltzing1fcd8ce2004-03-12 08:21:26 +0000221 rb->global_settings->volume = vol;
Linus Nielsen Feltzing81c9d582006-01-10 02:51:50 +0000222 draw_display();
Linus Nielsen Feltzing1fcd8ce2004-03-12 08:21:26 +0000223 }
224}
225
Linus Nielsen Feltzingc7c90692006-01-14 02:39:30 +0000226/*function to accelerate bpm change*/
227void change_bpm(int direction){
228 if((bpm_step_counter < 20)
229 || (bpm > 389)
230 || (bpm < 10))
231 bpm = bpm + direction;
232 else if (bpm_step_counter < 60)
233 bpm = bpm + direction * 2;
234 else
235 bpm = bpm + direction * 9;
Kevin Ferrarea2b14a52007-07-29 05:30:04 +0000236
Linus Nielsen Feltzingc7c90692006-01-14 02:39:30 +0000237 if (bpm > 400) bpm = 400;
238 if (bpm < 1) bpm = 1;
239 calc_period();
240 draw_display();
241 bpm_step_counter++;
Kevin Ferrarea2b14a52007-07-29 05:30:04 +0000242}
Linus Nielsen Feltzingc7c90692006-01-14 02:39:30 +0000243
Linus Nielsen Feltzing81c9d582006-01-10 02:51:50 +0000244void timer_callback(void)
245{
246 if(minitick >= period){
247 minitick = 0;
248 if(!sound_active && !sound_paused && !tap_count) {
Miika Pekkarinen0569fae2007-04-12 17:58:42 +0000249#if CONFIG_CODEC == SWCODEC
250 /* On SWCODEC we can't call play_tock() directly from an ISR. */
251 need_to_play = true;
252#else
Linus Nielsen Feltzing81c9d582006-01-10 02:51:50 +0000253 play_tock();
Miika Pekkarinen0569fae2007-04-12 17:58:42 +0000254#endif
Linus Nielsen Feltzing81c9d582006-01-10 02:51:50 +0000255 rb->reset_poweroff_timer();
256 }
Jens Arnold0df29b22004-08-28 09:07:21 +0000257 }
Linus Nielsen Feltzing4bd27022004-05-26 06:56:18 +0000258 else {
Linus Nielsen Feltzing81c9d582006-01-10 02:51:50 +0000259 minitick++;
Linus Nielsen Feltzing4bd27022004-05-26 06:56:18 +0000260 }
Kevin Ferrarea2b14a52007-07-29 05:30:04 +0000261
Linus Nielsen Feltzing569b9982004-11-26 13:31:40 +0000262 if (tap_count) {
263 tap_time++;
264 if (tap_count > 1 && tap_time > tap_timeout)
265 tap_count = 0;
266 }
Linus Nielsen Feltzing4bd27022004-05-26 06:56:18 +0000267}
268
Jens Arnoldc9e77262004-10-12 23:45:25 +0000269void cleanup(void *parameter)
270{
271 (void)parameter;
272
Jens Arnolde44372e2005-07-26 20:01:11 +0000273 rb->timer_unregister();
Linus Nielsen Feltzing81c9d582006-01-10 02:51:50 +0000274 MET_PLAY_STOP; /* stop audio ISR */
Kevin Ferrare81aac2a2007-07-29 03:43:37 +0000275 rb->led(0);
Jens Arnoldc9e77262004-10-12 23:45:25 +0000276}
Linus Nielsen Feltzing4bd27022004-05-26 06:56:18 +0000277
Linus Nielsen Feltzing569b9982004-11-26 13:31:40 +0000278void tap(void)
279{
Miika Pekkarinen0569fae2007-04-12 17:58:42 +0000280 if (tap_count == 0 || tap_time < tap_count) {
Linus Nielsen Feltzing569b9982004-11-26 13:31:40 +0000281 tap_time = 0;
282 }
283 else {
284 if (tap_time > 0) {
285 bpm = 61440/(tap_time/tap_count);
Kevin Ferrarea2b14a52007-07-29 05:30:04 +0000286
Linus Nielsen Feltzing569b9982004-11-26 13:31:40 +0000287 if (bpm > 400)
288 bpm = 400;
289 }
Kevin Ferrarea2b14a52007-07-29 05:30:04 +0000290
Linus Nielsen Feltzing569b9982004-11-26 13:31:40 +0000291 calc_period();
292 draw_display();
Kevin Ferrarea2b14a52007-07-29 05:30:04 +0000293
Linus Nielsen Feltzing569b9982004-11-26 13:31:40 +0000294 tap_timeout = (tap_count+2)*tap_time/tap_count;
295 }
Kevin Ferrarea2b14a52007-07-29 05:30:04 +0000296
Linus Nielsen Feltzing569b9982004-11-26 13:31:40 +0000297 tap_count++;
298 minitick = 0; /* sync tock to tapping */
Linus Nielsen Feltzing81c9d582006-01-10 02:51:50 +0000299 play_tock();
300
Linus Nielsen Feltzing569b9982004-11-26 13:31:40 +0000301 reset_tap = false;
302}
Linus Nielsen Feltzing1fcd8ce2004-03-12 08:21:26 +0000303
Steve Bavin65265772008-05-13 09:57:56 +0000304enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter){
Jens Arnoldc9e77262004-10-12 23:45:25 +0000305 int button;
Michael Sevakis4597ebe2007-06-10 02:10:47 +0000306 enum plugin_status status;
Jens Arnoldc9e77262004-10-12 23:45:25 +0000307
Linus Nielsen Feltzing1fcd8ce2004-03-12 08:21:26 +0000308 (void)parameter;
309 rb = api;
310
Linus Nielsen Feltzing81c9d582006-01-10 02:51:50 +0000311 if (MET_IS_PLAYING)
Daniel Stenberg220fafd2006-03-03 08:14:44 +0000312 MET_PLAY_STOP; /* stop audio IS */
Kevin Ferrarea2b14a52007-07-29 05:30:04 +0000313
Linus Nielsen Feltzing81c9d582006-01-10 02:51:50 +0000314#if CONFIG_CODEC != SWCODEC
Linus Nielsen Feltzing1fcd8ce2004-03-12 08:21:26 +0000315 rb->bitswap(sound, sizeof(sound));
Linus Nielsen Feltzing81c9d582006-01-10 02:51:50 +0000316#else
Linus Nielsen Feltzingbefd0ac2006-01-10 22:22:19 +0000317 prepare_tock();
Michael Sevakis4597ebe2007-06-10 02:10:47 +0000318#if INPUT_SRC_CAPS != 0
319 /* Select playback */
320 rb->audio_set_input_source(AUDIO_SRC_PLAYBACK, SRCF_PLAYBACK);
321 rb->audio_set_output_source(AUDIO_SRC_PLAYBACK);
322#endif
323 rb->pcm_set_frequency(SAMPR_44);
Kevin Ferrarea2b14a52007-07-29 05:30:04 +0000324#endif /* CONFIG_CODEC != SWCODEC */
Linus Nielsen Feltzing1fcd8ce2004-03-12 08:21:26 +0000325
Linus Nielsen Feltzing569b9982004-11-26 13:31:40 +0000326 calc_period();
Jens Arnoldac9b9272008-04-04 19:38:46 +0000327 rb->timer_register(1, NULL, TIMER_FREQ/1024, 1, timer_callback IF_COP(, CPU));
Kevin Ferrarea2b14a52007-07-29 05:30:04 +0000328
Linus Nielsen Feltzing569b9982004-11-26 13:31:40 +0000329 draw_display();
Linus Nielsen Feltzing1fcd8ce2004-03-12 08:21:26 +0000330
331 /* main loop */
332 while (true){
Linus Nielsen Feltzing569b9982004-11-26 13:31:40 +0000333 reset_tap = true;
Miika Pekkarinen0569fae2007-04-12 17:58:42 +0000334#if CONFIG_CODEC == SWCODEC
335 button = pluginlib_getaction(rb,1,plugin_contexts,PLA_ARRAY_COUNT);
336 if (need_to_play)
337 {
338 need_to_play = false;
339 play_tock();
340 }
341#else
Jonathan Gordon7b8b0ff2006-11-16 02:53:44 +0000342 button = pluginlib_getaction(rb,TIMEOUT_BLOCK,
343 plugin_contexts,PLA_ARRAY_COUNT);
Kevin Ferrarea2b14a52007-07-29 05:30:04 +0000344#endif /* SWCODEC */
Linus Nielsen Feltzing569b9982004-11-26 13:31:40 +0000345 switch (button) {
Jens Arnoldc9e77262004-10-12 23:45:25 +0000346
Linus Nielsen Feltzing569b9982004-11-26 13:31:40 +0000347 case METRONOME_QUIT:
348 /* get out of here */
349 cleanup(NULL);
Michael Sevakis4597ebe2007-06-10 02:10:47 +0000350 status = PLUGIN_OK;
351 goto metronome_exit;
Linus Nielsen Feltzing1fcd8ce2004-03-12 08:21:26 +0000352
Linus Nielsen Feltzing569b9982004-11-26 13:31:40 +0000353#if CONFIG_KEYPAD == ONDIO_PAD
354 case METRONOME_PLAY_TAP:
355 if(sound_paused) {
356 sound_paused = false;
357 calc_period();
358 draw_display();
359 }
360 else
361 tap();
362 break;
Kevin Ferrarea2b14a52007-07-29 05:30:04 +0000363
Linus Nielsen Feltzing569b9982004-11-26 13:31:40 +0000364 case METRONOME_PAUSE:
Jens Arnolda5a6fa92004-11-29 00:19:51 +0000365 if(!sound_paused) {
Linus Nielsen Feltzing569b9982004-11-26 13:31:40 +0000366 sound_paused = true;
Jens Arnolda5a6fa92004-11-29 00:19:51 +0000367 draw_display();
Kevin Ferrarea2b14a52007-07-29 05:30:04 +0000368 }
Linus Nielsen Feltzing569b9982004-11-26 13:31:40 +0000369 break;
Kevin Ferrarea2b14a52007-07-29 05:30:04 +0000370
Linus Nielsen Feltzing569b9982004-11-26 13:31:40 +0000371#else
372 case METRONOME_PLAYPAUSE:
373 if(sound_paused)
374 sound_paused = false;
375 else
376 sound_paused = true;
377 calc_period();
378 draw_display();
379 break;
Kevin Ferrarea2b14a52007-07-29 05:30:04 +0000380#endif /* ONDIO_PAD */
381
Linus Nielsen Feltzing569b9982004-11-26 13:31:40 +0000382 case METRONOME_VOL_UP:
Jonathan Gordon7b8b0ff2006-11-16 02:53:44 +0000383 case METRONOME_VOL_UP_REP:
Linus Nielsen Feltzing569b9982004-11-26 13:31:40 +0000384 change_volume(1);
385 calc_period();
386 break;
Kevin Ferrarea2b14a52007-07-29 05:30:04 +0000387
Linus Nielsen Feltzing569b9982004-11-26 13:31:40 +0000388 case METRONOME_VOL_DOWN:
Jonathan Gordon7b8b0ff2006-11-16 02:53:44 +0000389 case METRONOME_VOL_DOWN_REP:
Linus Nielsen Feltzing569b9982004-11-26 13:31:40 +0000390 change_volume(-1);
391 calc_period();
392 break;
Linus Nielsen Feltzing1fcd8ce2004-03-12 08:21:26 +0000393
Jonathan Gordon10b00132006-11-17 00:50:35 +0000394 case METRONOME_LEFT:
Linus Nielsen Feltzingc7c90692006-01-14 02:39:30 +0000395 bpm_step_counter = 0;
Jonathan Gordon10b00132006-11-17 00:50:35 +0000396 case METRONOME_LEFT_REP:
Linus Nielsen Feltzingc7c90692006-01-14 02:39:30 +0000397 change_bpm(-1);
Linus Nielsen Feltzing569b9982004-11-26 13:31:40 +0000398 break;
Linus Nielsen Feltzing1fcd8ce2004-03-12 08:21:26 +0000399
Jonathan Gordon10b00132006-11-17 00:50:35 +0000400 case METRONOME_RIGHT:
Linus Nielsen Feltzingc7c90692006-01-14 02:39:30 +0000401 bpm_step_counter = 0;
Jonathan Gordon10b00132006-11-17 00:50:35 +0000402 case METRONOME_RIGHT_REP:
Linus Nielsen Feltzingc7c90692006-01-14 02:39:30 +0000403 change_bpm(1);
Linus Nielsen Feltzing569b9982004-11-26 13:31:40 +0000404 break;
Linus Nielsen Feltzing1fcd8ce2004-03-12 08:21:26 +0000405
Jonathan Gordonfee5ca22006-11-26 07:20:04 +0000406#ifdef METRONOME_TAP
Linus Nielsen Feltzing569b9982004-11-26 13:31:40 +0000407 case METRONOME_TAP:
408 tap();
409 break;
410#endif
Linus Nielsen Feltzing81c9d582006-01-10 02:51:50 +0000411
Jonathan Gordon148c5532006-12-04 01:04:14 +0000412#ifdef MET_SYNC
Linus Nielsen Feltzing81c9d582006-01-10 02:51:50 +0000413 case METRONOME_SYNC:
414 minitick = period;
415 break;
416#endif
417
Linus Nielsen Feltzing569b9982004-11-26 13:31:40 +0000418 default:
419 if (rb->default_event_handler_ex(button, cleanup, NULL)
420 == SYS_USB_CONNECTED)
Michael Sevakis4597ebe2007-06-10 02:10:47 +0000421 {
422 status = PLUGIN_USB_CONNECTED;
423 goto metronome_exit;
424 }
Linus Nielsen Feltzing569b9982004-11-26 13:31:40 +0000425 reset_tap = false;
426 break;
Jens Arnoldcc8cff22004-10-16 00:07:43 +0000427
Linus Nielsen Feltzing569b9982004-11-26 13:31:40 +0000428 }
429 if (reset_tap) {
430 tap_count = 0;
431 }
Linus Nielsen Feltzing1fcd8ce2004-03-12 08:21:26 +0000432 }
Michael Sevakis4597ebe2007-06-10 02:10:47 +0000433
434metronome_exit:
Michael Sevakis43548782007-06-10 02:19:39 +0000435#if CONFIG_CODEC == SWCODEC
Michael Sevakis4597ebe2007-06-10 02:10:47 +0000436 rb->pcm_set_frequency(HW_SAMPR_DEFAULT);
Michael Sevakis43548782007-06-10 02:19:39 +0000437#endif
Michael Sevakis4597ebe2007-06-10 02:10:47 +0000438 return status;
Linus Nielsen Feltzing1fcd8ce2004-03-12 08:21:26 +0000439}