3 * Copyright (c) 2020 Project CHIP Authors
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 #include "console/console.h"
21 #include "pw_sys_io/sys_io.h"
26 #include <usb/usb_device.h>
29 extern "C" void pw_sys_io_Init()
34 err = usb_enable(nullptr);
42 namespace pw::sys_io {
44 Status ReadByte(std::byte * dest)
47 return Status::InvalidArgument();
49 const int c = console_getchar();
50 *dest = static_cast<std::byte>(c);
52 return c < 0 ? Status::FailedPrecondition() : OkStatus();
55 Status WriteByte(std::byte b)
57 return console_putchar(static_cast<char>(b)) < 0 ? Status::FailedPrecondition() : OkStatus();
60 // Writes a string using pw::sys_io, and add newline characters at the end.
61 StatusWithSize WriteLine(const std::string_view & s)
63 size_t chars_written = 0;
64 StatusWithSize result = WriteBytes(std::as_bytes(std::span(s)));
69 chars_written += result.size();
71 // Write trailing newline.
72 result = WriteBytes(std::as_bytes(std::span("\r\n", 2)));
73 chars_written += result.size();
75 return StatusWithSize(result.status(), chars_written);
78 } // namespace pw::sys_io