Update of the bombatuino with a pro micro board with native usb midi support.
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.

95 lines
2.4 KiB

  1. #include <Wire.h>
  2. #include <bombatuino_INPUT_MCP23017.h>
  3. #include <bombatuino_INPUT_74HC4051.h>
  4. #include "MIDIUSB.h"
  5. #define debug true
  6. //MCP23017 I2C addresses 0,1,2,3 and 4
  7. INPUT_MCP23017 input_MCP23017_0;
  8. INPUT_MCP23017 input_MCP23017_1;
  9. INPUT_MCP23017 input_MCP23017_2;
  10. INPUT_MCP23017 input_MCP23017_3;
  11. INPUT_MCP23017 input_MCP23017_4;
  12. //three 74HC4051 on analog pins A1,A2,A3; select on digital pins 7,8,9
  13. INPUT_74HC4051 input_4051_A1;
  14. INPUT_74HC4051 input_4051_A2;
  15. INPUT_74HC4051 input_4051_A3;
  16. int channel = 0;
  17. int velocity = 120;
  18. const uint8_t NO_PINS = 0b00000000;
  19. void setup() {
  20. #ifdef debug
  21. Serial.begin(9600);
  22. #endif
  23. //initialize MCP23017s
  24. input_MCP23017_0.begin(0,digitalCallback);
  25. input_MCP23017_1.begin(1,digitalCallback);
  26. input_MCP23017_2.begin(2,digitalCallback);
  27. input_MCP23017_3.begin(3,digitalCallback);
  28. input_MCP23017_4.begin(4,digitalCallback);
  29. //initialize 74HC4051s
  30. input_4051_A1.begin(A1,7,8,9,analogCallback);
  31. input_4051_A1.setPins(0b00001000);
  32. input_4051_A2.begin(A2,7,8,9,analogCallback);
  33. input_4051_A2.setPins(NO_PINS);
  34. input_4051_A3.begin(A3,7,8,9,analogCallback);
  35. input_4051_A3.setPins(NO_PINS);
  36. }
  37. void loop() {
  38. //loop MCP23017s for callbacks
  39. input_MCP23017_0.loop();
  40. input_MCP23017_1.loop();
  41. input_MCP23017_2.loop();
  42. input_MCP23017_3.loop();
  43. input_MCP23017_4.loop();
  44. //loop 74HC4051s for callback
  45. input_4051_A1.loop();
  46. input_4051_A2.loop();
  47. input_4051_A3.loop();
  48. MidiUSB.flush();
  49. }
  50. //callback for analog, sends CC message with unique controller id
  51. void analogCallback(int id, int pin, int value) {
  52. #ifdef debug
  53. Serial.print("analog");
  54. Serial.print("\tid\t");
  55. Serial.print(id-A0);
  56. Serial.print("\tpin\t");
  57. Serial.print(pin);
  58. Serial.print("\tvalue\t");
  59. Serial.println(value);
  60. #endif
  61. midiEventPacket_t event = {0x0B, 0xB0 | channel, ((id-A0) * 8 + pin), value};
  62. MidiUSB.sendMIDI(event);
  63. }
  64. //default callback for buttons, sends note-on/off message with unique note value
  65. void digitalCallback(int id, int pin, int value) {
  66. #ifdef debug
  67. Serial.print("digital");
  68. Serial.print("\tid\t");
  69. Serial.print(id);
  70. Serial.print("\tpin\t");
  71. Serial.print(pin);
  72. Serial.print("\tvalue\t");
  73. Serial.println(value);
  74. #endif
  75. if (value == HIGH) {
  76. midiEventPacket_t noteOn = {0x09, 0x90 | channel, (id * 16 + pin), velocity};
  77. MidiUSB.sendMIDI(noteOn);
  78. } else {
  79. midiEventPacket_t noteOff = {0x08, 0x80 | channel, (id * 16 + pin), 0};
  80. MidiUSB.sendMIDI(noteOff);
  81. }
  82. }