Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 1 | #ifndef MOTORS_PERIPHERAL_UART_BUFFER_H_ |
| 2 | #define MOTORS_PERIPHERAL_UART_BUFFER_H_ |
| 3 | |
| 4 | #include <array> |
Austin Schuh | 7fe0449 | 2022-01-02 13:37:21 -0800 | [diff] [blame] | 5 | #include <cstring> |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 6 | |
Austin Schuh | 7fe0449 | 2022-01-02 13:37:21 -0800 | [diff] [blame] | 7 | #include "absl/types/span.h" |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 8 | |
| 9 | namespace frc971 { |
| 10 | namespace teensy { |
| 11 | |
| 12 | // Manages a circular buffer of data to send out. |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 13 | template <int kSize> |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 14 | class UartBuffer { |
| 15 | public: |
| 16 | // Returns the number of characters added. |
Austin Schuh | 7fe0449 | 2022-01-02 13:37:21 -0800 | [diff] [blame] | 17 | __attribute__((warn_unused_result)) int PushSpan(absl::Span<const char> data); |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 18 | |
Brian Silverman | 333fc9e | 2019-02-24 15:09:17 -0800 | [diff] [blame] | 19 | // max is the maximum size the returned span should be. |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 20 | // The data in the result is only valid until another method is called. |
| 21 | // Note that this may not return all available data when doing so would |
| 22 | // require wrapping around, but it will always return a non-empty span if any |
| 23 | // data is available. |
Austin Schuh | 7fe0449 | 2022-01-02 13:37:21 -0800 | [diff] [blame] | 24 | absl::Span<const char> PopSpan(int max); |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 25 | |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 26 | bool empty() const { return size_ == 0; } |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 27 | bool full() const { return size_ == kSize; } |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 28 | |
Brian Silverman | 333fc9e | 2019-02-24 15:09:17 -0800 | [diff] [blame] | 29 | void clear() { size_ = 0; } |
| 30 | |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 31 | // This may only be called when !empty(). |
| 32 | char PopSingle(); |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 33 | // This may only be called when !full(). |
| 34 | void PushSingle(char c); |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 35 | |
| 36 | static constexpr int size() { return kSize; } |
| 37 | |
| 38 | private: |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 39 | // The index at which we will pop the next character. |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 40 | int start_ = 0; |
| 41 | // How many characters we currently have. |
| 42 | int size_ = 0; |
| 43 | |
| 44 | ::std::array<char, kSize> data_; |
| 45 | }; |
| 46 | |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 47 | template <int kSize> |
Austin Schuh | 7fe0449 | 2022-01-02 13:37:21 -0800 | [diff] [blame] | 48 | int UartBuffer<kSize>::PushSpan(absl::Span<const char> data) { |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 49 | const int end_location = (start_ + size_) % kSize; |
| 50 | const int remaining_end = ::std::min(kSize - size_, kSize - end_location); |
| 51 | const int on_end = ::std::min<int>(data.size(), remaining_end); |
| 52 | if (on_end > 0) { |
| 53 | memcpy(&data_[end_location], data.data(), on_end); |
| 54 | } |
| 55 | size_ += on_end; |
| 56 | const int not_on_end = data.size() - on_end; |
| 57 | if (not_on_end == 0) { |
| 58 | return data.size(); |
| 59 | } |
| 60 | |
| 61 | const int remaining_start = ::std::min(kSize - size_, start_); |
| 62 | const int on_start = ::std::min(not_on_end, remaining_start); |
| 63 | memcpy(data_.data(), &data[on_end], on_start); |
| 64 | size_ += on_start; |
| 65 | return on_end + on_start; |
| 66 | } |
| 67 | |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 68 | template <int kSize> |
Austin Schuh | 7fe0449 | 2022-01-02 13:37:21 -0800 | [diff] [blame] | 69 | absl::Span<const char> UartBuffer<kSize>::PopSpan(int max) { |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 70 | const size_t result_size = std::min(max, std::min(kSize - start_, size_)); |
Austin Schuh | 7fe0449 | 2022-01-02 13:37:21 -0800 | [diff] [blame] | 71 | const auto result = |
| 72 | absl::Span<const char>(data_).subspan(start_, result_size); |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 73 | start_ = (start_ + result_size) % kSize; |
| 74 | size_ -= result_size; |
| 75 | return result; |
| 76 | } |
| 77 | |
| 78 | template <int kSize> |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 79 | char UartBuffer<kSize>::PopSingle() { |
| 80 | const char r = data_[start_]; |
| 81 | --size_; |
| 82 | start_ = (start_ + 1) % kSize; |
| 83 | return r; |
| 84 | } |
| 85 | |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 86 | template <int kSize> |
| 87 | void UartBuffer<kSize>::PushSingle(char c) { |
| 88 | const int end_location = (start_ + size_) % kSize; |
| 89 | data_[end_location] = c; |
| 90 | ++size_; |
| 91 | } |
| 92 | |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 93 | } // namespace teensy |
| 94 | } // namespace frc971 |
| 95 | |
| 96 | #endif // MOTORS_PERIPHERAL_UART_BUFFER_H_ |