missing libraries were missing

This commit is contained in:
bigalex 2012-10-04 13:57:18 +02:00
parent 71fe43176e
commit e333bfd1f9
4 changed files with 182 additions and 0 deletions

56
firmware/lib/music.c Normal file
View File

@ -0,0 +1,56 @@
#include <stdint.h>
#include <math.h>
#include <avr/interrupt.h>
#include <avr/common.h>
#include <avr/io.h>
#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();
}

27
firmware/lib/music.h Normal file
View File

@ -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

71
firmware/lib/util.c Normal file
View File

@ -0,0 +1,71 @@
#include <stdint.h>
#include <avr/interrupt.h>
#include <avr/common.h>
#include <avr/io.h>
#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++;
}

28
firmware/lib/util.h Normal file
View File

@ -0,0 +1,28 @@
/* small timer library, uses timer2 */
#ifndef _UTIL_H
#define _UTIL_H
#include <stdint.h>
#include <stdbool.h>
/* 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