Represent valve states as array of bools

main
rahix 1 year ago
parent 767bbb1c56
commit 9cae0cbaa3

@ -26,8 +26,8 @@ pub enum TesterStatus {
pub struct TesterIo {
peripheral: dp::PeripheralHandle,
valve_states: u8,
tester_status: TesterStatus,
valve_states: [bool; 8],
}
impl TesterIo {
@ -76,20 +76,23 @@ impl TesterIo {
Self {
peripheral: handle,
valve_states: 0,
valve_states: [false; 8],
tester_status: TesterStatus::Ready,
}
}
pub fn valve_states(&self) -> u8 {
self.valve_states
pub fn valve_states(&self) -> &[bool] {
&self.valve_states
}
pub fn update(&mut self, dp_master: &mut dp::DpMaster, events: &dp::DpEvents) {
let peripheral = dp_master.get_mut(self.peripheral);
if peripheral.is_running() && events.cycle_completed {
// Die 8 Eingänge der ET200B werden auf die 8 Bits für die Ventile gemapped.
self.valve_states = peripheral.pi_i()[0];
let pi_i = peripheral.pi_i();
for (i, valve) in self.valve_states.iter_mut().enumerate() {
*valve = pi_i[i / 8] & (1 << (i % 8)) != 0;
}
// Ausgänge:
// - Bit 0: Leuchtmelder "Test-Station bereit"

@ -13,7 +13,7 @@ const VALVE_TERMINAL_IDENT_NUMBER: u16 = 0x0a35;
pub struct ValveTerminal {
peripheral: dp::PeripheralHandle,
address: profirust::Address,
valve_states: u8,
valve_states: [bool; 8],
}
impl ValveTerminal {
@ -68,7 +68,7 @@ impl ValveTerminal {
Self {
peripheral: handle,
address: default_address,
valve_states: 0,
valve_states: [false; 8],
}
}
@ -89,18 +89,27 @@ impl ValveTerminal {
let peripheral = dp_master.get_mut(self.peripheral);
peripheral.reset_address(address);
peripheral.pi_q_mut().fill(0u8);
self.valve_states = 0;
self.valve_states = [false; 8];
}
}
pub fn update_valve_states(&mut self, valve_states: u8) {
self.valve_states = valve_states;
pub fn update_valve_states(&mut self, valve_states: &[bool]) {
if self.valve_states != valve_states {
self.valve_states.copy_from_slice(valve_states);
log::info!("Ventile: {:?}", valve_states);
}
}
pub fn update(&mut self, dp_master: &mut dp::DpMaster, events: &dp::DpEvents) {
let peripheral = dp_master.get_mut(self.peripheral);
if peripheral.is_running() && events.cycle_completed {
peripheral.pi_q_mut()[0] = self.valve_states;
let pi_q = peripheral.pi_q_mut();
pi_q.fill(0);
for (i, valve) in self.valve_states.iter().enumerate() {
let bit_index = i * 2;
let bit_value = if *valve { 1 } else { 0 };
pi_q[bit_index / 8] |= bit_value << (bit_index % 8);
}
}
}
}

Loading…
Cancel
Save