Merge pull request #6305 from s-hadinger/tasmotaserial_config_buffer

Add ability to change the receive buffer size in TasmotaSerial.
This commit is contained in:
Theo Arends 2019-08-27 22:40:33 +02:00 committed by GitHub
commit 779faf5f39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 8 deletions

View File

@ -76,7 +76,7 @@ static void (*ISRList[16])() = {
tms_isr_15
};
TasmotaSerial::TasmotaSerial(int receive_pin, int transmit_pin, int hardware_fallback, int nwmode)
TasmotaSerial::TasmotaSerial(int receive_pin, int transmit_pin, int hardware_fallback, int nwmode, int buffer_size)
{
m_valid = false;
m_hardserial = 0;
@ -98,7 +98,7 @@ TasmotaSerial::TasmotaSerial(int receive_pin, int transmit_pin, int hardware_fal
}
else {
if (m_rx_pin > -1) {
m_buffer = (uint8_t*)malloc(TM_SERIAL_BUFFER_SIZE);
m_buffer = (uint8_t*)malloc(serial_buffer_size);
if (m_buffer == NULL) return;
// Use getCycleCount() loop to get as exact timing as possible
m_bit_time = ESP.getCpuFreqMHz() * 1000000 / TM_SERIAL_BAUDRATE;
@ -185,7 +185,7 @@ int TasmotaSerial::read()
} else {
if ((-1 == m_rx_pin) || (m_in_pos == m_out_pos)) return -1;
uint8_t ch = m_buffer[m_out_pos];
m_out_pos = (m_out_pos +1) % TM_SERIAL_BUFFER_SIZE;
m_out_pos = (m_out_pos +1) % serial_buffer_size;
return ch;
}
}
@ -196,7 +196,7 @@ int TasmotaSerial::available()
return Serial.available();
} else {
int avail = m_in_pos - m_out_pos;
if (avail < 0) avail += TM_SERIAL_BUFFER_SIZE;
if (avail < 0) avail += serial_buffer_size;
return avail;
}
}
@ -260,7 +260,7 @@ if (!m_nwmode) {
TM_SERIAL_WAIT;
}
// Store the received value in the buffer unless we have an overflow
unsigned int next = (m_in_pos+1) % TM_SERIAL_BUFFER_SIZE;
unsigned int next = (m_in_pos+1) % serial_buffer_size;
if (next != (int)m_out_pos) {
m_buffer[m_in_pos] = rec;
m_in_pos = next;
@ -297,7 +297,7 @@ if (!m_nwmode) {
ss_byte|=(1<<i);
}
//stobyte(0,ssp->ss_byte>>1);
unsigned int next = (m_in_pos+1) % TM_SERIAL_BUFFER_SIZE;
unsigned int next = (m_in_pos+1) % serial_buffer_size;
if (next != (int)m_out_pos) {
m_buffer[m_in_pos] = ss_byte>>1;
m_in_pos = next;
@ -311,7 +311,7 @@ if (!m_nwmode) {
if (diff>=LASTBIT) {
// bit zero was 0,
//stobyte(0,ssp->ss_byte>>1);
unsigned int next = (m_in_pos+1) % TM_SERIAL_BUFFER_SIZE;
unsigned int next = (m_in_pos+1) % serial_buffer_size;
if (next != (int)m_out_pos) {
m_buffer[m_in_pos] = ss_byte>>1;
m_in_pos = next;

View File

@ -38,7 +38,7 @@
class TasmotaSerial : public Stream {
public:
TasmotaSerial(int receive_pin, int transmit_pin, int hardware_fallback = 0,int nwmode = 0);
TasmotaSerial(int receive_pin, int transmit_pin, int hardware_fallback = 0,int nwmode = 0, int buffer_size = TM_SERIAL_BUFFER_SIZE);
virtual ~TasmotaSerial();
bool begin(long speed, int stop_bits = 1);
@ -75,6 +75,7 @@ class TasmotaSerial : public Stream {
unsigned int m_in_pos;
unsigned int m_out_pos;
uint8_t *m_buffer;
int serial_buffer_size;
};
#endif // TasmotaSerial_h