Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Format

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

env:
CARGO_TERM_COLOR: always

jobs:
format:
permissions:
# Give the default GITHUB_TOKEN write permission to commit and push the
# added or changed files to the repository.
contents: write

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@nightly
with:
components: rustfmt
- name: Run cargo fmt
run: cargo fmt
# Commit changes
- uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: format
16 changes: 4 additions & 12 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,11 @@ jobs:

steps:
- uses: actions/checkout@v4
- name: Install nightly rust
run: rustup toolchain install nightly
- name: Make nightly default
run: rustup default nightly
- name: Install build target
run: rustup target add thumbv7em-none-eabi
- name: Print installed toolchains
run: rustup show
- uses: dtolnay/rust-toolchain@nightly
with:
targets: aarch64-apple-darwin, thumbv7em-none-eabi
components: rust-src
- name: Build
run: cargo build --release --target thumbv7em-none-eabi
- name: Install test target
run: rustup target add aarch64-apple-darwin
- name: Add rust src
run: rustup component add rust-src --toolchain nightly-x86_64-unknown-linux-gnu
- name: Run tests
run: cargo test -Zbuild-std --target x86_64-unknown-linux-gnu
55 changes: 26 additions & 29 deletions shared/src/communication/eps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,31 @@ impl EpsCommand {
pub fn from_bytes(bytes: &[u8]) -> Result<EpsCommand, CommandParseError> {
let mut words = bytes.split(|b| b == b";".iter().next().unwrap());
match words.next() {
Some(word) => {
match word {
b"pwe" => {
let arg = words.next().ok_or(CommandParseError::IncompleteArgs)?;
let rail_num = core::str::from_utf8(arg)?.parse::<u8>()?;
Ok(EpsCommand::EnablePowerRail(rail_num))
},
b"pwd" => {
let arg = words.next().ok_or(CommandParseError::IncompleteArgs)?;
let rail_num = core::str::from_utf8(arg)?.parse::<u8>()?;
Ok(EpsCommand::DisablePowerRail(rail_num))
},
b"soh" => return Ok(Self::StateOfHealthReq),
b"gbv" => {
let arg = words.next().ok_or(CommandParseError::IncompleteArgs)?;
let rail_num = core::str::from_utf8(arg)?.parse::<u8>()?;
Ok(EpsCommand::GetBatteryVoltage(rail_num))
},
b"gprs" => {
let arg = words.next().ok_or(CommandParseError::IncompleteArgs)?;
let rail_num = core::str::from_utf8(arg)?.parse::<u8>()?;
Ok(EpsCommand::GetPowerRailState(rail_num))
},
_ => Err(CommandParseError::UnknownCommand)
Some(word) => match word {
b"pwe" => {
let arg = words.next().ok_or(CommandParseError::IncompleteArgs)?;
let rail_num = core::str::from_utf8(arg)?.parse::<u8>()?;
Ok(EpsCommand::EnablePowerRail(rail_num))
}
b"pwd" => {
let arg = words.next().ok_or(CommandParseError::IncompleteArgs)?;
let rail_num = core::str::from_utf8(arg)?.parse::<u8>()?;
Ok(EpsCommand::DisablePowerRail(rail_num))
}
b"soh" => return Ok(Self::StateOfHealthReq),
b"gbv" => {
let arg = words.next().ok_or(CommandParseError::IncompleteArgs)?;
let rail_num = core::str::from_utf8(arg)?.parse::<u8>()?;
Ok(EpsCommand::GetBatteryVoltage(rail_num))
}
b"gprs" => {
let arg = words.next().ok_or(CommandParseError::IncompleteArgs)?;
let rail_num = core::str::from_utf8(arg)?.parse::<u8>()?;
Ok(EpsCommand::GetPowerRailState(rail_num))
}
_ => Err(CommandParseError::UnknownCommand),
},
None => Err(CommandParseError::EmptyMessage)
None => Err(CommandParseError::EmptyMessage),
}
}
}
Expand All @@ -57,7 +55,7 @@ pub enum CommandParseError {
#[error("ParseIntError {0}")]
ParseIntError(#[from] ParseIntError),
#[error("Utf8Error {0}")]
Utf8Error(#[from] Utf8Error)
Utf8Error(#[from] Utf8Error),
}

impl CommandParseError {
Expand All @@ -68,7 +66,7 @@ impl CommandParseError {
UnknownCommand => b"err;501",
IncompleteArgs => b"err;502",
ParseIntError(_) => b"err;503",
Utf8Error(_) => b"err;504"
Utf8Error(_) => b"err;504",
}
}
}
Expand Down Expand Up @@ -104,5 +102,4 @@ mod tests {
let error = core::str::from_utf8(&[0xC0]).err().unwrap();
assert_eq!(b"err;504", Utf8Error(error).as_bytes());
}

}
}
2 changes: 1 addition & 1 deletion shared/src/communication/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod eps;
pub mod eps;
2 changes: 1 addition & 1 deletion shared/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#![no_std]

pub mod communication;
pub mod communication;