|
| 1 | +use crate::line_endings::normalized; |
| 2 | +use crossterm::event::{poll, read, Event, KeyCode, KeyEvent, KeyModifiers}; |
| 3 | +use crossterm::terminal::{disable_raw_mode, enable_raw_mode}; |
| 4 | +use miette::{IntoDiagnostic, Result}; |
| 5 | +use serial::{SerialPort, SystemPort}; |
| 6 | +use std::io::{stdout, ErrorKind, Read, Write}; |
| 7 | +use std::thread::sleep; |
| 8 | +use std::time::Duration; |
| 9 | + |
| 10 | +/// Converts key events from crossterm into appropriate character/escape sequences which are then |
| 11 | +/// sent over the serial connection. |
| 12 | +/// |
| 13 | +/// Adapted from https://github.com/dhylands/serial-monitor |
| 14 | +fn handle_key_event(key_event: KeyEvent) -> Option<Vec<u8>> { |
| 15 | + // The following escape sequences come from the MicroPython codebase. |
| 16 | + // |
| 17 | + // Up ESC [A |
| 18 | + // Down ESC [B |
| 19 | + // Right ESC [C |
| 20 | + // Left ESC [D |
| 21 | + // Home ESC [H or ESC [1~ |
| 22 | + // End ESC [F or ESC [4~ |
| 23 | + // Del ESC [3~ |
| 24 | + // Insert ESC [2~ |
| 25 | + |
| 26 | + let mut buf = [0; 4]; |
| 27 | + |
| 28 | + let key_str: Option<&[u8]> = match key_event.code { |
| 29 | + KeyCode::Backspace => Some(b"\x08"), |
| 30 | + KeyCode::Enter => Some(b"\r"), |
| 31 | + KeyCode::Left => Some(b"\x1b[D"), |
| 32 | + KeyCode::Right => Some(b"\x1b[C"), |
| 33 | + KeyCode::Home => Some(b"\x1b[H"), |
| 34 | + KeyCode::End => Some(b"\x1b[F"), |
| 35 | + KeyCode::Up => Some(b"\x1b[A"), |
| 36 | + KeyCode::Down => Some(b"\x1b[B"), |
| 37 | + KeyCode::Tab => Some(b"\x09"), |
| 38 | + KeyCode::Delete => Some(b"\x1b[3~"), |
| 39 | + KeyCode::Insert => Some(b"\x1b[2~"), |
| 40 | + KeyCode::Esc => Some(b"\x1b"), |
| 41 | + KeyCode::Char(ch) => { |
| 42 | + if key_event.modifiers & KeyModifiers::CONTROL == KeyModifiers::CONTROL { |
| 43 | + buf[0] = ch as u8; |
| 44 | + if ('a'..='z').contains(&ch) || (ch == ' ') { |
| 45 | + buf[0] &= 0x1f; |
| 46 | + Some(&buf[0..1]) |
| 47 | + } else if ('4'..='7').contains(&ch) { |
| 48 | + // crossterm returns Control-4 thru 7 for \x1c thru \x1f |
| 49 | + buf[0] = (buf[0] + 8) & 0x1f; |
| 50 | + Some(&buf[0..1]) |
| 51 | + } else { |
| 52 | + Some(ch.encode_utf8(&mut buf).as_bytes()) |
| 53 | + } |
| 54 | + } else { |
| 55 | + Some(ch.encode_utf8(&mut buf).as_bytes()) |
| 56 | + } |
| 57 | + } |
| 58 | + _ => None, |
| 59 | + }; |
| 60 | + key_str.map(|slice| slice.into()) |
| 61 | +} |
| 62 | + |
| 63 | +struct RawModeGuard; |
| 64 | + |
| 65 | +impl RawModeGuard { |
| 66 | + pub fn new() -> Result<Self> { |
| 67 | + enable_raw_mode().into_diagnostic()?; |
| 68 | + Ok(RawModeGuard) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +impl Drop for RawModeGuard { |
| 73 | + fn drop(&mut self) { |
| 74 | + if let Err(e) = disable_raw_mode() { |
| 75 | + eprintln!("{:#}", e) |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +pub fn monitor(mut serial: SystemPort) -> serial::Result<()> { |
| 81 | + println!("Commands:"); |
| 82 | + println!(" CTRL+R Reset chip"); |
| 83 | + println!(" CTRL+C Exit"); |
| 84 | + println!(); |
| 85 | + |
| 86 | + let mut buff = [0; 128]; |
| 87 | + serial.set_timeout(Duration::from_millis(5))?; |
| 88 | + |
| 89 | + let _raw_mode = RawModeGuard::new(); |
| 90 | + let stdout = stdout(); |
| 91 | + let mut stdout = stdout.lock(); |
| 92 | + loop { |
| 93 | + let read_count = match serial.read(&mut buff) { |
| 94 | + Ok(count) => Ok(count), |
| 95 | + Err(e) if e.kind() == ErrorKind::TimedOut => Ok(0), |
| 96 | + err => err, |
| 97 | + }?; |
| 98 | + if read_count > 0 { |
| 99 | + let data: Vec<u8> = normalized(buff[0..read_count].iter().copied()).collect(); |
| 100 | + stdout.write_all(&data).ok(); |
| 101 | + stdout.flush()?; |
| 102 | + } |
| 103 | + if poll(Duration::from_secs(0))? { |
| 104 | + if let Event::Key(key) = read()? { |
| 105 | + if key.modifiers.contains(KeyModifiers::CONTROL) { |
| 106 | + match key.code { |
| 107 | + KeyCode::Char('c') => break, |
| 108 | + KeyCode::Char('r') => { |
| 109 | + serial.set_dtr(false)?; |
| 110 | + serial.set_rts(true)?; |
| 111 | + |
| 112 | + sleep(Duration::from_millis(100)); |
| 113 | + |
| 114 | + serial.set_rts(false)?; |
| 115 | + continue; |
| 116 | + } |
| 117 | + _ => {} |
| 118 | + } |
| 119 | + } |
| 120 | + if let Some(bytes) = handle_key_event(key) { |
| 121 | + serial.write_all(&bytes)?; |
| 122 | + serial.flush()?; |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + Ok(()) |
| 128 | +} |
0 commit comments