1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use tokio::sync::mpsc::Sender;
use crate::brain::BrainFailure;
use crate::io::controls::{translate_get_gpio, translate_set_gpio};
use crate::python_like::control::misc_control::{ImmersionHeaterControl, WiserPowerControl};
use crate::{GPIOMode, MiscControls, PinUpdate};
use crate::{GPIOManager, SysFsGPIO};
use crate::io::gpio::GPIOError;

pub struct MiscGPIOControls {
    gpio: SysFsGPIO,
    immersion_heater_pin: usize,
    wiser_power_pin: usize,
}

impl MiscGPIOControls {
    pub fn create(immersion_heater_pin: usize, wiser_power_pin: usize, sender: Sender<PinUpdate>) -> Result<Self, GPIOError> {
        let mut gpio = SysFsGPIO::new(sender);
        gpio.setup(immersion_heater_pin, &GPIOMode::Output)?;
        gpio.setup(wiser_power_pin, &GPIOMode::Output)?;
        Ok(Self {
            gpio,
            immersion_heater_pin,
            wiser_power_pin,
        })
    }
}

impl MiscControls for MiscGPIOControls {
    fn as_ih(&mut self) -> &mut dyn ImmersionHeaterControl {
        self
    }

    fn as_wp(&mut self) -> &mut dyn WiserPowerControl {
        self
    }
}

impl ImmersionHeaterControl for MiscGPIOControls {
    fn try_set_immersion_heater(&mut self, on: bool) -> Result<(), BrainFailure> {
        translate_set_gpio(self.immersion_heater_pin, &mut self.gpio, on, "Failed to set immersion heater pin")
    }

    fn try_get_immersion_heater(&self) -> Result<bool, BrainFailure> {
        translate_get_gpio(self.immersion_heater_pin, &self.gpio, "Failed to get immersion heater pin")
    }
}

impl WiserPowerControl for MiscGPIOControls {
    // DEFAULT ON not OFF - So wrong way reported / set.

    fn try_set_wiser_power(&mut self, on: bool) -> Result<(), BrainFailure> {
        translate_set_gpio(self.wiser_power_pin, &mut self.gpio, !on, "Failed to set wiser power pin")
    }

    fn try_get_wiser_power(&mut self) -> Result<bool, BrainFailure> {
        translate_get_gpio(self.wiser_power_pin, &self.gpio, "Failed to get wiser power pin")
            .map(|b| !b)
    }
}