Guest User

Untitled

a guest
Dec 15th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <OPL2.h>
  3.  
  4. OPL2 opl2;
  5. unsigned char midiCmd[3];
  6.  
  7. void setup() {
  8. opl2.init();
  9. Serial.begin(1250000);
  10. // Sync with the 16u2
  11. Serial.write(0xfd);
  12. while (Serial.read() != 0xfd);
  13. }
  14.  
  15. void loop() {
  16. // We assume only 0x9n commands will be sent
  17. if (Serial.available() < 3)
  18. return;
  19.  
  20. Serial.readBytes(midiCmd, 3);
  21.  
  22. if ((midiCmd[0] & 0xf0) != 0x90) {
  23. // Error (push reset button)
  24. opl2.reset();
  25. SPI.end();
  26. pinMode(LED_BUILTIN, OUTPUT);
  27. for (;;) {
  28. digitalWrite(LED_BUILTIN, HIGH);
  29. delay(500);
  30. digitalWrite(LED_BUILTIN, LOW);
  31. delay(500);
  32. }
  33. }
  34.  
  35. // Signal to the 16u2 that a command was removed from the buffer
  36. Serial.write(0xf9);
  37.  
  38. if (midiCmd[0] & 0x8) {
  39. // Reset OPL2
  40. opl2.reset();
  41. return;
  42. }
  43.  
  44. opl2.write((midiCmd[0] << 7) | midiCmd[1], ((midiCmd[0] & 4) << 5) | midiCmd[2]);
  45. }
Add Comment
Please, Sign In to add comment