logging and pcb adjustment
This commit is contained in:
143
rust/src/log/mod.rs
Normal file
143
rust/src/log/mod.rs
Normal file
@@ -0,0 +1,143 @@
|
||||
use std::{collections::HashMap, sync::Mutex};
|
||||
use strum_macros::IntoStaticStr;
|
||||
|
||||
use esp_idf_svc::systime::EspSystemTime;
|
||||
use once_cell::sync::Lazy;
|
||||
use ringbuffer::{ConstGenericRingBuffer, RingBuffer};
|
||||
use text_template::Template;
|
||||
|
||||
const TXT_SHORT_LENGTH:usize = 8;
|
||||
const TXT_LONG_LENGTH:usize = 32;
|
||||
|
||||
const BUFFER_SIZE:usize = 210;
|
||||
|
||||
#[link_section = ".rtc.data"]
|
||||
static mut BUFFER:ConstGenericRingBuffer::<LogEntry, BUFFER_SIZE> = ConstGenericRingBuffer::<LogEntry, BUFFER_SIZE>::new();
|
||||
|
||||
static BUFFER_ACCESS: Lazy<Mutex<&mut ConstGenericRingBuffer::<LogEntry, BUFFER_SIZE>>> = Lazy::new(|| unsafe { Mutex::new(&mut BUFFER) });
|
||||
|
||||
|
||||
pub struct LogEntry {
|
||||
pub timestamp: u64,
|
||||
pub message_id: u32,
|
||||
pub a: u32,
|
||||
pub b: u32,
|
||||
pub txt_short: heapless::String<TXT_SHORT_LENGTH>,
|
||||
pub txt_long: heapless::String<TXT_LONG_LENGTH>
|
||||
}
|
||||
|
||||
pub fn init(){
|
||||
unsafe {
|
||||
BUFFER = ConstGenericRingBuffer::<LogEntry, BUFFER_SIZE>::new();
|
||||
};
|
||||
let mut access = BUFFER_ACCESS.lock().unwrap();
|
||||
access.drain().for_each(|_| {});
|
||||
}
|
||||
|
||||
fn limit_length <const LIMIT:usize> (input: &str, target: &mut heapless::String<LIMIT>){
|
||||
for char in input.chars() {
|
||||
match target.push(char) {
|
||||
Ok(_) => {}, //continue adding chars
|
||||
Err(_) => {
|
||||
//clear space for two asci chars
|
||||
while target.len()+2 >= LIMIT {
|
||||
target.pop().unwrap();
|
||||
}
|
||||
//add .. to shortened strings
|
||||
target.push('.').unwrap();
|
||||
target.push('.').unwrap();
|
||||
return;
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn log(message_key: LogMessage, number_a: u32, number_b: u32, txt_short:&str, txt_long:&str){
|
||||
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 = EspSystemTime {}.now().as_millis() as u64;
|
||||
|
||||
let template_string:&str = message_key.into();
|
||||
|
||||
|
||||
let mut values: HashMap<&str, &str> = HashMap::new();
|
||||
let number_a_str = number_a.to_string();
|
||||
let number_b_str = number_b.to_string();
|
||||
|
||||
values.insert("number_a", &number_a_str);
|
||||
values.insert("number_b", &number_b_str);
|
||||
values.insert("txt_short", txt_short);
|
||||
values.insert("txt_long", txt_long);
|
||||
|
||||
let template = Template::from(template_string);
|
||||
let serial_entry = template.fill_in(&values);
|
||||
|
||||
println!("{serial_entry}");
|
||||
|
||||
|
||||
let entry = LogEntry{
|
||||
timestamp: time,
|
||||
message_id: 1,
|
||||
a: number_a,
|
||||
b: number_b,
|
||||
txt_short: txt_short_stack,
|
||||
txt_long: txt_long_stack,
|
||||
};
|
||||
|
||||
|
||||
let mut buffer = BUFFER_ACCESS.lock().unwrap();
|
||||
buffer.push(entry);
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn within_limit() {
|
||||
let test = "12345678";
|
||||
|
||||
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(test, &mut txt_short_stack);
|
||||
limit_length(test, &mut txt_long_stack);
|
||||
|
||||
assert_eq!(txt_short_stack.as_str(), test);
|
||||
assert_eq!(txt_long_stack.as_str(), test);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#[derive(IntoStaticStr)]
|
||||
pub enum LogMessage {
|
||||
#[strum(serialize = "Reset due to {{txt_long}} requires rtc clear {{a}} and force config mode {{b}}")]
|
||||
reset_reason,
|
||||
#[strum(serialize = "Current restart to conf mode {{a}}")]
|
||||
restart_to_config,
|
||||
#[strum(serialize = "Current low voltage detection is {{a}}")]
|
||||
low_voltage,
|
||||
#[strum(serialize = "Error communicating with battery!! {{txt_long}}")]
|
||||
battery_communication_error,
|
||||
#[strum(serialize = "Tank sensor raw {{a}} percent {{b}}")]
|
||||
sensor_tank_raw,
|
||||
#[strum(serialize = "raw measure unscaled {{a}} hz {{b}}, plant {{txt_short}} sensor {{txt_long}}")]
|
||||
raw_measure,
|
||||
#[strum(serialize = "IP info: {{txt_long}}")]
|
||||
wifi_info,
|
||||
#[strum(serialize = "Plant:{{txt_short}} a:{{a}} b:{{b}}")]
|
||||
test_sensor,
|
||||
#[strum(serialize = "Stay alive topic is {{txt_long}}")]
|
||||
stay_alive,
|
||||
#[strum(serialize = "Connecting mqtt {{txt_short}} with id {{txt_long}}")]
|
||||
mqtt_info,
|
||||
#[strum(serialize = "Received stay alive with value {{txt_short}}")]
|
||||
mqtt_stay_alive_rec,
|
||||
#[strum(serialize = "Unknown topic recieved {{txt_long}}")]
|
||||
unknown_topic,
|
||||
}
|
Reference in New Issue
Block a user