Skip to content

Commit

Permalink
Implement isRealTimeCommand for BT client
Browse files Browse the repository at this point in the history
Simplify push2buffer code
  • Loading branch information
luc-github committed Dec 8, 2024
1 parent 3a6c216 commit 6e49a39
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 30 deletions.
54 changes: 24 additions & 30 deletions esp3d/src/modules/bluetooth/BT_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,51 +189,45 @@ void BTService::initAuthentication() {
}
ESP3DAuthenticationLevel BTService::getAuthentication() { return _auth; }

void BTService::flushbuffer() {
_buffer[_buffer_size] = 0x0;
// dispatch command
ESP3DMessage *msg = esp3d_message_manager.newMsg(
void BTService::flushData(const uint8_t *data, size_t size,
ESP3DMessageType type) {
ESP3DMessage *message = esp3d_message_manager.newMsg(
ESP3DClientType::bluetooth, esp3d_commands.getOutputClient(), _buffer,
_buffer_size, _auth);
if (msg) {
// process command
esp3d_commands.process(msg);

if (message) {
message->type = type;
esp3d_log("Process Message");
esp3d_commands.process(message);
} else {
esp3d_log_e("Cannot create message");
}
_lastflush = millis();
}

void BTService::flushChar(char c) {
flushData((uint8_t *)&c, 1, ESP3DMessageType::realtimecmd);
}

void BTService::flushBuffer() {
_buffer[_buffer_size] = 0x0;
flushData((uint8_t *)_buffer, _buffer_size, ESP3DMessageType::unique);
_buffer_size = 0;
}

// push collected data to buffer and proceed accordingly
void BTService::push2buffer(uint8_t *sbuf, size_t len) {
for (size_t i = 0; i < len; i++) {
_lastflush = millis();
// command is defined
if (char(sbuf[i]) == '\n') {
if (_buffer_size < ESP3D_BT_BUFFER_SIZE) {
_buffer[_buffer_size] = sbuf[i];
_buffer_size++;
}
flushbuffer();
} else if (esp3d_string::isPrintableChar(char(sbuf[i]))) {
if (_buffer_size < ESP3D_BT_BUFFER_SIZE) {
_buffer[_buffer_size] = sbuf[i];
_buffer_size++;
} else {
flushbuffer();
_buffer[_buffer_size] = sbuf[i];
_buffer_size++;
}
} else { // it is not printable char
// clean buffer first
if (_buffer_size > 0) {
flushbuffer();
}
// process char
if (esp3d_string::isRealTimeCommand(sbuf[i])) {
flushChar(sbuf[i]);
} else {
_buffer[_buffer_size] = sbuf[i];
_buffer_size++;
flushbuffer();
if (_buffer_size > ESP3D_SERIAL_BUFFER_SIZE ||
_buffer[_buffer_size - 1] == '\n') {
flushBuffer();
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions esp3d/src/modules/bluetooth/BT_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class BTService {
size_t _buffer_size;
void push2buffer(uint8_t* sbuf, size_t len);
void flushbuffer();
void flushChar(char c);
void flushData(const uint8_t* data, size_t size, ESP3DMessageType type);
bool _started;
};

Expand Down

0 comments on commit 6e49a39

Please sign in to comment.