Skip to content

Commit

Permalink
Decouple Serial object from SerialInjector (#787)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoseperez committed Apr 18, 2018
1 parent 8189f90 commit 91e25ea
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 56 deletions.
14 changes: 13 additions & 1 deletion code/espurna/debug.ino
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ void debugSend_P(PGM_P format_P, ...) {

#if DEBUG_WEB_SUPPORT

void debugSetup() {
void debugWebSetup() {

wsOnSendRegister([](JsonObject& root) {
root["dbgVisible"] = 1;
Expand All @@ -145,6 +145,17 @@ void debugSetup() {

#endif // DEBUG_WEB_SUPPORT

void debugSetup() {

#if DEBUG_SERIAL_SUPPORT
DEBUG_PORT.begin(SERIAL_BAUDRATE);
#if DEBUG_ESP_WIFI
DEBUG_PORT.setDebugOutput(true);
#endif
#endif

}

// -----------------------------------------------------------------------------
// Save crash info
// Taken from krzychb EspSaveCrash
Expand Down Expand Up @@ -274,4 +285,5 @@ void debugDumpCrashInfo() {
DEBUG_MSG_P(PSTR("<<<stack<<<\n"));

}

#endif // DEBUG_SUPPORT
7 changes: 6 additions & 1 deletion code/espurna/espurna.ino
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ void setup() {
// Basic modules, will always run
// -------------------------------------------------------------------------

// Serial debug
#if DEBUG_SUPPORT
debugSetup();
#endif

// Init EEPROM, Serial, SPIFFS and system check
systemSetup();

Expand Down Expand Up @@ -81,7 +86,7 @@ void setup() {
wsSetup();
apiSetup();
#if DEBUG_WEB_SUPPORT
debugSetup();
debugWebSetup();
#endif
#endif

Expand Down
50 changes: 26 additions & 24 deletions code/espurna/libs/StreamInjector.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,50 @@ class StreamInjector : public Stream {

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

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

~StreamInjector() {
delete[] _buffer;
}

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

virtual uint8_t inject(char ch) {
_buffer[_buffer_write] = ch;
_buffer_write = (_buffer_write + 1) % _buffer_size;
return 1;
}

virtual uint8_t inject(char *data, size_t len) {
for (int i=0; i<len; i++) {
inject(data[i]);
}
return len;
}

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

virtual void callback(writeCallback c) {
_callback = c;
}

virtual size_t write(uint8_t ch) {
if (_callback) _callback(ch);
return _stream.write(ch);
}

virtual int read() {
int ch = _stream.read();
if (ch == -1) {
if (_buffer_read != _buffer_write) {
ch = _buffer[_buffer_read];
_buffer_read = (_buffer_read + 1) % _buffer_size;
}
int ch = -1;
if (_buffer_read != _buffer_write) {
ch = _buffer[_buffer_read];
_buffer_read = (_buffer_read + 1) % _buffer_size;
}
return ch;
}

virtual int available() {
unsigned int bytes = _stream.available();
unsigned int bytes = 0;
if (_buffer_read > _buffer_write) {
bytes += (_buffer_write - _buffer_read + _buffer_size);
} else if (_buffer_read < _buffer_write) {
Expand All @@ -51,35 +65,23 @@ class StreamInjector : public Stream {
}

virtual int peek() {
int ch = _stream.peek();
if (ch == -1) {
if (_buffer_read != _buffer_write) {
ch = _buffer[_buffer_read];
}
int ch = -1;
if (_buffer_read != _buffer_write) {
ch = _buffer[_buffer_read];
}
return ch;
}

virtual void flush() {
_stream.flush();
_buffer_read = _buffer_write;
}

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) % _buffer_size;
}
}

private:

Stream& _stream;
char * _buffer;
unsigned char _buffer_size;
unsigned char _buffer_write = 0;
unsigned char _buffer_read = 0;
writeCallback _callback = NULL;


};
43 changes: 20 additions & 23 deletions code/espurna/settings.ino
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,10 @@ Copyright (C) 2016-2018 by Xose Pérez <xose dot perez at gmail dot com>
#include <vector>
#include "libs/EmbedisWrap.h"
#include <Stream.h>
#include "libs/StreamInjector.h"

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

#if TELNET_SUPPORT
#include "libs/StreamInjector.h"
StreamInjector _serial = StreamInjector(EMBEDIS_PORT, TERMINAL_BUFFER_SIZE);
#undef EMBEDIS_PORT
#define EMBEDIS_PORT _serial
#endif

EmbedisWrap embedis(EMBEDIS_PORT, TERMINAL_BUFFER_SIZE);
StreamInjector _serial = StreamInjector(TERMINAL_BUFFER_SIZE);
EmbedisWrap embedis(_serial, TERMINAL_BUFFER_SIZE);

#if TERMINAL_SUPPORT
#if SERIAL_RX_ENABLED
Expand Down Expand Up @@ -360,11 +349,9 @@ void resetSettings() {
// Settings
// -----------------------------------------------------------------------------

#if TELNET_SUPPORT
void settingsInject(void *data, size_t len) {
_serial.inject((char *) data, len);
}
#endif
void settingsInject(void *data, size_t len) {
_serial.inject((char *) data, len);
}

size_t settingsMaxSize() {
size_t size = EEPROM_SIZE;
Expand Down Expand Up @@ -420,11 +407,14 @@ void settingsSetup() {

EEPROM.begin(SPI_FLASH_SEC_SIZE);

#if TELNET_SUPPORT
_serial.callback([](uint8_t ch) {
_serial.callback([](uint8_t ch) {
#if TELNET_SUPPORT
telnetWrite(ch);
});
#endif
#endif
#if DEBUG_SERIAL_SUPPORT
DEBUG_PORT.write(ch);
#endif
});

Embedis::dictionary( F("EEPROM"),
SPI_FLASH_SEC_SIZE,
Expand Down Expand Up @@ -457,8 +447,15 @@ void settingsLoop() {
_settings_save = false;
}


#if TERMINAL_SUPPORT

#if DEBUG_SERIAL_SUPPORT
while (DEBUG_PORT.available()) {
_serial.inject(DEBUG_PORT.read());
}
#endif

embedis.process();

#if SERIAL_RX_ENABLED
Expand Down
7 changes: 0 additions & 7 deletions code/espurna/system.ino
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,6 @@ void systemSetup() {

EEPROM.begin(EEPROM_SIZE);

#if DEBUG_SERIAL_SUPPORT
DEBUG_PORT.begin(SERIAL_BAUDRATE);
#if DEBUG_ESP_WIFI
DEBUG_PORT.setDebugOutput(true);
#endif
#endif

#if SPIFFS_SUPPORT
SPIFFS.begin();
#endif
Expand Down

0 comments on commit 91e25ea

Please sign in to comment.