Skip to content

Commit

Permalink
Added in much deeper buffering, ZMODEM and YMODEM seem to work now
Browse files Browse the repository at this point in the history
  • Loading branch information
faydr committed May 6, 2021
1 parent 9b75226 commit 7052e68
Showing 1 changed file with 57 additions and 6 deletions.
63 changes: 57 additions & 6 deletions qmesh_interface/qmesh_interface.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,77 @@
//and also demonstrate that SerialBT have the same functionalities of a normal Serial

#include "BluetoothSerial.h"
#include <deque>

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BluetoothSerial SerialBT;
//HardwareSerial Serial;


void setup() {
Serial.begin(115200);
Serial.setRxBufferSize(16384);
Serial.setRxBufferSize(4096);
SerialBT.begin("ESP32test"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
}

uint8_t bt_buf[256];
#define MAX_DEQUE_SIZE 65536
#define MAX_WRITE_BYTES 16
std::deque<uint8_t> rx_serial, rx_btserial;

void loop() {
if (Serial.available()) {
SerialBT.write(Serial.read());
// First, store any received data
if(Serial.available()) {
while(Serial.available()) {
rx_serial.push_front(Serial.read());
if(rx_serial.size() > MAX_DEQUE_SIZE) {
rx_serial.clear();
}
}
}
if(SerialBT.available()) {
while(SerialBT.available()) {
rx_btserial.push_front(SerialBT.read());
if(rx_btserial.size() > MAX_DEQUE_SIZE) {
rx_btserial.clear();
}
}
}
if (SerialBT.available()) {
Serial.write(SerialBT.read());

// Next, send out the buffer data
if(rx_btserial.size() > 0) {
std::vector<uint8_t> write_bytes;
size_t num_write_bytes = Serial.availableForWrite();
if(num_write_bytes > MAX_DEQUE_SIZE) {
num_write_bytes = MAX_DEQUE_SIZE;
}
if(num_write_bytes > rx_btserial.size()) {
num_write_bytes = rx_btserial.size();
}
for(size_t i = 0; i < num_write_bytes; i++) {
write_bytes.push_back(rx_btserial.back());
rx_btserial.pop_back();
}
Serial.write(write_bytes.data(), write_bytes.size());
}

if(rx_serial.size() > 0) {
std::vector<uint8_t> write_bytes;
size_t num_write_bytes = rx_serial.size();
if(num_write_bytes > MAX_DEQUE_SIZE) {
num_write_bytes = MAX_DEQUE_SIZE;
}
std::deque<uint8_t>::iterator iter = rx_serial.end();
for(size_t i = 0; i < num_write_bytes; i++) {
write_bytes.push_back(*--iter);
}
if(SerialBT.write(write_bytes.data(), write_bytes.size())) {
rx_serial.erase(iter, rx_serial.end());
}
}
delay(1);

}

0 comments on commit 7052e68

Please sign in to comment.