blob: c2224d2cb259982a85da6a0b5de34e75da33c696 [file] [log] [blame]
Brian Silverman4787a6e2018-10-06 16:00:54 -07001#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
8namespace frc971 {
9namespace motors {
10
11// A printing implementation which uses externally-created functions. The stdout
12// one is required, while the debug one is optional.
13class 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
Austin Schuh7fe04492022-01-02 13:37:21 -080020 int WriteStdout(absl::Span<const char> buffer) override {
Brian Silverman4787a6e2018-10-06 16:00:54 -070021 return stdout_tty_->Write(buffer.data(), buffer.size());
22 }
23
Austin Schuh7fe04492022-01-02 13:37:21 -080024 int WriteDebug(absl::Span<const char> buffer) override {
Brian Silverman4787a6e2018-10-06 16:00:54 -070025 if (debug_tty_ == nullptr) {
26 return buffer.size();
27 }
28 return debug_tty_->Write(buffer.data(), buffer.size());
29 }
30
Brian Silverman83693e42019-03-02 15:45:52 -080031 aos::SizedArray<char, 4> ReadStdin() override {
32 aos::SizedArray<char, 4> result;
Tyler Chatowd0a49742022-02-25 22:06:19 -080033 result.resize(stdout_tty_->Read(result.data(), result.capacity()));
Brian Silverman83693e42019-03-02 15:45:52 -080034 return result;
35 }
36
Brian Silverman4787a6e2018-10-06 16:00:54 -070037 private:
38 teensy::AcmTty *const stdout_tty_;
39 teensy::AcmTty *const debug_tty_;
40};
41
42// A printing implementation which creates its own UsbDevice and functions, and
43// manages their lifecycle.
44class DedicatedUsbPrinting final : public PrintingImplementation {
45 public:
46 DedicatedUsbPrinting();
47 ~DedicatedUsbPrinting() override;
48
49 void Initialize() override;
50
Austin Schuh7fe04492022-01-02 13:37:21 -080051 int WriteStdout(absl::Span<const char> buffer) override {
Brian Silverman4787a6e2018-10-06 16:00:54 -070052 return stdout_tty_.Write(buffer.data(), buffer.size());
53 }
54
Austin Schuh7fe04492022-01-02 13:37:21 -080055 int WriteDebug(absl::Span<const char> buffer) override {
Brian Silverman4787a6e2018-10-06 16:00:54 -070056 return debug_tty_.Write(buffer.data(), buffer.size());
57 }
58
Brian Silverman83693e42019-03-02 15:45:52 -080059 aos::SizedArray<char, 4> ReadStdin() override {
60 aos::SizedArray<char, 4> result;
Tyler Chatowd0a49742022-02-25 22:06:19 -080061 result.resize(stdout_tty_.Read(result.data(), result.capacity()));
Brian Silverman83693e42019-03-02 15:45:52 -080062 return result;
63 }
64
Brian Silverman4787a6e2018-10-06 16:00:54 -070065 private:
66 teensy::UsbDevice usb_device_;
67 teensy::AcmTty stdout_tty_;
68 teensy::AcmTty debug_tty_;
69};
70
71} // namespace motors
72} // namespace frc971
73
74#endif // MOTORS_PRINT_USB_H_