trying to avoid internal buffers

This commit is contained in:
lathoub 2019-08-17 16:39:02 +02:00
parent 721fe62034
commit 0787571f5f
3 changed files with 42 additions and 24 deletions

View File

@ -7,6 +7,9 @@ bleMidi::BluetoothEsp32 sBluetoothEsp32;
bleMidi::BleMidiTransport<bleMidi::BluetoothEsp32> bm((bleMidi::BluetoothEsp32&) sBluetoothEsp32); bleMidi::BleMidiTransport<bleMidi::BluetoothEsp32> bm((bleMidi::BluetoothEsp32&) sBluetoothEsp32);
midi::MidiInterface<bleMidi::BleMidiTransport<bleMidi::BluetoothEsp32>> MIDI((bleMidi::BleMidiTransport<bleMidi::BluetoothEsp32>&)bm); midi::MidiInterface<bleMidi::BleMidiTransport<bleMidi::BluetoothEsp32>> MIDI((bleMidi::BleMidiTransport<bleMidi::BluetoothEsp32>&)bm);
unsigned long t0 = millis();
bool isConnected = false;
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// //
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
@ -16,7 +19,9 @@ void setup()
Serial.begin(115200); Serial.begin(115200);
while (!Serial); while (!Serial);
MIDI.begin("hihi", 1); Serial.println(F("booting"));
MIDI.begin("Huzzah BLE MIDI", 1);
bm.onConnected(OnBleMidiConnected); bm.onConnected(OnBleMidiConnected);
bm.onDisconnected(OnBleMidiDisconnected); bm.onDisconnected(OnBleMidiDisconnected);
@ -24,7 +29,7 @@ void setup()
MIDI.setHandleNoteOn(OnBleMidiNoteOn); MIDI.setHandleNoteOn(OnBleMidiNoteOn);
MIDI.setHandleNoteOff(OnBleMidiNoteOff); MIDI.setHandleNoteOff(OnBleMidiNoteOff);
Serial.println(F("looping")); Serial.println(F("ready"));
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
@ -34,10 +39,14 @@ void loop()
{ {
MIDI.read(); MIDI.read();
MIDI.sendNoteOn(60, 127, 1); // note 60, velocity 127 on channel 1 if (isConnected && (millis() - t0) > 1000)
MIDI.sendNoteOff(60, 127, 1); {
t0 = millis();
MIDI.sendNoteOn(60, 127, 1); // note 60, velocity 127 on channel 1
MIDI.sendNoteOff(60, 127, 1);
}
delay(1000);
} }
// ==================================================================================== // ====================================================================================
@ -49,6 +58,7 @@ void loop()
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void OnBleMidiConnected() { void OnBleMidiConnected() {
Serial.println(F("Connected")); Serial.println(F("Connected"));
isConnected = true;
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
@ -56,6 +66,7 @@ void OnBleMidiConnected() {
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void OnBleMidiDisconnected() { void OnBleMidiDisconnected() {
Serial.println(F("Disconnected")); Serial.println(F("Disconnected"));
isConnected = false;
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------

View File

@ -125,7 +125,7 @@ bool BluetoothEsp32::begin(const char* deviceName, BleMidiTransport<class Blueto
advertisementData.setFlags(0x04); advertisementData.setFlags(0x04);
advertisementData.setCompleteServices(BLEUUID(SERVICE_UUID)); advertisementData.setCompleteServices(BLEUUID(SERVICE_UUID));
advertisementData.setName(deviceName); advertisementData.setName(deviceName);
// Start advertising // Start advertising
_advertising = _server->getAdvertising(); _advertising = _server->getAdvertising();
_advertising->setAdvertisementData(advertisementData); _advertising->setAdvertisementData(advertisementData);

View File

@ -15,12 +15,10 @@ template<class BleClass>
class BleMidiTransport class BleMidiTransport
{ {
private: private:
uint8_t _midiPacket[5]; // outgoing midi::RingBuffer<byte, 44> mRxBuffer;
typedef midi::RingBuffer<byte, 44> TxBuffer; byte mTxBuffer[44];
typedef midi::RingBuffer<byte, 44> RxBuffer; unsigned mTxIndex;
TxBuffer mTxBuffer;
RxBuffer mRxBuffer;
bool _connected; bool _connected;
@ -29,35 +27,44 @@ private:
public: public:
inline BleMidiTransport(BleClass& inBleClass) inline BleMidiTransport(BleClass& inBleClass)
: mBleClass(inBleClass) : mBleClass(inBleClass), mTxIndex(0)
{ {
} }
inline ~BleMidiTransport() {} inline ~BleMidiTransport() {}
inline bool begin(int baudrate) {} // n/a inline bool begin(int baudrate) {} // n/a
inline bool begin(const char* deviceName) { return mBleClass.begin(deviceName, this); }
inline bool begin(const char* deviceName)
{
return mBleClass.begin(deviceName, this);
}
inline unsigned available() { return mRxBuffer.getLength(); } inline unsigned available() { return mRxBuffer.getLength(); }
inline byte read() { return mRxBuffer.read(); } inline byte read() { return mRxBuffer.read(); }
inline void beginWrite() { mTxBuffer.clear(); }
inline void write(byte inData) { mTxBuffer.write(inData); } inline void beginWrite()
{
getMidiTimestamp(&mTxBuffer[0], &mTxBuffer[1]);
mTxIndex = 2;
}
inline void write(byte inData)
{
// check for size! SysEx!!!
if (false)
{
// should only happen from SysEx!
// if we approach the end of the buffer, chop-up in segments until
// we reach F7 (end of SysEx)
}
mTxBuffer[mTxIndex++] = inData;
}
inline void endWrite() inline void endWrite()
{ {
getMidiTimestamp(&_midiPacket[0], &_midiPacket[1]); mBleClass.write(mTxBuffer, mTxIndex);
mTxBuffer.read(_midiPacket + 2, mTxBuffer.getLength());
mBleClass.write((uint8_t*)_midiPacket, mTxBuffer.getLength() + 2);
} }
public: public:
void receive(uint8_t* buffer, uint8_t length) void receive(uint8_t* buffer, uint8_t length)
{ {
// TODO: check for size!! (SysEx!!)
mRxBuffer.read(buffer, length); mRxBuffer.read(buffer, length);
} }