main: rework reporting

This commit is contained in:
Astro 2019-09-14 02:33:56 +02:00
parent ff3a793c19
commit 5ef8d6a747
1 changed files with 19 additions and 18 deletions

View File

@ -203,9 +203,10 @@ fn main() -> ! {
]; ];
let mut last_report = get_time(); let mut last_report = get_time();
let mut next_report = get_time();
// cumulative (sum, count) // cumulative (sum, count)
let mut sample = [(0u64, 0usize); 2]; let mut sample = [(0u64, 0usize); 2];
let mut report = None; let mut report = [None; 2];
loop { loop {
// ADC input // ADC input
adc.data_ready() adc.data_ready()
@ -218,22 +219,24 @@ fn main() -> ! {
sample[usize::from(channel)].1 += 1; sample[usize::from(channel)].1 += 1;
}); });
let now = get_time(); let now = get_time();
if now >= last_report + REPORT_INTERVAL { if now >= next_report {
if now < last_report + 2 * REPORT_INTERVAL { if now < next_report + REPORT_INTERVAL {
// Try to keep interval constant // Try to keep interval constant
last_report += REPORT_INTERVAL; next_report += REPORT_INTERVAL;
} else { } else {
// Bad jitter, catch up // Bad jitter, catch up
last_report = now; next_report = now + REPORT_INTERVAL;
}
for (channel, sample) in sample.iter().enumerate() {
if sample.1 > 0 {
// TODO: calculate med instead of avg?
report[channel] = Some(sample.0 / (sample.1 as u64));
}
} }
// TODO: calculate med instead of avg?
report = Some((now, [
sample[0].0 / (sample[0].1 as u64),
sample[1].0 / (sample[1].1 as u64),
]));
for (session, _) in sessions_handles.iter_mut() { for (session, _) in sessions_handles.iter_mut() {
session.set_report_pending(); session.set_report_pending();
} }
last_report = get_time();
} }
for (session, tcp_handle) in sessions_handles.iter_mut() { for (session, tcp_handle) in sessions_handles.iter_mut() {
@ -272,15 +275,13 @@ fn main() -> ! {
} }
} }
if socket.may_send() && session.is_report_pending() { if socket.may_send() && session.is_report_pending() {
match &report { let _ = write!(socket, "t={}", last_report);
Some((time, samples)) => { for (channel, report_data) in report.iter().enumerate() {
let _ = writeln!(socket, "t={} sens0={} sens1={}\r", report_data.map(|report_data| {
time, samples[0], samples[1] let _ = write!(socket, " sens{}={:06X}", channel, report_data);
); });
}
None =>
panic!("report_pending while is there is none yet"),
} }
let _ = writeln!(socket, "");
session.mark_report_sent(); session.mark_report_sent();
} }
} }