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 | |
| 8 | namespace frc971 { |
| 9 | namespace motors { |
| 10 | |
| 11 | // A printing implementation which uses externally-created functions. The stdout |
| 12 | // one is required, while the debug one is optional. |
| 13 | class UsbPrinting final : public PrintingImplementation { |
| 14 | public: |
| 15 | UsbPrinting(teensy::AcmTty *stdout_tty, teensy::AcmTty *debug_tty); |
| 16 | ~UsbPrinting() override; |
| 17 | |
| 18 | void Initialize() override; |
| 19 | |
| 20 | int WriteStdout(gsl::span<const char> buffer) override { |
| 21 | return stdout_tty_->Write(buffer.data(), buffer.size()); |
| 22 | } |
| 23 | |
| 24 | int WriteDebug(gsl::span<const char> buffer) override { |
| 25 | if (debug_tty_ == nullptr) { |
| 26 | return buffer.size(); |
| 27 | } |
| 28 | return debug_tty_->Write(buffer.data(), buffer.size()); |
| 29 | } |
| 30 | |
| 31 | private: |
| 32 | teensy::AcmTty *const stdout_tty_; |
| 33 | teensy::AcmTty *const debug_tty_; |
| 34 | }; |
| 35 | |
| 36 | // A printing implementation which creates its own UsbDevice and functions, and |
| 37 | // manages their lifecycle. |
| 38 | class DedicatedUsbPrinting final : public PrintingImplementation { |
| 39 | public: |
| 40 | DedicatedUsbPrinting(); |
| 41 | ~DedicatedUsbPrinting() override; |
| 42 | |
| 43 | void Initialize() override; |
| 44 | |
| 45 | int WriteStdout(gsl::span<const char> buffer) override { |
| 46 | return stdout_tty_.Write(buffer.data(), buffer.size()); |
| 47 | } |
| 48 | |
| 49 | int WriteDebug(gsl::span<const char> buffer) override { |
| 50 | return debug_tty_.Write(buffer.data(), buffer.size()); |
| 51 | } |
| 52 | |
| 53 | private: |
| 54 | teensy::UsbDevice usb_device_; |
| 55 | teensy::AcmTty stdout_tty_; |
| 56 | teensy::AcmTty debug_tty_; |
| 57 | }; |
| 58 | |
| 59 | } // namespace motors |
| 60 | } // namespace frc971 |
| 61 | |
| 62 | #endif // MOTORS_PRINT_USB_H_ |