aquarium_control/sensors/atlas_scientific_channels.rs
1/* Copyright 2025 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*/
9use crate::launch::channels::{AquaChannelError, AquaReceiver, AquaSender};
10use crate::sensors::i2c_interface::{I2cRequest, I2cResult};
11use crate::utilities::channel_content::InternalCommand;
12use std::fmt;
13
14/// Struct collects the channels for the communication between AtlasScientific and other components.
15pub struct AtlasScientificChannels {
16 /// Sender part of the channel for communication to I2CInterface
17 #[allow(unused)] // used in conditionally compiled code
18 pub tx_atlas_scientific_to_i2c_interface: AquaSender<I2cRequest>,
19
20 /// Receiver part of the channel for communication from I2CInterface
21 #[allow(unused)] // used in conditionally compiled code
22 pub rx_atlas_scientific_from_i2c_interface: AquaReceiver<I2cResult>,
23
24 /// Sender part of the channel for communication to the signal handler
25 #[allow(unused)] // used in conditionally compiled code
26 pub tx_atlas_scientific_to_signal_handler: AquaSender<bool>,
27
28 /// Receiver part of the channel for communication from the signal handler
29 #[allow(unused)] // used in conditionally compiled code
30 pub rx_atlas_scientific_from_signal_handler: AquaReceiver<InternalCommand>,
31}
32
33impl AtlasScientificChannels {
34 /// Sends a request to the I2C interface.
35 #[allow(unused)] // used in conditionally compiled code
36 pub fn send_to_i2c_interface(&mut self, request: I2cRequest) -> Result<(), AquaChannelError> {
37 self.tx_atlas_scientific_to_i2c_interface.send(request)
38 }
39
40 /// Receives a result from the I2C interface.
41 #[allow(unused)] // used in conditionally compiled code
42 pub fn receive_from_i2c_interface(&mut self) -> Result<I2cResult, AquaChannelError> {
43 self.rx_atlas_scientific_from_i2c_interface.try_recv()
44 }
45
46 /// Sends a request to the I2C interface.
47 pub fn send_to_signal_handler(&mut self, status: bool) -> Result<(), AquaChannelError> {
48 self.tx_atlas_scientific_to_signal_handler.send(status)
49 }
50}
51
52#[cfg(feature = "debug_channels")]
53impl fmt::Display for AtlasScientificChannels {
54 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55 writeln!(f, "=== AtlasScientificChannels ===")?;
56 writeln!(
57 f,
58 "tx_atlas_scientific_to_i2c_interface: {}",
59 self.tx_atlas_scientific_to_i2c_interface.count
60 )?;
61 writeln!(
62 f,
63 "rx_atlas_scientific_from_i2c_interface: {}",
64 self.rx_atlas_scientific_from_i2c_interface.count
65 )?;
66 writeln!(
67 f,
68 "tx_atlas_scientific_to_signal_handler: {}",
69 self.tx_atlas_scientific_to_signal_handler.count
70 )?;
71 write!(
72 f,
73 "rx_atlas_scientific_from_signal_handler: {}",
74 self.rx_atlas_scientific_from_signal_handler.count
75 )
76 }
77}
78
79#[cfg(not(feature = "debug_channels"))]
80impl fmt::Display for AtlasScientificChannels {
81 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
82 write!(
83 f,
84 "Channel counters are not active. Use --features \"debug_channels\" to enable them."
85 )
86 }
87}