refactor: consolidate logging and time handling, remove TIME_ACCESS and LOG_ACCESS
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
use crate::hal::TIME_ACCESS;
|
||||
use crate::BOARD_ACCESS;
|
||||
use crate::vec;
|
||||
use alloc::string::ToString;
|
||||
use alloc::vec::Vec;
|
||||
use bytemuck::{AnyBitPattern, Pod, Zeroable};
|
||||
use deranged::RangedU8;
|
||||
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
||||
use embassy_sync::channel::Channel;
|
||||
use embassy_sync::mutex::Mutex;
|
||||
use esp_hal::Persistable;
|
||||
use log::{info, warn};
|
||||
@@ -32,6 +33,34 @@ static mut LOG_ARRAY: LogArray = LogArray {
|
||||
pub static LOG_ACCESS: Mutex<CriticalSectionRawMutex, &'static mut LogArray> =
|
||||
unsafe { Mutex::new(&mut LOG_ARRAY) };
|
||||
|
||||
pub struct LogRequest {
|
||||
pub message_key: LogMessage,
|
||||
pub number_a: u32,
|
||||
pub number_b: u32,
|
||||
pub txt_short: heapless::String<TXT_SHORT_LENGTH>,
|
||||
pub txt_long: heapless::String<TXT_LONG_LENGTH>,
|
||||
}
|
||||
|
||||
static LOG_CHANNEL: Channel<CriticalSectionRawMutex, LogRequest, 16> = Channel::new();
|
||||
|
||||
#[embassy_executor::task]
|
||||
pub async fn log_task() {
|
||||
loop {
|
||||
let request = LOG_CHANNEL.receive().await;
|
||||
LOG_ACCESS
|
||||
.lock()
|
||||
.await
|
||||
.log(
|
||||
request.message_key,
|
||||
request.number_a,
|
||||
request.number_b,
|
||||
request.txt_short.as_str(),
|
||||
request.txt_long.as_str(),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
|
||||
const TXT_SHORT_LENGTH: usize = 8;
|
||||
const TXT_LONG_LENGTH: usize = 32;
|
||||
|
||||
@@ -87,11 +116,21 @@ pub async fn log(
|
||||
txt_short: &str,
|
||||
txt_long: &str,
|
||||
) {
|
||||
LOG_ACCESS
|
||||
.lock()
|
||||
.await
|
||||
.log(message_key, number_a, number_b, txt_short, txt_long)
|
||||
.await
|
||||
let mut txt_short_stack: heapless::String<TXT_SHORT_LENGTH> = heapless::String::new();
|
||||
let mut txt_long_stack: heapless::String<TXT_LONG_LENGTH> = heapless::String::new();
|
||||
|
||||
limit_length(txt_short, &mut txt_short_stack);
|
||||
limit_length(txt_long, &mut txt_long_stack);
|
||||
|
||||
LOG_CHANNEL
|
||||
.send(LogRequest {
|
||||
message_key,
|
||||
number_a,
|
||||
number_b,
|
||||
txt_short: txt_short_stack,
|
||||
txt_long: txt_long_stack,
|
||||
})
|
||||
.await;
|
||||
}
|
||||
|
||||
impl LogArray {
|
||||
@@ -122,15 +161,9 @@ impl LogArray {
|
||||
let mut head: RangedU8<0, MAX_LOG_ARRAY_INDEX> =
|
||||
RangedU8::new(self.head).unwrap_or(RangedU8::new_saturating(0));
|
||||
|
||||
let mut txt_short_stack: heapless::String<TXT_SHORT_LENGTH> = heapless::String::new();
|
||||
let mut txt_long_stack: heapless::String<TXT_LONG_LENGTH> = heapless::String::new();
|
||||
|
||||
limit_length(txt_short, &mut txt_short_stack);
|
||||
limit_length(txt_long, &mut txt_long_stack);
|
||||
|
||||
let time = {
|
||||
let guard = TIME_ACCESS.get().await.lock().await;
|
||||
guard.current_time_us()
|
||||
let mut guard = BOARD_ACCESS.get().await.lock().await;
|
||||
guard.board_hal.get_esp().rtc.current_time_us()
|
||||
} / 1000;
|
||||
|
||||
let ordinal = message_key.ordinal() as u16;
|
||||
@@ -148,12 +181,8 @@ impl LogArray {
|
||||
to_modify.message_id = ordinal;
|
||||
to_modify.a = number_a;
|
||||
to_modify.b = number_b;
|
||||
to_modify
|
||||
.txt_short
|
||||
.clone_from_slice(txt_short_stack.as_bytes());
|
||||
to_modify
|
||||
.txt_long
|
||||
.clone_from_slice(txt_long_stack.as_bytes());
|
||||
to_modify.txt_short.clone_from_slice(txt_short.as_bytes());
|
||||
to_modify.txt_long.clone_from_slice(txt_long.as_bytes());
|
||||
head = head.wrapping_add(1);
|
||||
self.head = head.get();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user