Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <OPL2.h>
- OPL2 opl2;
- unsigned char midiCmd[3];
- void setup() {
- opl2.init();
- Serial.begin(1250000);
- // Sync with the 16u2
- Serial.write(0xfd);
- while (Serial.read() != 0xfd);
- }
- void loop() {
- // We assume only 0x9n commands will be sent
- if (Serial.available() < 3)
- return;
- Serial.readBytes(midiCmd, 3);
- if ((midiCmd[0] & 0xf0) != 0x90) {
- // Error (push reset button)
- opl2.reset();
- SPI.end();
- pinMode(LED_BUILTIN, OUTPUT);
- for (;;) {
- digitalWrite(LED_BUILTIN, HIGH);
- delay(500);
- digitalWrite(LED_BUILTIN, LOW);
- delay(500);
- }
- }
- // Signal to the 16u2 that a command was removed from the buffer
- Serial.write(0xf9);
- if (midiCmd[0] & 0x8) {
- // Reset OPL2
- opl2.reset();
- return;
- }
- opl2.write((midiCmd[0] << 7) | midiCmd[1], ((midiCmd[0] & 4) << 5) | midiCmd[2]);
- }
Add Comment
Please, Sign In to add comment