mirror of
https://github.com/DCC-EX/CommandStation-EX.git
synced 2024-12-23 21:01:25 +01:00
6833d82789
And diag output of replies
87 lines
2.0 KiB
C++
87 lines
2.0 KiB
C++
/*
|
|
* © 2020, Chris Harlow. All rights reserved.
|
|
*
|
|
* This file is part of DCC-EX CommandStation-EX
|
|
*
|
|
* This is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* It is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "RingStream.h"
|
|
#include "DIAG.h"
|
|
|
|
RingStream::RingStream( const uint16_t len)
|
|
{
|
|
_len=len;
|
|
_buffer=new byte[len];
|
|
_pos_write=0;
|
|
_pos_read=0;
|
|
_buffer[0]=0;
|
|
_overflow=false;
|
|
_mark=0;
|
|
}
|
|
|
|
size_t RingStream::write(uint8_t b) {
|
|
if (_overflow) return 0;
|
|
_buffer[_pos_write] = b;
|
|
++_pos_write;
|
|
if (_pos_write>=_len) _pos_write=0;
|
|
if (_pos_write==_pos_read) {
|
|
_overflow=true;
|
|
DIAG(F("\nRingStream(%d) OVERFLOW %d %d \n"),_len, _pos_write, _pos_read);
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
int RingStream::read() {
|
|
if (_pos_read==_pos_write) return -1;
|
|
byte b=_buffer[_pos_read];
|
|
_pos_read++;
|
|
if (_pos_read>=_len) _pos_read=0;
|
|
_overflow=false;
|
|
return b;
|
|
}
|
|
|
|
|
|
int RingStream::count() {
|
|
int peek=_pos_read;
|
|
int counter=0;
|
|
while(_buffer[peek]) {
|
|
counter++;
|
|
peek++;
|
|
if (peek >= _len) peek=0;
|
|
}
|
|
return counter;
|
|
}
|
|
|
|
int RingStream::freeSpace() {
|
|
if (_pos_read>_pos_write) return _pos_read-_pos_write-2;
|
|
else return _len - _pos_write + _pos_read-2;
|
|
}
|
|
|
|
|
|
void RingStream::mark() {
|
|
_mark=_pos_write;
|
|
}
|
|
|
|
bool RingStream::commit() {
|
|
if (_overflow) {
|
|
_pos_write=_mark;
|
|
_overflow=false;
|
|
return false; // commit failed
|
|
}
|
|
write((uint8_t)0);
|
|
return true; // commit worked
|
|
}
|