Source

midi / midi-main.c

Full commit
/** \file
 * USB interface from MIDI pedals
 *
 * Read an MIDI streamand generate key press events
 * based on the events.
 *
 * (c) 2012 Trammell Hudson <hudson@osresearch.net>
 *
 * Based on the
 * Keyboard example with debug channel, for Teensy USB Development Board
 * http://www.pjrc.com/teensy/usb_keyboard.html
 * Copyright (c) 2008 PJRC.COM, LLC
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "usb_keyboard_debug.h"
#include "print.h"

#define CPU_PRESCALE(n)	(CLKPR = 0x80, CLKPR = (n))

#define LED_PIN (1 << 6)

static inline void
led_on(void)
{
	PORTD |= LED_PIN;
	DDRB |=  1 << 7; // OC0A enabled
}

static inline void
led_off(void)
{
	PORTD &= ~LED_PIN;
	DDRB &=  ~(1 << 7); // OC0A disabled
}

static inline void
led_config(void)
{
	DDRD |= LED_PIN;
}


static inline int
midi_ready(void)
{
	return bit_is_set(UCSR1A, RXC1);
}

static uint8_t
midi_getc(void)
{
	while (!midi_ready())
		continue;
	return UDR1;
}


/** Buttons */
static const uint8_t button_map[10] = {
	[0] = KEY_ESC,
	[1] = KEY_SPACE,
	[2] = KEY_INSERT | 0x80,
	[3] = KEY_DOWN,
	[4] = KEY_UP,
	[5] = KEY_F6,
	[6] = KEY_F7,
	[7] = KEY_F8,
	[8] = KEY_F9,
	[9] = KEY_F10,
};


int main(void)
{
	uint8_t b, d, mask, i, reset_idle;
	uint8_t b_prev=0xFF, d_prev=0xFF;

	// set for 16 MHz clock
	CPU_PRESCALE(0);
	led_config();

	// Configure all port B and port D pins as inputs with pullup resistors.
	// See the "Using I/O Pins" page for details.
	// http://www.pjrc.com/teensy/pins.html
	DDRD = 0x00;
	DDRB = 0x00;
	PORTB = 0xFF;
	PORTD = 0xFF;

	// Initialize the USB, and then wait for the host to set configuration.
	// If the Teensy is powered without a PC connected to the USB port,
	// this will wait forever.
	usb_init();
	while (!usb_configured())
	{
		/* busy wait */
	}

	// Turn on the hardware UART for MIDI,
	// 31250 N81 == 1600000 MHz / 32
	UBRR1 = 32;
	UCSR1C = 0
		| (0 << UCSZ12)
		| (1 << UCSZ11)
		| (1 << UCSZ11)
		;

	UCSR1B = (1 << RXEN1);

	// Configure timer 1 to overflow every 10 Hz for repeated
	// mouse motion.  This uses CTC mode; 
	OCR1A = 2500;
	TCCR1A = 0; // WGM11 and WGM10 = 0
	TCCR1B = 0
		| (0 << WGM13) // CTC mode
		| (1 << WGM12)
		| (0 << CS12)
		| (1 << CS11)
		| (1 << CS10) // clock / 64 == 16 Mhz / 64 / 25000 = 10 Hz
		;

	// Wait an extra second for the PC's operating system to load drivers
	// and do whatever it does to actually be ready for input
	_delay_ms(1000);
	print("MIDI decoder\r\n");

	int8_t pedal_value = 0;

	while (1)
	{
		if (!midi_ready())
		{
			if (pedal_value == 0)
				continue;

			if (bit_is_clear(TIFR1, OCF1A))
				continue;

			TIFR1 |= 1 << OCF1A;
			usb_mouse_move(0, 0, pedal_value);
			continue;
		}

		uint8_t c = midi_getc();

		if (c == 0x40)
		{
			uint8_t program = midi_getc();
			uint8_t button = program % 10;
			program /= 10;

			uint8_t c = button_map[button];
			if (c)
			{
				const uint8_t modbit = c & 0x80 ? KEY_SHIFT : 0;
				c &= ~0x80;
				usb_keyboard_press(c, modbit);
			}

			print("button ");
			phex(program);
			print(" ");
			phex(button + 1);
			print("\r\n");
			continue;
		}

		if (c == 0x30)
		{
			uint8_t chan = midi_getc();
			int8_t val = midi_getc();
			print("pedal ");
			phex(chan);
			print(" ");
			phex(val);
			print("\r\n");

			if (val < 2)
			{
				pedal_value = 0;
				continue;
			}

			// Small displacements still need to move the
			// screen, just not as fast.
			if (val < 32)
			{
				pedal_value = -val / 2;
				OCR1A = 64000; // slow
			} else
			if (val < 64)
			{
				pedal_value = -val / 2;
				OCR1A = 50000; // moderate speed
			} else
			if (val < 96)
			{
				pedal_value = -val / 4;
				OCR1A = 40000; // increasing speed
			} else
			{
				pedal_value = -val / 4;
				OCR1A = 32000; // high speed;
			}

			continue;
		}

		if (c == 0x10)
		{
			// Not sure?  Momentary?
			uint8_t chan = midi_getc();
			uint8_t val = midi_getc();
			print("momentary ");
			phex(chan);
			print(" ");
			phex(val);
			print("\r\n");
			continue;
		}

		phex(c);
		print(" ?\r\n");
	}

	// Not reached
}