summaryrefslogtreecommitdiff
path: root/firmware
diff options
context:
space:
mode:
Diffstat (limited to 'firmware')
-rw-r--r--firmware/Makefile45
-rw-r--r--firmware/dolmetsch-ctl.c166
-rw-r--r--firmware/eventqueue.c50
-rw-r--r--firmware/eventqueue.h30
-rw-r--r--firmware/keypad.c181
-rw-r--r--firmware/keypad.h41
6 files changed, 513 insertions, 0 deletions
diff --git a/firmware/Makefile b/firmware/Makefile
new file mode 100644
index 0000000..d662022
--- /dev/null
+++ b/firmware/Makefile
@@ -0,0 +1,45 @@
+##
+## spreadspace avr projects
+##
+##
+## Copyright (C) 2013-2015 Christian Pointner <equinox@spreadspace.org>
+##
+## This file is part of spreadspace avr projects.
+##
+## spreadspace avr projects is free software: you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published by
+## the Free Software Foundation, either version 3 of the License, or
+## any later version.
+##
+## spreadspace avr projects is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with spreadspace avr projects. If not, see <http://www.gnu.org/licenses/>.
+##
+
+NAME := dolmetsch-ctl
+BOARD_TYPE := teensy2
+OBJ := $(NAME).o keypad.o eventqueue.o
+LIBS := util led lufa-descriptor-midi
+EXTERNAL_LIBS := lufa
+SPREADAVR_PATH := ../contrib/avr-utils
+
+LUFA_PATH := $(SPREADAVR_PATH)/contrib/lufa-LUFA-140928
+LUFA_OPTS = -D USB_DEVICE_ONLY
+LUFA_OPTS += -D DEVICE_STATE_AS_GPIOR=0
+LUFA_OPTS += -D ORDERED_EP_CONFIG
+LUFA_OPTS += -D FIXED_CONTROL_ENDPOINT_SIZE=8
+LUFA_OPTS += -D FIXED_NUM_CONFIGURATIONS=1
+LUFA_OPTS += -D USE_FLASH_DESCRIPTORS
+LUFA_OPTS += -D USE_STATIC_OPTIONS="(USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL)"
+LUFA_OPTS += -D INTERRUPT_CONTROL_ENDPOINT
+
+LUFA_OPTS += -D USB_MANUFACTURER="L\"equinox\""
+LUFA_OPTS += -D USB_PRODUCT="L\"dolmetsch controller\""
+
+LUFA_COMPONENTS := USB USBCLASS
+
+include $(SPREADAVR_PATH)/include.mk
diff --git a/firmware/dolmetsch-ctl.c b/firmware/dolmetsch-ctl.c
new file mode 100644
index 0000000..1308a5b
--- /dev/null
+++ b/firmware/dolmetsch-ctl.c
@@ -0,0 +1,166 @@
+/*
+ * spreadspace avr projects
+ *
+ *
+ * Copyright (C) 2013-2015 Christian Pointner <equinox@spreadspace.org>
+ *
+ * This file is part of spreadspace avr projects.
+ *
+ * spreadspace avr projects is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * any later version.
+ *
+ * spreadspace avr projects is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with spreadspace avr projects. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <avr/io.h>
+#include <avr/wdt.h>
+#include <avr/interrupt.h>
+#include <avr/power.h>
+#include <stdio.h>
+
+/*
+ LUFA Library
+ Copyright (C) Dean Camera, 2012.
+
+ dean [at] fourwalledcubicle [dot] com
+ www.lufa-lib.org
+*/
+#include <LUFA/Version.h>
+#include <LUFA/Drivers/USB/USB.h>
+#include "lufa-descriptor-midi.h"
+
+/** LUFA MIDI Class driver interface configuration and state information. This structure is
+ * passed to all MIDI Class driver functions, so that multiple instances of the same class
+ * within a device can be differentiated from one another.
+ */
+USB_ClassInfo_MIDI_Device_t MIDI_Interface =
+ {
+ .Config =
+ {
+ .StreamingInterfaceNumber = INTERFACE_ID_AudioStream,
+ .DataINEndpoint =
+ {
+ .Address = MIDI_STREAM_IN_EPADDR,
+ .Size = MIDI_STREAM_EPSIZE,
+ .Banks = 1,
+ },
+ .DataOUTEndpoint =
+ {
+ .Address = MIDI_STREAM_OUT_EPADDR,
+ .Size = MIDI_STREAM_EPSIZE,
+ .Banks = 1,
+ },
+ },
+ };
+
+/** Event handler for the library USB Configuration Changed event. */
+void EVENT_USB_Device_ConfigurationChanged(void)
+{
+ MIDI_Device_ConfigureEndpoints(&MIDI_Interface);
+}
+
+/** Event handler for the library USB Control Request reception event. */
+void EVENT_USB_Device_ControlRequest(void)
+{
+ MIDI_Device_ProcessControlRequest(&MIDI_Interface);
+}
+
+/* end LUFA MIDI Class driver specific definitions*/
+
+#include "util.h"
+#include "keypad.h"
+#include "eventqueue.h"
+
+#define MIDI_CABLENUMBER 1
+
+static void process_incoming_midi(void)
+{
+ MIDI_EventPacket_t ReceivedMIDIEvent;
+ while(MIDI_Device_ReceiveEventPacket(&MIDI_Interface, &ReceivedMIDIEvent)) {
+ uint8_t cmd = ReceivedMIDIEvent.Data1 & 0xF0;
+ uint8_t note = ReceivedMIDIEvent.Data2;
+ uint8_t value = ReceivedMIDIEvent.Data3 & 0x7F;
+ if(cmd == MIDI_COMMAND_PROGRAM_CHANGE) {
+ switch(note) {
+ case 127: reset2bootloader();
+ }
+ continue;
+ }
+ if((note >= KEYPAD_MIDI_NOTE_OFFSET && note < KEYPAD_MIDI_NOTE_OFFSET + KEYPAD_NUM_KEYS) || // KEYPAD
+ note == KEYPAD_MIDI_NOTE_ALL) {
+ if(cmd == MIDI_COMMAND_CONTROL_CHANGE) {
+ switch(value) {
+ case 0: keypad_led_off(note - KEYPAD_MIDI_NOTE_OFFSET); break;
+ case 1: keypad_led_on(note - KEYPAD_MIDI_NOTE_OFFSET); break;
+ case 2: keypad_led_toggle(note - KEYPAD_MIDI_NOTE_OFFSET); break;
+ default: keypad_led_blink(note, value); break;
+ }
+ }
+ }
+ }
+}
+
+static void process_outgoing_midi(void)
+{
+ MIDI_EventPacket_t MIDIEventOn = (MIDI_EventPacket_t)
+ {
+ .Event = MIDI_EVENT(MIDI_CHANNEL(MIDI_CABLENUMBER), MIDI_COMMAND_NOTE_ON),
+ .Data1 = MIDI_COMMAND_NOTE_ON | 0,
+ .Data2 = 0,
+ .Data3 = 0x7F,
+ };
+ MIDI_EventPacket_t MIDIEventOff = (MIDI_EventPacket_t)
+ {
+ .Event = MIDI_EVENT(MIDI_CHANNEL(MIDI_CABLENUMBER), MIDI_COMMAND_NOTE_OFF),
+ .Data1 = MIDI_COMMAND_NOTE_OFF | 0,
+ .Data2 = 0,
+ .Data3 = 0,
+ };
+
+ uint8_t key, state;
+ while(eventqueue_pop(&key, &state)) {
+ if (USB_DeviceState == DEVICE_STATE_Configured) {
+ MIDI_EventPacket_t* MIDIEvent = NULL;
+ MIDIEvent = state ? &MIDIEventOn : &MIDIEventOff;
+ MIDIEvent->Data2 = key;
+
+ if(MIDIEvent)
+ MIDI_Device_SendEventPacket(&MIDI_Interface, MIDIEvent);
+ }
+ }
+ if (USB_DeviceState == DEVICE_STATE_Configured)
+ MIDI_Device_Flush(&MIDI_Interface);
+}
+
+int main(void)
+{
+ MCUSR &= ~(1 << WDRF);
+ wdt_disable();
+
+ cpu_init();
+ jtag_disable();
+ eventqueue_init();
+ keypad_init();
+
+ USB_Init();
+ sei();
+
+ for(;;) {
+ process_incoming_midi();
+
+ keypad_task();
+
+ process_outgoing_midi();
+
+ MIDI_Device_USBTask(&MIDI_Interface);
+ USB_USBTask();
+ }
+}
diff --git a/firmware/eventqueue.c b/firmware/eventqueue.c
new file mode 100644
index 0000000..40f1f21
--- /dev/null
+++ b/firmware/eventqueue.c
@@ -0,0 +1,50 @@
+/*
+ * spreadspace avr projects
+ *
+ *
+ * Copyright (C) 2013-2015 Christian Pointner <equinox@spreadspace.org>
+ *
+ * This file is part of spreadspace avr projects.
+ *
+ * spreadspace avr projects is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * any later version.
+ *
+ * spreadspace avr projects is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with spreadspace avr projects. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <LUFA/Drivers/Misc/RingBuffer.h>
+#include "eventqueue.h"
+
+static RingBuffer_t event_queue;
+static uint8_t event_queue_data[16];
+
+void eventqueue_init(void)
+{
+ RingBuffer_InitBuffer(&event_queue, event_queue_data, sizeof(event_queue_data));
+}
+
+uint8_t eventqueue_pop(uint8_t* key, uint8_t* state)
+{
+ if (RingBuffer_IsEmpty(&event_queue))
+ return 0;
+
+ uint8_t event = RingBuffer_Remove(&event_queue);
+ *state = (event & 0x80) ? 1 : 0;
+ *key = event & 0x7F;
+ return 1;
+}
+
+void eventqueue_push(uint8_t key, uint8_t state)
+{
+ uint8_t event = (state) ? 0x80 : 0;
+ event |= (key & 0x7F);
+ RingBuffer_Insert(&event_queue, event);
+}
diff --git a/firmware/eventqueue.h b/firmware/eventqueue.h
new file mode 100644
index 0000000..a4ec40c
--- /dev/null
+++ b/firmware/eventqueue.h
@@ -0,0 +1,30 @@
+/*
+ * spreadspace avr projects
+ *
+ *
+ * Copyright (C) 2013-2015 Christian Pointner <equinox@spreadspace.org>
+ *
+ * This file is part of spreadspace avr projects.
+ *
+ * spreadspace avr projects is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * any later version.
+ *
+ * spreadspace avr projects is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with spreadspace avr projects. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef DOLMETSCHCTL_eventqueue_h_INCLUDED
+#define DOLMETSCHCTL_eventqueue_h_INCLUDED
+
+void eventqueue_init(void);
+uint8_t eventqueue_pop(uint8_t* key, uint8_t* state);
+void eventqueue_push(uint8_t key, uint8_t state);
+
+#endif
diff --git a/firmware/keypad.c b/firmware/keypad.c
new file mode 100644
index 0000000..1291a6e
--- /dev/null
+++ b/firmware/keypad.c
@@ -0,0 +1,181 @@
+/*
+ * spreadspace avr projects
+ *
+ *
+ * Copyright (C) 2013-2015 Christian Pointner <equinox@spreadspace.org>
+ *
+ * This file is part of spreadspace avr projects.
+ *
+ * spreadspace avr projects is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * any later version.
+ *
+ * spreadspace avr projects is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with spreadspace avr projects. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <avr/io.h>
+#include <util/delay.h>
+
+#include "keypad.h"
+#include "eventqueue.h"
+
+#define KEYPAD_LP_CNT_MAX 200
+static struct {
+ uint8_t last_sent;
+ int16_t lp_cnt;
+} keypad_state[KEYPAD_NUM_KEYS];
+
+static struct {
+ uint16_t timebase;
+ uint16_t cnt;
+} led_blink_state[KEYPAD_NUM_KEYS];
+#define KEYPAD_BLINK_TIMEBASE_MULT 128
+
+void keypad_init(void)
+{
+ // KEYS are at F4..F7
+ DDRF &= 0x0F;
+ PORTF |= 0xF0;
+ uint8_t i;
+ for(i = 0; i < KEYPAD_NUM_KEYS; ++i) {
+ keypad_state[i].last_sent = 0;
+ keypad_state[i].lp_cnt = 0;
+
+ led_blink_state[i].timebase = 0;
+ led_blink_state[i].cnt = 0;
+ }
+
+ // LEDS are at D0..D3
+ DDRD |= 0x0F;
+ PORTD &= 0xFC;
+ PORTD |= 0x0C;
+}
+
+void keypad_led_on(uint8_t led)
+{
+ switch(led) {
+ case 0: PORTD |= 0x01; break;
+ case 1: PORTD |= 0x02; break;
+ case 2: PORTD &= 0xFB; break;
+ case 3: PORTD &= 0xF7; break;
+ case KEYPAD_MIDI_NOTE_ALL - KEYPAD_MIDI_NOTE_OFFSET: PORTD |= 0x03; PORTD &= 0xF3;; break;
+ }
+ keypad_led_blink(led, 0);
+}
+
+void keypad_led_off(uint8_t led)
+{
+ switch(led) {
+ case 0: PORTD &= 0xFE; break;
+ case 1: PORTD &= 0xFD; break;
+ case 2: PORTD |= 0x04; break;
+ case 3: PORTD |= 0x08; break;
+ case KEYPAD_MIDI_NOTE_ALL - KEYPAD_MIDI_NOTE_OFFSET: PORTD &= 0xFC; PORTD |= 0x0C; break;
+ }
+ keypad_led_blink(led, 0);
+}
+
+void keypad_led_toggle(uint8_t led)
+{
+ switch(led) {
+ case 0:
+ case 1:
+ case 2:
+ case 3: PORTD ^= (1 << led); break;
+ case KEYPAD_MIDI_NOTE_ALL - KEYPAD_MIDI_NOTE_OFFSET: PORTD ^= 0x0F; break;
+ }
+}
+
+static uint8_t keypad_led_get_state(uint8_t led)
+{
+ switch(led) {
+ case 0:
+ case 1: return (PORTD & (1 << led)) ? 0 : 1;
+ case 2:
+ case 3: return (PORTD & (1 << led)) ? 1 : 0;
+ default: return 0;
+ }
+}
+
+static void keypad_led_blink_sync(uint8_t led)
+{
+ uint8_t i;
+ for(i = 0; i < KEYPAD_NUM_KEYS; ++i) {
+ if(i != led && led_blink_state[i].timebase == led_blink_state[led].timebase) {
+ led_blink_state[led].cnt = led_blink_state[i].cnt;
+ if(keypad_led_get_state(led) != keypad_led_get_state(i))
+ keypad_led_toggle(led);
+ break;
+ }
+ }
+}
+
+void keypad_led_blink(uint8_t led, uint8_t value)
+{
+ if(led < KEYPAD_NUM_KEYS) {
+ led_blink_state[led].timebase = value * KEYPAD_BLINK_TIMEBASE_MULT;
+ led_blink_state[led].cnt = 0;
+ if(value)
+ keypad_led_blink_sync(led);
+
+ } else {
+ uint8_t i;
+ for(i = 0; i < KEYPAD_NUM_KEYS; ++i) {
+ led_blink_state[i].timebase = value * KEYPAD_BLINK_TIMEBASE_MULT;
+ led_blink_state[i].cnt = 0;
+ }
+ }
+}
+
+static inline void keypad_key_lowpass(uint8_t key_idx, uint8_t current_state)
+{
+ keypad_state[key_idx].lp_cnt += current_state ? -1 : +1;
+ if(keypad_state[key_idx].lp_cnt <= 0 ||
+ keypad_state[key_idx].lp_cnt >= KEYPAD_LP_CNT_MAX) {
+
+ keypad_state[key_idx].lp_cnt = keypad_state[key_idx].lp_cnt <= 0 ? 0 : KEYPAD_LP_CNT_MAX;
+
+ if(current_state != keypad_state[key_idx].last_sent) {
+ keypad_state[key_idx].last_sent = current_state;
+ eventqueue_push(KEYPAD_MIDI_NOTE_OFFSET + key_idx, ((current_state) ? 0 : 1));
+ }
+ }
+}
+
+static inline void keypad_led_blinking(uint8_t key_idx)
+{
+ if(led_blink_state[key_idx].timebase > 1) {
+ if(++led_blink_state[key_idx].cnt >= led_blink_state[key_idx].timebase) {
+ keypad_led_toggle(key_idx);
+ led_blink_state[key_idx].cnt = 0;
+ }
+ }
+}
+
+void keypad_task(void)
+{
+ uint8_t col, row;
+ for(col = 0; col < KEYPAD_NUM_COLS; ++col) {
+ /* KEYPAD_DDR = 1 << (col + 4); */
+ /* KEYPAD_PORT = 0x0F; */
+ _delay_us(20);
+
+ for(row = 0; row < KEYPAD_NUM_ROWS; ++row) {
+ uint8_t key_idx = col*KEYPAD_NUM_ROWS + row;
+
+// uint8_t current_state = KEYPAD_PIN & (1 << row);
+ uint8_t current_state = PINF & (1 << (row+4));
+ keypad_key_lowpass(key_idx, current_state);
+
+ keypad_led_blinking(key_idx);
+ }
+ }
+// KEYPAD_DDR = 0x00;
+}
diff --git a/firmware/keypad.h b/firmware/keypad.h
new file mode 100644
index 0000000..a1202bc
--- /dev/null
+++ b/firmware/keypad.h
@@ -0,0 +1,41 @@
+/*
+ * spreadspace avr projects
+ *
+ *
+ * Copyright (C) 2013-2015 Christian Pointner <equinox@spreadspace.org>
+ *
+ * This file is part of spreadspace avr projects.
+ *
+ * spreadspace avr projects is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * any later version.
+ *
+ * spreadspace avr projects is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with spreadspace avr projects. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef DOLMETSCHCTL_keypad_h_INCLUDED
+#define DOLMETSCHCTL_keypad_h_INCLUDED
+
+#define KEYPAD_NUM_COLS 1
+#define KEYPAD_NUM_ROWS 4
+#define KEYPAD_NUM_KEYS KEYPAD_NUM_COLS * KEYPAD_NUM_ROWS
+#define KEYPAD_MIDI_NOTE_OFFSET 0
+#define KEYPAD_MIDI_NOTE_ALL KEYPAD_MIDI_NOTE_OFFSET + 31
+
+void keypad_init(void);
+
+void keypad_led_on(uint8_t led);
+void keypad_led_off(uint8_t led);
+void keypad_led_toggle(uint8_t led);
+void keypad_led_blink(uint8_t led, uint8_t value);
+
+void keypad_task(void);
+
+#endif