use super::FlashLayout;
use std::{sync::Arc, time::Duration};
#[derive(Clone)]
pub struct FlashProgress {
handler: Arc<dyn Fn(ProgressEvent)>,
}
impl FlashProgress {
pub fn new(handler: impl Fn(ProgressEvent) + 'static) -> Self {
Self {
handler: Arc::new(handler),
}
}
fn emit(&self, event: ProgressEvent) {
(self.handler)(event);
}
pub(super) fn initialized(&self, flash_layout: FlashLayout) {
self.emit(ProgressEvent::Initialized { flash_layout });
}
pub(super) fn started_erasing(&self) {
self.emit(ProgressEvent::StartedErasing);
}
pub(super) fn started_filling(&self) {
self.emit(ProgressEvent::StartedFilling);
}
pub(super) fn started_programming(&self) {
self.emit(ProgressEvent::StartedProgramming);
}
pub(super) fn page_programmed(&self, size: u32, time: Duration) {
self.emit(ProgressEvent::PageProgrammed { size, time });
}
pub(super) fn sector_erased(&self, size: u64, time: Duration) {
self.emit(ProgressEvent::SectorErased { size, time });
}
pub(super) fn page_filled(&self, size: u64, time: Duration) {
self.emit(ProgressEvent::PageFilled { size, time });
}
pub(super) fn failed_programming(&self) {
self.emit(ProgressEvent::FailedProgramming);
}
pub(super) fn finished_programming(&self) {
self.emit(ProgressEvent::FinishedProgramming);
}
pub(super) fn failed_erasing(&self) {
self.emit(ProgressEvent::FailedErasing);
}
pub(super) fn finished_erasing(&self) {
self.emit(ProgressEvent::FinishedErasing);
}
pub(super) fn failed_filling(&self) {
self.emit(ProgressEvent::FailedFilling);
}
pub(super) fn finished_filling(&self) {
self.emit(ProgressEvent::FinishedFilling);
}
#[cfg(feature = "rtt")]
pub(super) fn message(&self, message: String) {
self.emit(ProgressEvent::DiagnosticMessage { message });
}
}
#[derive(Debug)]
pub enum ProgressEvent {
Initialized {
flash_layout: FlashLayout,
},
StartedFilling,
PageFilled {
size: u64,
time: Duration,
},
FailedFilling,
FinishedFilling,
StartedErasing,
SectorErased {
size: u64,
time: Duration,
},
FailedErasing,
FinishedErasing,
StartedProgramming,
PageProgrammed {
size: u32,
time: Duration,
},
FailedProgramming,
FinishedProgramming,
DiagnosticMessage {
message: String,
},
}