Skip to content

Commit

Permalink
Refactor software and component identification handling in main.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
yorickdewid committed May 30, 2024
1 parent d099d00 commit 191f06d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 24 deletions.
46 changes: 25 additions & 21 deletions hcu/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,31 +494,35 @@ mod app {

match pgn {
PGN::SoftwareIdentification => {
let id = IdBuilder::from_pgn(PGN::SoftwareIdentification)
.sa(config.j1939_address)
.build();

let frame = FrameBuilder::new(id)
.copy_from_slice(&[
0x01,
crate::PKG_VERSION_MAJOR.parse::<u8>().unwrap(),
crate::PKG_VERSION_MINOR.parse::<u8>().unwrap(),
crate::PKG_VERSION_PATCH.parse::<u8>().unwrap(),
FIELD_DELIMITER,
])
.build();

ctx.shared.canbus1.lock(|canbus1| canbus1.send(frame));
ctx.shared.canbus1.lock(|canbus1| {
let id = IdBuilder::from_pgn(PGN::SoftwareIdentification)
.sa(config.j1939_address)
.build();

let frame = FrameBuilder::new(id)
.copy_from_slice(&[
0x01,
crate::PKG_VERSION_MAJOR.parse::<u8>().unwrap(),
crate::PKG_VERSION_MINOR.parse::<u8>().unwrap(),
crate::PKG_VERSION_PATCH.parse::<u8>().unwrap(),
FIELD_DELIMITER,
])
.build();

canbus1.send(frame)
});
}
PGN::ComponentIdentification => {
let id = IdBuilder::from_pgn(PGN::ComponentIdentification)
.sa(config.j1939_address)
.build();
ctx.shared.canbus1.lock(|canbus1| {
let id = IdBuilder::from_pgn(PGN::ComponentIdentification)
.sa(config.j1939_address)
.build();

// TODO: Get the serial number from the EEPROM
let frame = FrameBuilder::new(id).build();
// TODO: Get the serial number from the EEPROM
let frame = FrameBuilder::new(id).build();

ctx.shared.canbus1.lock(|canbus1| canbus1.send(frame));
canbus1.send(frame)
});
}
PGN::AddressClaimed => {
ctx.shared.canbus1.lock(|canbus1| {
Expand Down
16 changes: 13 additions & 3 deletions vecraft/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ pub enum State {
}

impl State {
/// Converts the state to a byte value.
///
/// # Returns
///
/// The byte value corresponding to the state.
pub fn as_byte(&self) -> u8 {
match self {
State::Nominal => 0x14,
Expand All @@ -22,6 +27,11 @@ impl State {
}
}

/// Converts the state to an LED color.
///
/// # Returns
///
/// The LED color corresponding to the state.
pub fn as_led(&self) -> crate::led::Color {
match self {
State::Nominal => crate::led::GREEN,
Expand Down Expand Up @@ -100,14 +110,14 @@ impl System {

/// Get the current state of the system.
pub fn state(&self) -> State {
if self.ident {
State::Ident
} else if self.bus_error {
if self.bus_error {
State::FaultyBusError
} else if self.configuration_error {
State::ConfigurationError
} else if self.application_specific {
State::ApplicationSpecific
} else if self.ident {
State::Ident
} else {
State::Nominal
}
Expand Down

0 comments on commit 191f06d

Please sign in to comment.