|
| 1 | +#include <iostream> |
| 2 | + |
| 3 | +#include <sys/ioctl.h> |
| 4 | +#include <termios.h> |
| 5 | +#include <unistd.h> |
| 6 | +#include <stdio.h> |
| 7 | +#include "Serial.h" // allChannels |
| 8 | +#include "Driver/Console.h" |
| 9 | + |
| 10 | +#include "Channel.h" |
| 11 | +#include "lineedit.h" |
| 12 | + |
| 13 | +static struct termios _orig_termios; |
| 14 | + |
| 15 | +class PosixConsole : public Channel { |
| 16 | +private: |
| 17 | + Lineedit* _lineedit; |
| 18 | + |
| 19 | +public: |
| 20 | + PosixConsole(bool addCR = false) : Channel("PosixConsole", addCR) {} |
| 21 | + |
| 22 | + static void editModeOn() { |
| 23 | + // stdout changes? |
| 24 | + tcsetattr(STDIN_FILENO, TCSAFLUSH, &_orig_termios); |
| 25 | + } |
| 26 | + |
| 27 | + static void editModeOff() { |
| 28 | + tcgetattr(STDIN_FILENO, &_orig_termios); |
| 29 | + struct termios raw = _orig_termios; |
| 30 | + |
| 31 | + // input modes: no break, no CR to NL, no parity check, no strip char, |
| 32 | + // no start/stop output control. |
| 33 | + raw.c_iflag &= ~(BRKINT | /* ICRNL | */ INPCK | ISTRIP | IXON); |
| 34 | + |
| 35 | + // output modes - disable post processing */ |
| 36 | + // raw.c_oflag &= ~(OPOST); |
| 37 | + raw.c_oflag |= ONLCR; |
| 38 | + |
| 39 | + // control modes - set 8 bit chars */ |
| 40 | + raw.c_cflag |= (CS8); |
| 41 | + |
| 42 | + // local modes - choing off, canonical off, no extended functions, |
| 43 | + // no signal chars (^Z,^C) */ |
| 44 | + raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG); |
| 45 | + raw.c_lflag |= ISIG; |
| 46 | + |
| 47 | + // control chars - set return condition: min number of bytes and timer. |
| 48 | + // We want read to return every single byte, without timeout. */ |
| 49 | + raw.c_cc[VINTR] = 3; /* Ctrl-C interrupts process */ |
| 50 | + // raw.c_cc[VMIN] = 1; |
| 51 | + // raw.c_cc[VTIME] = 0; /* 1 byte, no timer */ |
| 52 | + |
| 53 | + // // The cfmakeraw function is a standard way to achieve raw mode |
| 54 | + // cfmakeraw(&raw); |
| 55 | + |
| 56 | + // // Additional configuration for read() behavior (VMIN and VTIME) |
| 57 | + // // VMIN=1, VTIME=0 means read() returns as soon as 1 character is available, blocking indefinitely until then |
| 58 | + // raw.c_cc[VMIN] = 1; |
| 59 | + // raw.c_cc[VTIME] = 0; |
| 60 | + |
| 61 | + tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw); |
| 62 | + |
| 63 | + atexit(editModeOn); // Ensure terminal mode is restored on exit |
| 64 | + } |
| 65 | + |
| 66 | + void init() override { |
| 67 | + editModeOff(); |
| 68 | + _lineedit = new Lineedit(this, _line, Channel::maxLine - 1); |
| 69 | + allChannels.registration(this); |
| 70 | + }; |
| 71 | + |
| 72 | + // Print methods (Stream inherits from Print) |
| 73 | + size_t write(uint8_t c) override { |
| 74 | + // fflush(STDOUT); |
| 75 | + return ::write(STDOUT_FILENO, &c, 1); |
| 76 | + } |
| 77 | + |
| 78 | + // Stream methods (Channel inherits from Stream) |
| 79 | + |
| 80 | + int available(void) override { |
| 81 | + int n; |
| 82 | + return ioctl(STDIN_FILENO, FIONREAD, &n) ? 0 : n; |
| 83 | + } |
| 84 | + |
| 85 | + int read() override { |
| 86 | + int n; |
| 87 | + if ((n = available()) < 1) { |
| 88 | + return -1; |
| 89 | + } |
| 90 | + |
| 91 | + char c; |
| 92 | + auto ret = ::read(STDIN_FILENO, &c, 1); |
| 93 | + return ret == 1 ? c : -1; |
| 94 | + } |
| 95 | + |
| 96 | + // Channel methods |
| 97 | + int rx_buffer_available() override { return 128 - available(); } |
| 98 | + |
| 99 | + bool realtimeOkay(char c) override { return _lineedit->realtime(c); } |
| 100 | + |
| 101 | + bool lineComplete(char* line, char c) override { |
| 102 | + if (_lineedit->step(c)) { |
| 103 | + _linelen = _lineedit->finish(); |
| 104 | + _line[_linelen] = '\0'; |
| 105 | + strcpy(line, _line); |
| 106 | + _linelen = 0; |
| 107 | + return true; |
| 108 | + } |
| 109 | + return false; |
| 110 | + } |
| 111 | +}; |
| 112 | + |
| 113 | +PosixConsole posixConsole(true); |
| 114 | +Channel& Console = posixConsole; |
0 commit comments