pentabug/firmware/apps/mariotheme.c

121 lines
5.3 KiB
C
Raw Normal View History

2013-09-05 00:46:54 +02:00
#include <stdlib.h>
#include <pentabug/hal.h>
#include <pentabug/helper.h>
#include <pentabug/app.h>
#include <pentabug/timer.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
static uint16_t osc[3];
static uint16_t sample;
static uint8_t note;
static uint8_t row;
enum {
NOTE_LENGTH = 600,
2013-09-05 01:25:37 +02:00
DUTY = 0xf000,
2013-09-05 00:46:54 +02:00
2013-09-05 01:25:37 +02:00
xxx = 0,
2013-09-05 00:46:54 +02:00
g_0, gs0, a_0, as0, b_0,
c_1, cs1, d_1, ds1, e_1, f_1, fs1, g_1, gs1, a_1, as1, b_1,
c_2, cs2, d_2, ds2, e_2, f_2, fs2, g_2, gs2, a_2, as2, b_2,
c_3, cs3, d_3, ds3, e_3, f_3, fs3, g_3, gs3, a_3, as3, b_3,
c_4,
};
const int patterns[][3][48] PROGMEM = {
{
2013-09-05 00:52:50 +02:00
{e_3,e_3,xxx,e_3,e_3,xxx,xxx,xxx,xxx,e_3,e_3,xxx,xxx,xxx,xxx,c_3,c_3,xxx,e_3,e_3,xxx,xxx,xxx,xxx,g_3,g_3,xxx,xxx,xxx,xxx,xxx,xxx,xxx,xxx,xxx,xxx,xxx,xxx,xxx,xxx,xxx,xxx,xxx,xxx,xxx,xxx,xxx,xxx,},
{fs2,fs2,xxx,fs2,fs2,xxx,xxx,xxx,xxx,fs2,fs2,xxx,xxx,xxx,xxx,fs2,fs2,xxx,fs2,fs2,xxx,xxx,xxx,xxx,b_2,b_2,xxx,xxx,xxx,xxx,xxx,xxx,xxx,xxx,xxx,xxx,g_2,g_2,xxx,xxx,xxx,xxx,xxx,xxx,xxx,xxx,xxx,xxx,},
{d_1,d_1,xxx,d_1,d_1,xxx,xxx,xxx,xxx,d_1,d_1,xxx,xxx,xxx,xxx,d_1,d_1,xxx,d_1,d_1,xxx,xxx,xxx,xxx,g_2,g_2,xxx,xxx,xxx,x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2013-09-05 00:46:54 +02:00
0,
2013-09-05 01:25:37 +02:00
412, 436, 462, 489, 518, 549, 582, 617, 653, 692, 733, 777,
823, 872, 924, 979, 1037, 1099, 1164, 1233, 1306, 1384, 1466,
1554, 1646, 1744, 1848, 1957, 2074, 2197, 2328, 2466, 2613,
2768, 2933, 3107, 3292, 3488, 3695, 3915, 4148, 4394
2013-09-05 00:46:54 +02:00
};
2013-09-05 01:25:37 +02:00
const uint8_t order[] = {
2013-09-05 00:46:54 +02:00
0,
1, 2, 1, 2,
3, 4, 3, 5,
3, 4, 3, 5,
};
static uint8_t prev_amp = 0;
static void mix_mario(void) {
if (++sample >= NOTE_LENGTH) {
sample = 0;
if (++note >= 48) {
note = 0;
2013-09-05 01:25:37 +02:00
if (++row >= ARRAY_SIZE(order)) row = 1; // skip 0th row
2013-09-05 00:46:54 +02:00
}
}
uint8_t p = order[row];
2013-09-05 00:56:41 +02:00
uint8_t n;
n = pgm_read_byte(&patterns[p][0][note]);
if (n == 0) osc[0] = 0;
2013-09-05 01:25:37 +02:00
osc[0] += pgm_read_word(&freq[n]);
2013-09-05 00:56:41 +02:00
n = pgm_read_byte(&patterns[p][1][note]);
if (n == 0) osc[1] = 0;
2013-09-05 01:25:37 +02:00
osc[1] += pgm_read_word(&freq[n]);
2013-09-05 00:56:41 +02:00
n = pgm_read_byte(&patterns[p][2][note]);
if (n == 0) osc[2] = 0;
2013-09-05 01:25:37 +02:00
osc[2] += pgm_read_word(&freq[n]);
2013-09-05 00:46:54 +02:00
2013-09-05 01:25:37 +02:00
uint8_t amp = (osc[0] > DUTY) | (osc[1] > DUTY) | (osc[2] > DUTY);
2013-09-05 00:46:54 +02:00
if (amp != prev_amp) {
buzzer_inv();
prev_amp = amp;
}
}
static void init_mario(void) {
sample = 0;
note = 0;
row = 0;
start_timer(PRESCALE_8, 100, mix_mario);
}
static void run_mario(void) {}
REGISTER(run_mario, init_mario, NULL);