Controllers.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include "Controllers.h"
  2. // MIDI
  3. // First parameter is the event type (0x09 = note on, 0x08 = note off).
  4. // Second parameter is note-on/note-off, combined with the channel.
  5. // Channel can be anything between 0-15. Typically reported to the user as 1-16.
  6. // Third parameter is the note number (48 = middle C).
  7. // Fourth parameter is the velocity (64 = normal, 127 = fastest).
  8. // First parameter is the event type (0x0B = control change).
  9. // Second parameter is the event type, combined with the channel.
  10. // Third parameter is the control number number (0-119).
  11. // Fourth parameter is the control value (0-127).
  12. void noteOn(byte channel, byte pitch, byte velocity) {
  13. midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
  14. #ifdef DEBUG
  15. Serial.print("Note on:");
  16. Serial.print(channel);
  17. Serial.print(",");
  18. Serial.print(pitch);
  19. Serial.print(",");
  20. Serial.println(velocity);
  21. #endif
  22. MidiUSB.sendMIDI(noteOn);
  23. }
  24. void noteOff(byte channel, byte pitch, byte velocity) {
  25. midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
  26. #ifdef DEBUG
  27. Serial.print("Note off:");
  28. Serial.print(channel);
  29. Serial.print(",");
  30. Serial.print(pitch);
  31. Serial.print(",");
  32. Serial.println(velocity);
  33. #endif
  34. MidiUSB.sendMIDI(noteOff);
  35. }
  36. void controlChange(byte channel, byte control, byte value) {
  37. midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
  38. #ifdef DEBUG
  39. Serial.print("Control change:");
  40. Serial.print(channel);
  41. Serial.print(",");
  42. Serial.print(control);
  43. Serial.print(",");
  44. Serial.println(value);
  45. #endif
  46. MidiUSB.sendMIDI(event);
  47. }
  48. // Constructor and methods for analog and buttons (encoders use a different library)
  49. AnalogControl::AnalogControl(int pin, byte effect, int middle) {
  50. this->pin = pin;
  51. this->effect = effect;
  52. this->middle = middle;
  53. pinMode(pin,INPUT);
  54. }
  55. void AnalogControl::checkStatus() {
  56. value = analogRead(pin);
  57. if (value < 0+STICKYNESS){
  58. value = 0;
  59. } else if (value > 1023-STICKYNESS) {
  60. value = 1023;
  61. } else if (value > middle-STICKYNESS && value < middle+STICKYNESS) {
  62. value = middle;
  63. }
  64. if ( abs(value - prev_value) > DEAD_ZONE ){
  65. controlChange(1,effect, map(value,0,1023,0,127) );
  66. prev_value = value;
  67. MidiUSB.flush();
  68. }
  69. }
  70. DigitalButton::DigitalButton(int pin, byte effect, byte effect_toggle, byte effect_long, byte effect_long_toggle) {
  71. this->effect = effect;
  72. this->effect_toggle = effect_toggle;
  73. this->effect_long = effect_long;
  74. this->effect_long_toggle = effect_long_toggle;
  75. this->pin = pin;
  76. pinMode(pin,INPUT_PULLUP);
  77. }
  78. void DigitalButton::checkStatus() {
  79. if (digitalRead(pin) == LOW && pressed == false) {
  80. pressed = true;
  81. long_pressed = false;
  82. debounce = millis();
  83. }
  84. if (pressed && (millis() - debounce <= 10) && digitalRead(pin) == HIGH) {
  85. pressed = false;
  86. return;
  87. }
  88. if (pressed && (millis() - debounce > 10) && digitalRead(pin) == HIGH) {
  89. if (long_pressed == true) {
  90. long_pressed = false;
  91. } else if (effect_toggle == 0) {
  92. controlChange(1,effect,1);
  93. } else if (toggled) {
  94. controlChange(1,effect,1);
  95. toggled = false;
  96. } else {
  97. controlChange(1,effect_toggle,1);
  98. toggled = true;
  99. }
  100. MidiUSB.flush();
  101. pressed = false;
  102. }
  103. if (pressed && (millis() - debounce > LONG_INTERVAL) && (digitalRead(pin) == LOW) && (long_pressed == false)) {
  104. long_pressed = true;
  105. if (effect_long_toggle == 0) {
  106. controlChange(1,effect_long,1);
  107. } else if (long_toggled) {
  108. controlChange(1,effect_long,1);
  109. long_toggled = false;
  110. } else {
  111. controlChange(1,effect_long_toggle,1);
  112. long_toggled = true;
  113. }
  114. MidiUSB.flush();
  115. }
  116. }