blob: 981c5d39b211a0c5e0c245318209a8e223f121a4 [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
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
Brian Silverman83693e42019-03-02 15:45:52 -080031 aos::SizedArray<char, 4> ReadStdin() override {
32 aos::SizedArray<char, 4> result;
33 result.set_size(stdout_tty_->Read(result.data(), result.max_size()));
34 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
51 int WriteStdout(gsl::span<const char> buffer) override {
52 return stdout_tty_.Write(buffer.data(), buffer.size());
53 }
54
55 int WriteDebug(gsl::span<const char> buffer) override {
56 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;
61 result.set_size(stdout_tty_.Read(result.data(), result.max_size()));
62 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_