Skip to content

Commit b28dc77

Browse files
committed
feat: use ag-lcd fork with async support fix: decrease button debounce time fix: ms time to str above 9 minutes 59seconds
1 parent bf0a160 commit b28dc77

File tree

9 files changed

+59
-57
lines changed

9 files changed

+59
-57
lines changed

Cargo.lock

Lines changed: 4 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ anyhow = { version = "1.0.97", default-features = false }
4444
embedded-hal-bus = { version = "0.3.0", features = ["async"] }
4545
embedded-tls = { version = "0.17.0", default-features = false, features = ["alloc", "embedded-io-adapters", "log"] }
4646
rand_core = { version = "0.6.4", features = ["getrandom"] }
47-
ag-lcd = { version = "0.3.0", features = [] }
47+
ag-lcd-async = { git = "https://github.com/filipton/ag-lcd-async", features = [] }
4848
port-expander = { version = "0.6.4", optional = true }
4949

5050
mfrc522-01 = { version = "0.1.1", package = "esp-hal-mfrc522", optional = true }
5151
mfrc522-02 = { version = "0.2.1", features = ["embassy-time"], package = "esp-hal-mfrc522", optional = true }
5252

5353
[features]
5454
default = ["esp32c3", "sleep"]
55-
esp32 = ["esp-backtrace/esp32", "esp-hal/esp32", "esp-println/esp32", "esp-wifi/esp32", "esp-hal-embassy/esp32", "esp-storage/esp32", "esp-hal-wifimanager/esp32", "esp-hal-ota/esp32", "dep:esp32", "dep:mfrc522-01", "dep:port-expander", "ag-lcd/i2c"]
55+
esp32 = ["esp-backtrace/esp32", "esp-hal/esp32", "esp-println/esp32", "esp-wifi/esp32", "esp-hal-embassy/esp32", "esp-storage/esp32", "esp-hal-wifimanager/esp32", "esp-hal-ota/esp32", "dep:esp32", "dep:mfrc522-01", "dep:port-expander", "ag-lcd-async/i2c"]
5656
esp32c3 = ["esp-backtrace/esp32c3", "esp-hal/esp32c3", "esp-println/esp32c3", "esp-wifi/esp32c3", "esp-hal-embassy/esp32c3", "esp-storage/esp32c3", "esp-hal-wifimanager/esp32c3", "esp-hal-ota/esp32c3", "dep:esp32c3", "dep:mfrc522-02"]
5757
gen_version = []
5858
bat_dev_lcd = []

src/consts.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ pub const SLEEP_AFTER_MS: u64 = 60000 * 9999;
88
#[cfg(not(feature = "sleep"))]
99
pub const DEEPER_SLEEP_AFTER_MS: u64 = 60000 * 99999;
1010

11+
pub const BUTTON_DEBOUNCE_TIME: u64 = 25;
12+
1113
pub const LOG_SEND_INTERVAL_MS: u64 = 5000;
1214
pub const PRINT_HEAP_INTERVAL_MS: u64 = 30000;
1315

src/lcd.rs

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use adv_shift_registers::wrappers::ShifterValueRange;
2-
use ag_lcd::LcdDisplay;
2+
use ag_lcd_async::LcdDisplay;
33
use alloc::{rc::Rc, string::ToString};
44
use embassy_sync::{blocking_mutex::raw::NoopRawMutex, signal::Signal};
55
use embassy_time::{Delay, Duration, Instant, Timer};
6-
use embedded_hal::{delay::DelayNs, digital::OutputPin};
6+
use embedded_hal::digital::OutputPin;
7+
use embedded_hal_async::delay::DelayNs;
78

