Skip to content

Commit

Permalink
Move CharBuffer class into its own file so it can be shared by (#300)
Browse files Browse the repository at this point in the history
power_board and telemetry_board sketches.
  • Loading branch information
jamessynge authored Jan 5, 2018
1 parent 9628289 commit 18c469b
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 116 deletions.
1 change: 1 addition & 0 deletions resources/arduino_files/power_board/CharBuffer.h
62 changes: 4 additions & 58 deletions resources/arduino_files/power_board/power_board.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "OneWire.h"
#include "DallasTemperature.h"
#include "DHT.h"
#include "CharBuffer.h"

#define DHTTYPE DHT22 // DHT 22 (AM2302)

Expand Down Expand Up @@ -84,61 +85,6 @@ void setup() {
digitalWrite(DSEL_1, LOW); // DSEL_1 LOW reads PROFET 1_0. DSEL_1 HIGH reades PROFET 1_1
}

// CharBuffer stores characters and supports (minimal) parsing of
// the buffered characters.
template <uint8_t kBufferSize>
class CharBuffer {
public:
CharBuffer() {
Reset();
}
void Reset() {
write_cursor_ = read_cursor_ = 0;
}
bool Append(char c) {
if (write_cursor_ < kBufferSize) {
buf_[write_cursor_++] = c;
return true;
}
return false;
}
bool Empty() {
return read_cursor_ >= write_cursor_;
}
char Next() {
return buf_[read_cursor_++];
}
char Peek() {
return buf_[read_cursor_];
}
bool ParseInt(int* output) {
int& v = *output;
v = 0;
size_t len = 0;
while (!Empty() && isdigit(Peek())) {
char c = Next();
v = v * 10 + c - '0';
++len;
if (len > 5) {
return false;
}
}
return len > 0;
}
bool MatchAndConsume(char c) {
if (Empty() || Peek() != c) {
return false;
}
Next();
return true;
}

private:
char buf_[kBufferSize];
uint8_t write_cursor_;
uint8_t read_cursor_;
};

// Accumulates a line, parses it and takes the requested action if it is valid.
class SerialInputHandler {
public:
Expand Down Expand Up @@ -172,10 +118,10 @@ class SerialInputHandler {
}

void ProcessInputBuffer() {
int relay_index, new_state;
if (input_buffer_.ParseInt(&relay_index) &&
uint8_t relay_index, new_state;
if (input_buffer_.ParseUInt8(&relay_index) &&
input_buffer_.MatchAndConsume(',') &&
input_buffer_.ParseInt(&new_state) &&
input_buffer_.ParseUInt8(&new_state) &&
input_buffer_.Empty()) {

int pin_num = relayArray[relay_index];
Expand Down
63 changes: 63 additions & 0 deletions resources/arduino_files/shared/CharBuffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// CharBuffer stores characters and supports (minimal) parsing of
// the buffered characters.
template <uint8_t kBufferSize>
class CharBuffer {
public:
CharBuffer() {
Reset();
}
void Reset() {
write_cursor_ = read_cursor_ = 0;
}
// Appends a character to the buffer if there is room.
// Returns true if there is room, else returns false.
bool Append(char c) {
if (write_cursor_ < kBufferSize) {
buf_[write_cursor_++] = c;
return true;
}
return false;
}
bool Empty() {
return read_cursor_ >= write_cursor_;
}
char Next() {
return buf_[read_cursor_++];
}
char Peek() {
return buf_[read_cursor_];
}
// Parses the integer (uint8_t) in buffer starting at read_cursor_.
// The integer must be non-negative (leading + or - are not supported).
// Returns true if successful, false if there is not an integer at
// read_cursor_ or if the integer is too big to fit into *output.
bool ParseUInt8(uint8_t* output) {
uint16_t v = 0;
uint8_t len = 0;
while (!Empty() && isdigit(Peek())) {
char c = Next();
v = v * 10 + c - '0';
++len;
if (len > 3) {
return false;
}
}
if (len == 0 || v > 255) {
return false;
}
*output = static_cast<uint8_t>(v);
return true;
}
bool MatchAndConsume(char c) {
if (Empty() || Peek() != c) {
return false;
}
Next();
return true;
}

private:
char buf_[kBufferSize];
uint8_t write_cursor_;
uint8_t read_cursor_;
};
1 change: 1 addition & 0 deletions resources/arduino_files/telemetry_board/CharBuffer.h
62 changes: 4 additions & 58 deletions resources/arduino_files/telemetry_board/telemetry_board.ino
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "OneWire.h"
#include "DallasTemperature.h"
#include "DHT.h"
#include "CharBuffer.h"

////////////////////////////////////////////////
// __ __ _ //
Expand Down Expand Up @@ -392,61 +393,6 @@ void Report(unsigned long now) {
//////////////////////////////////////////////////////////////////////////////////
// Serial input support

// CharBuffer stores characters and supports (minimal) parsing of
// the buffered characters.
template <uint8_t kBufferSize>
class CharBuffer {
public:
CharBuffer() {
Reset();
}
void Reset() {
write_cursor_ = read_cursor_ = 0;
}
bool Append(char c) {
if (write_cursor_ < kBufferSize) {
buf_[write_cursor_++] = c;
return true;
}
return false;
}
bool Empty() {
return read_cursor_ >= write_cursor_;
}
char Next() {
return buf_[read_cursor_++];
}
char Peek() {
return buf_[read_cursor_];
}
bool ParseInt(int* output) {
int& v = *output;
v = 0;
size_t len = 0;
while (!Empty() && isdigit(Peek())) {
char c = Next();
v = v * 10 + c - '0';
++len;
if (len > 5) {
return false;
}
}
return len > 0;
}
bool MatchAndConsume(char c) {
if (Empty() || Peek() != c) {
return false;
}
Next();
return true;
}

private:
char buf_[kBufferSize];
uint8_t write_cursor_;
uint8_t read_cursor_;
};

// Accumulates a line, parses it and takes the requested action if it is valid.
class SerialInputHandler {
public:
Expand Down Expand Up @@ -480,10 +426,10 @@ class SerialInputHandler {
}

void ProcessInputBuffer() {
int pin_num, pin_status;
if (input_buffer_.ParseInt(&pin_num) &&
uint8_t pin_num, pin_status;
if (input_buffer_.ParseUInt8(&pin_num) &&
input_buffer_.MatchAndConsume(',') &&
input_buffer_.ParseInt(&pin_status) &&
input_buffer_.ParseUInt8(&pin_status) &&
input_buffer_.Empty()) {
switch (pin_num) {
case COMP_RELAY:
Expand Down

0 comments on commit 18c469b

Please sign in to comment.