Skip to content

Commit

Permalink
Use same buffer size across all terminal-realted classes/methods. Set…
Browse files Browse the repository at this point in the history
… to 128 chars (#477)
  • Loading branch information
xoseperez committed Jan 28, 2018
1 parent 5655a5f commit 9e8e667
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
6 changes: 3 additions & 3 deletions code/espurna/config/general.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@
#define SERIAL_RX_BAUDRATE 115200 // Default baudrate
#endif

#define SERIAL_RX_BUFFER_SIZE 32

//------------------------------------------------------------------------------

// UDP debug log
Expand Down Expand Up @@ -124,9 +122,11 @@
//------------------------------------------------------------------------------

#ifndef TERMINAL_SUPPORT
#define TERMINAL_SUPPORT 1 // Enable terminal commands (0.97Kb)
#define TERMINAL_SUPPORT 1 // Enable terminal commands (0.97Kb)
#endif

#define TERMINAL_BUFFER_SIZE 128 // Max size for commands commands

//------------------------------------------------------------------------------
// SYSTEM CHECK
//------------------------------------------------------------------------------
Expand Down
19 changes: 13 additions & 6 deletions code/espurna/libs/StreamInjector.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@

#pragma once

#define STREAM_INJECTOR_BUFFER_SIZE 32
#include <Stream.h>

class StreamInjector : public Stream {

public:

typedef std::function<void(uint8_t ch)> writeCallback;

StreamInjector(Stream& serial) : _stream(serial), _buffer() {}
StreamInjector(Stream& serial, size_t buflen = 128) : _stream(serial), _buffer_size(buflen) {
char * _buffer = new char[buflen];
}

~StreamInjector() {
delete[] _buffer;
}

virtual void callback(writeCallback c) {
_callback = c;
Expand All @@ -28,7 +34,7 @@ class StreamInjector : public Stream {
if (ch == -1) {
if (_buffer_read != _buffer_write) {
ch = _buffer[_buffer_read];
_buffer_read = (_buffer_read + 1) % STREAM_INJECTOR_BUFFER_SIZE;
_buffer_read = (_buffer_read + 1) % _buffer_size;
}
}
return ch;
Expand All @@ -37,7 +43,7 @@ class StreamInjector : public Stream {
virtual int available() {
unsigned int bytes = _stream.available();
if (_buffer_read > _buffer_write) {
bytes += (_buffer_write - _buffer_read + STREAM_INJECTOR_BUFFER_SIZE);
bytes += (_buffer_write - _buffer_read + _buffer_size);
} else if (_buffer_read < _buffer_write) {
bytes += (_buffer_write - _buffer_read);
}
Expand All @@ -62,14 +68,15 @@ class StreamInjector : public Stream {
virtual void inject(char *data, size_t len) {
for (int i=0; i<len; i++) {
_buffer[_buffer_write] = data[i];
_buffer_write = (_buffer_write + 1) % STREAM_INJECTOR_BUFFER_SIZE;
_buffer_write = (_buffer_write + 1) % _buffer_size;
}
}

private:

Stream& _stream;
unsigned char _buffer[STREAM_INJECTOR_BUFFER_SIZE];
char * _buffer;
unsigned char _buffer_size;
unsigned char _buffer_write = 0;
unsigned char _buffer_read = 0;
writeCallback _callback = NULL;
Expand Down
29 changes: 14 additions & 15 deletions code/espurna/settings.ino
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,29 @@ Copyright (C) 2016-2018 by Xose Pérez <xose dot perez at gmail dot com>
*/

#include <EEPROM.h>
#include <vector>
#include "spi_flash.h"
#include "libs/EmbedisWrap.h"
#include <vector>
#include <StreamString.h>
#include <Stream.h>

#ifdef DEBUG_PORT
#define EMBEDIS_PORT DEBUG_PORT
#else
#define EMBEDIS_PORT Serial
#endif

#if TELNET_SUPPORT
#include "libs/StreamInjector.h"
#ifdef DEBUG_PORT
StreamInjector _serial = StreamInjector(DEBUG_PORT);
#else
StreamInjector _serial = StreamInjector(Serial);
#endif
EmbedisWrap embedis(_serial);
#else
#ifdef DEBUG_PORT
EmbedisWrap embedis(DEBUG_PORT);
#else
EmbedisWrap embedis(_serial);
#endif
StreamInjector _serial = StreamInjector(EMBEDIS_PORT, TERMINAL_BUFFER_SIZE);
#undef EMBEDIS_PORT
#define EMBEDIS_PORT _serial
#endif

EmbedisWrap embedis(EMBEDIS_PORT, TERMINAL_BUFFER_SIZE);

#if TERMINAL_SUPPORT
#ifdef SERIAL_RX_PORT
char _serial_rx_buffer[SERIAL_RX_BUFFER_SIZE];
char _serial_rx_buffer[TERMINAL_BUFFER_SIZE];
static unsigned char _serial_rx_pointer = 0;
#endif
#endif
Expand Down

0 comments on commit 9e8e667

Please sign in to comment.