diff --git a/resources/arduino_files/power_board/CharBuffer.h b/resources/arduino_files/power_board/CharBuffer.h new file mode 120000 index 000000000..4e1fd92c4 --- /dev/null +++ b/resources/arduino_files/power_board/CharBuffer.h @@ -0,0 +1 @@ +../shared/CharBuffer.h \ No newline at end of file diff --git a/resources/arduino_files/power_board/power_board.ino b/resources/arduino_files/power_board/power_board.ino index eb545ff0a..b7f103bd2 100644 --- a/resources/arduino_files/power_board/power_board.ino +++ b/resources/arduino_files/power_board/power_board.ino @@ -3,6 +3,7 @@ #include "OneWire.h" #include "DallasTemperature.h" #include "DHT.h" +#include "CharBuffer.h" #define DHTTYPE DHT22 // DHT 22 (AM2302) @@ -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 -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: @@ -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]; diff --git a/resources/arduino_files/shared/CharBuffer.h b/resources/arduino_files/shared/CharBuffer.h new file mode 100644 index 000000000..132751327 --- /dev/null +++ b/resources/arduino_files/shared/CharBuffer.h @@ -0,0 +1,63 @@ +// CharBuffer stores characters and supports (minimal) parsing of +// the buffered characters. +template +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(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_; +}; diff --git a/resources/arduino_files/telemetry_board/CharBuffer.h b/resources/arduino_files/telemetry_board/CharBuffer.h new file mode 120000 index 000000000..4e1fd92c4 --- /dev/null +++ b/resources/arduino_files/telemetry_board/CharBuffer.h @@ -0,0 +1 @@ +../shared/CharBuffer.h \ No newline at end of file diff --git a/resources/arduino_files/telemetry_board/telemetry_board.ino b/resources/arduino_files/telemetry_board/telemetry_board.ino index 6853bb287..e45ea0a61 100644 --- a/resources/arduino_files/telemetry_board/telemetry_board.ino +++ b/resources/arduino_files/telemetry_board/telemetry_board.ino @@ -27,6 +27,7 @@ #include "OneWire.h" #include "DallasTemperature.h" #include "DHT.h" +#include "CharBuffer.h" //////////////////////////////////////////////// // __ __ _ // @@ -392,61 +393,6 @@ void Report(unsigned long now) { ////////////////////////////////////////////////////////////////////////////////// // Serial input support -// CharBuffer stores characters and supports (minimal) parsing of -// the buffered characters. -template -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: @@ -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: