From 19cd3416b06526f72d40cf2d6c8f19df3fa426a8 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Tue, 21 Oct 2014 17:38:37 +0200 Subject: added dolmetsch-ctl --- dolmetsch-ctl/Makefile | 44 ++++++++++ dolmetsch-ctl/dolmetsch-ctl.c | 158 ++++++++++++++++++++++++++++++++++ dolmetsch-ctl/eventqueue.c | 50 +++++++++++ dolmetsch-ctl/eventqueue.h | 30 +++++++ dolmetsch-ctl/keypad.c | 194 ++++++++++++++++++++++++++++++++++++++++++ dolmetsch-ctl/keypad.h | 41 +++++++++ 6 files changed, 517 insertions(+) create mode 100644 dolmetsch-ctl/Makefile create mode 100644 dolmetsch-ctl/dolmetsch-ctl.c create mode 100644 dolmetsch-ctl/eventqueue.c create mode 100644 dolmetsch-ctl/eventqueue.h create mode 100644 dolmetsch-ctl/keypad.c create mode 100644 dolmetsch-ctl/keypad.h diff --git a/dolmetsch-ctl/Makefile b/dolmetsch-ctl/Makefile new file mode 100644 index 0000000..ab842a1 --- /dev/null +++ b/dolmetsch-ctl/Makefile @@ -0,0 +1,44 @@ +## +## spreadspace avr utils +## +## +## Copyright (C) 2013 Christian Pointner +## +## This file is part of spreadspace avr utils. +## +## spreadspace avr utils 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 utils 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 utils. If not, see . +## + +NAME := dolmetsch-ctl +BOARD_TYPE := teensy2 +OBJ := $(NAME).o keypad.o eventqueue.o +LIBS := util led lufa-descriptor-midi +EXTERNAL_LIBS := lufa + +LUFA_PATH := ../contrib/LUFA-120219 +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\"" -D USB_MANUFACTURER_LEN=7 +LUFA_OPTS += -D USB_PRODUCT="L\"dolmetsch controller\"" -D USB_PRODUCT_LEN=20 + +LUFA_COMPONENTS := USB USBCLASS + +include ../include.mk diff --git a/dolmetsch-ctl/dolmetsch-ctl.c b/dolmetsch-ctl/dolmetsch-ctl.c new file mode 100644 index 0000000..87a2f62 --- /dev/null +++ b/dolmetsch-ctl/dolmetsch-ctl.c @@ -0,0 +1,158 @@ +/* + * spreadspace avr utils + * + * + * Copyright (C) 2013 Christian Pointner + * + * This file is part of spreadspace avr utils. + * + * spreadspace avr utils 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 utils 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 utils. If not, see . + */ + +#include +#include +#include +#include +#include + +/* + LUFA Library + Copyright (C) Dean Camera, 2012. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +*/ +#include +#include +#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 = 1, + + .DataINEndpointNumber = MIDI_STREAM_IN_EPNUM, + .DataINEndpointSize = MIDI_STREAM_EPSIZE, + .DataINEndpointDoubleBank = false, + + .DataOUTEndpointNumber = MIDI_STREAM_OUT_EPNUM, + .DataOUTEndpointSize = MIDI_STREAM_EPSIZE, + .DataOUTEndpointDoubleBank = false, + }, + }; + +/** 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_COMMAND_CONTROL_CHANGE 0xB0 + +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((note >= KEYPAD_MIDI_NOTE_OFFSET && note < KEYPAD_MIDI_NOTE_OFFSET + KEYPAD_NUM_KEYS) || // KEYPAD + note == KEYPAD_MIDI_NOTE_ALL) { + if(cmd == MIDI_COMMAND_NOTE_ON) { + switch(value) { + case 0: keypad_led_on(note - KEYPAD_MIDI_NOTE_OFFSET); break; + case 1: keypad_led_toggle(note - KEYPAD_MIDI_NOTE_OFFSET); break; + default: keypad_led_blink(note, value); break; + } + } else if(cmd == MIDI_COMMAND_NOTE_OFF) + keypad_led_off(note - KEYPAD_MIDI_NOTE_OFFSET); + + } + } +} + +static void process_outgoing_midi(void) +{ + MIDI_EventPacket_t MIDIEventOn = (MIDI_EventPacket_t) + { + .CableNumber = 0, // TODO: hardcoded value + .Command = (MIDI_COMMAND_NOTE_ON >> 4), + .Data1 = MIDI_COMMAND_NOTE_ON | 0, + .Data2 = 0, + .Data3 = 0x7F, + }; + MIDI_EventPacket_t MIDIEventOff = (MIDI_EventPacket_t) + { + .CableNumber = 0, // TODO: hardcoded value + .Command = (MIDI_COMMAND_NOTE_OFF >> 4), + .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(); + 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/dolmetsch-ctl/eventqueue.c b/dolmetsch-ctl/eventqueue.c new file mode 100644 index 0000000..26f64fc --- /dev/null +++ b/dolmetsch-ctl/eventqueue.c @@ -0,0 +1,50 @@ +/* + * spreadspace avr utils + * + * + * Copyright (C) 2013 Christian Pointner + * + * This file is part of spreadspace avr utils. + * + * spreadspace avr utils 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 utils 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 utils. If not, see . + */ + +#include +#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/dolmetsch-ctl/eventqueue.h b/dolmetsch-ctl/eventqueue.h new file mode 100644 index 0000000..8e20903 --- /dev/null +++ b/dolmetsch-ctl/eventqueue.h @@ -0,0 +1,30 @@ +/* + * spreadspace avr utils + * + * + * Copyright (C) 2013 Christian Pointner + * + * This file is part of spreadspace avr utils. + * + * spreadspace avr utils 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 utils 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 utils. If not, see . + */ + +#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/dolmetsch-ctl/keypad.c b/dolmetsch-ctl/keypad.c new file mode 100644 index 0000000..1321519 --- /dev/null +++ b/dolmetsch-ctl/keypad.c @@ -0,0 +1,194 @@ +/* + * spreadspace avr utils + * + * + * Copyright (C) 2013 Christian Pointner + * + * This file is part of spreadspace avr utils. + * + * spreadspace avr utils 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 utils 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 utils. If not, see . + */ + +#include +#include + +#include "keypad.h" +#include "eventqueue.h" + +#define KEYPAD_PIN PINB +#define KEYPAD_PORT PORTB +#define KEYPAD_DDR DDRB + +#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 delay; + uint16_t cnt; +} led_blink_state[KEYPAD_NUM_KEYS]; + +void keypad_init(void) +{ + KEYPAD_DDR = 0x00; + KEYPAD_PORT = 0x0F; + 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].delay = 0; + led_blink_state[i].cnt = 0; + } + + /* DDRC = 0xFF; */ + /* DDRB |= 0xF0; */ + /* DDRD |= 0xF0; */ + + /* PORTC = 0x00; */ + /* PORTB &= 0x0F; */ + /* PORTD &= 0x0F; */ +} + +void keypad_led_on(uint8_t led) +{ + /* switch(led) { */ + /* case 0: */ + /* case 1: */ + /* case 2: */ + /* case 3: */ + /* case 4: */ + /* case 5: */ + /* case 6: */ + /* case 7: PORTC |= (1 << led); break; */ + /* case 8: */ + /* case 9: */ + /* case 10: */ + /* case 11: PORTB |= (1 << (led - 8 + 4)); break; */ + /* case 12: */ + /* case 13: */ + /* case 14: */ + /* case 15: PORTD |= (1 << (led - 12 + 4)); break; */ + /* case KEYPAD_MIDI_NOTE_ALL - KEYPAD_MIDI_NOTE_OFFSET: PORTC = 0xFF; PORTB |= 0xF0; PORTD |= 0xF0; break; */ + /* } */ + /* keypad_led_blink(led, 0); */ +} + +void keypad_led_off(uint8_t led) +{ + /* switch(led) { */ + /* case 0: */ + /* case 1: */ + /* case 2: */ + /* case 3: */ + /* case 4: */ + /* case 5: */ + /* case 6: */ + /* case 7: PORTC &= ~(1 << led); break; */ + /* case 8: */ + /* case 9: */ + /* case 10: */ + /* case 11: PORTB &= ~(1 << (led - 8 + 4)); break; */ + /* case 12: */ + /* case 13: */ + /* case 14: */ + /* case 15: PORTD &= ~(1 << (led - 12 + 4)); break; */ + /* case KEYPAD_MIDI_NOTE_ALL - KEYPAD_MIDI_NOTE_OFFSET: PORTC = 0x00; PORTB &= 0x0F; PORTD &= 0x0F; break; */ + /* } */ + /* keypad_led_blink(led, 0); */ +} + +void keypad_led_toggle(uint8_t led) +{ + /* switch(led) { */ + /* case 0: */ + /* case 1: */ + /* case 2: */ + /* case 3: */ + /* case 4: */ + /* case 5: */ + /* case 6: */ + /* case 7: PORTC ^= (1 << led); break; */ + /* case 8: */ + /* case 9: */ + /* case 10: */ + /* case 11: PORTB ^= (1 << (led - 8 + 4)); break; */ + /* case 12: */ + /* case 13: */ + /* case 14: */ + /* case 15: PORTD ^= (1 << (led - 12 + 4)); break; */ + /* case KEYPAD_MIDI_NOTE_ALL - KEYPAD_MIDI_NOTE_OFFSET: PORTC ^= 0xFF; PORTB ^= 0xF0; PORTD ^= 0xF0; break; */ + /* } */ +} + +void keypad_led_blink(uint8_t led, uint8_t value) +{ + if(led < KEYPAD_NUM_KEYS) { + led_blink_state[led].delay = value * 64; + led_blink_state[led].cnt = 0; + } else { + uint8_t i; + for(i = 0; i < KEYPAD_NUM_KEYS; ++i) { + led_blink_state[i].delay = value * 64; + 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].delay > 1) { + if(++led_blink_state[key_idx].cnt >= led_blink_state[key_idx].delay) { + 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(10); + + 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); + keypad_key_lowpass(key_idx, current_state); + + keypad_led_blinking(key_idx); + } + } + KEYPAD_DDR = 0x00; +} diff --git a/dolmetsch-ctl/keypad.h b/dolmetsch-ctl/keypad.h new file mode 100644 index 0000000..bb671d0 --- /dev/null +++ b/dolmetsch-ctl/keypad.h @@ -0,0 +1,41 @@ +/* + * spreadspace avr utils + * + * + * Copyright (C) 2013 Christian Pointner + * + * This file is part of spreadspace avr utils. + * + * spreadspace avr utils 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 utils 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 utils. If not, see . + */ + +#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 -- cgit v1.2.3