diff --git a/esp32/src/main.rs b/esp32/src/main.rs index 71e3588..3f1740e 100644 --- a/esp32/src/main.rs +++ b/esp32/src/main.rs @@ -31,12 +31,15 @@ mod screen; pub use input::ANGLE; +use crate::screen::overwrite_lcd; + const WIFI_NETWORK: &str = "flamme"; const WIFI_PASSWORD: &str = "12345678"; const TARGET_IP: &str = "84.238.32.253"; const TARGET_PORT: u16 = 7070; const WHEEL_PRECISION: i32 = 32; const WHEEL_INVERTED: bool = false; +const DEVICE_ID: &str = "esp32-1"; enum QuestionType { Choice, @@ -75,6 +78,16 @@ static WHEEL_VALUE: Mutex = Mutex::new(Wheel accumulated: 0, }); +pub async fn reset_state() { + *QUESTION.lock().await = None; + *WHEEL_VALUE.lock().await = WheelData { + value: 0, + min: 0, + max: 0, + accumulated: 0, + }; +} + #[panic_handler] fn panic(info: &core::panic::PanicInfo) -> ! { println!("PANIC! {:?}", info); @@ -116,6 +129,10 @@ pub async fn tcp_read_loop( accumulated: 0, }; for line in str.lines() { + if line == "##" { + overwrite_lcd("Waiting", DEVICE_ID).await; + break; + } if line == "$$" { counter = 1; continue; @@ -189,6 +206,10 @@ pub async fn tcp_write_loop( mut write: TcpWriter<'_>, cancel: &Signal, ) -> Result<(), TcpDisconnect> { + if write.write(DEVICE_ID.as_bytes()).await.is_err() { + cancel.signal(()); + return Err(TcpDisconnect::WriteError); + } let mut buffer = buffer::WriteBuffer::<256>::new(); loop { let input_fut = input::INPUT.receive(); diff --git a/esp32/src/net.rs b/esp32/src/net.rs index 993971e..39da53b 100644 --- a/esp32/src/net.rs +++ b/esp32/src/net.rs @@ -152,7 +152,7 @@ pub async fn network_setup_task( break; } - overwrite_lcd("Connection closed", "").await; + overwrite_lcd("Connection close", "").await; Timer::after_millis(500).await; } } diff --git a/esp32/src/screen.rs b/esp32/src/screen.rs index 37818cb..5dade20 100644 --- a/esp32/src/screen.rs +++ b/esp32/src/screen.rs @@ -23,8 +23,10 @@ pub async fn overwrite_lcd(line1: &str, line2: &str) { buffer.line2_ptr = 0; buffer.line1.fill(0); buffer.line2.fill(0); - buffer.line1[..line1.len()].copy_from_slice(line1.as_bytes()); - buffer.line2[..line2.len()].copy_from_slice(line2.as_bytes()); + let len1 = line1.len().min(buffer.line1.len()); + let len2 = line2.len().min(buffer.line2.len()); + buffer.line1[..len1].copy_from_slice(line1[..len1].as_bytes()); + buffer.line2[..len2].copy_from_slice(line2[..len2].as_bytes()); LCD_UPDATE.signal(()); }