Skip to content

Commit

Permalink
Implement Read_line method in Terminal_type and refactor Read_input t…
Browse files Browse the repository at this point in the history
…o use String for improved functionality
  • Loading branch information
AlixANNERAUD committed Dec 26, 2024
1 parent 8f0c736 commit fc4c208
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 9 deletions.
6 changes: 5 additions & 1 deletion Modules/Executables/Terminal/src/Device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ use File_system::Device_trait;
use crate::Terminal::Terminal_type;

impl Device_trait for Terminal_type {
fn Read(&self, Buffer: &mut [u8]) -> File_system::Result_type<File_system::Size_type> {
fn Read(&self, _: &mut [u8]) -> File_system::Result_type<File_system::Size_type> {
Err(File_system::Error_type::Unsupported_operation)
}

fn Read_line(&self, Buffer: &mut String) -> File_system::Result_type<File_system::Size_type> {
let Size = self
.Read_input(Buffer)
.map_err(|_| File_system::Error_type::Internal_error)?;
Expand Down
59 changes: 51 additions & 8 deletions Modules/Executables/Terminal/src/Terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ unsafe impl Send for Terminal_type {}
unsafe impl Sync for Terminal_type {}

impl Terminal_type {
const Clear: &'static str = "\x1B[2J";
const Home: &'static str = "\x1B[H";

pub fn New() -> Result_type<Self> {
let _Lock = Graphics::Get_instance().Lock()?;

Expand All @@ -35,6 +38,7 @@ impl Terminal_type {

LVGL::lv_obj_set_width(Container, LVGL::lv_pct(100));
LVGL::lv_obj_set_flex_grow(Container, 1);
LVGL::lv_obj_set_scroll_snap_y(Container, LVGL::lv_scroll_snap_t_LV_SCROLL_SNAP_END);

Container
};
Expand Down Expand Up @@ -99,19 +103,61 @@ impl Terminal_type {
Inner.Buffer.remove(Last_index);
}

Inner.Buffer += Text;
let Start_index = if let Some(Last_clear) = Text.rfind(Self::Clear) {
Inner.Buffer.clear();
Last_clear + Self::Clear.len()
} else {
0
};

let Start_index = if let Some(Last_home) = Text.rfind(Self::Home) {
Inner.Buffer.clear();
Last_home + Self::Home.len()
} else {
Start_index
};

Inner.Buffer += &Text[Start_index..];
Inner.Buffer += "\0";

let _Lock = Graphics::Get_instance().Lock().unwrap();

unsafe {
LVGL::lv_label_set_text(Inner.Display, Inner.Buffer.as_ptr() as *const i8);
LVGL::lv_obj_scroll_to_view(Inner.Display, LVGL::lv_anim_enable_t_LV_ANIM_OFF);
}

Ok(())
}

fn Print_line_internal(Inner: &mut Inner_type, Text: &str) -> Result_type<()> {
if !Inner.Buffer.is_empty() {
let Last_index = Inner.Buffer.len() - 1;

Inner.Buffer.remove(Last_index);
}

let Start_index = if let Some(Last_clear) = Text.rfind("\x1B[2J") {
Inner.Buffer.clear();
Last_clear + 4
} else {
0
};

Inner.Buffer += Text[Start_index..].trim();
Inner.Buffer += "\n\0";

let _Lock = Graphics::Get_instance().Lock().unwrap();

unsafe {
LVGL::lv_label_set_text(Inner.Display, Inner.Buffer.as_ptr() as *const i8);
LVGL::lv_obj_scroll_to_view(Inner.Display, LVGL::lv_anim_enable_t_LV_ANIM_OFF);
}

Ok(())
}

pub fn Read_input(&self, String: &mut [u8]) -> Result_type<usize> {
pub fn Read_input(&self, String: &mut String) -> Result_type<usize> {
let mut Inner = self.0.write()?;

if !Inner.Validated {
Expand All @@ -121,23 +167,20 @@ impl Terminal_type {
let _Lock = Graphics::Get_instance().Lock()?;

let Text = unsafe {
LVGL::lv_textarea_add_char(Inner.Input, '\n' as u32);
let Text = LVGL::lv_textarea_get_text(Inner.Input);

CStr::from_ptr(Text).to_str()?
};

let Length = Text.len().min(String.len());

String[..Length].copy_from_slice(&Text.as_bytes()[..Length]);
String.push_str(Text);

unsafe {
LVGL::lv_textarea_set_text(Inner.Input, c"".as_ptr());
}

Inner.Validated = false;

Ok(Length)
Ok(Text.len())
}

pub fn Event_handler(&self) -> Result_type<bool> {
Expand All @@ -163,7 +206,7 @@ impl Terminal_type {

drop(_Lock);

Self::Print_internal(&mut Inner, Text)?;
Self::Print_line_internal(&mut Inner, Text)?;

Inner.Validated = true;
}
Expand Down

0 comments on commit fc4c208

Please sign in to comment.