Last active
September 28, 2025 09:13
-
-
Save yoggy/c82d3258d070d39ada27108ba0a5abbc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // https://qiita.com/wdice02136/items/25b2ac3789918c398d43 | |
| // | |
| // M5StickC Plus2でBLE MIDIのテスト | |
| // ボタンを押すとC4のNote ON/OFFを送信するだけ | |
| // | |
| #include <M5Unified.h> | |
| #include <BLEDevice.h> | |
| #include <BLEServer.h> | |
| const char *DEVICE_NAME = "M5StickCP2"; | |
| BLEServer *ble_server; | |
| BLEService *ble_service; | |
| BLECharacteristic *characteristic; | |
| bool is_connected = false; | |
| class MyCallBacks : public BLEServerCallbacks { | |
| void onConnect(BLEServer *pServer) { | |
| is_connected = true; | |
| Serial.println("onConnect()"); | |
| BLEDevice::stopAdvertising(); | |
| }; | |
| void onDisconnect(BLEServer *pServer) { | |
| is_connected = false; | |
| Serial.println("onDisconnect()"); | |
| BLEDevice::startAdvertising(); | |
| } | |
| }; | |
| void sendNoteOn(int note, int velocity) { | |
| uint8_t buff[5]; | |
| buff[0] = 0x80; // header | |
| buff[1] = 0x80; // timestamp | |
| buff[2] = 0x90; // midi status (note on 0x90 + channel) | |
| buff[3] = note; | |
| buff[4] = velocity; | |
| characteristic->setValue(buff, 5); | |
| characteristic->notify(); | |
| } | |
| void sendNoteOff(int note) { | |
| uint8_t buff[5]; | |
| buff[0] = 0x80; // header | |
| buff[1] = 0x80; // timestamp | |
| buff[2] = 0x80; // midi status (note off 0x80 + channel) | |
| buff[3] = note; | |
| buff[4] = 0; | |
| characteristic->setValue(buff, 5); | |
| characteristic->notify(); | |
| } | |
| void setup() { | |
| auto cfg = M5.config(); | |
| cfg.serial_baudrate = 115200; | |
| M5.begin(cfg); | |
| M5.Display.setRotation(1); | |
| M5.Display.clear(TFT_GREEN); | |
| M5.Display.setBrightness(10); | |
| M5.Display.setTextSize(2); | |
| BLEDevice::init(DEVICE_NAME); | |
| ble_server = BLEDevice::createServer(); | |
| ble_server->setCallbacks(new MyCallBacks()); | |
| ble_service = ble_server->createService("03B80E5A-EDE8-4B33-A751-6CE34EC4C700"); | |
| characteristic = ble_service->createCharacteristic( | |
| "7772E5DB-3868-4112-A1A9-F2669D106BF3", | |
| BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY); | |
| ble_service->start(); | |
| BLEAdvertising *adv = ble_server->getAdvertising(); | |
| adv->addServiceUUID("7772E5DB-3868-4112-A1A9-F2669D106BF3"); | |
| adv->setScanResponse(true); | |
| adv->setMinPreferred(0x06); | |
| adv->setMinPreferred(0x12); | |
| BLEDevice::startAdvertising(); | |
| } | |
| void loop() { | |
| M5.update(); | |
| if (is_connected) { | |
| if (M5.BtnA.wasPressed()) { | |
| sendNoteOn(60, 100); // C4 | |
| } else if (M5.BtnA.wasReleased()) { | |
| sendNoteOff(60); | |
| } | |
| delay(1); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment