Linus Nielsen Feltzing | 1fcd8ce | 2004-03-12 08:21:26 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2004 Matthias Wientapper |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 12 | * 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 Feltzing | 1fcd8ce | 2004-03-12 08:21:26 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
Jonathan Gordon | 7b8b0ff | 2006-11-16 02:53:44 +0000 | [diff] [blame] | 21 | #include "pluginlib_actions.h" |
Kevin Ferrare | 81aac2a | 2007-07-29 03:43:37 +0000 | [diff] [blame] | 22 | #include "metronome.h" |
Linus Nielsen Feltzing | 1fcd8ce | 2004-03-12 08:21:26 +0000 | [diff] [blame] | 23 | |
Jens Arnold | a36b1d4 | 2006-01-15 18:20:18 +0000 | [diff] [blame] | 24 | PLUGIN_HEADER |
Kevin Ferrare | 81aac2a | 2007-07-29 03:43:37 +0000 | [diff] [blame] | 25 | #define METRONOME_QUIT PLA_QUIT |
Kevin Ferrare | ebe5acf | 2008-01-23 08:25:04 +0000 | [diff] [blame] | 26 | #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 Ferrare | 81aac2a | 2007-07-29 03:43:37 +0000 | [diff] [blame] | 30 | #define METRONOME_LEFT PLA_LEFT |
| 31 | #define METRONOME_RIGHT PLA_RIGHT |
Jonathan Gordon | 10b0013 | 2006-11-17 00:50:35 +0000 | [diff] [blame] | 32 | #define METRONOME_LEFT_REP PLA_LEFT_REPEAT |
| 33 | #define METRONOME_RIGHT_REP PLA_RIGHT_REPEAT |
Jonathan Gordon | 7b8b0ff | 2006-11-16 02:53:44 +0000 | [diff] [blame] | 34 | enum { |
| 35 | METRONOME_PLAY_TAP = LAST_PLUGINLIB_ACTION+1, |
Jonathan Gordon | fee5ca2 | 2006-11-26 07:20:04 +0000 | [diff] [blame] | 36 | #if CONFIG_KEYPAD == ONDIO_PAD |
Jonathan Gordon | 7b8b0ff | 2006-11-16 02:53:44 +0000 | [diff] [blame] | 37 | METRONOME_PAUSE, |
Kevin Ferrare | a2b14a5 | 2007-07-29 05:30:04 +0000 | [diff] [blame] | 38 | #endif /* ONDIO_PAD */ |
Jonathan Gordon | fee5ca2 | 2006-11-26 07:20:04 +0000 | [diff] [blame] | 39 | #if (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD) |
Jonathan Gordon | 7b8b0ff | 2006-11-16 02:53:44 +0000 | [diff] [blame] | 40 | METRONOME_SYNC |
Kevin Ferrare | a2b14a5 | 2007-07-29 05:30:04 +0000 | [diff] [blame] | 41 | #endif /* IRIVER_H100_PAD||IRIVER_H300_PAD */ |
Jonathan Gordon | 7b8b0ff | 2006-11-16 02:53:44 +0000 | [diff] [blame] | 42 | }; |
Jens Arnold | c9e7726 | 2004-10-12 23:45:25 +0000 | [diff] [blame] | 43 | |
Jonathan Gordon | 7b8b0ff | 2006-11-16 02:53:44 +0000 | [diff] [blame] | 44 | |
| 45 | #if CONFIG_KEYPAD == ONDIO_PAD |
Jonathan Gordon | fee5ca2 | 2006-11-26 07:20:04 +0000 | [diff] [blame] | 46 | #define METRONOME_TAP PLA_START |
Jens Arnold | b8c9a0c | 2004-12-26 14:56:05 +0000 | [diff] [blame] | 47 | #define METRONOME_MSG_START "start: mode" |
| 48 | #define METRONOME_MSG_STOP "pause: hold mode" |
Jonathan Gordon | 7b8b0ff | 2006-11-16 02:53:44 +0000 | [diff] [blame] | 49 | static const struct button_mapping ondio_action[] = |
| 50 | { |
Jonathan Gordon | 10b0013 | 2006-11-17 00:50:35 +0000 | [diff] [blame] | 51 | {METRONOME_PLAY_TAP, BUTTON_MENU|BUTTON_REL, BUTTON_MENU }, |
Jonathan Gordon | fee5ca2 | 2006-11-26 07:20:04 +0000 | [diff] [blame] | 52 | {METRONOME_PAUSE, BUTTON_MENU|BUTTON_REPEAT, BUTTON_NONE }, |
| 53 | {CONTEXT_CUSTOM,BUTTON_NONE,BUTTON_NONE} |
Jonathan Gordon | 7b8b0ff | 2006-11-16 02:53:44 +0000 | [diff] [blame] | 54 | }; |
Kevin Ferrare | a2b14a5 | 2007-07-29 05:30:04 +0000 | [diff] [blame] | 55 | #else /* !ONDIO_PAD */ |
Jonathan Gordon | fee5ca2 | 2006-11-26 07:20:04 +0000 | [diff] [blame] | 56 | #define METRONOME_TAP PLA_FIRE |
| 57 | #define METRONOME_PLAYPAUSE PLA_START |
Daniel Stenberg | a00619f | 2005-02-04 10:02:42 +0000 | [diff] [blame] | 58 | #define METRONOME_MSG_START "press play" |
| 59 | #define METRONOME_MSG_STOP "press pause" |
Dave Chapman | ccf3283 | 2006-01-10 08:59:08 +0000 | [diff] [blame] | 60 | |
Jonathan Gordon | 7b8b0ff | 2006-11-16 02:53:44 +0000 | [diff] [blame] | 61 | #if (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD) |
Jonathan Gordon | 148c553 | 2006-12-04 01:04:14 +0000 | [diff] [blame] | 62 | #define MET_SYNC |
Martin Scarratt | 1612275 | 2006-12-03 16:49:36 +0000 | [diff] [blame] | 63 | static const struct button_mapping iriver_syncaction[] = |
Jonathan Gordon | 7b8b0ff | 2006-11-16 02:53:44 +0000 | [diff] [blame] | 64 | { |
| 65 | {METRONOME_SYNC, BUTTON_REC, BUTTON_NONE }, |
Jonathan Gordon | fee5ca2 | 2006-11-26 07:20:04 +0000 | [diff] [blame] | 66 | {CONTEXT_CUSTOM,BUTTON_NONE,BUTTON_NONE} |
Jonathan Gordon | 7b8b0ff | 2006-11-16 02:53:44 +0000 | [diff] [blame] | 67 | }; |
Kevin Ferrare | a2b14a5 | 2007-07-29 05:30:04 +0000 | [diff] [blame] | 68 | #endif /* IRIVER_H100_PAD||IRIVER_H300_PAD */ |
Jonathan Gordon | 7b8b0ff | 2006-11-16 02:53:44 +0000 | [diff] [blame] | 69 | #endif /* #if CONFIG_KEYPAD == ONDIO_PAD */ |
Linus Nielsen Feltzing | 81c9d58 | 2006-01-10 02:51:50 +0000 | [diff] [blame] | 70 | |
Kevin Ferrare | a2b14a5 | 2007-07-29 05:30:04 +0000 | [diff] [blame] | 71 | const struct button_mapping *plugin_contexts[]={ |
Kevin Ferrare | ebe5acf | 2008-01-23 08:25:04 +0000 | [diff] [blame] | 72 | generic_increase_decrease, |
Kevin Ferrare | a2b14a5 | 2007-07-29 05:30:04 +0000 | [diff] [blame] | 73 | 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 Ferrare | ebe5acf | 2008-01-23 08:25:04 +0000 | [diff] [blame] | 81 | #define PLA_ARRAY_COUNT sizeof(plugin_contexts)/sizeof(plugin_contexts[0]) |
Linus Nielsen Feltzing | 81c9d58 | 2006-01-10 02:51:50 +0000 | [diff] [blame] | 82 | |
Steve Bavin | 6526577 | 2008-05-13 09:57:56 +0000 | [diff] [blame] | 83 | static const struct plugin_api* rb; |
Linus Nielsen Feltzing | 1fcd8ce | 2004-03-12 08:21:26 +0000 | [diff] [blame] | 84 | |
Nils Wallménius | c7f9ca4 | 2007-06-13 15:35:07 +0000 | [diff] [blame] | 85 | MEM_FUNCTION_WRAPPERS(rb); |
| 86 | |
Linus Nielsen Feltzing | 4bd2702 | 2004-05-26 06:56:18 +0000 | [diff] [blame] | 87 | static int bpm = 120; |
| 88 | static int period = 0; |
| 89 | static int minitick = 0; |
Linus Nielsen Feltzing | 1fcd8ce | 2004-03-12 08:21:26 +0000 | [diff] [blame] | 90 | |
| 91 | static bool sound_active = false; |
| 92 | static bool sound_paused = true; |
Linus Nielsen Feltzing | c5067bf | 2004-03-17 14:54:32 +0000 | [diff] [blame] | 93 | |
Linus Nielsen Feltzing | 4bd2702 | 2004-05-26 06:56:18 +0000 | [diff] [blame] | 94 | static char buffer[30]; |
Linus Nielsen Feltzing | 1fcd8ce | 2004-03-12 08:21:26 +0000 | [diff] [blame] | 95 | |
Kevin Ferrare | a2b14a5 | 2007-07-29 05:30:04 +0000 | [diff] [blame] | 96 | static bool reset_tap = false; |
Linus Nielsen Feltzing | 569b998 | 2004-11-26 13:31:40 +0000 | [diff] [blame] | 97 | static int tap_count = 0; |
| 98 | static int tap_time = 0; |
| 99 | static int tap_timeout = 0; |
| 100 | |
Linus Nielsen Feltzing | c7c9069 | 2006-01-14 02:39:30 +0000 | [diff] [blame] | 101 | int bpm_step_counter = 0; |
| 102 | |
Linus Nielsen Feltzing | 81c9d58 | 2006-01-10 02:51:50 +0000 | [diff] [blame] | 103 | #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énius | c7f9ca4 | 2007-06-13 15:35:07 +0000 | [diff] [blame] | 108 | void callback(unsigned char** start, size_t* size){ |
Jörg Hohensohn | fd5714b | 2004-03-19 22:15:52 +0000 | [diff] [blame] | 109 | (void)start; /* unused parameter, avoid warning */ |
Marianne Arnold | 071c2ac | 2008-01-13 18:27:54 +0000 | [diff] [blame] | 110 | *size = 0; /* end of data */ |
Linus Nielsen Feltzing | 1fcd8ce | 2004-03-12 08:21:26 +0000 | [diff] [blame] | 111 | sound_active = false; |
Kevin Ferrare | 81aac2a | 2007-07-29 03:43:37 +0000 | [diff] [blame] | 112 | rb->led(0); |
Linus Nielsen Feltzing | 1fcd8ce | 2004-03-12 08:21:26 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void play_tock(void){ |
| 116 | sound_active = true; |
Kevin Ferrare | 81aac2a | 2007-07-29 03:43:37 +0000 | [diff] [blame] | 117 | rb->led(1); |
Linus Nielsen Feltzing | 1fcd8ce | 2004-03-12 08:21:26 +0000 | [diff] [blame] | 118 | rb->mp3_play_data(sound, sizeof(sound), callback); |
| 119 | rb->mp3_play_pause(true); /* kickoff audio */ |
| 120 | } |
| 121 | |
Kevin Ferrare | a2b14a5 | 2007-07-29 05:30:04 +0000 | [diff] [blame] | 122 | #else /* CONFIG_CODEC == SWCODEC */ |
Linus Nielsen Feltzing | 1fcd8ce | 2004-03-12 08:21:26 +0000 | [diff] [blame] | 123 | |
Linus Nielsen Feltzing | 81c9d58 | 2006-01-10 02:51:50 +0000 | [diff] [blame] | 124 | #define MET_IS_PLAYING rb->pcm_is_playing() |
Linus Nielsen Feltzing | c7c9069 | 2006-01-14 02:39:30 +0000 | [diff] [blame] | 125 | #define MET_PLAY_STOP rb->audio_stop() |
Linus Nielsen Feltzing | 81c9d58 | 2006-01-10 02:51:50 +0000 | [diff] [blame] | 126 | |
Linus Nielsen Feltzing | 81c9d58 | 2006-01-10 02:51:50 +0000 | [diff] [blame] | 127 | |
| 128 | int tock; |
Miika Pekkarinen | 0569fae | 2007-04-12 17:58:42 +0000 | [diff] [blame] | 129 | bool need_to_play = false; |
Linus Nielsen Feltzing | 81c9d58 | 2006-01-10 02:51:50 +0000 | [diff] [blame] | 130 | |
Linus Nielsen Feltzing | befd0ac | 2006-01-10 22:22:19 +0000 | [diff] [blame] | 131 | short sndbuf[sizeof(sound)*2]; |
| 132 | |
| 133 | /* Convert the mono "tock" sample to interleaved stereo */ |
| 134 | void 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 Feltzing | 81c9d58 | 2006-01-10 02:51:50 +0000 | [diff] [blame] | 143 | void play_tock(void) { |
Brandon Low | 413da2a | 2006-02-07 20:38:55 +0000 | [diff] [blame] | 144 | rb->pcm_play_data(NULL,(unsigned char *)sndbuf,sizeof(sndbuf)); |
Linus Nielsen Feltzing | 81c9d58 | 2006-01-10 02:51:50 +0000 | [diff] [blame] | 145 | tock++; |
| 146 | } |
| 147 | |
Kevin Ferrare | a2b14a5 | 2007-07-29 05:30:04 +0000 | [diff] [blame] | 148 | #endif /* CONFIG_CODEC != SWCODEC */ |
Linus Nielsen Feltzing | 81c9d58 | 2006-01-10 02:51:50 +0000 | [diff] [blame] | 149 | |
| 150 | void calc_period(void) |
| 151 | { |
Daniel Stenberg | 220fafd | 2006-03-03 08:14:44 +0000 | [diff] [blame] | 152 | period = 61440/bpm-1; /* (60*1024)/bpm; */ |
Linus Nielsen Feltzing | 81c9d58 | 2006-01-10 02:51:50 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | |
Kevin Ferrare | 81aac2a | 2007-07-29 03:43:37 +0000 | [diff] [blame] | 156 | void metronome_draw(struct screen* display) |
Linus Nielsen Feltzing | 81c9d58 | 2006-01-10 02:51:50 +0000 | [diff] [blame] | 157 | { |
Kevin Ferrare | 81aac2a | 2007-07-29 03:43:37 +0000 | [diff] [blame] | 158 | display->clear_display(); |
Linus Nielsen Feltzing | 1fcd8ce | 2004-03-12 08:21:26 +0000 | [diff] [blame] | 159 | |
| 160 | #ifdef HAVE_LCD_BITMAP |
Kevin Ferrare | 81aac2a | 2007-07-29 03:43:37 +0000 | [diff] [blame] | 161 | 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 Feltzing | 81c9d58 | 2006-01-10 02:51:50 +0000 | [diff] [blame] | 168 | #ifdef HAVE_REMOTE_LCD |
Kevin Ferrare | 81aac2a | 2007-07-29 03:43:37 +0000 | [diff] [blame] | 169 | else |
| 170 | { |
| 171 | display->puts(0, 5, "Rec to TAP"); |
| 172 | display->puts(0, 6, "Mode to SYNC"); |
| 173 | } |
| 174 | #endif |
Kevin Ferrare | a2b14a5 | 2007-07-29 05:30:04 +0000 | [diff] [blame] | 175 | #endif /* HAVE_LCD_BITMAP */ |
Linus Nielsen Feltzing | c5067bf | 2004-03-17 14:54:32 +0000 | [diff] [blame] | 176 | |
Linus Nielsen Feltzing | 1fcd8ce | 2004-03-12 08:21:26 +0000 | [diff] [blame] | 177 | rb->snprintf(buffer, sizeof(buffer), "BPM: %d ",bpm); |
| 178 | #ifdef HAVE_LCD_BITMAP |
Kevin Ferrare | 81aac2a | 2007-07-29 03:43:37 +0000 | [diff] [blame] | 179 | display->puts(0,3, buffer); |
Linus Nielsen Feltzing | 1fcd8ce | 2004-03-12 08:21:26 +0000 | [diff] [blame] | 180 | #else |
Kevin Ferrare | 81aac2a | 2007-07-29 03:43:37 +0000 | [diff] [blame] | 181 | display->puts(0,0, buffer); |
Kevin Ferrare | a2b14a5 | 2007-07-29 05:30:04 +0000 | [diff] [blame] | 182 | #endif /* HAVE_LCD_BITMAP */ |
Linus Nielsen Feltzing | 81c9d58 | 2006-01-10 02:51:50 +0000 | [diff] [blame] | 183 | |
Linus Nielsen Feltzing | 1fcd8ce | 2004-03-12 08:21:26 +0000 | [diff] [blame] | 184 | rb->snprintf(buffer, sizeof(buffer), "Vol: %d", |
| 185 | rb->global_settings->volume); |
| 186 | #ifdef HAVE_LCD_BITMAP |
Kevin Ferrare | 81aac2a | 2007-07-29 03:43:37 +0000 | [diff] [blame] | 187 | display->puts(10, 3, buffer); |
Linus Nielsen Feltzing | 1fcd8ce | 2004-03-12 08:21:26 +0000 | [diff] [blame] | 188 | #else |
Kevin Ferrare | f3114f3 | 2007-07-29 04:01:46 +0000 | [diff] [blame] | 189 | display->puts(0,1, buffer); |
Kevin Ferrare | a2b14a5 | 2007-07-29 05:30:04 +0000 | [diff] [blame] | 190 | #endif /* HAVE_LCD_BITMAP */ |
Linus Nielsen Feltzing | 1fcd8ce | 2004-03-12 08:21:26 +0000 | [diff] [blame] | 191 | |
| 192 | #ifdef HAVE_LCD_BITMAP |
Jens Arnold | fef8255 | 2008-04-12 09:51:16 +0000 | [diff] [blame] | 193 | display->hline(0, 111, 12); |
Linus Nielsen Feltzing | 1fcd8ce | 2004-03-12 08:21:26 +0000 | [diff] [blame] | 194 | if(sound_paused) |
Kevin Ferrare | 81aac2a | 2007-07-29 03:43:37 +0000 | [diff] [blame] | 195 | display->puts(0,2,METRONOME_MSG_START); |
Linus Nielsen Feltzing | 1fcd8ce | 2004-03-12 08:21:26 +0000 | [diff] [blame] | 196 | else |
Kevin Ferrare | 81aac2a | 2007-07-29 03:43:37 +0000 | [diff] [blame] | 197 | display->puts(0,2,METRONOME_MSG_STOP); |
Jonathan Gordon | 4404724 | 2008-03-09 02:36:17 +0000 | [diff] [blame] | 198 | display->setfont(FONT_UI); |
Kevin Ferrare | a2b14a5 | 2007-07-29 05:30:04 +0000 | [diff] [blame] | 199 | #endif /* HAVE_LCD_BITMAP */ |
Kevin Ferrare | 81aac2a | 2007-07-29 03:43:37 +0000 | [diff] [blame] | 200 | display->update(); |
Linus Nielsen Feltzing | 1fcd8ce | 2004-03-12 08:21:26 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Kevin Ferrare | 4f08483 | 2007-07-29 04:16:12 +0000 | [diff] [blame] | 203 | void draw_display(void) |
Kevin Ferrare | 81aac2a | 2007-07-29 03:43:37 +0000 | [diff] [blame] | 204 | { |
| 205 | int i; |
| 206 | FOR_NB_SCREENS(i) |
| 207 | metronome_draw(rb->screens[i]); |
| 208 | } |
Kevin Ferrare | a2b14a5 | 2007-07-29 05:30:04 +0000 | [diff] [blame] | 209 | |
Linus Nielsen Feltzing | c5067bf | 2004-03-17 14:54:32 +0000 | [diff] [blame] | 210 | /* helper function to change the volume by a certain amount, +/- |
| 211 | ripped from video.c */ |
Linus Nielsen Feltzing | 1fcd8ce | 2004-03-12 08:21:26 +0000 | [diff] [blame] | 212 | void change_volume(int delta){ |
Jens Arnold | c80e0c1 | 2005-12-17 21:13:30 +0000 | [diff] [blame] | 213 | int minvol = rb->sound_min(SOUND_VOLUME); |
| 214 | int maxvol = rb->sound_max(SOUND_VOLUME); |
Linus Nielsen Feltzing | 1fcd8ce | 2004-03-12 08:21:26 +0000 | [diff] [blame] | 215 | int vol = rb->global_settings->volume + delta; |
Jens Arnold | c80e0c1 | 2005-12-17 21:13:30 +0000 | [diff] [blame] | 216 | |
| 217 | if (vol > maxvol) vol = maxvol; |
| 218 | else if (vol < minvol) vol = minvol; |
Linus Nielsen Feltzing | 1fcd8ce | 2004-03-12 08:21:26 +0000 | [diff] [blame] | 219 | if (vol != rb->global_settings->volume) { |
Linus Nielsen Feltzing | 674eaca | 2005-04-01 13:41:03 +0000 | [diff] [blame] | 220 | rb->sound_set(SOUND_VOLUME, vol); |
Linus Nielsen Feltzing | 1fcd8ce | 2004-03-12 08:21:26 +0000 | [diff] [blame] | 221 | rb->global_settings->volume = vol; |
Linus Nielsen Feltzing | 81c9d58 | 2006-01-10 02:51:50 +0000 | [diff] [blame] | 222 | draw_display(); |
Linus Nielsen Feltzing | 1fcd8ce | 2004-03-12 08:21:26 +0000 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
Linus Nielsen Feltzing | c7c9069 | 2006-01-14 02:39:30 +0000 | [diff] [blame] | 226 | /*function to accelerate bpm change*/ |
| 227 | void 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 Ferrare | a2b14a5 | 2007-07-29 05:30:04 +0000 | [diff] [blame] | 236 | |
Linus Nielsen Feltzing | c7c9069 | 2006-01-14 02:39:30 +0000 | [diff] [blame] | 237 | if (bpm > 400) bpm = 400; |
| 238 | if (bpm < 1) bpm = 1; |
| 239 | calc_period(); |
| 240 | draw_display(); |
| 241 | bpm_step_counter++; |
Kevin Ferrare | a2b14a5 | 2007-07-29 05:30:04 +0000 | [diff] [blame] | 242 | } |
Linus Nielsen Feltzing | c7c9069 | 2006-01-14 02:39:30 +0000 | [diff] [blame] | 243 | |
Linus Nielsen Feltzing | 81c9d58 | 2006-01-10 02:51:50 +0000 | [diff] [blame] | 244 | void timer_callback(void) |
| 245 | { |
| 246 | if(minitick >= period){ |
| 247 | minitick = 0; |
| 248 | if(!sound_active && !sound_paused && !tap_count) { |
Miika Pekkarinen | 0569fae | 2007-04-12 17:58:42 +0000 | [diff] [blame] | 249 | #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 Feltzing | 81c9d58 | 2006-01-10 02:51:50 +0000 | [diff] [blame] | 253 | play_tock(); |
Miika Pekkarinen | 0569fae | 2007-04-12 17:58:42 +0000 | [diff] [blame] | 254 | #endif |
Linus Nielsen Feltzing | 81c9d58 | 2006-01-10 02:51:50 +0000 | [diff] [blame] | 255 | rb->reset_poweroff_timer(); |
| 256 | } |
Jens Arnold | 0df29b2 | 2004-08-28 09:07:21 +0000 | [diff] [blame] | 257 | } |
Linus Nielsen Feltzing | 4bd2702 | 2004-05-26 06:56:18 +0000 | [diff] [blame] | 258 | else { |
Linus Nielsen Feltzing | 81c9d58 | 2006-01-10 02:51:50 +0000 | [diff] [blame] | 259 | minitick++; |
Linus Nielsen Feltzing | 4bd2702 | 2004-05-26 06:56:18 +0000 | [diff] [blame] | 260 | } |
Kevin Ferrare | a2b14a5 | 2007-07-29 05:30:04 +0000 | [diff] [blame] | 261 | |
Linus Nielsen Feltzing | 569b998 | 2004-11-26 13:31:40 +0000 | [diff] [blame] | 262 | if (tap_count) { |
| 263 | tap_time++; |
| 264 | if (tap_count > 1 && tap_time > tap_timeout) |
| 265 | tap_count = 0; |
| 266 | } |
Linus Nielsen Feltzing | 4bd2702 | 2004-05-26 06:56:18 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Jens Arnold | c9e7726 | 2004-10-12 23:45:25 +0000 | [diff] [blame] | 269 | void cleanup(void *parameter) |
| 270 | { |
| 271 | (void)parameter; |
| 272 | |
Jens Arnold | e44372e | 2005-07-26 20:01:11 +0000 | [diff] [blame] | 273 | rb->timer_unregister(); |
Linus Nielsen Feltzing | 81c9d58 | 2006-01-10 02:51:50 +0000 | [diff] [blame] | 274 | MET_PLAY_STOP; /* stop audio ISR */ |
Kevin Ferrare | 81aac2a | 2007-07-29 03:43:37 +0000 | [diff] [blame] | 275 | rb->led(0); |
Jens Arnold | c9e7726 | 2004-10-12 23:45:25 +0000 | [diff] [blame] | 276 | } |
Linus Nielsen Feltzing | 4bd2702 | 2004-05-26 06:56:18 +0000 | [diff] [blame] | 277 | |
Linus Nielsen Feltzing | 569b998 | 2004-11-26 13:31:40 +0000 | [diff] [blame] | 278 | void tap(void) |
| 279 | { |
Miika Pekkarinen | 0569fae | 2007-04-12 17:58:42 +0000 | [diff] [blame] | 280 | if (tap_count == 0 || tap_time < tap_count) { |
Linus Nielsen Feltzing | 569b998 | 2004-11-26 13:31:40 +0000 | [diff] [blame] | 281 | tap_time = 0; |
| 282 | } |
| 283 | else { |
| 284 | if (tap_time > 0) { |
| 285 | bpm = 61440/(tap_time/tap_count); |
Kevin Ferrare | a2b14a5 | 2007-07-29 05:30:04 +0000 | [diff] [blame] | 286 | |
Linus Nielsen Feltzing | 569b998 | 2004-11-26 13:31:40 +0000 | [diff] [blame] | 287 | if (bpm > 400) |
| 288 | bpm = 400; |
| 289 | } |
Kevin Ferrare | a2b14a5 | 2007-07-29 05:30:04 +0000 | [diff] [blame] | 290 | |
Linus Nielsen Feltzing | 569b998 | 2004-11-26 13:31:40 +0000 | [diff] [blame] | 291 | calc_period(); |
| 292 | draw_display(); |
Kevin Ferrare | a2b14a5 | 2007-07-29 05:30:04 +0000 | [diff] [blame] | 293 | |
Linus Nielsen Feltzing | 569b998 | 2004-11-26 13:31:40 +0000 | [diff] [blame] | 294 | tap_timeout = (tap_count+2)*tap_time/tap_count; |
| 295 | } |
Kevin Ferrare | a2b14a5 | 2007-07-29 05:30:04 +0000 | [diff] [blame] | 296 | |
Linus Nielsen Feltzing | 569b998 | 2004-11-26 13:31:40 +0000 | [diff] [blame] | 297 | tap_count++; |
| 298 | minitick = 0; /* sync tock to tapping */ |
Linus Nielsen Feltzing | 81c9d58 | 2006-01-10 02:51:50 +0000 | [diff] [blame] | 299 | play_tock(); |
| 300 | |
Linus Nielsen Feltzing | 569b998 | 2004-11-26 13:31:40 +0000 | [diff] [blame] | 301 | reset_tap = false; |
| 302 | } |
Linus Nielsen Feltzing | 1fcd8ce | 2004-03-12 08:21:26 +0000 | [diff] [blame] | 303 | |
Steve Bavin | 6526577 | 2008-05-13 09:57:56 +0000 | [diff] [blame] | 304 | enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter){ |
Jens Arnold | c9e7726 | 2004-10-12 23:45:25 +0000 | [diff] [blame] | 305 | int button; |
Michael Sevakis | 4597ebe | 2007-06-10 02:10:47 +0000 | [diff] [blame] | 306 | enum plugin_status status; |
Jens Arnold | c9e7726 | 2004-10-12 23:45:25 +0000 | [diff] [blame] | 307 | |
Linus Nielsen Feltzing | 1fcd8ce | 2004-03-12 08:21:26 +0000 | [diff] [blame] | 308 | (void)parameter; |
| 309 | rb = api; |
| 310 | |
Linus Nielsen Feltzing | 81c9d58 | 2006-01-10 02:51:50 +0000 | [diff] [blame] | 311 | if (MET_IS_PLAYING) |
Daniel Stenberg | 220fafd | 2006-03-03 08:14:44 +0000 | [diff] [blame] | 312 | MET_PLAY_STOP; /* stop audio IS */ |
Kevin Ferrare | a2b14a5 | 2007-07-29 05:30:04 +0000 | [diff] [blame] | 313 | |
Linus Nielsen Feltzing | 81c9d58 | 2006-01-10 02:51:50 +0000 | [diff] [blame] | 314 | #if CONFIG_CODEC != SWCODEC |
Linus Nielsen Feltzing | 1fcd8ce | 2004-03-12 08:21:26 +0000 | [diff] [blame] | 315 | rb->bitswap(sound, sizeof(sound)); |
Linus Nielsen Feltzing | 81c9d58 | 2006-01-10 02:51:50 +0000 | [diff] [blame] | 316 | #else |
Linus Nielsen Feltzing | befd0ac | 2006-01-10 22:22:19 +0000 | [diff] [blame] | 317 | prepare_tock(); |
Michael Sevakis | 4597ebe | 2007-06-10 02:10:47 +0000 | [diff] [blame] | 318 | #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 Ferrare | a2b14a5 | 2007-07-29 05:30:04 +0000 | [diff] [blame] | 324 | #endif /* CONFIG_CODEC != SWCODEC */ |
Linus Nielsen Feltzing | 1fcd8ce | 2004-03-12 08:21:26 +0000 | [diff] [blame] | 325 | |
Linus Nielsen Feltzing | 569b998 | 2004-11-26 13:31:40 +0000 | [diff] [blame] | 326 | calc_period(); |
Jens Arnold | ac9b927 | 2008-04-04 19:38:46 +0000 | [diff] [blame] | 327 | rb->timer_register(1, NULL, TIMER_FREQ/1024, 1, timer_callback IF_COP(, CPU)); |
Kevin Ferrare | a2b14a5 | 2007-07-29 05:30:04 +0000 | [diff] [blame] | 328 | |
Linus Nielsen Feltzing | 569b998 | 2004-11-26 13:31:40 +0000 | [diff] [blame] | 329 | draw_display(); |
Linus Nielsen Feltzing | 1fcd8ce | 2004-03-12 08:21:26 +0000 | [diff] [blame] | 330 | |
| 331 | /* main loop */ |
| 332 | while (true){ |
Linus Nielsen Feltzing | 569b998 | 2004-11-26 13:31:40 +0000 | [diff] [blame] | 333 | reset_tap = true; |
Miika Pekkarinen | 0569fae | 2007-04-12 17:58:42 +0000 | [diff] [blame] | 334 | #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 Gordon | 7b8b0ff | 2006-11-16 02:53:44 +0000 | [diff] [blame] | 342 | button = pluginlib_getaction(rb,TIMEOUT_BLOCK, |
| 343 | plugin_contexts,PLA_ARRAY_COUNT); |
Kevin Ferrare | a2b14a5 | 2007-07-29 05:30:04 +0000 | [diff] [blame] | 344 | #endif /* SWCODEC */ |
Linus Nielsen Feltzing | 569b998 | 2004-11-26 13:31:40 +0000 | [diff] [blame] | 345 | switch (button) { |
Jens Arnold | c9e7726 | 2004-10-12 23:45:25 +0000 | [diff] [blame] | 346 | |
Linus Nielsen Feltzing | 569b998 | 2004-11-26 13:31:40 +0000 | [diff] [blame] | 347 | case METRONOME_QUIT: |
| 348 | /* get out of here */ |
| 349 | cleanup(NULL); |
Michael Sevakis | 4597ebe | 2007-06-10 02:10:47 +0000 | [diff] [blame] | 350 | status = PLUGIN_OK; |
| 351 | goto metronome_exit; |
Linus Nielsen Feltzing | 1fcd8ce | 2004-03-12 08:21:26 +0000 | [diff] [blame] | 352 | |
Linus Nielsen Feltzing | 569b998 | 2004-11-26 13:31:40 +0000 | [diff] [blame] | 353 | #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 Ferrare | a2b14a5 | 2007-07-29 05:30:04 +0000 | [diff] [blame] | 363 | |
Linus Nielsen Feltzing | 569b998 | 2004-11-26 13:31:40 +0000 | [diff] [blame] | 364 | case METRONOME_PAUSE: |
Jens Arnold | a5a6fa9 | 2004-11-29 00:19:51 +0000 | [diff] [blame] | 365 | if(!sound_paused) { |
Linus Nielsen Feltzing | 569b998 | 2004-11-26 13:31:40 +0000 | [diff] [blame] | 366 | sound_paused = true; |
Jens Arnold | a5a6fa9 | 2004-11-29 00:19:51 +0000 | [diff] [blame] | 367 | draw_display(); |
Kevin Ferrare | a2b14a5 | 2007-07-29 05:30:04 +0000 | [diff] [blame] | 368 | } |
Linus Nielsen Feltzing | 569b998 | 2004-11-26 13:31:40 +0000 | [diff] [blame] | 369 | break; |
Kevin Ferrare | a2b14a5 | 2007-07-29 05:30:04 +0000 | [diff] [blame] | 370 | |
Linus Nielsen Feltzing | 569b998 | 2004-11-26 13:31:40 +0000 | [diff] [blame] | 371 | #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 Ferrare | a2b14a5 | 2007-07-29 05:30:04 +0000 | [diff] [blame] | 380 | #endif /* ONDIO_PAD */ |
| 381 | |
Linus Nielsen Feltzing | 569b998 | 2004-11-26 13:31:40 +0000 | [diff] [blame] | 382 | case METRONOME_VOL_UP: |
Jonathan Gordon | 7b8b0ff | 2006-11-16 02:53:44 +0000 | [diff] [blame] | 383 | case METRONOME_VOL_UP_REP: |
Linus Nielsen Feltzing | 569b998 | 2004-11-26 13:31:40 +0000 | [diff] [blame] | 384 | change_volume(1); |
| 385 | calc_period(); |
| 386 | break; |
Kevin Ferrare | a2b14a5 | 2007-07-29 05:30:04 +0000 | [diff] [blame] | 387 | |
Linus Nielsen Feltzing | 569b998 | 2004-11-26 13:31:40 +0000 | [diff] [blame] | 388 | case METRONOME_VOL_DOWN: |
Jonathan Gordon | 7b8b0ff | 2006-11-16 02:53:44 +0000 | [diff] [blame] | 389 | case METRONOME_VOL_DOWN_REP: |
Linus Nielsen Feltzing | 569b998 | 2004-11-26 13:31:40 +0000 | [diff] [blame] | 390 | change_volume(-1); |
| 391 | calc_period(); |
| 392 | break; |
Linus Nielsen Feltzing | 1fcd8ce | 2004-03-12 08:21:26 +0000 | [diff] [blame] | 393 | |
Jonathan Gordon | 10b0013 | 2006-11-17 00:50:35 +0000 | [diff] [blame] | 394 | case METRONOME_LEFT: |
Linus Nielsen Feltzing | c7c9069 | 2006-01-14 02:39:30 +0000 | [diff] [blame] | 395 | bpm_step_counter = 0; |
Jonathan Gordon | 10b0013 | 2006-11-17 00:50:35 +0000 | [diff] [blame] | 396 | case METRONOME_LEFT_REP: |
Linus Nielsen Feltzing | c7c9069 | 2006-01-14 02:39:30 +0000 | [diff] [blame] | 397 | change_bpm(-1); |
Linus Nielsen Feltzing | 569b998 | 2004-11-26 13:31:40 +0000 | [diff] [blame] | 398 | break; |
Linus Nielsen Feltzing | 1fcd8ce | 2004-03-12 08:21:26 +0000 | [diff] [blame] | 399 | |
Jonathan Gordon | 10b0013 | 2006-11-17 00:50:35 +0000 | [diff] [blame] | 400 | case METRONOME_RIGHT: |
Linus Nielsen Feltzing | c7c9069 | 2006-01-14 02:39:30 +0000 | [diff] [blame] | 401 | bpm_step_counter = 0; |
Jonathan Gordon | 10b0013 | 2006-11-17 00:50:35 +0000 | [diff] [blame] | 402 | case METRONOME_RIGHT_REP: |
Linus Nielsen Feltzing | c7c9069 | 2006-01-14 02:39:30 +0000 | [diff] [blame] | 403 | change_bpm(1); |
Linus Nielsen Feltzing | 569b998 | 2004-11-26 13:31:40 +0000 | [diff] [blame] | 404 | break; |
Linus Nielsen Feltzing | 1fcd8ce | 2004-03-12 08:21:26 +0000 | [diff] [blame] | 405 | |
Jonathan Gordon | fee5ca2 | 2006-11-26 07:20:04 +0000 | [diff] [blame] | 406 | #ifdef METRONOME_TAP |
Linus Nielsen Feltzing | 569b998 | 2004-11-26 13:31:40 +0000 | [diff] [blame] | 407 | case METRONOME_TAP: |
| 408 | tap(); |
| 409 | break; |
| 410 | #endif |
Linus Nielsen Feltzing | 81c9d58 | 2006-01-10 02:51:50 +0000 | [diff] [blame] | 411 | |
Jonathan Gordon | 148c553 | 2006-12-04 01:04:14 +0000 | [diff] [blame] | 412 | #ifdef MET_SYNC |
Linus Nielsen Feltzing | 81c9d58 | 2006-01-10 02:51:50 +0000 | [diff] [blame] | 413 | case METRONOME_SYNC: |
| 414 | minitick = period; |
| 415 | break; |
| 416 | #endif |
| 417 | |
Linus Nielsen Feltzing | 569b998 | 2004-11-26 13:31:40 +0000 | [diff] [blame] | 418 | default: |
| 419 | if (rb->default_event_handler_ex(button, cleanup, NULL) |
| 420 | == SYS_USB_CONNECTED) |
Michael Sevakis | 4597ebe | 2007-06-10 02:10:47 +0000 | [diff] [blame] | 421 | { |
| 422 | status = PLUGIN_USB_CONNECTED; |
| 423 | goto metronome_exit; |
| 424 | } |
Linus Nielsen Feltzing | 569b998 | 2004-11-26 13:31:40 +0000 | [diff] [blame] | 425 | reset_tap = false; |
| 426 | break; |
Jens Arnold | cc8cff2 | 2004-10-16 00:07:43 +0000 | [diff] [blame] | 427 | |
Linus Nielsen Feltzing | 569b998 | 2004-11-26 13:31:40 +0000 | [diff] [blame] | 428 | } |
| 429 | if (reset_tap) { |
| 430 | tap_count = 0; |
| 431 | } |
Linus Nielsen Feltzing | 1fcd8ce | 2004-03-12 08:21:26 +0000 | [diff] [blame] | 432 | } |
Michael Sevakis | 4597ebe | 2007-06-10 02:10:47 +0000 | [diff] [blame] | 433 | |
| 434 | metronome_exit: |
Michael Sevakis | 4354878 | 2007-06-10 02:19:39 +0000 | [diff] [blame] | 435 | #if CONFIG_CODEC == SWCODEC |
Michael Sevakis | 4597ebe | 2007-06-10 02:10:47 +0000 | [diff] [blame] | 436 | rb->pcm_set_frequency(HW_SAMPR_DEFAULT); |
Michael Sevakis | 4354878 | 2007-06-10 02:19:39 +0000 | [diff] [blame] | 437 | #endif |
Michael Sevakis | 4597ebe | 2007-06-10 02:10:47 +0000 | [diff] [blame] | 438 | return status; |
Linus Nielsen Feltzing | 1fcd8ce | 2004-03-12 08:21:26 +0000 | [diff] [blame] | 439 | } |