M
I know i'm getting close. I was ablet o send midi over BLE with a patch called Core_2midi_ChrordPads (yes there's a typo in the name).
tried copying relevant information to my patch.
Now my computer sees the core2 as a MIDI Bluetooth device, (instead of normal bluetooth only) but its unable to connect to it.
#include <M5Core2.h>
#include <BLEMIDI_Transport.h>
#include <hardware/BLEMIDI_ESP32_NimBLE.h>
#define MIDI_CHANNEL 1
#define CC_X 20
#define CC_Y 21
#define CC_Z 22
#define DEVICE_NAME "M5Core2 MIDI"
BLEMIDI_CREATE_INSTANCE(DEVICE_NAME, MIDI);
bool isConnected = false;
bool calibrated = false;
float refGyroX = 0, refGyroY = 0, refGyroZ = 0;
float relAngleX = 0, relAngleY = 0, relAngleZ = 0;
float alpha = 0.5;
float accAngleX = 0, accAngleY = 0, accAngleZ = 0;
unsigned long lastTime = 0;
void calibrateGyroscope() {
M5.Lcd.fillScreen(TFT_BLACK);
M5.Lcd.setTextColor(TFT_GREEN); // Set text color to green
M5.Lcd.setTextSize(2); // Set text size to 2
M5.Lcd.setCursor(0, 0); // Move cursor to top-left corner
M5.Lcd.print("MAYSUN DEVICE"); // Display "MAYSUN DEVICE" at top of screen
M5.Lcd.setCursor(0, M5.Lcd.height() / 2 - 20); // Center vertically
M5.Lcd.printf("GYRO Calibrating...");
// Wait for user to press Button C to calibrate
while (!M5.Touch.ispressed()) {
delay(100);
}
// Capture gyroscope data for calibration
M5.IMU.getGyroData(&refGyroX, &refGyroY, &refGyroZ);
// Update calibration status
calibrated = true;
// Reset relative angles
relAngleX = 0;
relAngleY = 0;
relAngleZ = 0;
// Display calibration complete message
M5.Lcd.fillScreen(TFT_BLACK);
M5.Lcd.setCursor(0, 0); // Move cursor to top-left corner
M5.Lcd.setTextColor(TFT_GREEN); // Set text color to green
M5.Lcd.print("MAYSUN DEVICE"); // Display "MAYSUN DEVICE" at top of screen
M5.Lcd.setCursor(0, M5.Lcd.height() / 2 - 20); // Center vertically
M5.Lcd.printf("GYRO Calibration complete!");
delay(1000);
}
void updateOrientation(float gyroX, float gyroY, float gyroZ, float accX, float accY, float accZ) {
float dt = (millis() - lastTime) / 1000.0; // Convert milliseconds to seconds
// Calculate orientation from accelerometer data (roll and pitch)
accAngleX = atan2(accY, accZ) * 180 / PI;
accAngleY = atan2(-accX, sqrt(accY * accY + accZ * accZ)) * 180 / PI;
// Apply low-pass filter to gyro data
relAngleX = alpha * (relAngleX + gyroX * dt) + (1 - alpha) * accAngleX;
relAngleY = alpha * (relAngleY + gyroY * dt) + (1 - alpha) * accAngleY;
relAngleZ = alpha * (relAngleZ + gyroZ * dt);
lastTime = millis();
}
void setup() {
M5.begin();
M5.IMU.Init();
M5.Lcd.setRotation(3);
M5.Lcd.setTextSize(2);
M5.Lcd.fillScreen(TFT_BLACK); // Clear the screen
BLEMIDI.begin(); // Initialize BLE MIDI
MIDI.begin();
// Display initialization message
M5.Lcd.setCursor(0, 0);
M5.Lcd.setTextColor(TFT_GREEN);
M5.Lcd.print("MAYSUN DEVICE");
M5.Lcd.setCursor(0, M5.Lcd.height() / 2 - 20);
M5.Lcd.print("Initializing...");
delay(1000);
M5.Lcd.fillScreen(TFT_BLACK); // Clear the screen after initialization
}
void loop() {
if (M5.Touch.ispressed()) {
calibrateGyroscope();
return; // Exit loop to ensure data is not processed before calibration
}
if (!calibrated) {
return;
}
float gyroX, gyroY, gyroZ;
M5.IMU.getGyroData(&gyroX, &gyroY, &gyroZ);
// Read accelerometer data
float accX, accY, accZ;
M5.IMU.getAccelData(&accX, &accY, &accZ);
// Update orientation with low-pass filter
updateOrientation(gyroX, gyroY, gyroZ, accX, accY, accZ);
// Display data on the screen
M5.Lcd.fillScreen(TFT_BLACK);
M5.Lcd.setCursor(0, 0); // Move cursor to top-left corner
M5.Lcd.setTextColor(TFT_GREEN); // Set text color to green
M5.Lcd.setTextSize(2); // Set text size to 2
M5.Lcd.print("MAYSUN DEVICE"); // Display "MAYSUN DEVICE" at top of screen
// Display initial data
M5.Lcd.setCursor(0, M5.Lcd.height() / 2 - 60); // Center vertically
M5.Lcd.setTextColor(TFT_RED); // Set text color to red
M5.Lcd.printf("Initial X: %.2f\n", relAngleX);
M5.Lcd.printf("Initial Y: %.2f\n", relAngleY);
M5.Lcd.printf("Initial Z: %.2f\n", relAngleZ);
// Display final data
M5.Lcd.setTextColor(TFT_GREEN); // Set text color to green
M5.Lcd.printf("\nFinal X: %.2f\n", relAngleX);
M5.Lcd.printf("Final Y: %.2f\n", relAngleY);
M5.Lcd.printf("Final Z: %.2f\n", relAngleZ);
// Read battery voltage and calculate percentage
float batVoltage = M5.Axp.GetBatVoltage();
float batPercentage = (batVoltage < 3.2) ? 0 : (batVoltage - 3.2) * 100;
// Display battery percentage
M5.Lcd.setTextColor(TFT_YELLOW); // Set text color to yellow
M5.Lcd.printf("\n\nBattery %%: %.1f\n", batPercentage);
}
// BLE-MIDI
// -----------------------------------------------------------------------------
// Device connected
// -----------------------------------------------------------------------------
void OnConnected() {
isConnected = true;
M5.Lcd.fillRect( 200, 0, 120, 50, BLACK);
//digitalWrite(LED_BUILTIN, HIGH);
M5.Lcd.setTextSize(1);
M5.Lcd.setTextColor(WHITE, BLACK);
M5.Lcd.setCursor(200, 18);
M5.Lcd.printf(" Connected.");
}
// -----------------------------------------------------------------------------
// Device disconnected
// -----------------------------------------------------------------------------
void OnDisconnected() {
isConnected = false;
//digitalWrite(LED_BUILTIN, LOW);
M5.Lcd.fillRect( 200, 0, 120, 50, BLACK);
M5.Lcd.setTextSize(1);
M5.Lcd.setTextColor(WHITE, BLACK);
M5.Lcd.setCursor(200, 18);
M5.Lcd.printf("Disconnected.");
}