blob: 7bffa0deb4195818e853224d73804cb9da7c5513 [file] [log] [blame]
Austin Schuh29b6be02019-03-02 22:12:58 -08001#include <unistd.h>
2
3#include <chrono>
4
Austin Schuh29b6be02019-03-02 22:12:58 -08005#include "aos/time/time.h"
Philipp Schrader790cb542023-07-05 21:06:52 -07006#include "y2019/jevois/serial.h"
Austin Schuh29b6be02019-03-02 22:12:58 -08007
8using ::aos::monotonic_clock;
9using ::y2019::jevois::open_via_terminos;
10
11namespace chrono = ::std::chrono;
12
13int 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}