sources and files for the bombatuino project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.8 KiB

  1. #include "Arduino.h"
  2. #include "bombatuino_MIDI.h"
  3. void MIDI::begin(int channel) {
  4. Serial.begin(31250);
  5. //if given channel is not valid, set default channel to 1
  6. if (channel > MIDI_MAX_CHANNEL)
  7. channel = MIDI_DEFAULT_CHANNEL;
  8. _channel = channel;
  9. }
  10. bool MIDI::message(int status, int data, int data2, int channel) {
  11. //check if status byte is valid
  12. if (status > MIDI_MAX_STATUS) return false;
  13. //check if first data byte is valid
  14. if (data > MIDI_MAX_DATA) return false;
  15. //check if second data byte is valid
  16. if (data2 > MIDI_MAX_DATA) return false;
  17. //if no specific channel given, use default channel
  18. if (channel == MIDI_NULL_CHANNEL) channel = _channel;
  19. //check if channel is valid
  20. if (channel > MIDI_MAX_CHANNEL) return false;
  21. //write bytes to serial
  22. Serial.write(status+channel);
  23. Serial.write(data);
  24. //check if second data byte should be send
  25. if (status != MIDI_PROGRAMM_CHANGE && status != MIDI_CHANNEL_PRESSURE)
  26. Serial.write(data2);
  27. return true;
  28. }
  29. bool MIDI::noteOff(int note, int velocity, int channel) {
  30. return message(MIDI_NOTE_OFF,note,velocity,channel);
  31. }
  32. bool MIDI::noteOn(int note, int velocity, int channel) {
  33. return message(MIDI_NOTE_ON,note,velocity,channel);
  34. }
  35. bool MIDI::polyphonicKeyPressure(int note, int velocity, int channel) {
  36. return message(MIDI_POLYPHONIC_KEY_PRESSURE,note,velocity,channel);
  37. }
  38. bool MIDI::controlChange(int controller, int value, int channel) {
  39. return message(MIDI_CONTROL_CHANGE,controller,value,channel);
  40. }
  41. bool MIDI::programChange(int programm, int channel) {
  42. return message(MIDI_PROGRAMM_CHANGE,programm,0,channel);
  43. }
  44. bool MIDI::channelPressure(int pressure, int channel) {
  45. return message(MIDI_CHANNEL_PRESSURE,pressure,0,channel);
  46. }
  47. bool MIDI::pitchWheelChange(int last, int most, int channel) {
  48. return message(MIDI_PITCH_WHEEL_CHANGE,last,most,channel);
  49. }