Skip to content

Commit 7edff88

Browse files
committed
wip: config menu
1 parent fbad27f commit 7edff88

File tree

5 files changed

+107
-41
lines changed

5 files changed

+107
-41
lines changed

src/buttons.rs

Lines changed: 65 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,19 @@ pub async fn buttons_task(
2525
);
2626
handler.add_handler(
2727
Button::Third,
28-
ButtonTrigger::HoldOnce(15000),
29-
submit_reset_wifi(),
28+
ButtonTrigger::HoldOnce(10000),
29+
submit_config_menu(),
3030
);
3131

32-
handler.add_handler(Button::First, ButtonTrigger::Down, room_left());
32+
handler.add_handler(Button::First, ButtonTrigger::Down, sel_left());
3333
handler.add_handler(Button::First, ButtonTrigger::Down, inspection_start());
3434
handler.add_handler(
3535
Button::First,
3636
ButtonTrigger::HoldOnce(1000),
3737
inspection_hold_stop(),
3838
);
39-
handler.add_handler(
40-
Button::First,
41-
ButtonTrigger::HoldOnce(15000),
42-
connect_bluetooth_display(),
43-
);
4439

45-
handler.add_handler(Button::Fourth, ButtonTrigger::Down, room_right());
40+
handler.add_handler(Button::Fourth, ButtonTrigger::Down, sel_right());
4641
handler.add_handler(Button::Fourth, ButtonTrigger::HoldOnce(1000), dnf_button());
4742
handler.add_handler(Button::Fourth, ButtonTrigger::Up, penalty_button());
4843

@@ -90,12 +85,20 @@ async fn wakeup_button(
9085
}
9186

