From e333bfd1f9ff00e457039e2395fb094c19485bdb Mon Sep 17 00:00:00 2001 From: bigalex Date: Thu, 4 Oct 2012 13:57:18 +0200 Subject: [PATCH] missing libraries were missing --- firmware/lib/music.c | 56 ++++++++++++++++++++++++++++++++++ firmware/lib/music.h | 27 +++++++++++++++++ firmware/lib/util.c | 71 ++++++++++++++++++++++++++++++++++++++++++++ firmware/lib/util.h | 28 +++++++++++++++++ 4 files changed, 182 insertions(+) create mode 100644 firmware/lib/music.c create mode 100644 firmware/lib/music.h create mode 100644 firmware/lib/util.c create mode 100644 firmware/lib/util.h diff --git a/firmware/lib/music.c b/firmware/lib/music.c new file mode 100644 index 0000000..d7628ab --- /dev/null +++ b/firmware/lib/music.c @@ -0,0 +1,56 @@ + +#include +#include +#include +#include +#include +#include "util.h" +#include "bughal.h" +#include "music.h" + +//static volatile uint16_t currentNote; + +void music_init(void){ + + TCCR1A = 0; //TIMER1, normal, no PWM + TCCR1B = (1 << WGM12) | (1 << CS11); //CTC Mode, Clear Timer on Compare, Prescaler = 8 +// OCR1A = 500; //invert with 2 KHz --> 1 KHz sound --> 8000000/8/2000 +// OCR1A = 10000; +// TIMSK1 |= (1 << OCIE1A); //enable Output Compare Interrupt + TIMSK1 &= ~(1 << OCIE1A); //disable Output Compare Interrupt + return; +}; + + +void music_setNote(uint16_t note, uint8_t octave){ + + + cli(); + if (note != NOTE_PAUSE){ + //Play a Note + buzzr_up(); + TIMSK1 |= (1 << OCIE1A); //enable Output Compare Interrupt + OCR1A = octave==0 ? note : note / (1 << octave); + } else { // Pause (silence) + buzzr_off(); + TIMSK1 &= ~(1 << OCIE1A); //disable Output Compare Interrupt + } + sei(); + return; +} + + + + +/* timer interrupt function */ +ISR(TIMER1_COMPA_vect, ISR_NOBLOCK) { + + // invert buzzer polarity + buzzr_inv(); + + +} + + + + diff --git a/firmware/lib/music.h b/firmware/lib/music.h new file mode 100644 index 0000000..4533e51 --- /dev/null +++ b/firmware/lib/music.h @@ -0,0 +1,27 @@ +/* small timer library, uses timer2 */ + +#ifndef _MUSIC_H +#define _MUSIC_H + + +#define NOTE_PAUSE (65000) //Pause + +//Values for Octave 0 +#define NOTE_C (30577) // note C +#define NOTE_Db (28862) // note C# / Db +#define NOTE_D (27242) // note D +#define NOTE_Eb (25713) // note D# / Eb +#define NOTE_E (24270) // note E +#define NOTE_F (22908) // note F +#define NOTE_Gb (21622) // note F# / Gb +#define NOTE_G (20408) // note G +#define NOTE_Ab (19263) // note G# / Ab +#define NOTE_A (18182) // note A +#define NOTE_Bb (17161) // note A# / Bb +#define NOTE_B (16198) // note B + + + +void music_init(void); +void music_setNote(uint16_t note, uint8_t octave); +#endif diff --git a/firmware/lib/util.c b/firmware/lib/util.c new file mode 100644 index 0000000..f29f9a3 --- /dev/null +++ b/firmware/lib/util.c @@ -0,0 +1,71 @@ + +#include +#include +#include +#include +#include "util.h" + +static volatile uint8_t internal_counter; + +void timer_init(void) +{ + /* initialize timer2, CTC at 10ms, prescaler 1024 */ + OCR2A = F_CPU/1024/100; + TCCR2A = _BV(WGM21); + TCCR2B = _BV(CS22) | _BV(CS21) | _BV(CS20); + TIMSK2 = _BV(OCIE2A); +} + +void timer_set(timer_t *t, uint8_t timeout) +{ + t->current = internal_counter; + t->timeout = timeout; +} + +void timerL_set(timerL_t *t, uint16_t timeout) +{ + t->current = internal_counter; + t->timeout = timeout; +} + + + + +bool timer_expired(timer_t *t) +{ + if (t->timeout == 0) + return true; + + /* attention: may fail if internal counter is incremented by more than one + * between two calls of timer_expired()! (that is if its is called less than every 10ms*/ + if (t->current != internal_counter) { + t->timeout--; + t->current = internal_counter; + } + + return false; +} + + +bool timerL_expired(timerL_t *t) +{ + if (t->timeout == 0) + return true; + + /* attention: may fail if internal counter is incremented by more than one + * between two calls of timer_expired()! */ + if (t->current != internal_counter) { + t->timeout--; + t->current = internal_counter; + } + + return false; +} + + + +/* timer interrupt function */ +ISR(TIMER2_COMPA_vect, ISR_NOBLOCK) { + internal_counter++; +} + diff --git a/firmware/lib/util.h b/firmware/lib/util.h new file mode 100644 index 0000000..cacb04b --- /dev/null +++ b/firmware/lib/util.h @@ -0,0 +1,28 @@ +/* small timer library, uses timer2 */ + +#ifndef _UTIL_H +#define _UTIL_H + +#include +#include + +/* structures */ +typedef struct { + uint8_t current; + uint8_t timeout; +} timer_t; + +typedef struct { + uint16_t current; + uint16_t timeout; +} timerL_t; + + +/* functions */ +void timer_init(void); +void timer_set(timer_t *t, uint8_t timeout); +bool timer_expired(timer_t *t); +void timerL_set(timerL_t *t, uint16_t timeout); +bool timerL_expired(timerL_t *t); + +#endif