| 1 | //! UART #1: what the 8051 transmits, and a line to type into its receiver. |
| 2 | |
| 3 | use bench51_core::sfr::SCON; |
| 4 | use eframe::egui::{self, Align, FontId, Key, Layout, RichText, Ui}; |
| 5 | |
| 6 | use crate::state::State; |
| 7 | use crate::theme::palette; |
| 8 | use crate::widgets::{self, PillKind}; |
| 9 | |
| 10 | pub fn show(ui: &mut Ui, st: &mut State) { |
| 11 | let p = palette(ui.ctx()); |
| 12 | let Some(doc) = st.docs.get_mut(st.active) else { |
| 13 | widgets::empty_state(ui, "No program", "Open or create a source file."); |
| 14 | return; |
| 15 | }; |
| 16 | let s = &mut doc.session; |
| 17 | let scon = s.sim.sfr(SCON); |
| 18 | ui.horizontal(|ui| { |
| 19 | ui.label(p.label(format!("Mode {}", scon >> 6))); |
| 20 | ui.add_space(8.0); |
| 21 | ui.spacing_mut().item_spacing.x = 4.0; |
| 22 | for (name, mask) in [("REN", 0x10), ("TI", 0x02), ("RI", 0x01)] { |
| 23 | super::sfr_chip(ui, s, name, SCON, mask); |
| 24 | } |
| 25 | ui.add_space(8.0); |
| 26 | ui.label( |
| 27 | p.label(format!("{} queued", s.sim.uart_rx_pending().len())) |
| 28 | .color(p.text_disabled), |
| 29 | ); |
| 30 | ui.with_layout(Layout::right_to_left(Align::Center), |ui| { |
| 31 | if widgets::pill(ui, "Clear", PillKind::Ghost).clicked() { |
| 32 | s.sim.uart.tx_log.clear(); |
| 33 | } |
| 34 | }); |
| 35 | }); |
| 36 | ui.add_space(6.0); |
| 37 | |
| 38 | let mut text = String::new(); |
| 39 | for &b in &s.sim.uart.tx_log { |
| 40 | match b { |
| 41 | b'\n' => text.push('\n'), |
| 42 | b'\r' => {} |
| 43 | 0x20..=0x7E => text.push(b as char), |
| 44 | _ => text.push('·'), |
| 45 | } |
| 46 | } |
| 47 | let input_h = 34.0; |
| 48 | egui::ScrollArea::vertical() |
| 49 | .stick_to_bottom(true) |
| 50 | .auto_shrink([false; 2]) |
| 51 | .max_height((ui.available_height() - input_h).max(40.0)) |
| 52 | .show(ui, |ui| { |
| 53 | if text.is_empty() { |
| 54 | ui.label(p.label("[no output yet]").color(p.text_disabled)); |
| 55 | } else { |
| 56 | ui.label( |
| 57 | RichText::new(text) |
| 58 | .font(FontId::monospace(13.0)) |
| 59 | .color(p.text_primary), |
| 60 | ); |
| 61 | } |
| 62 | }); |
| 63 | ui.add_space(6.0); |
| 64 | ui.horizontal(|ui| { |
| 65 | ui.label( |
| 66 | RichText::new(">") |
| 67 | .font(FontId::monospace(13.0)) |
| 68 | .color(p.text_secondary), |
| 69 | ); |
| 70 | let w = ui.available_width(); |
| 71 | let r = widgets::underline_input( |
| 72 | ui, |
| 73 | &mut st.serial_input, |
| 74 | "type, then Enter sends it with CR", |
| 75 | w, |
| 76 | ); |
| 77 | if r.lost_focus() && ui.input(|i| i.key_pressed(Key::Enter)) { |
| 78 | let mut bytes = std::mem::take(&mut st.serial_input).into_bytes(); |
| 79 | bytes.push(b'\r'); |
| 80 | if let Some(doc) = st.docs.get_mut(st.active) { |
| 81 | doc.session.sim.uart_send(&bytes); |
| 82 | } |
| 83 | r.request_focus(); |
| 84 | } |
| 85 | }); |
| 86 | } |