Skip to content

Commit

Permalink
C++ guidelines.
Browse files Browse the repository at this point in the history
- Removed casts.
- Removed macros.
- Const correctness.
- Using new/delete instead of realloc/free.
- Variable naming.
- Interface documentation.
  • Loading branch information
jfjlaros committed Oct 29, 2022
1 parent 2fb6f03 commit a3cf8bd
Show file tree
Hide file tree
Showing 9 changed files with 312 additions and 302 deletions.
5 changes: 4 additions & 1 deletion docs/api/serialmux.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@ Serial multiplexer
Class definition
----------------

.. doxygenclass:: SerialMux
.. doxygenclass:: SerialMux_
:members:

.. doxygentypedef:: SerialMux
:members:
140 changes: 70 additions & 70 deletions src/buffer.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -2,113 +2,113 @@

#include <Arduino.h>

/*!
* Ring buffer.
/*! Ring buffer.
*
* \tparam bits Buffer size control (2^`bits`).
*/
template <uint8_t bits>
class Buffer {
public:
Buffer();

uint8_t available();
uint8_t read(uint8_t*, uint8_t);
int16_t read();
void write(uint8_t*, uint8_t);
void write(uint8_t);
int16_t peek();

private:
uint8_t _buffer[1ul << bits] = {};
uint8_t _start: bits;
uint8_t _end: bits;
uint8_t _mask = (1ul << bits) - 1;
public:
/*! Create a ring buffer. */
Buffer();


/*! Get the number of bytes available for reading.
*
* \return Number of bytes.
*/
uint8_t available() const;

/*! Read `size` bytes of data.
*
* \param[out] data Buffer to receive `size` bytes of data.
* \param[in] size Number of bytes to read.
*
* \return Number of bytes read.
*/
uint8_t read(uint8_t* const, uint8_t const);

/*! Read one byte of data.
*
* \return The first byte of incoming data or `-1` if no data is available.
*/
int16_t read();

/*! Write `size` bytes of data.
*
* \param[in] data Buffer containing `size` bytes of data.
* \param[in] size Number of bytes to write.
*
* \return Number of bytes written.
*/
void write(uint8_t* const, uint8_t const);

/*! Write one byte of data.
*
* \param[in] data Data.
*/
void write(uint8_t const);

/*! Return the next byte of incoming data without removing it from the
* buffer.
*
* \return The first byte of incoming data or `-1` if no data is available.
*/
int16_t peek() const;

private:
uint8_t buffer_[1ul << bits]{};
uint8_t start_: bits;
uint8_t end_: bits;
uint8_t mask_ = (1ul << bits) - 1;
};


/*!
* Constructor.
*/
template <uint8_t bits>
Buffer<bits>::Buffer() {
_start = 0;
_end = 0;
start_ = 0;
end_ = 0;
}


/*!
* Get the number of bytes available for reading.
*
* \return Number of bytes.
*/
template <uint8_t bits>
uint8_t Buffer<bits>::available() {
return (_end - _start) & _mask;
uint8_t Buffer<bits>::available() const {
return (end_ - start_) & mask_;
}

/*!
* Read `size` bytes of data.
*
* \param data Buffer to receive `size` bytes of data.
* \param size Number of bytes to read.
*
* \return Number of bytes read.
*/
template <uint8_t bits>
uint8_t Buffer<bits>::read(uint8_t* data, uint8_t size) {
uint8_t Buffer<bits>::read(uint8_t* const data, uint8_t const size) {
uint8_t size_ = min(size, available());
for (uint8_t i = 0; i < size_; i++) {
data[i] = _buffer[_start++];
data[i] = buffer_[start_++];
}
return size_;
}

/*!
* Read one byte of data.
*
* \return The first byte of incoming data or `-1` if no data is available.
*/
template <uint8_t bits>
int16_t Buffer<bits>::read() {
if (_start != _end) {
return _buffer[_start++];
if (start_ != end_) {
return buffer_[start_++];
}
return -1;
}

/*!
* Write `size` bytes of data.
*
* \param data Buffer containing `size` bytes of data.
* \param size Number of bytes to write.
*
* \return Number of bytes written.
*/
template <uint8_t bits>
void Buffer<bits>::write(uint8_t* data, uint8_t size) {
void Buffer<bits>::write(uint8_t* const data, uint8_t size) {
for (uint8_t i = 0; i < size; i++) {
_buffer[_end++] = data[i];
buffer_[end_++] = data[i];
}
}

/*!
* Write one byte of data.
*
* \param data Data.
*/
template <uint8_t bits>
void Buffer<bits>::write(uint8_t data) {
_buffer[_end++] = data;
buffer_[end_++] = data;
}

/*!
* Return the next byte of incoming data without removing it from the buffer.
*
* \return The first byte of incoming data or `-1` if no data is available.
*/
template <uint8_t bits>
int16_t Buffer<bits>::peek() {
if (_start != _end) {
return _buffer[_start];
int16_t Buffer<bits>::peek() const {
if (start_ != end_) {
return buffer_[start_];
}
return -1;
}
3 changes: 0 additions & 3 deletions src/serialMux.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#pragma once

#include "vSerial.tcc"

#define SerialMux SerialMux<>
#define VSerial VSerial<>

0 comments on commit a3cf8bd

Please sign in to comment.