pentabug/firmware/lib/music.c

52 lines
942 B
C
Raw Normal View History

#include <pentabug/music.h>
#include <avr/io.h>
2013-08-27 00:22:38 +02:00
#include <pentabug/timer.h>
#include <pentabug/lifecycle.h>
2013-08-29 23:25:46 +02:00
#include <pentabug/hal.h>
static void tune(void) {
PORTB ^= 1 << 7;
}
void set_note(uint16_t note, uint8_t octave) {
if(note != NOTE_PAUSE) {
start_timer(PRESCALE_8, note >> octave, tune);
} else {
stop_timer();
}
}
void stop_note(void) {
stop_timer();
}
2013-08-27 00:22:38 +02:00
void play_melody(uint16_t notes[], size_t len, uint8_t octave, int speed) {
int pause = speed / 20;
2013-08-29 23:09:01 +02:00
uint8_t length = 4;
2013-08-27 00:22:38 +02:00
size_t i;
// TODO: get away from the int to double conversion to free half of our flash ...
for(i = 0; i < len; ++i) {
if(notes[i] == MLDY_PAUSE) {
++i;
2013-08-29 23:09:01 +02:00
wait_ms(notes[i] * 4 / length);
} else if(notes[i] == MLDY_LENGTH) {
++i;
length = notes[i];
2013-08-27 00:22:38 +02:00
} else {
set_note(notes[i], octave);
test_stop_app();
2013-08-29 23:09:01 +02:00
wait_ms(speed * 4 / length);
2013-08-27 00:22:38 +02:00
stop_note();
test_stop_app();
2013-08-29 23:09:01 +02:00
wait_ms(pause);
length = 4;
2013-08-27 00:22:38 +02:00
}
}
}