sound effect

This commit is contained in:
twobit 2012-10-13 17:06:27 +02:00
parent d26f20631b
commit 914962951d
3 changed files with 117 additions and 50 deletions

65
firmware/apps/mariofx.c Normal file
View File

@ -0,0 +1,65 @@
#include <inttypes.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#define __DELAY_BACKWARD_COMPATIBLE__
#include <util/delay.h>
#include <stdlib.h>
#include <avr/pgmspace.h>
#include <lib/bughal.h>
#include <lib/freq_table.h>
#include <lib/apps.h>
static uint16_t osc;
static uint16_t sample;
static uint8_t row;
#define LENGTH(x) (sizeof(x) / sizeof(x[0]))
static const uint8_t mushroom[] = {6,1,6,10,13,18,13,1,6,8,13,8,13,18,20,25,20,3,8,11,15,11,15,20,23,27,23};
static void mariofx_ISR(void) {
if (row < LENGTH(mushroom)) {
osc += pgm_read_word(&freq_table[mushroom[row] + 18]) * 3;
if (++sample == 800 ) {
sample = 0;
row++;
}
} else {
if (btn_state(BTNST_SUP, BTN_RIGHT)) {
button_clear(BTN_RIGHT);
row = 0;
}
}
if (osc >= 0xc000) {
PORTB |= (1 << PORTB2);
PORTC &= ~(1<< PORTC5);
}
else {
PORTB &= ~(1<< PORTB2);
PORTC |= (1 << PORTC5);
}
}
static void mariofx(void) {
/* initialisation required */
if (mode_uninitialized) {
mode_uninitialized = false;
init_buzzr();
osc = sample = row = 0;
start_timer(mariofx_ISR);
}
/*deinialisation required */
if (mode_last_tick) {
stop_timer();
}
return;
}
REGISTER(mariofx);

51
firmware/lib/freq_table.c Normal file
View File

@ -0,0 +1,51 @@
#include <avr/pgmspace.h>
const uint16_t freq_table[] PROGMEM = {
/*0x00*/ 0,
/*0x01*/ 130,
/*0x02*/ 137,
/*0x03*/ 145,
/*0x04*/ 154,
/*0x05*/ 163,
/*0x06*/ 173,
/*0x07*/ 183,
/*0x08*/ 194,
/*0x09*/ 206,
/*0x0a*/ 218,
/*0x0b*/ 231,
/*0x0c*/ 245,
/*0x0d*/ 259,
/*0x0e*/ 275,
/*0x0f*/ 291,
/*0x10*/ 308,
/*0x11*/ 327,
/*0x12*/ 346,
/*0x13*/ 367,
/*0x14*/ 388,
/*0x15*/ 412,
/*0x16*/ 436,
/*0x17*/ 462,
/*0x18*/ 489,
/*0x19*/ 518,
/*0x1a*/ 549,
/*0x1b*/ 582,
/*0x1c*/ 617,
/*0x1d*/ 653,
/*0x1e*/ 692,
/*0x1f*/ 733,
/*0x20*/ 777,
/*0x21*/ 823,
/*0x22*/ 872,
/*0x23*/ 924,
/*0x24*/ 979,
/*0x25*/ 1037,
/*0x26*/ 1099,
/*0x27*/ 1164,
/*0x28*/ 1233,
/*0x29*/ 1306,
/*0x2a*/ 1384,
/*0x2b*/ 1466,
/*0x2c*/ 1554,
/*0x2d*/ 1646,
/*0x2e*/ 1744,
/*0x2f*/ 1848,
};

View File

@ -1,50 +1 @@
const uint16_t freq_table[] PROGMEM = { extern const uint16_t freq_table[] PROGMEM;
/*0x00*/ 0,
/*0x01*/ 130,
/*0x02*/ 137,
/*0x03*/ 145,
/*0x04*/ 154,
/*0x05*/ 163,
/*0x06*/ 173,
/*0x07*/ 183,
/*0x08*/ 194,
/*0x09*/ 206,
/*0x0a*/ 218,
/*0x0b*/ 231,
/*0x0c*/ 245,
/*0x0d*/ 259,
/*0x0e*/ 275,
/*0x0f*/ 291,
/*0x10*/ 308,
/*0x11*/ 327,
/*0x12*/ 346,
/*0x13*/ 367,
/*0x14*/ 388,
/*0x15*/ 412,
/*0x16*/ 436,
/*0x17*/ 462,
/*0x18*/ 489,
/*0x19*/ 518,
/*0x1a*/ 549,
/*0x1b*/ 582,
/*0x1c*/ 617,
/*0x1d*/ 653,
/*0x1e*/ 692,
/*0x1f*/ 733,
/*0x20*/ 777,
/*0x21*/ 823,
/*0x22*/ 872,
/*0x23*/ 924,
/*0x24*/ 979,
/*0x25*/ 1037,
/*0x26*/ 1099,
/*0x27*/ 1164,
/*0x28*/ 1233,
/*0x29*/ 1306,
/*0x2a*/ 1384,
/*0x2b*/ 1466,
/*0x2c*/ 1554,
/*0x2d*/ 1646,
/*0x2e*/ 1744,
/*0x2f*/ 1848,
};