-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move CharBuffer class into its own file so it can be shared by (#300)
power_board and telemetry_board sketches.
- Loading branch information
1 parent
9628289
commit 18c469b
Showing
5 changed files
with
73 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../shared/CharBuffer.h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../shared/CharBuffer.h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters