Add debug info when motors or sensor not connected

This commit is contained in:
Max Känner 2023-02-01 17:39:38 +01:00
parent ab775e32de
commit a5dcd818b4
2 changed files with 50 additions and 8 deletions

View File

@ -21,6 +21,30 @@ pub enum Parameter {
Speed,
}
#[allow(clippy::similar_names)]
pub fn draw_debug<'a>(screen: &mut Screen, text: impl IntoIterator<Item = &'a str>, font: &Font) {
#[allow(clippy::cast_precision_loss)]
let scale = Scale {
x: screen.xres() as f32 / 10.0,
y: screen.yres() as f32 / 4.0 - 8.0,
};
let xres = i32::try_from(screen.xres()).expect("range checked");
let yres = i32::try_from(screen.yres()).expect("range checked");
let center = xres / 2;
clear(screen);
for (i, line) in text.into_iter().enumerate().take(4) {
draw_centered_text(
screen,
center,
yres * i32::try_from(i).expect("there is only place for 4 lines") / 4 + 4,
scale,
font,
line,
);
}
screen.update();
}
#[allow(clippy::similar_names)]
pub fn draw_calibration(screen: &mut Screen, parameter: &str, value: impl Display, font: &Font) {
#[allow(clippy::cast_precision_loss)]

View File

@ -2,25 +2,43 @@ mod buttons;
mod display;
use buttons::SmartEv3Buttons;
use display::{draw_calibration, draw_driving, draw_finished, draw_settings, font, Parameter};
use ev3dev_lang_rust::{motors::LargeMotor, sensors::ColorSensor, Ev3Error, Ev3Result, Screen};
use display::{
draw_calibration, draw_debug, draw_driving, draw_finished, draw_settings, font, Parameter,
};
use ev3dev_lang_rust::{
motors::LargeMotor, sensors::ColorSensor, Device, Ev3Error, Ev3Result, Screen,
};
use pid::Pid;
use rand::{thread_rng, Rng};
use rusttype::Font;
use std::{
error::Error,
iter,
thread::sleep,
time::{Duration, Instant},
};
use thiserror::Error;
fn main() -> Result<(), Box<dyn Error>> {
let motors = LargeMotor::list()?;
let [right_motor, left_motor] = &motors[..] else {println!("motors: {motors:?}"); Err("need two motors")?};
let sensor = ColorSensor::find()?;
let mut buttons = SmartEv3Buttons::new()?;
let mut screen = Screen::new()?;
fn main() {
while try_main().is_err() {
sleep(Duration::from_millis(10));
}
}
fn try_main() -> Result<(), Box<dyn Error>> {
let font = font();
let mut screen = Screen::new()?;
let motors = LargeMotor::list()?;
let [right_motor, left_motor] = &motors[..] else {
let motor_texts: Vec<_> = motors.iter().map(|motor| format!("{:?}", motor.get_driver_name())).collect();
draw_debug(&mut screen, iter::once("motors:").chain(motor_texts.iter().map(String::as_str)), &font);
Err("need two motors")?
};
let sensor = ColorSensor::find().map_err(|e| {
draw_debug(&mut screen, ["no color sensor"], &font);
e
})?;
let mut buttons = SmartEv3Buttons::new()?;
left_motor.set_polarity(LargeMotor::POLARITY_INVERSED)?;
right_motor.set_polarity(LargeMotor::POLARITY_INVERSED)?;