Good evening.
I've been working on a circular buffer library that's based on the HardwareSerial library. When I say based, I mean copied and then hacked around a bit to remove functions I don't need. As far as the buffer is concerned, it's actually doing exactly what I wanted, but somewhere along the way, I appear to have stopped it printing text.
Code like this works as expected and prints out to the terminal program (PuTTY).
mySerial.print(Byte[i], HEX);
Code like this prints nothing, but the cursor does move to the next line. It looks like any characters enclosed in quotes don't print, but printing the contents of a variable or array print fine.
mySerial.println("--This doesn't print any more--");
This is the chopped down HardwareSerial.h and HardwareSerial.cpp files (renamed CircularBuffer.h and CircularBuffer.cpp) . If someone can have a look and possibly identify what I've done wrong, I'd appreciate it. I've been comparing these modified files with the originals for hours, but can't find what I've done wrong.
/*
CircularBuffer.h - Circular Buffer library
*/
#ifndef CircularBuffer_h
#define CircularBuffer_h
#endif
#include <inttypes.h>
#include "Stream.h"
struct ring_buffer;
class CircularBuffer : public Stream
{
private:
ring_buffer *_ibus_buffer;
public:
CircularBuffer(ring_buffer *ibus_buffer);
virtual int available(void);
virtual int peek(void);
virtual int peekn(int a);
virtual void remove(uint8_t);
virtual int read(void);
virtual void flush(void);
virtual size_t write(uint8_t);
inline size_t write(unsigned long n) { return write((uint8_t)n); }
inline size_t write(long n) { return write((uint8_t)n); }
inline size_t write(unsigned int n) { return write((uint8_t)n); }
inline size_t write(int n) { return write((uint8_t)n); }
using Print::write; // pull in write(str) and write(buf, size) from Print
operator bool();
};
extern CircularBuffer Buffer;
/*
CircularBuffer.cpp - Circular Buffer library.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include "Arduino.h"
#include "wiring_private.h"
#include "CircularBuffer.h"
#define BUFFER_SIZE 256
struct ring_buffer
{
unsigned char buffer[BUFFER_SIZE];
volatile unsigned int head;
volatile unsigned int tail;
};
ring_buffer ibus_buffer = { { 0 }, 0, 0};
inline void store_char(unsigned char c, ring_buffer *buffer)
{
int i = (unsigned int)(buffer->head + 1) % BUFFER_SIZE;
// if we should be storing the received character into the location
// just before the tail (meaning that the head would advance to the
// current location of the tail), we're about to overflow the buffer
// and so we don't write the character or advance the head.
if (i != buffer->tail) {
buffer->buffer[buffer->head] = c;
buffer->head = i;
}
}
// Constructors ////////////////////////////////////////////////////////////////
CircularBuffer::CircularBuffer(ring_buffer *ibus_buffer)
{
_ibus_buffer = ibus_buffer;
}
// Public Methods //////////////////////////////////////////////////////////////
// clear any received data
//_ibus_buffer->head = _ibus_buffer->tail;
int CircularBuffer::available(void)
{
return (unsigned int)(BUFFER_SIZE + _ibus_buffer->head - _ibus_buffer->tail) % BUFFER_SIZE;
}
int CircularBuffer::peek(void)
{
if (_ibus_buffer->head == _ibus_buffer->tail) {
return -1;
} else {
return _ibus_buffer->buffer[_ibus_buffer->tail];
}
}
int CircularBuffer::read(void)
{
// if the head isn't ahead of the tail, we don't have any characters
if (_ibus_buffer->head == _ibus_buffer->tail) {
return -1;
} else {
unsigned char c = _ibus_buffer->buffer[_ibus_buffer->tail];
_ibus_buffer->tail = (unsigned int)(_ibus_buffer->tail + 1) % BUFFER_SIZE;
return c;
}
}
void CircularBuffer::flush()
{
// UDR is kept full while the buffer is not empty, so TXC triggers when EMPTY && SENT
}
size_t CircularBuffer::write(uint8_t c)
{
int i = (_ibus_buffer->head + 1) % BUFFER_SIZE;
// If the output buffer is full, there's nothing for it other than to
// wait for the interrupt handler to empty it a bit
// ???: return 0 here instead?
while (i == _ibus_buffer->tail)
;
_ibus_buffer->buffer[_ibus_buffer->head] = c;
_ibus_buffer->head = i;
return 1;
}
CircularBuffer::operator bool() {
return true;
}
// Preinstantiate Objects //////////////////////////////////////////////////////
CircularBuffer Buffer(&ibus_buffer);