Austin Schuh | 29b6be0 | 2019-03-02 22:12:58 -0800 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | |
| 3 | #include <chrono> |
| 4 | |
Austin Schuh | 29b6be0 | 2019-03-02 22:12:58 -0800 | [diff] [blame] | 5 | #include "aos/time/time.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame^] | 6 | #include "y2019/jevois/serial.h" |
Austin Schuh | 29b6be0 | 2019-03-02 22:12:58 -0800 | [diff] [blame] | 7 | |
| 8 | using ::aos::monotonic_clock; |
| 9 | using ::y2019::jevois::open_via_terminos; |
| 10 | |
| 11 | namespace chrono = ::std::chrono; |
| 12 | |
| 13 | int main(int /*argc*/, char ** /*argv*/) { |
| 14 | int serial_fd = open_via_terminos("/dev/ttyS0"); |
| 15 | |
| 16 | // Print out a startup message. The Teensy will ignore it as a corrupt |
| 17 | // packet, but it gives us some warm fuzzies that things are booting right. |
| 18 | (void)write( |
| 19 | serial_fd, |
| 20 | "Starting target_sender in 3 seconds. Press 10 a's to interrupt.\r\n", |
| 21 | 66); |
| 22 | |
| 23 | // Give the user 3 seconds to press 10 a's. If they don't do it in time, |
| 24 | // return 0 to signal that we should boot, or 1 that we are asked not to boot. |
| 25 | constexpr int kACount = 10; |
| 26 | int a_count = 0; |
| 27 | const monotonic_clock::time_point end_time = |
| 28 | monotonic_clock::now() + chrono::seconds(3); |
| 29 | do { |
| 30 | constexpr size_t kBufferSize = 16; |
| 31 | char data[kBufferSize]; |
| 32 | ssize_t n = ::read(serial_fd, &data[0], kBufferSize); |
| 33 | for (int i = 0; i < n; ++i) { |
| 34 | if (data[i] == 'a') { |
| 35 | ++a_count; |
| 36 | if (a_count > kACount) { |
| 37 | ::close(serial_fd); |
| 38 | return 1; |
| 39 | } |
| 40 | } else { |
| 41 | a_count = 0; |
| 42 | } |
| 43 | } |
| 44 | } while (monotonic_clock::now() < end_time); |
| 45 | |
| 46 | ::close(serial_fd); |
| 47 | return 0; |
| 48 | } |