Skip to content

Commit

Permalink
Make some functions more readable by using mutable references
Browse files Browse the repository at this point in the history
  • Loading branch information
punkto committed Aug 10, 2024
1 parent 068a683 commit dc94f64
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 48 deletions.
30 changes: 15 additions & 15 deletions apps/line_follower/src/fsm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub enum FSMState {
}

pub trait Runnable {
fn run(&self, status: LineFollowerStatus) -> (FSMEvent, LineFollowerStatus);
fn run(&self, status: &mut LineFollowerStatus) -> FSMEvent;
}

#[derive(Clone, Copy)]
Expand All @@ -27,31 +27,31 @@ impl FSMState {
pub fn next(
self,
event: FSMEvent,
mut status: LineFollowerStatus,
) -> (FSMState, LineFollowerStatus) {
status: &mut LineFollowerStatus,
) -> FSMState {
match (self, event) {
(FSMState::Idle, FSMEvent::Button1Pressed) => (FSMState::HardwareCheck, status),
(FSMState::Idle, FSMEvent::Button2Pressed) => (FSMState::Calibration, status),
(FSMState::Idle, FSMEvent::BatteryIsLow) => (FSMState::BatteryLow, status),
(FSMState::Idle, FSMEvent::Button1Pressed) => FSMState::HardwareCheck,
(FSMState::Idle, FSMEvent::Button2Pressed) => FSMState::Calibration,
(FSMState::Idle, FSMEvent::BatteryIsLow) => FSMState::BatteryLow,

(FSMState::HardwareCheck, FSMEvent::NothingHappend) => (FSMState::Idle, status),
(FSMState::HardwareCheck, FSMEvent::BatteryIsLow) => (FSMState::BatteryLow, status),
(FSMState::HardwareCheck, FSMEvent::NothingHappend) => FSMState::Idle,
(FSMState::HardwareCheck, FSMEvent::BatteryIsLow) => FSMState::BatteryLow,

(FSMState::Calibration, FSMEvent::Button1Pressed) => (FSMState::LineFollowing, status),
(FSMState::Calibration, FSMEvent::Button2Pressed) => (FSMState::Idle, status),
(FSMState::Calibration, FSMEvent::BatteryIsLow) => (FSMState::BatteryLow, status),
(FSMState::Calibration, FSMEvent::Button1Pressed) => FSMState::LineFollowing,
(FSMState::Calibration, FSMEvent::Button2Pressed) => FSMState::Idle,
(FSMState::Calibration, FSMEvent::BatteryIsLow) => FSMState::BatteryLow,

(FSMState::LineFollowing, FSMEvent::Button2Pressed) => (FSMState::Idle, status),
(FSMState::LineFollowing, FSMEvent::BatteryIsLow) => (FSMState::BatteryLow, status),
(FSMState::LineFollowing, FSMEvent::Button2Pressed) => FSMState::Idle,
(FSMState::LineFollowing, FSMEvent::BatteryIsLow) => FSMState::BatteryLow,

(_s, _e) => {
Logger::new(&mut status.board.serial.tx).log("default to idle state\r\n");
(FSMState::Idle, status)
FSMState::Idle
}
}
}

pub fn run<'a>(&self, mut status: LineFollowerStatus) -> (FSMEvent, LineFollowerStatus) {
pub fn run<'a>(&self, status: &mut LineFollowerStatus) -> FSMEvent {
let mut logger = Logger::new(&mut status.board.serial.tx);
match *self {
FSMState::Idle {} => fsm_states::idle::run(status),
Expand Down
4 changes: 2 additions & 2 deletions apps/line_follower/src/fsm_states/battery_low.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::line_follower_status::LineFollowerStatus;

use logging::Logger;

pub fn run(mut status: LineFollowerStatus) -> (FSMEvent, LineFollowerStatus) {
pub fn run(status: & mut LineFollowerStatus) -> FSMEvent {
let mut logger = Logger::new(&mut status.board.serial.tx);
logger.log("Battery low state\r\n");

Expand All @@ -33,7 +33,7 @@ pub fn run(mut status: LineFollowerStatus) -> (FSMEvent, LineFollowerStatus) {

if !status.board.battery_sensor.is_battery_low() {
logger.log("Battery is no longer low\r\n");
return (FSMEvent::NothingHappend, status);
return FSMEvent::NothingHappend;
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions apps/line_follower/src/fsm_states/calibration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::line_follower_status::LineFollowerStatus;

use logging::Logger;

pub fn run(mut status: LineFollowerStatus) -> (FSMEvent, LineFollowerStatus) {
pub fn run(status: & mut LineFollowerStatus) -> FSMEvent {
let mut logger = Logger::new(&mut status.board.serial.tx);
logger.log("Calibration state\r\n");

Expand All @@ -39,14 +39,14 @@ pub fn run(mut status: LineFollowerStatus) -> (FSMEvent, LineFollowerStatus) {
}
if status.board.btn_2.is_pressed() {
logger.log("Exit calibration\r\n");
return (FSMEvent::Button2Pressed, status);
return FSMEvent::Button2Pressed;
}
if let Ok(serial_input) = status.board.serial.rx.read() {
match serial_input {
b'1' => break,
b'2' => {
logger.log("Exit calibration\r\n");
return (FSMEvent::Button2Pressed, status);
return FSMEvent::Button2Pressed;
}
_ => {}
}
Expand All @@ -67,16 +67,16 @@ pub fn run(mut status: LineFollowerStatus) -> (FSMEvent, LineFollowerStatus) {
logger.log("Press button 2 to go back to idle\r\n");
loop {
if status.board.btn_1.is_pressed() {
return (FSMEvent::Button1Pressed, status);
return FSMEvent::Button1Pressed;
}
if status.board.btn_2.is_pressed() {
logger.log("Exit calibration\r\n");
return (FSMEvent::Button2Pressed, status);
return FSMEvent::Button2Pressed;
}
if let Ok(serial_input) = status.board.serial.rx.read() {
match serial_input {
b'1' => return (FSMEvent::Button1Pressed, status),
b'2' => return (FSMEvent::Button2Pressed, status),
b'1' => return FSMEvent::Button1Pressed,
b'2' => return FSMEvent::Button2Pressed,
_ => {}
}
}
Expand Down
6 changes: 3 additions & 3 deletions apps/line_follower/src/fsm_states/hardware_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::line_follower_status::LineFollowerStatus;

use logging::Logger;

pub fn run(mut status: LineFollowerStatus) -> (FSMEvent, LineFollowerStatus) {
pub fn run(status: & mut LineFollowerStatus) -> FSMEvent {
let mut logger = Logger::new(&mut status.board.serial.tx);
logger.log("Hardware check state\r\n");

Expand Down Expand Up @@ -50,9 +50,9 @@ pub fn run(mut status: LineFollowerStatus) -> (FSMEvent, LineFollowerStatus) {

if status.board.battery_sensor.is_battery_low() {
logger.log("Battery is low\r\n");
(FSMEvent::BatteryIsLow, status)
FSMEvent::BatteryIsLow
} else {
(FSMEvent::NothingHappend, status)
FSMEvent::NothingHappend
}
}

Expand Down
19 changes: 9 additions & 10 deletions apps/line_follower/src/fsm_states/idle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,37 @@ use crate::line_follower_status::LineFollowerStatus;

use logging::Logger;

pub fn run(mut status: LineFollowerStatus) -> (FSMEvent, LineFollowerStatus) {
pub fn run(status: &mut LineFollowerStatus) -> FSMEvent {
let mut logger = Logger::new(&mut status.board.serial.tx);
logger.log("Idle state\r\n");
status.board.led_d1.set_high();
status.board.led_d2.set_low();

logger = print_menu(logger);
print_menu(&mut logger);

loop {
if status.board.btn_1.is_pressed() {
return (FSMEvent::Button1Pressed, status);
return FSMEvent::Button1Pressed;
}

if status.board.btn_2.is_pressed() {
return (FSMEvent::Button2Pressed, status);
return FSMEvent::Button2Pressed;
}

if status.board.battery_sensor.is_battery_low() {
return (FSMEvent::BatteryIsLow, status);
return FSMEvent::BatteryIsLow;
}

if let Ok(byte) = status.board.serial.rx.read() {
match byte {
b'1' => {
return (FSMEvent::Button1Pressed, status);
return FSMEvent::Button1Pressed;
}
b'2' => {
return (FSMEvent::Button2Pressed, status);
return FSMEvent::Button2Pressed;
}
b'l' => {
return (FSMEvent::BatteryIsLow, status);
return FSMEvent::BatteryIsLow;
}
_ => {
logger.log("Invalid input\r\n");
Expand All @@ -62,13 +62,12 @@ pub fn run(mut status: LineFollowerStatus) -> (FSMEvent, LineFollowerStatus) {
}
}

fn print_menu(mut logger: Logger) -> Logger {
fn print_menu(logger: &mut Logger) {
logger.log("Menu:\r\n");
logger.log(" press button 1 to go to hardware check state\r\n");
logger.log(" press button 2 to go to calibration state\r\n");
logger.log(" you can also use the following keys:\r\n");
logger.log(" press '1' to go to hardware check state\r\n");
logger.log(" press '2' to go to calibration state\r\n");
logger.log(" press 'l' to go to battery low state\r\n");
logger
}
10 changes: 5 additions & 5 deletions apps/line_follower/src/fsm_states/line_following.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::line_follower_status::LineFollowerStatus;

use logging::Logger;

pub fn run(mut status: LineFollowerStatus) -> (FSMEvent, LineFollowerStatus) {
pub fn run( status: & mut LineFollowerStatus) -> FSMEvent {
let mut logger = Logger::new(&mut status.board.serial.tx);
logger.log("Line following state\r\n");

Expand All @@ -28,15 +28,15 @@ pub fn run(mut status: LineFollowerStatus) -> (FSMEvent, LineFollowerStatus) {
status.board.delay.delay_ms(50u32);

if status.board.btn_2.is_pressed() {
return (FSMEvent::Button2Pressed, status);
return FSMEvent::Button2Pressed;
}
if status.board.battery_sensor.is_battery_low() {
return (FSMEvent::BatteryIsLow, status);
return FSMEvent::BatteryIsLow;
}
if let Ok(serial_input) = status.board.serial.rx.read() {
match serial_input {
b'2' => {
return (FSMEvent::Button2Pressed, status);
return FSMEvent::Button2Pressed;
}
_ => {}
}
Expand All @@ -45,5 +45,5 @@ pub fn run(mut status: LineFollowerStatus) -> (FSMEvent, LineFollowerStatus) {

logger.log("End of line following\r\n");

(FSMEvent::NothingHappend, status)
FSMEvent::NothingHappend
}
12 changes: 6 additions & 6 deletions apps/line_follower/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ fn main() -> ! {
let mut fsm_event;

loop {
(fsm_event, line_follower_status) = fsm_state.run(line_follower_status);
fsm_event = fsm_state.run(&mut line_follower_status);
log_event(&mut line_follower_status.board, fsm_event);
(fsm_state, line_follower_status) = fsm_state.next(fsm_event, line_follower_status);
fsm_state = fsm_state.next(fsm_event, &mut line_follower_status);
}
}

fn log_event(board: &mut Mightybuga_BSC, event: FSMEvent) {
let mut logger = Logger::new(&mut board.serial.tx);
match event {
FSMEvent::NothingHappend => logger.log("Nothing happened\r\n"),
FSMEvent::Button1Pressed => logger.log("Button 1 pressed\r\n"),
FSMEvent::Button2Pressed => logger.log("Button 2 pressed\r\n"),
FSMEvent::BatteryIsLow => logger.log("Battery is low\r\n"),
FSMEvent::NothingHappend => logger.log(" - Nothing happened -\r\n"),
FSMEvent::Button1Pressed => logger.log(" - Button 1 pressed -\r\n"),
FSMEvent::Button2Pressed => logger.log(" - Button 2 pressed -\r\n"),
FSMEvent::BatteryIsLow => logger.log(" - Battery is low -\r\n"),
}
}

0 comments on commit dc94f64

Please sign in to comment.