Compare commits
No commits in common. "4c00548646eb8e6bf2d4b6ee0b1eb9b3c3a99630" and "4587406d44c589c4cdeccd40512f0c285f42f38b" have entirely different histories.
4c00548646
...
4587406d44
|
@ -10,6 +10,7 @@ use nom::{
|
|||
error::ErrorKind,
|
||||
};
|
||||
use lexical_core as lexical;
|
||||
use super::session::ReportMode;
|
||||
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
|
@ -62,8 +63,7 @@ impl fmt::Display for Error {
|
|||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ShowCommand {
|
||||
Input,
|
||||
Reporting,
|
||||
ReportMode,
|
||||
Pid,
|
||||
}
|
||||
|
||||
|
@ -83,7 +83,7 @@ pub enum PidParameter {
|
|||
pub enum Command {
|
||||
Quit,
|
||||
Show(ShowCommand),
|
||||
Reporting(bool),
|
||||
Report(ReportMode),
|
||||
Pwm {
|
||||
width: u32,
|
||||
total: u32,
|
||||
|
@ -112,9 +112,10 @@ fn float(input: &[u8]) -> IResult<&[u8], Result<f32, lexical::Error>> {
|
|||
Ok((input, result))
|
||||
}
|
||||
|
||||
fn off_on(input: &[u8]) -> IResult<&[u8], bool> {
|
||||
alt((value(false, tag("off")),
|
||||
value(true, tag("on"))
|
||||
fn report_mode(input: &[u8]) -> IResult<&[u8], ReportMode> {
|
||||
alt((value(ReportMode::Off, tag("off")),
|
||||
value(ReportMode::Once, tag("once")),
|
||||
value(ReportMode::Continuous, tag("continuous"))
|
||||
))(input)
|
||||
}
|
||||
|
||||
|
@ -132,16 +133,13 @@ fn report(input: &[u8]) -> IResult<&[u8], Command> {
|
|||
alt((
|
||||
preceded(
|
||||
whitespace,
|
||||
// `report mode <on | off>` - Switch repoting mode
|
||||
map(off_on,
|
||||
|reporting| Command::Reporting(reporting))
|
||||
map(report_mode,
|
||||
|mode| Command::Report(mode))
|
||||
),
|
||||
// `report mode` - Show current reporting state
|
||||
|input| Ok((input, Command::Show(ShowCommand::Reporting)))
|
||||
|input| Ok((input, Command::Show(ShowCommand::ReportMode)))
|
||||
))
|
||||
)),
|
||||
// `report` - Report once
|
||||
|input| Ok((input, Command::Show(ShowCommand::Input)))
|
||||
|input| Ok((input, Command::Report(ReportMode::Once)))
|
||||
))
|
||||
)(input)
|
||||
}
|
||||
|
@ -175,6 +173,7 @@ fn pid_parameter(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
|
|||
value(PidParameter::IntegralMax, tag("integral_max"))
|
||||
))(input)?;
|
||||
let (input, _) = whitespace(input)?;
|
||||
// TODO: parse float
|
||||
let (input, value) = float(input)?;
|
||||
let result = value
|
||||
.map(|value| Command::Pid { parameter, value })
|
||||
|
|
|
@ -76,6 +76,8 @@ macro_rules! create_socket {
|
|||
)
|
||||
}
|
||||
|
||||
/// In nanoseconds
|
||||
const REPORT_INTERVAL: u64 = 100_000;
|
||||
const DEFAULT_PID_PARAMETERS: pid::Parameters = pid::Parameters {
|
||||
kp: 1.0,
|
||||
ki: 1.0,
|
||||
|
@ -86,8 +88,6 @@ const DEFAULT_PID_PARAMETERS: pid::Parameters = pid::Parameters {
|
|||
integral_max: 0xffff as f32,
|
||||
};
|
||||
|
||||
pub const CHANNELS: usize = 2;
|
||||
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
let mut stdout = hio::hstdout().unwrap();
|
||||
|
@ -204,7 +204,11 @@ fn main() -> ! {
|
|||
(Session::new(), tcp_handle7),
|
||||
];
|
||||
|
||||
let mut report = [None; CHANNELS];
|
||||
let mut last_report = get_time();
|
||||
let mut next_report = get_time();
|
||||
// cumulative (sum, count)
|
||||
let mut sample = [(0u64, 0usize); 2];
|
||||
let mut report = [None; 2];
|
||||
loop {
|
||||
// ADC input
|
||||
adc.data_ready()
|
||||
|
@ -212,14 +216,30 @@ fn main() -> ! {
|
|||
writeln!(stdout, "ADC error: {:?}", e).unwrap();
|
||||
None
|
||||
}).map(|channel| {
|
||||
let now = get_time();
|
||||
let data = adc.read_data().unwrap();
|
||||
report[usize::from(channel)] = Some((now, data));
|
||||
|
||||
for (session, _) in sessions_handles.iter_mut() {
|
||||
session.set_report_pending(channel.into());
|
||||
}
|
||||
sample[usize::from(channel)].0 += u64::from(data);
|
||||
sample[usize::from(channel)].1 += 1;
|
||||
});
|
||||
let now = get_time();
|
||||
if now >= next_report {
|
||||
if now < next_report + REPORT_INTERVAL {
|
||||
// Try to keep interval constant
|
||||
next_report += REPORT_INTERVAL;
|
||||
} else {
|
||||
// Bad jitter, catch up
|
||||
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));
|
||||
}
|
||||
}
|
||||
for (session, _) in sessions_handles.iter_mut() {
|
||||
session.set_report_pending();
|
||||
}
|
||||
last_report = get_time();
|
||||
}
|
||||
|
||||
for (session, tcp_handle) in sessions_handles.iter_mut() {
|
||||
let socket = &mut *sockets.get::<TcpSocket>(*tcp_handle);
|
||||
|
@ -240,18 +260,11 @@ fn main() -> ! {
|
|||
Ok(SessionOutput::Command(command)) => match command {
|
||||
Command::Quit =>
|
||||
socket.close(),
|
||||
Command::Reporting(reporting) => {
|
||||
let _ = writeln!(socket, "Report mode set to {}", if reporting { "on" } else { "off" });
|
||||
Command::Report(mode) => {
|
||||
let _ = writeln!(socket, "Report mode: {}", mode);
|
||||
}
|
||||
Command::Show(ShowCommand::Reporting) => {
|
||||
let _ = writeln!(socket, "Report mode: {}", if session.reporting() { "on" } else { "off" });
|
||||
}
|
||||
Command::Show(ShowCommand::Input) => {
|
||||
for (channel, report) in report.iter().enumerate() {
|
||||
report.map(|(time, data)| {
|
||||
let _ = writeln!(socket, "t={}, sens{}={}", time, channel, data);
|
||||
});
|
||||
}
|
||||
Command::Show(ShowCommand::ReportMode) => {
|
||||
let _ = writeln!(socket, "Report mode: {}", session.report_mode());
|
||||
}
|
||||
Command::Show(ShowCommand::Pid) => {
|
||||
let _ = writeln!(socket, "PID settings");
|
||||
|
@ -303,13 +316,15 @@ fn main() -> ! {
|
|||
Err(_) => {}
|
||||
}
|
||||
}
|
||||
if socket.may_send() {
|
||||
if let Some(channel) = session.is_report_pending() {
|
||||
report[channel].map(|(time, data)| {
|
||||
let _ = writeln!(socket, "t={} sens{}={:06X}", time, channel, data);
|
||||
if socket.may_send() && session.is_report_pending() {
|
||||
let _ = write!(socket, "t={}", last_report);
|
||||
for (channel, report_data) in report.iter().enumerate() {
|
||||
report_data.map(|report_data| {
|
||||
let _ = write!(socket, " sens{}={:06X}", channel, report_data);
|
||||
});
|
||||
session.mark_report_sent(channel);
|
||||
}
|
||||
let _ = writeln!(socket, "");
|
||||
session.mark_report_sent();
|
||||
}
|
||||
}
|
||||
match iface.poll(&mut sockets, Instant::from_millis((get_time() / 1000) as i64)) {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use core::ops::Deref;
|
||||
use core::fmt;
|
||||
use super::command_parser::{Command, Error as ParserError};
|
||||
use super::CHANNELS;
|
||||
|
||||
const MAX_LINE_LEN: usize = 64;
|
||||
|
||||
|
@ -54,6 +54,23 @@ impl Deref for LineResult {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum ReportMode {
|
||||
Off,
|
||||
Once,
|
||||
Continuous,
|
||||
}
|
||||
|
||||
impl fmt::Display for ReportMode {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
||||
match self {
|
||||
ReportMode::Off => "off",
|
||||
ReportMode::Once => "once",
|
||||
ReportMode::Continuous => "continuous",
|
||||
}.fmt(fmt)
|
||||
}
|
||||
}
|
||||
|
||||
pub enum SessionOutput {
|
||||
Nothing,
|
||||
Command(Command),
|
||||
|
@ -69,16 +86,16 @@ impl From<Result<Command, ParserError>> for SessionOutput {
|
|||
|
||||
pub struct Session {
|
||||
reader: LineReader,
|
||||
reporting: bool,
|
||||
report_pending: [bool; CHANNELS],
|
||||
report_mode: ReportMode,
|
||||
report_pending: bool,
|
||||
}
|
||||
|
||||
impl Session {
|
||||
pub fn new() -> Self {
|
||||
Session {
|
||||
reader: LineReader::new(),
|
||||
reporting: false,
|
||||
report_pending: [false; CHANNELS],
|
||||
report_mode: ReportMode::Off,
|
||||
report_pending: false,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -86,34 +103,30 @@ impl Session {
|
|||
self.reader.pos > 0
|
||||
}
|
||||
|
||||
pub fn reporting(&self) -> bool {
|
||||
self.reporting
|
||||
pub fn report_mode(&self) -> ReportMode {
|
||||
self.report_mode
|
||||
}
|
||||
|
||||
pub fn set_report_pending(&mut self, channel: usize) {
|
||||
if self.reporting {
|
||||
self.report_pending[channel] = true;
|
||||
pub fn set_report_pending(&mut self) {
|
||||
self.report_pending = true;
|
||||
}
|
||||
|
||||
pub fn is_report_pending(&self) -> bool {
|
||||
match self.report_mode {
|
||||
ReportMode::Off => false,
|
||||
_ => self.report_pending,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_report_pending(&self) -> Option<usize> {
|
||||
if ! self.reporting {
|
||||
None
|
||||
} else {
|
||||
self.report_pending.iter()
|
||||
.enumerate()
|
||||
.fold(None, |result, (channel, report_pending)| {
|
||||
result.or_else(|| {
|
||||
if *report_pending { Some(channel) } else { None }
|
||||
})
|
||||
})
|
||||
pub fn mark_report_sent(&mut self) {
|
||||
self.report_pending = false;
|
||||
match self.report_mode {
|
||||
ReportMode::Once =>
|
||||
self.report_mode = ReportMode::Off,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn mark_report_sent(&mut self, channel: usize) {
|
||||
self.report_pending[channel] = false;
|
||||
}
|
||||
|
||||
pub fn feed(&mut self, buf: &[u8]) -> (usize, SessionOutput) {
|
||||
let mut buf_bytes = 0;
|
||||
for (i, b) in buf.iter().enumerate() {
|
||||
|
@ -123,8 +136,8 @@ impl Session {
|
|||
Some(line) => {
|
||||
let command = Command::parse(&line);
|
||||
match command {
|
||||
Ok(Command::Reporting(reporting)) => {
|
||||
self.reporting = reporting;
|
||||
Ok(Command::Report(mode)) => {
|
||||
self.report_mode = mode;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue