Controllers.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. AnalogControl::AnalogControl(int pin, byte effect, int middle) {
  49. this->pin = pin;
  50. this->effect = effect;
  51. this->middle = middle;
  52. pinMode(pin,INPUT);
  53. }
  54. void AnalogControl::checkStatus() {
  55. value = analogRead(pin);
  56. if (value < 0+STICKYNESS){
  57. value = 0;
  58. } else if (value > 1023-STICKYNESS) {
  59. value = 1023;
  60. } else if (value > middle-STICKYNESS && value < middle+STICKYNESS) {
  61. value = middle;
  62. }
  63. if ( abs(value - prev_value) > DEAD_ZONE ){
  64. controlChange(1,effect, map(value,0,1023,0,127) );
  65. prev_value = value;
  66. MidiUSB.flush();
  67. }
  68. }
  69. DigitalButton::DigitalButton(int pin, byte effect, byte effect_toggle, byte effect_long, byte effect_long_toggle) {
  70. this->effect = effect;
  71. this->effect_toggle = effect_toggle;
  72. this->effect_long = effect_long;
  73. this->effect_long_toggle = effect_long_toggle;
  74. this->pin = pin;
  75. pinMode(pin,INPUT_PULLUP);
  76. }
  77. void DigitalButton::checkStatus() {
  78. if (digitalRead(pin) == LOW && pressed == false) {
  79. pressed = true;
  80. long_pressed = false;
  81. debounce = millis();
  82. }
  83. if (pressed && (millis() - debounce <= 10) && digitalRead(pin) == HIGH) {
  84. pressed = false;
  85. return;
  86. }
  87. if (pressed && (millis() - debounce > 10) && digitalRead(pin) == HIGH) {
  88. if (long_pressed == true) {
  89. long_pressed = false;
  90. } else if (effect_toggle == 0) {
  91. controlChange(1,effect,1);
  92. } else if (toggled) {
  93. controlChange(1,effect,1);
  94. toggled = false;
  95. } else {
  96. controlChange(1,effect_toggle,1);
  97. toggled = true;
  98. }
  99. MidiUSB.flush();
  100. pressed = false;
  101. }
  102. if (pressed && (millis() - debounce > LONG_INTERVAL) && (digitalRead(pin) == LOW) && (long_pressed == false)) {
  103. long_pressed = true;
  104. if (effect_long_toggle == 0) {
  105. controlChange(1,effect_long,1);
  106. } else if (long_toggled) {
  107. controlChange(1,effect_long,1);
  108. long_toggled = false;
  109. } else {
  110. controlChange(1,effect_long_toggle,1);
  111. long_toggled = true;
  112. }
  113. MidiUSB.flush();
  114. }
  115. }