Skip to main content

sesh_debug/
lib.rs

1use colored::*;
2use sesh_ast::*;
3use sesh_runtime::SeshRuntime;
4
5pub struct SeshDebugger {
6    pub runtime: SeshRuntime,
7    pub breakpoints: Vec<usize>, // line numbers
8}
9
10impl Default for SeshDebugger {
11    fn default() -> Self {
12        Self::new()
13    }
14}
15
16impl SeshDebugger {
17    pub fn new() -> Self {
18        Self {
19            runtime: SeshRuntime::new(),
20            breakpoints: Vec::new(),
21        }
22    }
23
24    pub fn inspect_program(&mut self, program: &Program) -> String {
25        let mut out = String::new();
26        out.push_str(&format!(
27            "{}\n",
28            "========================================================================"
29                .bright_magenta()
30                .bold()
31        ));
32        out.push_str(&format!(
33            "{}  --  {}\n",
34            "SESH REAL-TIME EMBEDDED DEBUGGER & HARDWARE INSPECTOR"
35                .bright_yellow()
36                .bold(),
37            "V1.0.0".cyan()
38        ));
39        out.push_str(&format!(
40            "{}\n\n",
41            "========================================================================"
42                .bright_magenta()
43                .bold()
44        ));
45
46        let item_count = program.items.len();
47        let import_count = program.imports.len();
48        out.push_str(&format!(
49            "    Loaded Program AST: {} Imports | {} Top-Level Declarations\n\n",
50            import_count, item_count
51        ));
52
53        // Execute one pass in simulation to inspect states
54        let _ = self.runtime.execute_program(program);
55
56        out.push_str(&format!("{}\n", "[1] ROBOT INSTANCES & PIN ALLOCATIONS".bold()));
57        for item in &program.items {
58            if let Item::Robot(r) = item {
59                out.push_str(&format!(
60                    "    Robot '{}': {} Pins | {} Tasks | {} ROS Handlers\n",
61                    r.name.bright_green(),
62                    r.pins.len(),
63                    r.tasks.len(),
64                    r.topic_handlers.len()
65                ));
66                for pin in &r.pins {
67                    let level_str = if self.runtime.gpio_read(pin.pin_num) {
68                        "HIGH".green()
69                    } else {
70                        "LOW".red()
71                    };
72                    out.push_str(&format!(
73                        "        -> Pin '{}' (GPIO {}) : Mode = {:?} | State = {}\n",
74                        pin.name.cyan(),
75                        pin.pin_num,
76                        pin.mode,
77                        level_str
78                    ));
79                }
80            }
81        }
82        out.push('\n');
83
84        out.push_str(&format!("{}\n", "[2] REAL-TIME TASK DEADLINE & PRIORITY AUDIT".bold()));
85        for item in &program.items {
86            if let Item::Robot(r) = item {
87                for task in &r.tasks {
88                    out.push_str(&format!(
89                        "    Task '{}::{}' : Deadline = {} ms | Priority = {:?}\n",
90                        r.name.cyan(),
91                        task.name.bright_yellow(),
92                        task.deadline_ms,
93                        task.priority
94                    ));
95                }
96            } else if let Item::Task(task) = item {
97                out.push_str(&format!(
98                    "    Global Task '{}' : Deadline = {} ms | Priority = {:?}\n",
99                    task.name.bright_yellow(),
100                    task.deadline_ms,
101                    task.priority
102                ));
103            }
104        }
105        out.push('\n');
106
107        out.push_str(&format!("{}\n", "[3] HARDWARE BUS & ROS 2 TOPIC AUDIT".bold()));
108        if self.runtime.ros_topics.is_empty() {
109            out.push_str("    No ROS topics published during inspection pass.\n");
110        } else {
111            for (topic, msgs) in &self.runtime.ros_topics {
112                out.push_str(&format!(
113                    "    Topic '/{}' : {} published messages\n",
114                    topic.bright_cyan(),
115                    msgs.len()
116                ));
117            }
118        }
119
120        out.push_str(&format!(
121            "\n{}\n",
122            "========================================================================"
123                .bright_magenta()
124                .bold()
125        ));
126        out
127    }
128}