updated libaries, debug mode for MIDI libary, commented bambatu.ino sketch

This commit is contained in:
Lukas 2013-05-18 10:28:50 +02:00
parent 89c20bb101
commit d053ca9637
7 changed files with 115 additions and 64 deletions

View File

@ -7,65 +7,73 @@
MIDI Midi; MIDI Midi;
//MCP23017 I2C addresses 0,1,3 and 4 (2 is unused yet)
INPUT_MCP23017 input_MCP23017_0; INPUT_MCP23017 input_MCP23017_0;
INPUT_MCP23017 input_MCP23017_1; INPUT_MCP23017 input_MCP23017_1;
INPUT_MCP23017 input_MCP23017_3; INPUT_MCP23017 input_MCP23017_3;
INPUT_MCP23017 input_MCP23017_4; INPUT_MCP23017 input_MCP23017_4;
//three 74HC4051 on analog pins A0,A1,A2; select on digital pins 8,9,10
INPUT_74HC4051 input_4051_A0; INPUT_74HC4051 input_4051_A0;
INPUT_74HC4051 input_4051_A1; INPUT_74HC4051 input_4051_A1;
INPUT_74HC4051 input_4051_A2; INPUT_74HC4051 input_4051_A2;
ROTARY_ENCODER rotary_encoder_jogwheel_left(rotary_encoder_jogwheel_left_inc, rotary_encoder_jogwheel_left_dec); //three rotary encoders connected to input_MCP23017_1 and input_MCP23017_3
ROTARY_ENCODER rotary_encoder_jogwheel_right(rotary_encoder_jogwheel_right_inc, rotary_encoder_jogwheel_right_dec); ROTARY_ENCODER rotary_encoder_jogwheel_left(rotary_encoder_jogwheel_left_inc, rotary_encoder_jogwheel_left_dec); //input_MCP23017_1 pins 6 and 7
ROTARY_ENCODER rotary_encoder_browse(rotary_encoder_browse_inc,rotary_encoder_browse_dec); ROTARY_ENCODER rotary_encoder_jogwheel_right(rotary_encoder_jogwheel_right_inc, rotary_encoder_jogwheel_right_dec); //input_MCP23017_3 pins 9 and 1
ROTARY_ENCODER rotary_encoder_browse(rotary_encoder_browse_inc,rotary_encoder_browse_dec); //input_MCP23017_3 pins 5 and 6
void setup() { void setup() {
//initialize MIDI
Midi.begin(); Midi.begin();
//initialize MCP23017s
input_MCP23017_0.begin(0,digitalCallback); input_MCP23017_0.begin(0,digitalCallback);
input_MCP23017_1.begin(1,digitalCallback1); input_MCP23017_1.begin(1,digitalCallback1);
input_MCP23017_3.begin(3,digitalCallback3); input_MCP23017_3.begin(3,digitalCallback3);
input_MCP23017_4.begin(4,digitalCallback); input_MCP23017_4.begin(4,digitalCallback);
//initialize 74HC4051s
input_4051_A0.begin(A0,8,9,10,analogCallback); input_4051_A0.begin(A0,8,9,10,analogCallback);
input_4051_A1.begin(A1,8,9,10,analogCallback); input_4051_A1.begin(A1,8,9,10,analogCallback);
input_4051_A2.begin(A2,8,9,10,analogCallback); input_4051_A2.begin(A2,8,9,10,analogCallback);
} }
void loop() { void loop() {
//loop MCP23017s for callbacks
input_MCP23017_0.loop(); input_MCP23017_0.loop();
input_MCP23017_1.loop(); input_MCP23017_1.loop();
input_MCP23017_3.loop(); input_MCP23017_3.loop();
input_MCP23017_4.loop(); input_MCP23017_4.loop();
//loop 74HC4051s for callback
input_4051_A0.loop(); input_4051_A0.loop();
input_4051_A1.loop(); input_4051_A1.loop();
input_4051_A2.loop(); input_4051_A2.loop();
} }
//callback for analog, sends CC message with unique controller id
void analogCallback(int id, int pin, int value) { void analogCallback(int id, int pin, int value) {
Midi.controlChange((id-A0) * 8 + pin,value); Midi.controlChange((id-A0) * 8 + pin,value);
} }
//default callback for buttons, sends note-on/off message with unique note value
void digitalCallback(int id, int pin, int value) { void digitalCallback(int id, int pin, int value) {
if (value == HIGH) if (value == HIGH)
Midi.noteOn(id * 16 + pin, MIDI_MAX_DATA); Midi.noteOn(id * 16 + pin, MIDI_MAX_DATA);
else Midi.noteOff(id * 16 + pin); else Midi.noteOff(id * 16 + pin);
} }
//callback for input_MCP23017_1, checks for rotary_encoder_jogwheel_left, otherwise default callback
void digitalCallback1(int id, int pin, int value) { void digitalCallback1(int id, int pin, int value) {
if (pin == 6) if (pin == 6)
rotary_encoder_jogwheel_left.setPinA(value); rotary_encoder_jogwheel_left.setPinA(value);
if (pin == 7) if (pin == 7)
rotary_encoder_jogwheel_left.setPinB(value); rotary_encoder_jogwheel_left.setPinB(value);
if (pin != 6 && pin !=7) { if (pin != 6 && pin !=7)
if (value == HIGH) digitalCallback(id,pin,value);
Midi.noteOn(id * 16 + pin, MIDI_MAX_DATA);
else Midi.noteOff(id * 16 + pin);
}
} }
//callback for input_MCP23017_3, checks for rotary_encoder_jogwheel_right and rotary_encoder_browse, otherwise default callback
void digitalCallback3(int id, int pin, int value) { void digitalCallback3(int id, int pin, int value) {
if (pin == 9) if (pin == 9)
rotary_encoder_jogwheel_right.setPinA(value); rotary_encoder_jogwheel_right.setPinA(value);
@ -75,37 +83,39 @@ void digitalCallback3(int id, int pin, int value) {
rotary_encoder_browse.setPinA(value); rotary_encoder_browse.setPinA(value);
if (pin == 5) if (pin == 5)
rotary_encoder_browse.setPinB(value); rotary_encoder_browse.setPinB(value);
if (pin!= 9 && pin != 10 && pin != 6 && pin != 5){ if (pin!= 9 && pin != 10 && pin != 6 && pin != 5)
if (value == HIGH) digitalCallback(id,pin,value);
Midi.noteOn(id * 16 + pin, MIDI_MAX_DATA);
else Midi.noteOff(id * 16 + pin);
}
} }
//left jogwheel //left jogwheel, sends CC message on controller id 0x7A with value 65 (increment)
void rotary_encoder_jogwheel_left_inc() { void rotary_encoder_jogwheel_left_inc() {
Midi.controlChange(MIDI_MAX_DATA-5,65); Midi.controlChange(MIDI_MAX_DATA-5,65);
} }
//left jogwheel, sends CC message on controller id 0x7A with value 63 (decrement)
void rotary_encoder_jogwheel_left_dec() { void rotary_encoder_jogwheel_left_dec() {
Midi.controlChange(MIDI_MAX_DATA-5,63); Midi.controlChange(MIDI_MAX_DATA-5,63);
} }
//right jogwheel
//right jogwheel, sends CC message on controller id 0x79 with value 65 (increment)
void rotary_encoder_jogwheel_right_inc() { void rotary_encoder_jogwheel_right_inc() {
Midi.controlChange(MIDI_MAX_DATA-6,65); Midi.controlChange(MIDI_MAX_DATA-6,65);
} }
//right jogwheel, sends CC message on controller id 0x79 with value 63 (decrement)
void rotary_encoder_jogwheel_right_dec() { void rotary_encoder_jogwheel_right_dec() {
Midi.controlChange(MIDI_MAX_DATA-6,63); Midi.controlChange(MIDI_MAX_DATA-6,63);
} }
//Browse
//browse, sends note-on/off signal with note 0x36 [equals to default digital callback note] (increment)
void rotary_encoder_browse_inc() { void rotary_encoder_browse_inc() {
Midi.noteOn(3 * 16 + 6, MIDI_MAX_DATA); Midi.noteOn(3 * 16 + 6, MIDI_MAX_DATA);
Midi.noteOff(3 * 16 + 6); Midi.noteOff(3 * 16 + 6);
} }
//browse, sends note-on/off signal with note 0x35 [equals to default digital callback note] (decrement)
void rotary_encoder_browse_dec() { void rotary_encoder_browse_dec() {
Midi.noteOn(3 * 16 + 5, MIDI_MAX_DATA); Midi.noteOn(3 * 16 + 5, MIDI_MAX_DATA);
Midi.noteOff(3 * 16 + 5); Midi.noteOff(3 * 16 + 5);

View File

@ -47,10 +47,9 @@ void INPUT_74HC4051::loop() {
digitalWrite(_s0, r0); digitalWrite(_s0, r0);
digitalWrite(_s1, r1); digitalWrite(_s1, r1);
digitalWrite(_s2, r2); digitalWrite(_s2, r2);
//not sure if nessesary //divide by 8 to get value beetween 0-127 for MIDI
//delayMicroseconds(10);
value = analogRead(_analog) / 8; value = analogRead(_analog) / 8;
if (_value[pin] < value - INPUT_74HC4051_TOLERANCE || _value[pin] > value + INPUT_74HC4051_TOLERANCE) { if (_value[pin] < value || _value[pin] > value) {
_value[pin] = value; _value[pin] = value;
(*_callbackFunction)(_analog,pin,value); (*_callbackFunction)(_analog,pin,value);
} }

View File

@ -13,8 +13,6 @@
#ifndef bombatuino_INPUT_74HC4051_h #ifndef bombatuino_INPUT_74HC4051_h
#define bombatuino_INPUT_74HC4051_h #define bombatuino_INPUT_74HC4051_h
#define INPUT_74HC4051_TOLERANCE 0 /**< I/O DIRECTION REGISTER PORT A - Controls the direction of the data I/O. */
#if !defined(CallbackFunction) #if !defined(CallbackFunction)
/** /**
* callback function * callback function

View File

@ -2,7 +2,11 @@
#include "bombatuino_MIDI.h" #include "bombatuino_MIDI.h"
void MIDI::begin(int channel) { void MIDI::begin(int channel) {
#ifdef DEBUG
Serial.begin(9600);
#else
Serial.begin(31250); Serial.begin(31250);
#endif
//if given channel is not valid, set default channel to 1 //if given channel is not valid, set default channel to 1
if (channel > MIDI_MAX_CHANNEL) if (channel > MIDI_MAX_CHANNEL)
channel = MIDI_DEFAULT_CHANNEL; channel = MIDI_DEFAULT_CHANNEL;
@ -21,39 +25,76 @@ bool MIDI::message(int status, int data, int data2, int channel) {
//check if channel is valid //check if channel is valid
if (channel > MIDI_MAX_CHANNEL) return false; if (channel > MIDI_MAX_CHANNEL) return false;
//write bytes to serial //write bytes to serial
#ifdef DEBUG
Serial.print("status: ");
Serial.print(status+channel);
Serial.print(" data: ");
Serial.print(data);
#else
Serial.write(status+channel); Serial.write(status+channel);
Serial.write(data); Serial.write(data);
#endif
//check if second data byte should be send //check if second data byte should be send
if (status != MIDI_PROGRAMM_CHANGE && status != MIDI_CHANNEL_PRESSURE) if (status != MIDI_PROGRAMM_CHANGE && status != MIDI_CHANNEL_PRESSURE) {
#ifdef DEBUG
Serial.print(" data2: ");
Serial.print(data2);
#else
Serial.write(data2); Serial.write(data2);
#endif
}
#ifdef DEBUG
Serial.println();
#endif
return true; return true;
} }
bool MIDI::noteOff(int note, int velocity, int channel) { bool MIDI::noteOff(int note, int velocity, int channel) {
#ifdef DEBUG
Serial.print(" noteOff ");
#endif
return message(MIDI_NOTE_OFF,note,velocity,channel); return message(MIDI_NOTE_OFF,note,velocity,channel);
} }
bool MIDI::noteOn(int note, int velocity, int channel) { bool MIDI::noteOn(int note, int velocity, int channel) {
#ifdef DEBUG
Serial.print(" noteOn ");
#endif
return message(MIDI_NOTE_ON,note,velocity,channel); return message(MIDI_NOTE_ON,note,velocity,channel);
} }
bool MIDI::polyphonicKeyPressure(int note, int velocity, int channel) { bool MIDI::polyphonicKeyPressure(int note, int velocity, int channel) {
#ifdef DEBUG
Serial.print(" polyphonicKeyPressure ");
#endif
return message(MIDI_POLYPHONIC_KEY_PRESSURE,note,velocity,channel); return message(MIDI_POLYPHONIC_KEY_PRESSURE,note,velocity,channel);
} }
bool MIDI::controlChange(int controller, int value, int channel) { bool MIDI::controlChange(int controller, int value, int channel) {
#ifdef DEBUG
Serial.print(" controlChange ");
#endif
return message(MIDI_CONTROL_CHANGE,controller,value,channel); return message(MIDI_CONTROL_CHANGE,controller,value,channel);
} }
bool MIDI::programChange(int programm, int channel) { bool MIDI::programChange(int programm, int channel) {
#ifdef DEBUG
Serial.print(" programChange ");
#endif
return message(MIDI_PROGRAMM_CHANGE,programm,0,channel); return message(MIDI_PROGRAMM_CHANGE,programm,0,channel);
} }
bool MIDI::channelPressure(int pressure, int channel) { bool MIDI::channelPressure(int pressure, int channel) {
#ifdef DEBUG
Serial.print(" channelPressure ");
#endif
return message(MIDI_CHANNEL_PRESSURE,pressure,0,channel); return message(MIDI_CHANNEL_PRESSURE,pressure,0,channel);
} }
bool MIDI::pitchWheelChange(int last, int most, int channel) { bool MIDI::pitchWheelChange(int last, int most, int channel) {
#ifdef DEBUG
Serial.print(" pitchWheelChange ");
#endif
return message(MIDI_PITCH_WHEEL_CHANGE,last,most,channel); return message(MIDI_PITCH_WHEEL_CHANGE,last,most,channel);
} }

View File

@ -11,6 +11,9 @@
* *
* */ * */
//uncomment for debugging
//#define DEBUG
#ifndef bombatuino_MIDI_h #ifndef bombatuino_MIDI_h
#define bombatuino_MIDI_h #define bombatuino_MIDI_h

View File

@ -5,7 +5,7 @@
* *
* @date February, 2013 * @date February, 2013
* *
* @brief arduino library for handling a rotary encoder * @brief arduino library for handling a rotary encoder (simplified)
* *
* library is for specialiced use: increment- and decrement-functions are called on change of pin A. * library is for specialiced use: increment- and decrement-functions are called on change of pin A.
* *