Привет ардуинщикам! Помогите новичку!
ESP32 по Bluetooth midi-сообщения и передает по uart на arduino mega.
Скетч для ESP32
#include <Arduino.h>
#include <BLEMidi.h>
#include <MIDI.h>
#define LED_BUILTIN 2
MIDI_CREATE_INSTANCE(HardwareSerial, Serial2, MIDI);
void connected();
void onNoteOn(uint8_t channel, uint8_t note, uint8_t velocity, uint16_t timestamp) {
Serial.printf("NoteOn, channel: %d, note: %d, velocity: %d (timestamp %dms)\n", channel, note, velocity, timestamp);
}
void onNoteOff(uint8_t channel, uint8_t note, uint8_t velocity, uint16_t timestamp) {
Serial.printf("NoteOff, channel: %d, note: %d, velocity: %d (timestamp %dms)\n", channel, note, velocity, timestamp);
}
void onControlChange(uint8_t channel, uint8_t controller, uint8_t value, uint16_t timestamp) {
Serial.printf("ControlChange, channel: %d, controller: %d, value %d (timestamp %dms)\n", channel, controller, value, timestamp);
}
void connected() {
Serial.println("Connected");
}
void setup() {
Serial.begin(115200);
BLEMidiServer.begin("MIDI device");
BLEMidiServer.setOnConnectCallback(connected);
BLEMidiServer.setOnDisconnectCallback([]() { // To show how to make a callback with a lambda function
Serial.println("Disconnected");
});
BLEMidiServer.setNoteOnCallback(onNoteOn);
BLEMidiServer.setNoteOffCallback(onNoteOff);
BLEMidiServer.setControlChangeCallback(onControlChange);
//BLEMidiServer.enableDebugging();
pinMode(LED_BUILTIN, OUTPUT);
Serial2.begin(31250); // 31250 пробую 9600
MIDI.begin(MIDI_CHANNEL_OMNI);
MIDI.turnThruOff();
}
void loop() {
MIDI.read();
delay(30);
}
void OnSysEx(byte* data, unsigned int length) {
// Перенаправляем System Exclusive сообщение на Serial2
for (unsigned int i = 0; i < length; i++) {
Serial2.write(data[i]);
}
}
Скетч для arduino mega
#include <MIDI.h>
struct CustomBaud : public midi::DefaultSettings{
static const long BaudRate = 31250; // Baud rate for hairless
};
MIDI_CREATE_CUSTOM_INSTANCE(HardwareSerial, Serial1, MIDI, CustomBaud);
void handleNoteOn(uint8_t channel, uint8_t pitch, uint8_t velocity)
{
Serial.print("NoteOn, channel: ");
Serial.print(channel);
Serial.print(", note: ");
Serial.print(pitch);
Serial.print(", velocity: ");
Serial.println(velocity);
}
void handleNoteOff(uint8_t channel, uint8_t pitch, uint8_t velocity)
{
Serial.print("NoteOff, channel: ");
Serial.print(channel);
Serial.print(", note: ");
Serial.print(pitch);
Serial.print(", velocity: ");
Serial.println(velocity);
}
void handleControlChange(uint8_t channel, uint8_t number, uint8_t value)
{
// Обработка Control Change сообщений
}
void setup()
{
Serial.begin(31250); // Для отладки
Serial1.begin(31250); // Соединение с ESP32 через UART
MIDI.setHandleNoteOn(handleNoteOn);
MIDI.setHandleNoteOff(handleNoteOff);
MIDI.setHandleControlChange(handleControlChange);
MIDI.begin(MIDI_CHANNEL_OMNI);
}
void loop()
{
MIDI.read();
delay(30);
}
В мониторе ESP32 всё правильно:
Connected
NoteOn, channel: 0, note: 56, velocity: 127 (timestamp 0ms)
NoteOff, channel: 0, note: 56, velocity: 0 (timestamp 0ms)
NoteOn, channel: 0, note: 31, velocity: 127 (timestamp 0ms)
NoteOff, channel: 0, note: 31, velocity: 0 (timestamp 0ms)
NoteOn, channel: 0, note: 50, velocity: 127 (timestamp 0ms)
NoteOff, channel: 0, note: 50, velocity: 0 (timestamp 0ms)
А в мониторе мега не правильные числа:
NoteOff, channel: 11, note: 10, velocity: 231
NoteOff, channel: 11, note: 47, velocity: 57
NoteOff, channel: 8, note: 195, velocity: 46
NoteOff, channel: 8, note: 78, velocity: 199
NoteOff, channel: 4, note: 3, velocity: 11
NoteOff, channel: 4, note: 42, velocity: 203
В чём моя ошибка???