diff --git a/firmware/apps/crazy_synth.c b/firmware/apps/crazy_synth.c index af132c4..cd79bce 100644 --- a/firmware/apps/crazy_synth.c +++ b/firmware/apps/crazy_synth.c @@ -8,8 +8,10 @@ #include "../lib/freq_table.h" #include -#include "../lib/apps.h" +#include +#include +void tetris_play(void); static uint16_t osc0; static uint16_t osc1; @@ -21,29 +23,17 @@ static uint16_t speedtime; void synth_init(void){ init_buzzr(); - cli(); osc0 = osc1 = sample = row = 0; speedtime = 3000; - /* set timer0 to CTC & prescaler 64 → 125kHz increment */ - TCCR0A = (1 << WGM01); - TCCR0B = (1 << CS00) | (1 << CS01); + start_timer(tetris_play); - OCR0A = 4; /* TOP */ - TCNT0 = 0; - - /*enable interrupt */ - TIMSK0 |= (1 << OCIE0A); - - sei(); return; } void synth_deinit(void) { - cli(); - TIMSK0 = 0; - sei(); + stop_timer(); return; } @@ -101,8 +91,7 @@ const char music_data[2][SONG_LENGTH] PROGMEM = { -ISR(TIMER0_COMPA_vect,ISR_NOBLOCK) -{ +void tetris_play(void) { osc0 += pgm_read_word(&freq_table[ pgm_read_byte(&music_data[0][row])]); osc1 += pgm_read_word(&freq_table[ pgm_read_byte(&music_data[1][row])]); if (++sample == speedtime ) { diff --git a/firmware/lib/timer.c b/firmware/lib/timer.c new file mode 100644 index 0000000..973b8b5 --- /dev/null +++ b/firmware/lib/timer.c @@ -0,0 +1,40 @@ +#include +#include +#include + +#include + +timer_cb cur_cb; + +void start_timer(timer_cb cb) { + cur_cb = cb; + + cli(); + + /* set timer0 to CTC & prescaler 64 → 125kHz increment */ + TCCR0A = (1 << WGM01); + TCCR0B = (1 << CS00) | (1 << CS01); + + OCR0A = 4; /* TOP */ + TCNT0 = 0; + + /*enable interrupt */ + TIMSK0 |= (1 << OCIE0A); + + sei(); +} + +void stop_timer(void) { + cli(); + TIMSK0 = 0; + sei(); + + cur_cb = NULL; +} + +ISR(TIMER0_COMPA_vect,ISR_NOBLOCK) { + if(cur_cb != NULL) { + cur_cb(); + } +} + diff --git a/firmware/lib/timer.h b/firmware/lib/timer.h new file mode 100644 index 0000000..900cc84 --- /dev/null +++ b/firmware/lib/timer.h @@ -0,0 +1,10 @@ +#ifndef TIMER_H +#define TIMER_H + +typedef void (*timer_cb)(void); + +void start_timer(timer_cb cb); + +void stop_timer(void); + +#endif /* TIMER_H */