Improved music code

This commit is contained in:
Thammi 2013-08-29 23:09:01 +02:00
parent 26c2c6aa9c
commit 1b962ea397
3 changed files with 14 additions and 7 deletions

View File

@ -19,6 +19,7 @@
#define NOTE_B 16198 // note B
#define MLDY_PAUSE 0xfffe // user defined pause for melody, the next value is the amount of ms to wait
#define MLDY_LENGTH 0xfffd // user defined note length for melody, the next value is the "shortness" of the note (1/n)
// plays the note (see defines above) at the given octave until another note is played or the note is stopped
void set_note(uint16_t note, uint8_t octave);

View File

@ -23,6 +23,7 @@ void stop_note(void) {
void play_melody(uint16_t notes[], size_t len, uint8_t octave, int speed) {
int pause = speed / 20;
uint8_t length = 4;
size_t i;
@ -30,15 +31,20 @@ void play_melody(uint16_t notes[], size_t len, uint8_t octave, int speed) {
for(i = 0; i < len; ++i) {
if(notes[i] == MLDY_PAUSE) {
++i;
_delay_ms(notes[i]);
wait_ms(notes[i] * 4 / length);
} else if(notes[i] == MLDY_LENGTH) {
++i;
length = notes[i];
} else {
set_note(notes[i], octave);
test_stop_app();
_delay_ms(speed);
wait_ms(speed * 4 / length);
stop_note();
test_stop_app();
_delay_ms(pause);
wait_ms(pause);
length = 4;
}
}
}

View File

@ -8,8 +8,8 @@
#include <pentabug/music.h>
#include <pentabug/helper.h>
static uint16_t up_mldy[] = { NOTE_C, NOTE_D, NOTE_F, NOTE_PAUSE };
static uint16_t down_mldy[] = { NOTE_F, NOTE_E, NOTE_C, NOTE_PAUSE };
static uint16_t up_mldy[] = { NOTE_C, NOTE_D, MLDY_LENGTH, 2, NOTE_F, NOTE_PAUSE };
static uint16_t down_mldy[] = { NOTE_F, NOTE_E, MLDY_LENGTH, 2, NOTE_C, NOTE_PAUSE };
static inline void run_app(struct app_t* app) {
app_should_stop = 0;
@ -51,7 +51,7 @@ int main(void) {
reset_hw();
if(app_direction > 0) {
play_melody(up_mldy, ARRAY_SIZE(up_mldy), 4, 60);
play_melody(up_mldy, ARRAY_SIZE(up_mldy), 4, 100);
app_index++;
@ -59,7 +59,7 @@ int main(void) {
app_index = 0;
}
} else {
play_melody(down_mldy, ARRAY_SIZE(down_mldy), 4, 60);
play_melody(down_mldy, ARRAY_SIZE(down_mldy), 4, 100);
if(app_index == 0) {
app_index = MAX_APPS - 1;