blob: 5e811b37be167dd669dd752971509788f7e2ac18 [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
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -08008namespace frc971::motors {
Brian Silverman4787a6e2018-10-06 16:00:54 -07009
10// A printing implementation which uses externally-created functions. The stdout
11// one is required, while the debug one is optional.
12class 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 Schuh7fe04492022-01-02 13:37:21 -080019 int WriteStdout(absl::Span<const char> buffer) override {
Brian Silverman4787a6e2018-10-06 16:00:54 -070020 return stdout_tty_->Write(buffer.data(), buffer.size());
21 }
22
Austin Schuh7fe04492022-01-02 13:37:21 -080023 int WriteDebug(absl::Span<const char> buffer) override {
Brian Silverman4787a6e2018-10-06 16:00:54 -070024 if (debug_tty_ == nullptr) {
25 return buffer.size();
26 }
27 return debug_tty_->Write(buffer.data(), buffer.size());
28 }
29
Brian Silverman83693e42019-03-02 15:45:52 -080030 aos::SizedArray<char, 4> ReadStdin() override {
31 aos::SizedArray<char, 4> result;
Tyler Chatowd0a49742022-02-25 22:06:19 -080032 result.resize(stdout_tty_->Read(result.data(), result.capacity()));
Brian Silverman83693e42019-03-02 15:45:52 -080033 return result;
34 }
35
Brian Silverman4787a6e2018-10-06 16:00:54 -070036 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.
43class DedicatedUsbPrinting final : public PrintingImplementation {
44 public:
45 DedicatedUsbPrinting();
46 ~DedicatedUsbPrinting() override;
47
48 void Initialize() override;
49
Austin Schuh7fe04492022-01-02 13:37:21 -080050 int WriteStdout(absl::Span<const char> buffer) override {
Brian Silverman4787a6e2018-10-06 16:00:54 -070051 return stdout_tty_.Write(buffer.data(), buffer.size());
52 }
53
Austin Schuh7fe04492022-01-02 13:37:21 -080054 int WriteDebug(absl::Span<const char> buffer) override {
Brian Silverman4787a6e2018-10-06 16:00:54 -070055 return debug_tty_.Write(buffer.data(), buffer.size());
56 }
57
Brian Silverman83693e42019-03-02 15:45:52 -080058 aos::SizedArray<char, 4> ReadStdin() override {
59 aos::SizedArray<char, 4> result;
Tyler Chatowd0a49742022-02-25 22:06:19 -080060 result.resize(stdout_tty_.Read(result.data(), result.capacity()));
Brian Silverman83693e42019-03-02 15:45:52 -080061 return result;
62 }
63
Brian Silverman4787a6e2018-10-06 16:00:54 -070064 private:
65 teensy::UsbDevice usb_device_;
66 teensy::AcmTty stdout_tty_;
67 teensy::AcmTty debug_tty_;
68};
69
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080070} // namespace frc971::motors
Brian Silverman4787a6e2018-10-06 16:00:54 -070071
72#endif // MOTORS_PRINT_USB_H_