Brian Silverman | 4787a6e | 2018-10-06 16:00:54 -0700 | [diff] [blame] | 1 | #include "motors/print/usb.h" |
| 2 | |
| 3 | #include <atomic> |
| 4 | |
| 5 | #include "motors/core/kinetis.h" |
| 6 | |
| 7 | namespace frc971 { |
| 8 | namespace motors { |
| 9 | namespace { |
| 10 | |
| 11 | ::std::atomic<teensy::AcmTty *> global_stdout{nullptr}; |
| 12 | |
| 13 | } // namespace |
| 14 | |
| 15 | ::std::unique_ptr<PrintingImplementation> CreatePrinting( |
| 16 | const PrintingParameters ¶meters) { |
| 17 | if (parameters.dedicated_usb) { |
| 18 | return ::std::unique_ptr<PrintingImplementation>( |
| 19 | new DedicatedUsbPrinting()); |
| 20 | } |
| 21 | if (parameters.stdout_tty != nullptr) { |
| 22 | return ::std::unique_ptr<PrintingImplementation>( |
| 23 | new UsbPrinting(parameters.stdout_tty, parameters.debug_tty)); |
| 24 | } |
| 25 | return ::std::unique_ptr<PrintingImplementation>(new NopPrinting()); |
| 26 | } |
| 27 | |
| 28 | extern "C" int _write(const int /*file*/, char *const ptr, const int len) { |
| 29 | teensy::AcmTty *const tty = global_stdout.load(::std::memory_order_acquire); |
| 30 | if (tty != nullptr) { |
| 31 | return tty->Write(ptr, len); |
| 32 | } |
| 33 | return len; |
| 34 | } |
| 35 | |
| 36 | UsbPrinting::UsbPrinting(teensy::AcmTty *stdout_tty, teensy::AcmTty *debug_tty) |
| 37 | : stdout_tty_(stdout_tty), debug_tty_(debug_tty) {} |
| 38 | |
| 39 | UsbPrinting::~UsbPrinting() { |
| 40 | global_stdout.store(nullptr, ::std::memory_order_release); |
| 41 | } |
| 42 | |
| 43 | void UsbPrinting::Initialize() { |
| 44 | global_stdout.store(stdout_tty_, ::std::memory_order_release); |
| 45 | } |
| 46 | |
| 47 | DedicatedUsbPrinting::DedicatedUsbPrinting() |
| 48 | : usb_device_{0, 0x16c0, 0x0490}, |
| 49 | stdout_tty_{&usb_device_}, |
| 50 | debug_tty_{&usb_device_} { |
| 51 | usb_device_.SetManufacturer("FRC 971 Spartan Robotics"); |
| 52 | usb_device_.SetProduct("FET12v2"); |
| 53 | NVIC_SET_SANE_PRIORITY(IRQ_USBOTG, 0x7); |
| 54 | } |
| 55 | |
| 56 | DedicatedUsbPrinting::~DedicatedUsbPrinting() { |
| 57 | global_stdout.store(nullptr, ::std::memory_order_release); |
| 58 | } |
| 59 | |
| 60 | void DedicatedUsbPrinting::Initialize() { |
| 61 | usb_device_.Initialize(); |
| 62 | global_stdout.store(&stdout_tty_, ::std::memory_order_release); |
| 63 | } |
| 64 | |
| 65 | } // namespace motors |
| 66 | } // namespace frc971 |