Skip to content

Commit

Permalink
BLE
Browse files Browse the repository at this point in the history
  • Loading branch information
pschatzmann committed Nov 4, 2023
1 parent 7346c18 commit ecd8ddf
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @file communications-ble-client-send.ino
* @author Phil Schatzmann
* @brief Sending audio via BLE: the client acts as audio source
* @version 0.1
* @date 2022-11-04
*
* @copyright Copyright (c) 2022
*/


#include "AudioTools.h"
#include "AudioCodecs/CodecADPCM.h" // https://github.com/pschatzmann/adpcm
#include "Sandbox/BLE/AudioBLE.h"

AudioInfo info(16000, 1, 16);
SineWaveGenerator<int16_t> sineWave( 32000); // subclass of SoundGenerator with max amplitude of 32000
GeneratedSoundStream<int16_t> sound( sineWave); // Stream generated from sine wave
Throttle throttle(sound);
AudioBLEClient ble;
ADPCMEncoder adpcm(AV_CODEC_ID_ADPCM_IMA_WAV);
EncodedAudioStream encoder(&ble, &adpcm);
StreamCopy copier(encoder, throttle);

void setup() {
Serial.begin(115200);
AudioLogger::instance().begin(Serial, AudioLogger::Info);

sineWave.begin(info, N_B4);
throttle.begin(info);
encoder.begin(info);
ble.begin("ble-send", 60*10);

Serial.println("started...");
}

void loop() {
copier.copy();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @file communications-ble-server-receive.ino
* @author Phil Schatzmann
* @brief Receiving audio via BLE: the server acts as audio sink
* @version 0.1
* @date 2022-11-04
*
* @copyright Copyright (c) 2022
*/

#include "AudioTools.h"
#include "AudioCodecs/CodecADPCM.h" // https://github.com/pschatzmann/adpcm
#include "Sandbox/BLE/AudioBLE.h"
//#include "AudioLibs/AudioKit.h"

AudioInfo info(16000, 1, 16);
ADPCMDecoder adpcm(AV_CODEC_ID_ADPCM_IMA_WAV);
I2SStream i2s; // or AudioKitStream ...
EncodedAudioStream decoder(&i2s, &adpcm);
AudioBLEServer ble;
StreamCopy copier(decoder, ble);

void setup() {
Serial.begin(115200);
AudioLogger::instance().begin(Serial, AudioLogger::Warning);

decoder.begin(info);

auto cfg = i2s.defaultConfig();
cfg.copyFrom(info);
i2s.begin(cfg);

ble.begin("ble-receive");
}

void loop() {
copier.copy();
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* @file example-serial-receive.ino
* @file communications-ble-client-receive.ino
* @author Phil Schatzmann
* @brief Receiving audio via BLE and writing to I2S
* @brief Receiving audio via BLE: The client acts as audio sink and is writing to I2S
* @version 0.1
* @date 2022-03-09
* @date 2022-11-04
*
* @copyright Copyright (c) 2022
* @copyright Copyright (c) 2023
*/


Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* @file communications-ble-send.ino
* @file communications-ble-server-send.ino
* @author Phil Schatzmann
* @brief Sending audio via BLE
* @brief Sending audio via BLE: The server acts as audio source
* @version 0.1
* @date 2022-03-09
* @date 2022-11-04
*
* @copyright Copyright (c) 2022
* @copyright Copyright (c) 2023
*/

#include "AudioTools.h"
Expand Down
49 changes: 42 additions & 7 deletions src/Sandbox/BLE/AudioBLEClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,31 @@ class AudioBLEClient : public AudioBLEStream,
size_t write(const uint8_t *data, size_t dataSize) override {
TRACED();
setupBLEClient();
int result = 0;
if (!is_client_connected || !is_client_set_up)
return 0;
if (ch02_char->canWrite()) {
ch02_char->writeValue((uint8_t *)data, dataSize, false);
result = dataSize;
if (!ch02_char->canWrite()){
return 0;
}

if (is_framed){
writeChannel2Characteristic(data, dataSize);
delay(1);
} else {
// send only data with max mtu
for (int j=0; j<dataSize; j++){
write_buffer.write(data[j]);
if (write_buffer.isFull()){
writeChannel2Characteristic(write_buffer.data(), write_buffer.available());
write_buffer.reset();
}
}
}
return result;
return dataSize;
}

int availableForWrite() override { return BLE_MTU - BLE_MTU_OVERHEAD; }
int availableForWrite() override {
return is_framed ? (BLE_MTU - BLE_MTU_OVERHEAD) : DEFAULT_BUFFER_SIZE;
}

bool connected() override {
if (!setupBLEClient()) {
Expand All @@ -89,7 +103,13 @@ class AudioBLEClient : public AudioBLEStream,
return is_client_connected;
}

void doLoop() { setupBLEClient(); }
void setWriteThrottle(int ms){
write_throttle = ms;
}

void setConfirmWrite(bool flag){
write_confirmation_flag = flag;
}

protected:
// client
Expand All @@ -105,6 +125,9 @@ class AudioBLEClient : public AudioBLEStream,
BLEUUID BLUEID_CH1_UUID{BLE_CH1_UUID};
BLEUUID BLUEID_CH2_UUID{BLE_CH2_UUID};
BLEUUID BLUEID_INFO_UUID{BLE_INFO_UUID};
SingleBuffer<uint8_t> write_buffer{0};
int write_throttle = 0;
bool write_confirmation_flag = false;

volatile bool is_client_connected = false;
bool is_client_set_up = false;
Expand All @@ -125,6 +148,13 @@ class AudioBLEClient : public AudioBLEStream,
info_char->writeValue((uint8_t *)&info, sizeof(AudioInfo));
}

void writeChannel2Characteristic(const uint8_t*data, size_t len){
if (ch02_char->canWrite()) {
ch02_char->writeValue((uint8_t *)data, len, write_confirmation_flag);
delay(write_throttle);
}
}

bool readAudioInfoCharacteristic(){
if (!info_char->canRead())
return false;
Expand Down Expand Up @@ -166,6 +196,11 @@ class AudioBLEClient : public AudioBLEStream,

TRACEI();

// setup buffer
if (write_buffer.size()==0){
write_buffer.resize(getMTU() - BLE_MTU_OVERHEAD);
}

if (p_client == nullptr)
p_client = BLEDevice::createClient();

Expand Down

0 comments on commit ecd8ddf

Please sign in to comment.