89
use crate::{
910
consts::{
@@ -33,13 +34,14 @@ pub async fn lcd_task(
3334

3435
#[cfg(feature = "esp32")]
3536
let mut lcd = LcdDisplay::new_pcf8574(&mut i2c_expander, Delay)
36-
.with_display(ag_lcd::Display::On)
37-
.with_blink(ag_lcd::Blink::Off)
38-
.with_cursor(ag_lcd::Cursor::Off)
39-
.with_size(ag_lcd::Size::Dots5x8)
37+
.with_display(ag_lcd_async::Display::On)
38+
.with_blink(ag_lcd_async::Blink::Off)
39+
.with_cursor(ag_lcd_async::Cursor::Off)
40+
.with_size(ag_lcd_async::Size::Dots5x8)
4041
.with_cols(16)
41-
.with_lines(ag_lcd::Lines::TwoLines)
42-
.build();
42+
.with_lines(ag_lcd_async::Lines::TwoLines)
43+
.build()
44+
.await;
4345

4446
#[cfg(feature = "esp32c3")]
4547
let mut lcd = {
@@ -50,19 +52,20 @@ pub async fn lcd_task(
5052
let d5_pin = lcd_shifter.get_pin_mut(5, false);
5153
let d6_pin = lcd_shifter.get_pin_mut(6, false);
5254
let d7_pin = lcd_shifter.get_pin_mut(7, false);
53-
ag_lcd::LcdDisplay::new(rs_pin, en_pin, Delay)
54-
.with_display(ag_lcd::Display::On)
55-
.with_blink(ag_lcd::Blink::Off)
56-
.with_cursor(ag_lcd::Cursor::Off)
57-
.with_size(ag_lcd::Size::Dots5x8)
55+
LcdDisplay::new(rs_pin, en_pin, Delay)
56+
.with_display(ag_lcd_async::Display::On)
57+
.with_blink(ag_lcd_async::Blink::Off)
58+
.with_cursor(ag_lcd_async::Cursor::Off)
59+
.with_size(ag_lcd_async::Size::Dots5x8)
5860
.with_cols(16)
59-
.with_lines(ag_lcd::Lines::TwoLines)
61+
.with_lines(ag_lcd_async::Lines::TwoLines)
6062
.with_half_bus(d4_pin, d5_pin, d6_pin, d7_pin)
6163
.with_backlight(bl_pin)
6264
.build()
65+
.await
6366
};
6467

65-
lcd.clear();
68+
lcd.clear().await;
6669
lcd.backlight_on();
6770

6871
let mut lcd_driver: LcdAbstract<80, 16, 2, 3> = LcdAbstract::new();
@@ -79,15 +82,15 @@ pub async fn lcd_task(
7982
PrintAlign::Left,
8083
true,
8184
);
82-
lcd_driver.display_on_lcd(&mut lcd);
85+
lcd_driver.display_on_lcd(&mut lcd).await;
8386

8487
_ = lcd_driver.print(
8588
0,
8689
&alloc::format!("{}%", global_state.show_battery.wait().await),
8790
PrintAlign::Right,
8891
false,
8992
);
90-
lcd_driver.display_on_lcd(&mut lcd);
93+
lcd_driver.display_on_lcd(&mut lcd).await;
9194

9295
#[cfg(not(feature = "bat_dev_lcd"))]
9396
Timer::after_millis(2500).await;
@@ -118,15 +121,15 @@ pub async fn lcd_task(
118121
&display,
119122
)
120123
.await;
121-
lcd_driver.display_on_lcd(&mut lcd);
124+
lcd_driver.display_on_lcd(&mut lcd).await;
122125

123126
let mut scroll_ticker =
124127
embassy_time::Ticker::every(Duration::from_millis(SCROLL_TICKER_INVERVAL_MS));
125128
loop {
126129
scroll_ticker.next().await;
127130
let changed = lcd_driver.scroll_step();
128131
if changed.is_ok_and(|c| c) {
129-
lcd_driver.display_on_lcd(&mut lcd);
132+
lcd_driver.display_on_lcd(&mut lcd).await;
130133
}
131134

132135
#[cfg(not(any(feature = "e2e", feature = "qa")))]
@@ -136,7 +139,7 @@ pub async fn lcd_task(
136139
{
137140
_ = lcd_driver.print(0, "Sleep", PrintAlign::Center, true);
138141
_ = lcd_driver.print(1, "Press any key", PrintAlign::Center, true);
139-
lcd_driver.display_on_lcd(&mut lcd);
142+
lcd_driver.display_on_lcd(&mut lcd).await;
140143
lcd.backlight_off();
141144

142145
unsafe {
@@ -151,7 +154,7 @@ pub async fn lcd_task(
151154
{
152155
_ = lcd_driver.print(0, "Deep Sleep", PrintAlign::Center, true);
153156
_ = lcd_driver.print(1, "Press any key", PrintAlign::Center, true);
154-
lcd_driver.display_on_lcd(&mut lcd);
157+
lcd_driver.display_on_lcd(&mut lcd).await;
155158
crate::utils::deeper_sleep();
156159
}
157160
}
@@ -286,7 +289,7 @@ async fn process_lcd<T: OutputPin, D: DelayNs>(
286289
)
287290
.ok()?;
288291

289-
lcd_driver.display_on_lcd(lcd);
292+
lcd_driver.display_on_lcd(lcd).await;
290293
wifi_setup_sig.wait().await;
291294
global_state.state.lock().await.scene = Scene::AutoSetupWait;
292295
}
@@ -408,6 +411,10 @@ async fn process_lcd<T: OutputPin, D: DelayNs>(
408411
.inspection_start
409412
.unwrap_or(Instant::now());
410413

414+
lcd_driver
415+
.print(1, "Inspection", PrintAlign::Center, true)
416+
.ok()?;
417+
411418
loop {
412419
let elapsed = (Instant::now() - inspection_start).as_millis();
413420
let time_str = ms_to_time_str(elapsed);
@@ -416,7 +423,7 @@ async fn process_lcd<T: OutputPin, D: DelayNs>(
416423
.print(0, &time_str, PrintAlign::Center, true)
417424
.ok()?;
418425

419-
lcd_driver.display_on_lcd(lcd);
426+
lcd_driver.display_on_lcd(lcd).await;
420427
Timer::after_millis(LCD_INSPECTION_FRAME_TIME).await;
421428
}
422429
}
@@ -428,7 +435,7 @@ async fn process_lcd<T: OutputPin, D: DelayNs>(
428435
.ok()?;
429436

430437
display.set_data_raw(&crate::utils::stackmat::time_str_to_display(&time_str));
431-
lcd_driver.display_on_lcd(lcd);
438+
lcd_driver.display_on_lcd(lcd).await;
432439
},
433440
Scene::Finished => {
434441
let solve_time = current_state.solve_time.unwrap_or(0);
@@ -513,7 +520,7 @@ async fn process_lcd<T: OutputPin, D: DelayNs>(
513520
let progress = global_state.update_progress.wait().await;
514521
_ = lcd_driver.print(1, &alloc::format!("{progress}%"), PrintAlign::Center, true);
515522

516-
lcd_driver.display_on_lcd(lcd);
523+
lcd_driver.display_on_lcd(lcd).await;
517524
}
518525
}
519526
}

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ async fn main(spawner: Spawner) {
5959

6060
#[cfg(feature = "esp32")]
6161
{
62-
config = config.with_cpu_clock(esp_hal::clock::CpuClock::max());
62+
config = config.with_cpu_clock(esp_hal::clock::CpuClock::_80MHz);
6363
}
6464

6565
#[cfg(feature = "esp32c3")]
6666
{
67-
config = config.with_cpu_clock(esp_hal::clock::CpuClock::max());
67+
config = config.with_cpu_clock(esp_hal::clock::CpuClock::_80MHz);
6868
}
6969

7070
config

src/state.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use crate::{structs::PossibleGroup, utils::signaled_mutex::SignaledMutex};
22
use alloc::{rc::Rc, string::String, vec::Vec};
3-
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, signal::Signal};
3+
use embassy_sync::{
4+
blocking_mutex::raw::{CriticalSectionRawMutex, NoopRawMutex},
5+
signal::Signal,
6+
};
47
use embassy_time::{Duration, Instant, Timer};
58
use esp_hal_wifimanager::Nvs;
69
use serde::{Deserialize, Serialize};
@@ -118,7 +121,7 @@ impl End2End {
118121
pub type GlobalState = Rc<GlobalStateInner>;
119122
pub struct GlobalStateInner {
120123
pub state: SignaledMutex<CriticalSectionRawMutex, SignaledGlobalStateInner>,
121-
pub timer_signal: Signal<CriticalSectionRawMutex, u64>,
124+
pub timer_signal: Signal<NoopRawMutex, u64>,
122125
pub show_battery: Signal<CriticalSectionRawMutex, u8>,
123126
pub update_progress: Signal<CriticalSectionRawMutex, u8>,
124127

src/utils/buttons.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::{
22
buttons::HandlersDerive,
3+
consts::BUTTON_DEBOUNCE_TIME,
34
state::{sleep_state, GlobalState},
45
};
56
use alloc::vec::Vec;
@@ -133,7 +134,7 @@ impl ButtonsHandler {
133134
debounce_time = esp_hal::time::Instant::now();
134135
} else if old_debounced != out_val {
135136
let duration = esp_hal::time::Instant::now() - debounce_time;
136-
if duration.as_millis() > 50 || sleep_state() {
137+
if duration.as_millis() > BUTTON_DEBOUNCE_TIME || sleep_state() {
137138
if old_debounced == 0 {
138139
#[cfg(not(feature = "qa"))]
139140
self.button_down((out_val as u8).into(), state).await;

src/utils/lcd_abstract.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use ag_lcd::LcdDisplay;
2-
use embedded_hal::{delay::DelayNs, digital::OutputPin};
1+
use ag_lcd_async::LcdDisplay;
2+
use embedded_hal::digital::OutputPin;
3+
use embedded_hal_async::delay::DelayNs;
34

45
pub enum PrintAlign {
56
Left,
@@ -151,12 +152,13 @@ impl<const LINE_SIZE: usize, const X: usize, const Y: usize, const SCROLLER_WT:
151152
Ok(())
152153
}
153154

154-
pub fn display_on_lcd<T: OutputPin, D: DelayNs>(&mut self, lcd: &mut LcdDisplay<T, D>) {
155+
pub async fn display_on_lcd<T: OutputPin, D: DelayNs>(&mut self, lcd: &mut LcdDisplay<T, D>) {
155156
let display_data = self.display_data();
156157
for (y, line) in display_data.0.iter().enumerate() {
157158
if line.1 {
158-
lcd.set_position(0, y as u8);
159-
lcd.print(unsafe { core::str::from_utf8_unchecked(line.0) });
159+
lcd.set_position(0, y as u8).await;
160+
lcd.print(unsafe { core::str::from_utf8_unchecked(line.0) })
161+
.await;
160162

161163
display_data.1[y].copy_from_slice(line.0);
162164
}

src/utils/stackmat.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,14 @@ pub fn time_str_to_display(time: &str) -> [u8; 6] {
103103
data
104104
}
105105

106-
pub fn ms_to_time_str(ms: u64) -> heapless::String<8> {
106+
pub fn ms_to_time_str(ms: u64) -> heapless::String<12> {
107107
let minutes: u8 = (ms / 60000) as u8;
108108
let seconds: u8 = ((ms % 60000) / 1000) as u8;
109109
let ms: u16 = (ms % 1000) as u16;
110110

111-
let mut time_str = heapless::String::<8>::new();
111+
let mut time_str = heapless::String::<12>::new();
112112
if minutes > 0 {
113-
_ = time_str.push((minutes + b'0') as char);
114-
_ = time_str.push(':');
115-
_ = time_str.push_str(&alloc::format!("{seconds:02}.{ms:03}"));
113+
_ = time_str.push_str(&alloc::format!("{minutes}:{seconds:02}.{ms:03}"));
116114
} else {
117115
_ = time_str.push_str(&alloc::format!("{seconds:01}.{ms:03}"));
118116
}

0 commit comments

Comments
 (0)