blob: 19015bbf15583dece28935c3174c56a6723fd6f9 [file] [log] [blame]
Brian Silverman55c62022018-09-03 19:13:44 -07001#ifndef MOTORS_PERIPHERAL_UART_BUFFER_H_
2#define MOTORS_PERIPHERAL_UART_BUFFER_H_
3
4#include <array>
5
6#include "third_party/GSL/include/gsl/gsl"
7
8namespace frc971 {
9namespace teensy {
10
11// Manages a circular buffer of data to send out.
12template<int kSize>
13class UartBuffer {
14 public:
15 // Returns the number of characters added.
Brian Silverman4787a6e2018-10-06 16:00:54 -070016 __attribute__((warn_unused_result)) int PushSpan(gsl::span<const char> data);
Brian Silverman55c62022018-09-03 19:13:44 -070017
Brian Silverman333fc9e2019-02-24 15:09:17 -080018 // max is the maximum size the returned span should be.
Brian Silverman4f958792019-02-16 18:20:04 -080019 // The data in the result is only valid until another method is called.
20 // Note that this may not return all available data when doing so would
21 // require wrapping around, but it will always return a non-empty span if any
22 // data is available.
23 gsl::span<const char> PopSpan(int max);
24
Brian Silverman55c62022018-09-03 19:13:44 -070025 bool empty() const { return size_ == 0; }
Brian Silverman4f958792019-02-16 18:20:04 -080026 bool full() const { return size_ == kSize; }
Brian Silverman55c62022018-09-03 19:13:44 -070027
Brian Silverman333fc9e2019-02-24 15:09:17 -080028 void clear() { size_ = 0; }
29
Brian Silverman55c62022018-09-03 19:13:44 -070030 // This may only be called when !empty().
31 char PopSingle();
Brian Silverman4f958792019-02-16 18:20:04 -080032 // This may only be called when !full().
33 void PushSingle(char c);
Brian Silverman55c62022018-09-03 19:13:44 -070034
35 static constexpr int size() { return kSize; }
36
37 private:
Brian Silverman4f958792019-02-16 18:20:04 -080038 // The index at which we will pop the next character.
Brian Silverman55c62022018-09-03 19:13:44 -070039 int start_ = 0;
40 // How many characters we currently have.
41 int size_ = 0;
42
43 ::std::array<char, kSize> data_;
44};
45
Brian Silverman4f958792019-02-16 18:20:04 -080046template <int kSize>
Brian Silverman4787a6e2018-10-06 16:00:54 -070047int UartBuffer<kSize>::PushSpan(gsl::span<const char> data) {
Brian Silverman55c62022018-09-03 19:13:44 -070048 const int end_location = (start_ + size_) % kSize;
49 const int remaining_end = ::std::min(kSize - size_, kSize - end_location);
50 const int on_end = ::std::min<int>(data.size(), remaining_end);
51 if (on_end > 0) {
52 memcpy(&data_[end_location], data.data(), on_end);
53 }
54 size_ += on_end;
55 const int not_on_end = data.size() - on_end;
56 if (not_on_end == 0) {
57 return data.size();
58 }
59
60 const int remaining_start = ::std::min(kSize - size_, start_);
61 const int on_start = ::std::min(not_on_end, remaining_start);
62 memcpy(data_.data(), &data[on_end], on_start);
63 size_ += on_start;
64 return on_end + on_start;
65}
66
Brian Silverman4f958792019-02-16 18:20:04 -080067template <int kSize>
68gsl::span<const char> UartBuffer<kSize>::PopSpan(int max) {
69 const size_t result_size = std::min(max, std::min(kSize - start_, size_));
70 const auto result = gsl::span<const char>(data_).subspan(start_, result_size);
71 start_ = (start_ + result_size) % kSize;
72 size_ -= result_size;
73 return result;
74}
75
76template <int kSize>
Brian Silverman55c62022018-09-03 19:13:44 -070077char UartBuffer<kSize>::PopSingle() {
78 const char r = data_[start_];
79 --size_;
80 start_ = (start_ + 1) % kSize;
81 return r;
82}
83
Brian Silverman4f958792019-02-16 18:20:04 -080084template <int kSize>
85void UartBuffer<kSize>::PushSingle(char c) {
86 const int end_location = (start_ + size_) % kSize;
87 data_[end_location] = c;
88 ++size_;
89}
90
Brian Silverman55c62022018-09-03 19:13:44 -070091} // namespace teensy
92} // namespace frc971
93
94#endif // MOTORS_PERIPHERAL_UART_BUFFER_H_