9287
#[macros::button_handler]
93-
async fn room_left(
88+
async fn sel_left(
9489
_triggered: &ButtonTrigger,
9590
_hold_time: u64,
9691
state: &GlobalState,
9792
) -> Result<bool, ()> {
9893
let mut state = state.state.lock().await;
94+
if let Some(sel) = state.selected_config_menu.as_mut() {
95+
*sel = sel
96+
.wrapping_sub(1)
97+
.min(crate::structs::CONFIG_MENU_ITEMS.len() - 1);
98+
99+
return Ok(true);
100+
}
101+
99102
if state.scene == Scene::GroupSelect {
100103
state.group_selected_idx = state
101104
.group_selected_idx
@@ -109,12 +112,21 @@ async fn room_left(
109112
}
110113

111114
#[macros::button_handler]
112-
async fn room_right(
115+
async fn sel_right(
113116
_triggered: &ButtonTrigger,
114117
_hold_time: u64,
115118
state: &GlobalState,
116119
) -> Result<bool, ()> {
117120
let mut state = state.state.lock().await;
121+
if let Some(sel) = state.selected_config_menu.as_mut() {
122+
*sel += 1;
123+
if *sel == crate::structs::CONFIG_MENU_ITEMS.len() {
124+
*sel = 0;
125+
}
126+
127+
return Ok(true);
128+
}
129+
118130
if state.scene == Scene::GroupSelect {
119131
state.group_selected_idx += 1;
120132
if state.group_selected_idx == state.possible_groups.len() {
@@ -140,7 +152,44 @@ async fn submit_up(
140152
state_val.error_text = None;
141153
state.state.signal();
142154

143-
return Ok(false);
155+
return Ok(true);
156+
}
157+
158+
if let Some(sel) = state_val.selected_config_menu {
159+
match sel {
160+
0 => {
161+
// Reset WiFi
162+
_ = state
163+
.nvs
164+
.invalidate_key(esp_hal_wifimanager::WIFI_NVS_KEY)
165+
.await;
166+
_ = state.nvs.invalidate_key(b"SIGN_KEY").await;
167+
168+
Timer::after_millis(250).await;
169+
esp_hal::system::software_reset();
170+
}
171+
1 => {
172+
// BT Display
173+
state_val.error_text = Some("Not Implemented".to_string());
174+
}
175+
2 => {
176+
// Sign Cards
177+
state_val.error_text = Some("Not Implemented".to_string());
178+
}
179+
3 => {
180+
// Un-Sign Cards
181+
state_val.error_text = Some("Not Implemented".to_string());
182+
}
183+
4 => {} // Exit
184+
_ => {
185+
state_val.error_text = Some("Not Implemented".to_string());
186+
}
187+
}
188+
189+
state_val.selected_config_menu = None;
190+
state.state.signal();
191+
192+
return Ok(true);
144193
}
145194

146195
// Device add
@@ -161,7 +210,7 @@ async fn submit_up(
161210
})
162211
.await;
163212

164-
return Ok(false);
213+
return Ok(true);
165214
}
166215

167216
if state_val.should_skip_other_actions() {
@@ -249,16 +298,6 @@ async fn inspection_hold_stop(
249298
Ok(false)
250299
}
251300

252-
#[macros::button_handler]
253-
async fn connect_bluetooth_display(
254-
_triggered: &ButtonTrigger,
255-
_hold_time: u64,
256-
state: &GlobalState,
257-
) -> Result<bool, ()> {
258-
_ = state;
259-
Ok(false)
260-
}
261-
262301
#[macros::button_handler]
263302
async fn dnf_button(
264303
_triggered: &ButtonTrigger,
@@ -337,24 +376,17 @@ async fn submit_reset_competitor(
337376
}
338377

339378
#[macros::button_handler]
340-
async fn submit_reset_wifi(
379+
async fn submit_config_menu(
341380
_triggered: &ButtonTrigger,
342381
_hold_time: u64,
343382
state: &GlobalState,
344383
) -> Result<bool, ()> {
345-
_ = state
346-
.nvs
347-
.invalidate_key(esp_hal_wifimanager::WIFI_NVS_KEY)
348-
.await;
349-
_ = state.nvs.invalidate_key(b"SIGN_KEY").await;
350-
351384
{
352385
let mut state = state.state.lock().await;
353-
state.custom_message = Some(("Resetting WIFI".to_string(), "Restart in 5s...".to_string()));
386+
state.selected_config_menu = Some(0);
354387
}
355388

356-
Timer::after_millis(5000).await;
357-
esp_hal::system::software_reset();
389+
Ok(true)
358390
}
359391

360392
#[macros::button_handler]

src/lcd.rs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,35 @@ async fn process_lcd<T: OutputPin, D: DelayNs>(
193193
return Some(());
194194
}
195195

196+
// display custom message on top of everything!
197+
if let Some((line1, line2)) = &current_state.custom_message {
198+
_ = lcd_driver.print(0, line1, PrintAlign::Center, true);
199+
_ = lcd_driver.print(1, line2, PrintAlign::Center, true);
200+
201+
return Some(());
202+
}
203+
204+
if let Some(sel) = current_state.selected_config_menu {
205+
lcd_driver.clear_all().ok()?;
206+
lcd_driver.print(0, "<", PrintAlign::Left, false).ok()?;
207+
lcd_driver.print(0, ">", PrintAlign::Right, false).ok()?;
208+
209+
lcd_driver
210+
.print(0, "Config Menu", PrintAlign::Center, false)
211+
.ok()?;
212+
213+
lcd_driver
214+
.print(
215+
1,
216+
&alloc::format!("{}. {}", sel + 1, crate::structs::CONFIG_MENU_ITEMS[sel]),
217+
PrintAlign::Left,
218+
true,
219+
)
220+
.ok()?;
221+
222+
return Some(());
223+
}
224+
196225
let overwritten = process_lcd_overwrite(&current_state, global_state, lcd_driver).await;
197226
if overwritten {
198227
return Some(());
@@ -305,6 +334,7 @@ async fn process_lcd<T: OutputPin, D: DelayNs>(
305334
.ok()?;
306335
}
307336
Scene::GroupSelect => {
337+
lcd_driver.clear_all().ok()?;
308338
lcd_driver.print(0, "<", PrintAlign::Left, false).ok()?;
309339
lcd_driver.print(1, "<", PrintAlign::Left, false).ok()?;
310340
lcd_driver.print(0, ">", PrintAlign::Right, false).ok()?;
@@ -510,14 +540,6 @@ async fn process_lcd_overwrite(
510540
_global_state: &GlobalState,
511541
lcd_driver: &mut LcdAbstract<80, 16, 2, 3>,
512542
) -> bool {
513-
// display custom message on top of everything!
514-
if let Some((line1, line2)) = &current_state.custom_message {
515-
_ = lcd_driver.print(0, line1, PrintAlign::Center, true);
516-
_ = lcd_driver.print(1, line2, PrintAlign::Center, true);
517-
518-
return true;
519-
}
520-
521543
if !current_state.scene.can_be_lcd_overwritten() {
522544
return false;
523545
}

src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,11 +309,13 @@ async fn main(spawner: Spawner) {
309309
spawner.must_spawn(logger_task(global_state.clone()));
310310

311311
// BUG: this fails after wifi setup (its good if wifi is saved and ble isnt spawned)
312+
/*
312313
spawner.must_spawn(bluetooth::bluetooth_timer_task(
313314
wifi_res.wifi_init,
314315
board.bt,
315316
global_state.clone(),
316317
));
318+
*/
317319

318320
set_brownout_detection(true);
319321
global_state.state.lock().await.scene = Scene::WaitingForCompetitor;

src/state.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ pub struct SignaledGlobalStateInner {
172172
pub error_text: Option<String>,
173173
pub possible_groups: Vec<PossibleGroup>,
174174
pub group_selected_idx: usize,
175+
pub selected_config_menu: Option<usize>,
175176

176177
pub device_added: Option<bool>,
177178
pub server_connected: Option<bool>,
@@ -220,6 +221,7 @@ impl SignaledGlobalStateInner {
220221
error_text: None,
221222
possible_groups: Vec::new(),
222223
group_selected_idx: 0,
224+
selected_config_menu: None,
223225

224226
device_added: None,
225227
server_connected: None,

src/structs.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ use alloc::{
44
};
55
use serde::{Deserialize, Serialize};
66

7+
pub const CONFIG_MENU_ITEMS: [&str; 5] = [
8+
"Reset WiFi",
9+
"BT Display",
10+
"Sign Cards",
11+
"Un-Sign Cards",
12+
"Exit",
13+
];
14+
715
#[derive(Deserialize, Debug)]
816
pub struct ConnSettings {
917
pub mdns: bool,

0 commit comments

Comments
 (0)