Skip to content

Commit 5dc68d8

Browse files
committed
chore: move consts to consts.rs
1 parent 342edf9 commit 5dc68d8

File tree

9 files changed

+49
-38
lines changed

9 files changed

+49
-38
lines changed

src/battery.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use embassy_time::Timer;
1+
use crate::consts::BATTERY_SEND_INTERVAL_MS;
2+
use embassy_time::{Instant, Timer};
23
use esp_hal::{
34
analog::adc::{Adc, AdcConfig, Attenuation},
45
gpio::GpioPin,
@@ -33,7 +34,7 @@ pub async fn battery_read_task(
3334

3435
let mut adc = Adc::new(adc, adc_config);
3536

36-
let mut count = 0;
37+
let mut battery_start = Instant::now();
3738

3839
let base_freq = 2.0;
3940
let sample_freq = 1000.0;
@@ -46,14 +47,11 @@ pub async fn battery_read_task(
4647
.unwrap_or(0);
4748
let read = smoother.tick(read as f32);
4849

49-
count += 1;
50-
51-
if count < 30 {
52-
// 15s
50+
if (Instant::now() - battery_start).as_millis() < BATTERY_SEND_INTERVAL_MS {
5351
continue;
5452
}
5553

56-
count = 0;
54+
battery_start = Instant::now();
5755
let bat_calc_mv = calculate(read as f64);
5856
let bat_percentage = bat_perctentage(bat_calc_mv);
5957

src/consts.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
pub const LOG_SEND_INTERVAL_MS: u64 = 5000;
2+
pub const PRINT_HEAP_INTERVAL_MS: u64 = 30000;
3+
4+
pub const BATTERY_SEND_INTERVAL_MS: u64 = 60000;
5+
6+
pub const SCROLL_TICKER_INVERVAL_MS: u64 = 500;
7+
pub const LCD_INSPECTION_FRAME_TIME: u64 = 1000 / 30;
8+
9+
pub const RFID_RETRY_INIT_MS: u64 = 1500;
10+
pub const WS_RETRY_MS: u64 = 1000;
11+
12+
pub const MDNS_RESEND_INTERVAL: u64 = 500;
13+
14+
pub const INSPECTION_TIME_DNF: u64 = 17000;
15+
pub const INSPECTION_TIME_PLUS2: u64 = 15000;

src/lcd.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use hd44780_driver::{
88
};
99

1010
use crate::{
11+
consts::{INSPECTION_TIME_PLUS2, LCD_INSPECTION_FRAME_TIME, SCROLL_TICKER_INVERVAL_MS},
1112
state::{sleep_state, GlobalState, Scene, SignaledGlobalStateInner},
1213
translations::get_translation,
1314
utils::{
@@ -134,7 +135,8 @@ pub async fn lcd_task(
134135
.await;
135136
lcd_driver.display_on_lcd(&mut lcd, &mut delay).unwrap();
136137

137-
let mut scroll_ticker = embassy_time::Ticker::every(Duration::from_millis(500));
138+
let mut scroll_ticker =
139+
embassy_time::Ticker::every(Duration::from_millis(SCROLL_TICKER_INVERVAL_MS));
138140
loop {
139141
scroll_ticker.next().await;
140142
let changed = lcd_driver.scroll_step().unwrap();
@@ -336,7 +338,7 @@ async fn process_lcd<C: CharsetWithFallback>(
336338
.ok()?;
337339

338340
lcd_driver.display_on_lcd(lcd, delay).ok()?;
339-
Timer::after_millis(1000 / 30).await;
341+
Timer::after_millis(LCD_INSPECTION_FRAME_TIME).await;
340342
}
341343
}
342344
Scene::Timer => loop {
@@ -360,7 +362,8 @@ async fn process_lcd<C: CharsetWithFallback>(
360362
.inspection_start
361363
.map(|x| (current_state.inspection_end.unwrap() - x).as_millis());
362364

363-
if current_state.use_inspection && inspection_time.unwrap_or(0) > 15000 {
365+
if current_state.use_inspection && inspection_time.unwrap_or(0) > INSPECTION_TIME_PLUS2
366+
{
364367
let inspections_seconds = inspection_time.unwrap_or(0) / 1000;
365368
lcd_driver
366369
.print(

src/main.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
extern crate alloc;
66
use alloc::{rc::Rc, vec::Vec};
7+
use consts::{LOG_SEND_INTERVAL_MS, PRINT_HEAP_INTERVAL_MS};
78
use core::str::FromStr;
89
use embassy_executor::Spawner;
910
use embassy_sync::signal::Signal;
10-
use embassy_time::Timer;
11+
use embassy_time::{Instant, Timer};
1112
use esp_backtrace as _;
1213
use esp_hal::{
1314
gpio::{Input, Output},
@@ -25,6 +26,7 @@ use utils::{
2526

2627
mod battery;
2728
mod buttons;
29+
mod consts;
2830
mod lcd;
2931
mod mdns;
3032
mod rfid;
@@ -126,6 +128,7 @@ async fn main(spawner: Spawner) {
126128
let shifter_latch_pin = Output::new(peripherals.GPIO1, esp_hal::gpio::Level::Low);
127129
#[cfg(feature = "esp32c3")]
128130
let shifter_clk_pin = if crate::utils::get_efuse_u32() == 1342310409 {
131+
// TODO: remove this if
129132
Output::new(peripherals.GPIO7, esp_hal::gpio::Level::Low)
130133
} else {
131134
Output::new(peripherals.GPIO21, esp_hal::gpio::Level::Low)
@@ -295,10 +298,9 @@ async fn main(spawner: Spawner) {
295298
.parse_saved_state(saved_state);
296299
}
297300

298-
let mut counter = 0;
301+
let mut heap_start = Instant::now();
299302
loop {
300-
counter += 1;
301-
Timer::after_millis(5000).await;
303+
Timer::after_millis(LOG_SEND_INTERVAL_MS).await;
302304

303305
// TODO: move to own task
304306
unsafe {
@@ -323,13 +325,13 @@ async fn main(spawner: Spawner) {
323325
}
324326
}
325327

326-
if counter == 12 {
328+
if (Instant::now() - heap_start).as_millis() >= PRINT_HEAP_INTERVAL_MS {
327329
log::info!("Heap info:");
328330
log::info!("Size: {}", esp_alloc::HEAP.used() + esp_alloc::HEAP.free());
329331
log::info!("Used: {}", esp_alloc::HEAP.used());
330332
log::info!("Free: {}", esp_alloc::HEAP.free());
331333

332-
counter = 0;
334+
heap_start = Instant::now();
333335
}
334336
}
335337
}

src/mdns.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use embassy_net::{
55
use embassy_time::{Duration, Timer};
66
use esp_hal_mdns::MdnsQuery;
77

8+
use crate::consts::MDNS_RESEND_INTERVAL;
9+
810
pub async fn mdns_query(stack: Stack<'static>) -> Option<heapless::String<255>> {
911
let mut rx_buffer = [0; 1024];
1012
let mut tx_buffer = [0; 1024];
@@ -23,7 +25,7 @@ pub async fn mdns_query(stack: Stack<'static>) -> Option<heapless::String<255>>
2325
_ = sock.bind(5353);
2426
_ = stack.join_multicast_group(ip_addr);
2527

26-
let mut mdns = MdnsQuery::new("_stackmat._tcp.local", 500, || {
28+
let mut mdns = MdnsQuery::new("_stackmat._tcp.local", MDNS_RESEND_INTERVAL, || {
2729
esp_hal::time::now().duration_since_epoch().to_millis()
2830
});
2931
let mut data_buf = [0; 1024];

src/rfid.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::consts::RFID_RETRY_INIT_MS;
12
use crate::state::{current_epoch, GlobalState};
23
use crate::structs::{CardInfoResponsePacket, SolveConfirmPacket};
34
use alloc::string::ToString;
@@ -58,8 +59,8 @@ pub async fn rfid_task(
5859
break;
5960
}
6061

61-
log::error!("MFRC522 init failed! Try to power cycle to module! Retrying in 1s...");
62-
Timer::after(Duration::from_millis(1000)).await;
62+
log::error!("MFRC522 init failed! Try to power cycle to module! Retrying...");
63+
Timer::after(Duration::from_millis(RFID_RETRY_INIT_MS)).await;
6364
}
6465

6566
log::debug!("PCD ver: {:?}", mfrc522.pcd_get_version().await);

src/stackmat.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::{
2+
consts::{INSPECTION_TIME_DNF, INSPECTION_TIME_PLUS2},
23
state::{GlobalState, Scene},
34
utils::stackmat::{
45
ms_to_time_str, parse_stackmat_data, time_str_to_display, StackmatTimerState,
@@ -68,17 +69,14 @@ pub async fn stackmat_task(
6869
if state.solve_time.is_none() && state.last_solve_time != Some(parsed.1) {
6970
let inspection_time = state
7071
.inspection_end
71-
.and_then(|x| Some(x - state.inspection_start?));
72-
let inspection_time = if let Some(ins) = inspection_time {
73-
ins.as_millis()
74-
} else {
75-
0
76-
};
72+
.zip(state.inspection_start)
73+
.map(|(end, start)| (end - start).as_millis())
74+
.unwrap_or(0);
7775

7876
state.solve_time = Some(parsed.1);
79-
state.penalty = if inspection_time >= 17000 {
77+
state.penalty = if inspection_time >= INSPECTION_TIME_DNF {
8078
Some(-1)
81-
} else if inspection_time >= 15000 {
79+
} else if inspection_time >= INSPECTION_TIME_PLUS2 {
8280
Some(2)
8381
} else {
8482
None

src/translations.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,6 @@ impl TranslationRecord {
2121

2222
pub static TRANSLATIONS: Mutex<CriticalSectionRawMutex, Vec<TranslationRecord>> =
2323
Mutex::new(Vec::new());
24-
/*
25-
pub const TRANSLATION: &[TranslationRecord] = &[
26-
TranslationRecord {
27-
key: String::from("dsa"),
28-
translation: "".to_string(),
29-
}, //TranslationRecord::new("SCAN_COMPETITOR_1", "Scan the card"),
30-
//TranslationRecord::new("SCAN_COMPETITOR_2", "of a competitor"),
31-
];
32-
*/
3324

3425
pub fn init_translations() {
3526
if let Ok(mut t) = TRANSLATIONS.try_lock() {

src/ws.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::{
2+
consts::WS_RETRY_MS,
23
state::GlobalState,
34
structs::{ApiError, FromPacket, TimerPacket, TimerPacketInner},
45
};
@@ -46,7 +47,7 @@ pub async fn ws_task(stack: Stack<'static>, ws_url: String, global_state: Global
4647
let r = socket.connect(remote_endpoint).await;
4748
if let Err(e) = r {
4849
log::error!("connect error: {:?}", e);
49-
Timer::after_millis(1000).await;
50+
Timer::after_millis(WS_RETRY_MS).await;
5051
continue;
5152
}
5253

@@ -96,7 +97,7 @@ pub async fn ws_task(stack: Stack<'static>, ws_url: String, global_state: Global
9697

9798
if res.is_err() {
9899
log::error!("ws: reader or writer err!");
99-
Timer::after_millis(1000).await;
100+
Timer::after_millis(WS_RETRY_MS).await;
100101
break;
101102
}
102103
}

0 commit comments

Comments
 (0)