Brian Silverman | 4787a6e | 2018-10-06 16:00:54 -0700 | [diff] [blame] | 1 | #ifndef MOTORS_PRINT_USB_H_ |
| 2 | #define MOTORS_PRINT_USB_H_ |
| 3 | |
| 4 | #include "motors/print/print.h" |
| 5 | #include "motors/usb/cdc.h" |
| 6 | #include "motors/usb/usb.h" |
| 7 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame^] | 8 | namespace frc971::motors { |
Brian Silverman | 4787a6e | 2018-10-06 16:00:54 -0700 | [diff] [blame] | 9 | |
| 10 | // A printing implementation which uses externally-created functions. The stdout |
| 11 | // one is required, while the debug one is optional. |
| 12 | class UsbPrinting final : public PrintingImplementation { |
| 13 | public: |
| 14 | UsbPrinting(teensy::AcmTty *stdout_tty, teensy::AcmTty *debug_tty); |
| 15 | ~UsbPrinting() override; |
| 16 | |
| 17 | void Initialize() override; |
| 18 | |
Austin Schuh | 7fe0449 | 2022-01-02 13:37:21 -0800 | [diff] [blame] | 19 | int WriteStdout(absl::Span<const char> buffer) override { |
Brian Silverman | 4787a6e | 2018-10-06 16:00:54 -0700 | [diff] [blame] | 20 | return stdout_tty_->Write(buffer.data(), buffer.size()); |
| 21 | } |
| 22 | |
Austin Schuh | 7fe0449 | 2022-01-02 13:37:21 -0800 | [diff] [blame] | 23 | int WriteDebug(absl::Span<const char> buffer) override { |
Brian Silverman | 4787a6e | 2018-10-06 16:00:54 -0700 | [diff] [blame] | 24 | if (debug_tty_ == nullptr) { |
| 25 | return buffer.size(); |
| 26 | } |
| 27 | return debug_tty_->Write(buffer.data(), buffer.size()); |
| 28 | } |
| 29 | |
Brian Silverman | 83693e4 | 2019-03-02 15:45:52 -0800 | [diff] [blame] | 30 | aos::SizedArray<char, 4> ReadStdin() override { |
| 31 | aos::SizedArray<char, 4> result; |
Tyler Chatow | d0a4974 | 2022-02-25 22:06:19 -0800 | [diff] [blame] | 32 | result.resize(stdout_tty_->Read(result.data(), result.capacity())); |
Brian Silverman | 83693e4 | 2019-03-02 15:45:52 -0800 | [diff] [blame] | 33 | return result; |
| 34 | } |
| 35 | |
Brian Silverman | 4787a6e | 2018-10-06 16:00:54 -0700 | [diff] [blame] | 36 | private: |
| 37 | teensy::AcmTty *const stdout_tty_; |
| 38 | teensy::AcmTty *const debug_tty_; |
| 39 | }; |
| 40 | |
| 41 | // A printing implementation which creates its own UsbDevice and functions, and |
| 42 | // manages their lifecycle. |
| 43 | class DedicatedUsbPrinting final : public PrintingImplementation { |
| 44 | public: |
| 45 | DedicatedUsbPrinting(); |
| 46 | ~DedicatedUsbPrinting() override; |
| 47 | |
| 48 | void Initialize() override; |
| 49 | |
Austin Schuh | 7fe0449 | 2022-01-02 13:37:21 -0800 | [diff] [blame] | 50 | int WriteStdout(absl::Span<const char> buffer) override { |
Brian Silverman | 4787a6e | 2018-10-06 16:00:54 -0700 | [diff] [blame] | 51 | return stdout_tty_.Write(buffer.data(), buffer.size()); |
| 52 | } |
| 53 | |
Austin Schuh | 7fe0449 | 2022-01-02 13:37:21 -0800 | [diff] [blame] | 54 | int WriteDebug(absl::Span<const char> buffer) override { |
Brian Silverman | 4787a6e | 2018-10-06 16:00:54 -0700 | [diff] [blame] | 55 | return debug_tty_.Write(buffer.data(), buffer.size()); |
| 56 | } |
| 57 | |
Brian Silverman | 83693e4 | 2019-03-02 15:45:52 -0800 | [diff] [blame] | 58 | aos::SizedArray<char, 4> ReadStdin() override { |
| 59 | aos::SizedArray<char, 4> result; |
Tyler Chatow | d0a4974 | 2022-02-25 22:06:19 -0800 | [diff] [blame] | 60 | result.resize(stdout_tty_.Read(result.data(), result.capacity())); |
Brian Silverman | 83693e4 | 2019-03-02 15:45:52 -0800 | [diff] [blame] | 61 | return result; |
| 62 | } |
| 63 | |
Brian Silverman | 4787a6e | 2018-10-06 16:00:54 -0700 | [diff] [blame] | 64 | private: |
| 65 | teensy::UsbDevice usb_device_; |
| 66 | teensy::AcmTty stdout_tty_; |
| 67 | teensy::AcmTty debug_tty_; |
| 68 | }; |
| 69 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame^] | 70 | } // namespace frc971::motors |
Brian Silverman | 4787a6e | 2018-10-06 16:00:54 -0700 | [diff] [blame] | 71 | |
| 72 | #endif // MOTORS_PRINT_USB_H_ |