aquarium_control/relays/device_relay_logic.rs
1/* Copyright 2024 Uwe Martin
2
3Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
5The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
7THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8*/
9#[cfg(all(target_os = "linux", feature = "target_hw"))]
10use crate::relays::relay_error::RelayError;
11#[cfg(all(target_os = "linux", feature = "target_hw"))]
12use crate::utilities::channel_content::{AquariumDevice, InternalCommand};
13
14#[cfg(all(target_os = "linux", feature = "target_hw"))]
15/// An enum to represent the physical logic of a relay.
16enum RelayLogic {
17 NormallyOpen,
18 NormallyClosed,
19}
20
21#[cfg(all(target_os = "linux", feature = "target_hw"))]
22/// Determines the target GPIO state based on a command and the relay's physical logic.
23///
24/// # Arguments
25/// * `command` - The `InternalCommand` to interpret.
26/// * `logic` - The `RelayLogic` of the device being controlled.
27///
28/// # Returns
29/// A `Result` containing the target GPIO state (`true` for HIGH, `false` for LOW).
30///
31/// # Errors
32/// Propagates errors for irrelevant or unsupported commands.
33fn get_gpio_state(command: &InternalCommand, logic: RelayLogic) -> Result<bool, RelayError> {
34 match command {
35 InternalCommand::SwitchOn(_) => match logic {
36 RelayLogic::NormallyOpen => Ok(true), // ON is HIGH
37 RelayLogic::NormallyClosed => Ok(false), // ON is LOW
38 },
39 InternalCommand::SwitchOff(_) => match logic {
40 RelayLogic::NormallyOpen => Ok(false), // OFF is LOW
41 RelayLogic::NormallyClosed => Ok(true), // OFF is HIGH
42 },
43 InternalCommand::Pulse(d, _) => Err(RelayError::PulseNotAllowedForDevice(
44 module_path!().to_string(),
45 d.clone(),
46 )),
47 _ => Err(RelayError::IrrelevantCommand(
48 module_path!().to_string(),
49 command.clone(),
50 )),
51 }
52}
53
54#[cfg(all(target_os = "linux", feature = "target_hw"))]
55/// Determines the appropriate GPIO pin state (`true` for low, `false` for high)
56/// required to actuate a specific aquarium device based on its configured relay logic.
57///
58/// This function serves as the central mapping for GPIO control. It takes a high-level
59/// `InternalCommand` (like `SwitchOn` or `SwitchOff`) and an `AquariumDevice`
60/// and determines the correct boolean state for the GPIO pin, accounting for whether
61/// the connected relay is normally open or normally closed.
62///
63/// # Arguments
64/// * `internal_command` - The `InternalCommand` (e.g., `SwitchOn(device)`)
65/// that indicates the desired action.
66/// * `device` - The `AquariumDevice` for which the GPIO state is to be determined.
67///
68/// # Returns
69/// A `Result` containing a boolean that represents the target GPIO pin state.
70/// - `Ok(true)`: The GPIO pin should be set to `high`.
71/// - `Ok(false)`: The GPIO pin should be set to `low`.
72///
73/// # Errors
74/// This function will return a `RelayError` if the command is not a valid actuation command:
75/// - `RelayError::PulseNotAllowedForDevice`: If a `Pulse` command is given, as GPIO-controlled
76/// devices in this design do not support timed pulses.
77/// - `RelayError::IrrelevantCommand`: For any other `InternalCommand` that is not
78/// a recognized actuation command (e.g., `RequestSignal`).
79pub fn get_relay_command_gpio(
80 internal_command: &InternalCommand,
81 device: AquariumDevice,
82) -> Result<bool, RelayError> {
83 // First, determine the relay logic for the device. This separates the
84 // "what is it" from the "how to control it".
85 let logic = match device {
86 AquariumDevice::Skimmer
87 | AquariumDevice::MainPump1
88 | AquariumDevice::MainPump2
89 | AquariumDevice::AuxPump1
90 | AquariumDevice::AuxPump2
91 | AquariumDevice::Ventilation => RelayLogic::NormallyClosed,
92
93 AquariumDevice::Heater
94 | AquariumDevice::RefillPump
95 | AquariumDevice::Feeder
96 | AquariumDevice::PeristalticPump1
97 | AquariumDevice::PeristalticPump2
98 | AquariumDevice::PeristalticPump3
99 | AquariumDevice::PeristalticPump4 => RelayLogic::NormallyOpen,
100 };
101
102 // Then, get the GPIO state based on that single, unified logic function.
103 get_gpio_state(internal_command, logic)
104}