Skip to content

Instantly share code, notes, and snippets.

@dagon666
dagon666 / beeper_1kHz
Created November 16, 2013 12:52
1 kHz tone generation
#include "pca.h"
int main(void)
{
// initialize timer 0 as a tone generator
// TDELAY_IMPLEMENT_T0_INT must be set to 1 in config.h
beeper_init(E_TIMER0);
while (1) {
beeper_beep(E_TIMER0, 1000, 9000);
@dagon666
dagon666 / pwm_int.c
Created November 8, 2013 13:16
pwm playback interrupt
ISR(TIMER0_COMPA_vect, ISR_NOBLOCK) {
if (g_tail != g_head) {
OCR1AH = 0x00;
OCR1BH = 0x00;
#if MODE == MODE_8K_16B
uint16_t sample = (0x8000 + (p[(g_tail >> 6)].samples[g_tail & 0x3f]));
OCR1AL = sample >> 8 ;
OCR1BL = (sample) & 0xff;
#else
@dagon666
dagon666 / sample_collect.c
Created October 30, 2013 18:50
sample collection function
static void serial_collect_samples(void *a_data __attribute__((unused))) {
struct packet *data = (struct packet *)a_data;
uint8_t size = 0x00;
uint16_t buffs = (PWM_SAMPLES_BUFFER*PWM_BUFFERS);
uint16_t avail = (buffs + g_head - g_tail) % buffs;
if (avail > 192) {
serial_poll_send("WAIT", 4);
}
@dagon666
dagon666 / buffers.c
Created October 30, 2013 18:46
audio binary packet
#define PWM_SAMPLES_BUFFER 64
#define PWM_BUFFERS 4
struct packet {
uint16_t crc16;
uint8_t num;
uint8_t samples[PWM_SAMPLES_BUFFER];
};
static volatile struct packet p[PWM_BUFFERS];
@dagon666
dagon666 / musicbox.c
Last active December 25, 2015 19:39
Arduino MIDI musicbox
#include "beeper.h"
#include "notes.h"
#include "serial.h"
#include "slip.h"
#include <avr/io.h>
#include <util/delay.h>
struct note {
uint16_t note;
@dagon666
dagon666 / msg.h
Created October 17, 2013 18:18
music box binary messages format
struct note {
uint16_t note;
uint16_t duration;
};
struct packet {
uint16_t crc16;
unsigned char num;
struct note notes[4];
};
@dagon666
dagon666 / mid_converter.pl
Last active December 25, 2015 19:39
mid_converter processing loop
while (1) {
my @alsa_event = MIDI::ALSA::input();
# last event -> exit
last if ($alsa_event[0] == SND_SEQ_EVENT_PORT_UNSUBSCRIBED());
# midi event to midi score event
my @score_event = MIDI::ALSA::alsa2scoreevent( @alsa_event );
# filter out empty events
@dagon666
dagon666 / host_slip.c
Created October 13, 2013 15:19
slip binary IO in C
int main(int argc, char const *argv[]) {
g_fd = 0x00;
struct response *resp;
struct timer_data *data;
uint8_t buff[8] = {0x00};
uint16_t crc = 0x00;
if (argc <= 3) {
fprintf(stderr, "host_slip <prescaler> <ocr> <st_max>\n");
@dagon666
dagon666 / serial_init.c
Created October 13, 2013 14:57
serial port initialization routines
#define BAUD_2400 B2400
#define BAUD_4800 B4800
#define BAUD_9600 B9600
#define BAUD_38400 B38400
#define BAUD_57600 B57600
#define BAUD_115200 B115200
static int tty_attrib_conf(int fd, int speed, int parity) {
@dagon666
dagon666 / crc16.c
Created October 13, 2013 14:55
crc16 implementation taken from the linux kernel
#include <stdint.h>
#include "crc.h"
/** CRC table for the CRC-16. The poly is 0x8005 (x^16 + x^15 + x^2 + 1) */
uint16_t const crc16_table[256] = {
0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40,
0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841,
0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40,