summaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: b3ddcd9fe47cf095532583a59e8fa739f528a2f4 (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//! Host-side control interface for B&K Precision 16xB Power Supplies.
//!
//! The library optimizes for the 1687B and 1688B models. This library
//! doe not support two decimal places for current on the 1685B model.
//!
//! This library has no affiliation with B&K Precision.

use std::{
    fmt::{Debug, Display},
    str::from_utf8,
};

use serialport::SerialPort;
pub use serialport::{Error as SerialError, ErrorKind as SerialErrorKind};

pub enum Error {
    UnexpectedResponse,
    Parse,
    Serial(SerialError),
}

impl From<SerialError> for Error {
    fn from(value: SerialError) -> Self {
        Self::Serial(value)
    }
}

impl From<std::io::Error> for Error {
    fn from(value: std::io::Error) -> Self {
        Self::Serial(value.into())
    }
}

impl From<std::num::ParseFloatError> for Error {
    fn from(_: std::num::ParseFloatError) -> Self {
        Self::Parse
    }
}

impl From<std::str::Utf8Error> for Error {
    fn from(_: std::str::Utf8Error) -> Self {
        Self::Parse
    }
}

pub type Result<T> = std::result::Result<T, Error>;

/// An interface to the power supply.
pub struct PowerSupply {
    port: Box<dyn SerialPort>,
}

impl PowerSupply {
    pub fn open(path: impl AsRef<str>) -> Result<Self> {
        serialport::new(path.as_ref(), 9600)
            .data_bits(serialport::DataBits::Eight)
            .parity(serialport::Parity::None)
            .stop_bits(serialport::StopBits::One)
            .flow_control(serialport::FlowControl::None)
            .timeout(std::time::Duration::from_millis(100))
            .open()
            .map(|port| PowerSupply { port })
            .map_err(From::from)
    }

    fn parse_volts(&mut self) -> Result<f64> {
        let mut buff = [0u8; 3];
        self.port.read_exact(&mut buff)?;
        let volts: f64 = from_utf8(&buff)?.parse()?;
        Ok(volts / 10.0)
    }

    fn parse_current(&mut self) -> Result<f64> {
        let mut buff = [0u8; 3];
        self.port.read_exact(&mut buff)?;
        let current: f64 = from_utf8(&buff)?.parse()?;
        Ok(current / 10.0)
    }

    fn parse_cr(&mut self) -> Result<()> {
        let mut buff = [0; 1];
        self.port.read_exact(&mut buff)?;
        if buff[0] == b'\r' {
            Ok(())
        } else {
            Err(Error::UnexpectedResponse)
        }
    }

    fn parse_ok(&mut self) -> Result<()> {
        let mut buff = [0; 2];
        self.port.read_exact(&mut buff)?;
        if &buff == b"OK" {
            Ok(())
        } else {
            Err(Error::UnexpectedResponse)
        }
    }

    fn parse_end(&mut self) -> Result<()> {
        self.parse_ok()?;
        self.parse_cr()?;
        Ok(())
    }

    fn write_command(&mut self, cmd: &[u8]) -> Result<()> {
        self.port.write_all(cmd)?;
        self.port.flush()?;
        Ok(())
    }

    /// Set the supply output state.
    pub fn set_output(&mut self, output: Output) -> Result<()> {
        self.write_command(match output {
            Output::Off => b"SOUT1\r",
            Output::On => b"SOUT0\r",
        })?;
        self.parse_end()?;
        Ok(())
    }

    /// Read the supply's maximum voltage and current.
    pub fn max_voltage_current(&mut self) -> Result<(f64, f64)> {
        self.write_command(b"GMAX\r")?;

        let volts = self.parse_volts()?;
        let amps = self.parse_current()?;
        self.parse_end()?;

        Ok((volts, amps))
    }
}

/// The power supply output state.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Output {
    /// Supply off.
    Off,
    /// Supply on.
    On,
}

impl From<bool> for Output {
    fn from(value: bool) -> Self {
        if value {
            Self::On
        } else {
            Self::Off
        }
    }
}

/// Power supply model
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Model {
    /// BK Precision 1687B.
    ///
    /// 36V - 10A.
    Bkp1687B,
}

/// A voltage setting.
///
/// This has a 0.1V resolution across all power supply models.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Voltage(
    u32, /* Internally represented as mV; see GETD command. */
);

impl Display for Voltage {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {}
}

impl Voltage {
    pub const fn from_f64_v(volts: f64) -> Voltage {
        Voltage((volts * 1000.0f64) as u32)
    }

    pub const fn from_f32_v(volts: f32) -> Voltage {
        Voltage((volts * 1000.0f32) as u32)
    }

    pub const fn from_mv(millivolts: u32) -> Voltage {
        Voltage(millivolts / 10 * 10)
    }
}

/// A current setting.
///
/// On the 1687B and 1688B models, this has
/// a 0.1A resolution. On the 1685B model, this
/// has a 0.01A resolution.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Current(
    u32, /* Internally represented as mA; see GETD command. */
);

impl Current {
    pub const fn from_f64_a(amps: f64) -> Current {
        Current((amps * 1000.0f64) as u32)
    }

    pub const fn from_f32_a(amps: f32) -> Current {
        Current((amps * 1000.0f32) as u32)
    }

    pub const fn from_ma(milliamps: u32) -> Current {
        Current(milliamps)
    }
}