diff --git a/firmware/lib/music.c b/firmware/lib/music.c index c620ee1..1d5b7fa 100644 --- a/firmware/lib/music.c +++ b/firmware/lib/music.c @@ -12,15 +12,20 @@ static void tune(void) { void set_note(uint16_t note, uint8_t octave) { if(note != NOTE_PAUSE) { + // bring buzzer to a state where inverting it creates sound buzzer_up(); + // start timer with doubled frequency of tone we want to play start_timer(PRESCALE_8, note >> octave, tune); } else { + // do nothing when paused stop_note(); } } void stop_note(void) { + // turn buzzer off to save energy buzzer_off(); + // we do not need the timer anymore stop_timer(); } @@ -32,20 +37,25 @@ 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) { + // user defined pause ++i; - wait_ms(notes[i] * 4 / length); + wait_ms(notes[i]); } else if(notes[i] == MLDY_LENGTH) { + // sets length for next tone ++i; length = notes[i]; } else { + // play note set_note(notes[i], octave); test_stop_app(); wait_ms(speed * 4 / length); + // pause after note stop_note(); test_stop_app(); wait_ms(pause); + // reset length for next note length = 4; } }