diff --git a/src/hardware/BLEMIDI_Client_ESP32.h b/src/hardware/BLEMIDI_Client_ESP32.h index 6b066e3..dd81b58 100644 --- a/src/hardware/BLEMIDI_Client_ESP32.h +++ b/src/hardware/BLEMIDI_Client_ESP32.h @@ -37,6 +37,24 @@ #define BLEMIDI_CLIENT_DEFAULT_NAME "BLEMIDI-CLIENT" #endif //Not modify +/* +###### TX POWER ##### +*/ +/** + * Set power transmision + * + * ESP_PWR_LVL_N12 // Corresponding to -12dbm Minimum + * ESP_PWR_LVL_N9 // Corresponding to -9dbm + * ESP_PWR_LVL_N6 // Corresponding to -6dbm + * ESP_PWR_LVL_N3 // Corresponding to -3dbm + * ESP_PWR_LVL_N0 // Corresponding to 0dbm + * ESP_PWR_LVL_P3 // Corresponding to +3dbm + * ESP_PWR_LVL_P6 // Corresponding to +6dbm + * ESP_PWR_LVL_P9 // Corresponding to +9dbm Maximum +*/ + +#define BLEMIDI_TX_PWR ESP_PWR_LVL_P9 + /* ###### SECURITY ##### */ @@ -58,7 +76,7 @@ * Uncomment what you need * These are the default values. */ -//#define BLEMIDI_CLIENT_BOND +#define BLEMIDI_CLIENT_BOND //#define BLEMIDI_CLIENT_MITM #define BLEMIDI_CLIENT_PAIR @@ -78,7 +96,7 @@ static uint32_t userOnPassKeyRequest() return passkey; }; -/* + /* ###### BLE COMMUNICATION PARAMS ###### */ /** Set connection parameters: @@ -94,10 +112,10 @@ static uint32_t userOnPassKeyRequest() * 0 latency (Number of intervals allowed to skip), * TimeOut (unit: 10ms) 51 * 10ms = 510ms. Timeout should be minimum 100ms. */ -#define BLEMIDI_CLIENT_COMM_MIN_INTERVAL 6 // 7.5ms -#define BLEMIDI_CLIENT_COMM_MAX_INTERVAL 35 // 40ms -#define BLEMIDI_CLIENT_COMM_LATENCY 0 -#define BLEMIDI_CLIENT_COMM_TIMEOUT 200 //2000ms +#define BLEMIDI_CLIENT_COMM_MIN_INTERVAL 6 // 7.5ms +#define BLEMIDI_CLIENT_COMM_MAX_INTERVAL 35 // 40ms +#define BLEMIDI_CLIENT_COMM_LATENCY 0 // +#define BLEMIDI_CLIENT_COMM_TIMEOUT 200 //2000ms /* ############################################# @@ -191,6 +209,7 @@ private: BLEAdvertising *_advertising = nullptr; BLERemoteCharacteristic *_characteristic = nullptr; BLERemoteService *pSvc = nullptr; + bool firstTimeSend = true; //First writeValue get sends like Write with reponse for clean security flags. After first time, all messages are send like WriteNoResponse for increase transmision speed. BLEMIDI_Transport *_bleMidiTransport = nullptr; @@ -228,7 +247,18 @@ public: return; if (_characteristic == NULL) return; - _characteristic->writeValue(data, length, true); + + if (firstTimeSend) + { + _characteristic->writeValue(data, length, true); + firstTimeSend = false; + return; + } + + if (!_characteristic->writeValue(data, length, !_characteristic->canWriteNoResponse())) + firstTimeSend = true; + + return; } bool available(byte *pvBuffer); @@ -236,7 +266,7 @@ public: void add(byte value) { // called from BLE-MIDI, to add it to a buffer here - xQueueSend(mRxQueue, &value, portMAX_DELAY/2); + xQueueSend(mRxQueue, &value, portMAX_DELAY / 2); } protected: @@ -254,6 +284,7 @@ protected: { _bleMidiTransport->_connectedCallback(); } + firstTimeSend = true; } void disconnected() @@ -262,6 +293,7 @@ protected: { _bleMidiTransport->_disconnectedCallback(); } + firstTimeSend = true; } void notifyCB(NimBLERemoteCharacteristic *pRemoteCharacteristic, uint8_t *pData, size_t length, bool isNotify); @@ -290,7 +322,7 @@ protected: void onConnect(BLEClient *pClient) { //Serial.println("##Connected##"); - pClient->updateConnParams(BLEMIDI_CLIENT_COMM_MIN_INTERVAL, BLEMIDI_CLIENT_COMM_MAX_INTERVAL, BLEMIDI_CLIENT_COMM_LATENCY, BLEMIDI_CLIENT_COMM_TIMEOUT); + //pClient->updateConnParams(BLEMIDI_CLIENT_COMM_MIN_INTERVAL, BLEMIDI_CLIENT_COMM_MAX_INTERVAL, BLEMIDI_CLIENT_COMM_LATENCY, BLEMIDI_CLIENT_COMM_TIMEOUT); vTaskDelay(1); if (_bluetoothEsp32) { @@ -326,10 +358,11 @@ protected: { /** Number of intervals allowed to skip */ return false; } - else if (params->supervision_timeout > BLEMIDI_CLIENT_COMM_TIMEOUT + 10) + else if (params->supervision_timeout > BLEMIDI_CLIENT_COMM_TIMEOUT) { /** 10ms units */ return false; } + pClient->updateConnParams(params->itvl_min, params->itvl_max, params->latency, params->supervision_timeout); return true; }; @@ -380,7 +413,7 @@ bool BLEMIDI_Client_ESP32::begin(const char *deviceName, BLEMIDI_Transport_characteristic == pRemoteCharacteristic) //Redundant protection { - receive(pData, length); + receive(pData, length); } } @@ -466,14 +499,14 @@ bool BLEMIDI_Client_ESP32::connect() //Re-connection SUCCESS return true; } - } + } /** Disconnect if subscribe failed */ _client->disconnect(); } /* If any connection problem exits, delete previous client and try again in the next attemp as new client*/ NimBLEDevice::deleteClient(_client); _client = nullptr; - return false; + return false; } /*If client does not match, delete previous client and create a new one*/ NimBLEDevice::deleteClient(_client); @@ -485,14 +518,14 @@ bool BLEMIDI_Client_ESP32::connect() Serial.println("Max clients reached - no more connections available"); return false; } - + // Create and setup a new client _client = BLEDevice::createClient(); _client->setClientCallbacks(new MyClientCallbacks(this), false); _client->setConnectionParams(BLEMIDI_CLIENT_COMM_MIN_INTERVAL, BLEMIDI_CLIENT_COMM_MAX_INTERVAL, BLEMIDI_CLIENT_COMM_LATENCY, BLEMIDI_CLIENT_COMM_TIMEOUT); - + /** Set how long we are willing to wait for the connection to complete (seconds), default is 30. */ _client->setConnectTimeout(15); @@ -523,15 +556,15 @@ bool BLEMIDI_Client_ESP32::connect() Serial.print("RSSI: "); Serial.println(_client->getRssi()); */ - + /** Now we can read/write/subscribe the charateristics of the services we are interested in */ pSvc = _client->getService(SERVICE_UUID); if (pSvc) /** make sure it's not null */ - { + { _characteristic = pSvc->getCharacteristic(CHARACTERISTIC_UUID); - + if (_characteristic) /** make sure it's not null */ - { + { if (_characteristic->canNotify()) { if (_characteristic->subscribe(true, std::bind(&BLEMIDI_Client_ESP32::notifyCB, this, _1, _2, _3, _4))) @@ -542,7 +575,7 @@ bool BLEMIDI_Client_ESP32::connect() } } } - + //If anything fails, disconnect and delete client _client->disconnect(); NimBLEDevice::deleteClient(_client);