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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#![deny(warnings)]
use anyhow::Context;
use std::fmt;
use std::io;
use std::str;
use std::str::FromStr;
use std::time;

pub mod cli;

#[derive(Debug, PartialEq, Clone, Copy)]
/// Current
pub struct I {
    ampere: u32,
    milli_ampere: u32,
}

#[derive(Debug, PartialEq, Clone, Copy)]
/// Voltage
pub struct V {
    volts: u32,
    milli_volts: u32,
}

#[derive(Debug, PartialEq, Copy, Clone)]
pub enum Switch {
    On,
    Off,
}

#[derive(Debug)]
pub enum Command {
    /// Enable/Disable Power
    Power(Switch),
    /// Enable/Disable Beep
    Beep(Switch),
    /// Enable/Disable over voltage protection
    Ovp(Switch),
    /// Enable/Disable over current protection
    Ocp(Switch),
    /// Store current pannel settings to memory
    Save(u32),
    /// Load stored setting into pannel
    Load(u32),
    /// Sets the voltage
    Voltage(V),
    /// Sets the current
    Current(I),
}

#[derive(Debug, PartialEq)]
pub struct Flags {
    flags: u8,
}

#[derive(Debug, PartialEq)]
pub enum Channel {
    _1,
    _2,
}

#[derive(Debug, PartialEq)]
pub enum Lock {
    Locked,
    Unlocked,
}

#[derive(Debug, PartialEq)]
pub enum Mode {
    CC,
    CV,
}

pub struct Status {
    flags: Flags,
    voltage: V,
    current: I,
}

impl I {
    pub fn new(ampere: u32, milli_ampere: u32) -> Self {
        I {
            ampere,
            milli_ampere,
        }
    }
}

impl V {
    pub fn new(volts: u32, milli_volts: u32) -> Self {
        V { volts, milli_volts }
    }
}

impl std::str::FromStr for Switch {
    type Err = anyhow::Error;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_ref() {
            "on" => Ok(Switch::On),
            "off" => Ok(Switch::Off),
            _ => Err(anyhow::anyhow!("Value must be either 'on' or 'off'")),
        }
    }
}

impl fmt::Display for V {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}.{} V", self.volts, self.milli_volts)
    }
}

impl fmt::Display for I {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}.{} A", self.ampere, self.milli_ampere)
    }
}

impl fmt::Display for Status {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Voltage: {}, Current: {}, Channel1: {:?}, Channel2: {:?} Lock: {:?}, Beep: {:?}, Output: {:?}",
            self.voltage,
            self.current,
            self.flags.mode(Channel::_1),
            self.flags.mode(Channel::_2),
            self.flags.lock(),
            self.flags.beep(),
            self.flags.output(),
        )
    }
}

impl std::str::FromStr for V {
    type Err = std::num::ParseIntError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let normalized = s.trim().to_lowercase();
        let parts: Vec<&str> = normalized.split('.').collect();
        let v = parts[0].parse::<u32>()?;
        let mv = parts[1].parse::<u32>()?;
        Ok(V::new(v, mv))
    }
}

impl std::str::FromStr for I {
    type Err = std::num::ParseIntError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let normalized = s.trim().to_lowercase();
        let parts: Vec<&str> = normalized.split('.').collect();
        let a = parts[0].parse::<u32>()?;
        let ma = parts[1].parse::<u32>()?;
        Ok(I::new(a, ma))
    }
}

impl Status {
    pub fn new(flags: Flags, voltage: V, current: I) -> Self {
        Status {
            flags,
            voltage,
            current,
        }
    }
}

impl Flags {
    pub fn new(flags: u8) -> Self {
        Flags { flags }
    }

    pub fn mode(&self, channel: Channel) -> Mode {
        let bitmask = match channel {
            Channel::_1 => 1,
            Channel::_2 => 2,
        };
        if (self.flags & bitmask) == 0 {
            Mode::CC
        } else {
            Mode::CV
        }
    }

    pub fn beep(&self) -> Switch {
        let bitmask = 16;
        if (self.flags & bitmask) != 0 {
            Switch::On
        } else {
            Switch::Off
        }
    }

    pub fn lock(&self) -> Lock {
        let bitmask = 32;
        if (self.flags & bitmask) != 0 {
            Lock::Locked
        } else {
            Lock::Unlocked
        }
    }

    pub fn output(&self) -> Switch {
        let bitmask = 64;
        if (self.flags & bitmask) != 0 {
            Switch::On
        } else {
            Switch::Off
        }
    }
}

pub fn find_serial_port() -> anyhow::Result<Box<dyn serialport::SerialPort>> {
    let serial_devices: Vec<serialport::SerialPortInfo> = serialport::available_ports()
        .unwrap()
        .into_iter()
        .filter(|info| match &info.port_type {
            serialport::SerialPortType::UsbPort(usb) => usb.vid == 1046,
            _ => false,
        })
        .collect();

    match serial_devices.len() {
        0 => Err(anyhow::anyhow!("No Power Supply Found!")),
        1 => {
            let mut serial = serialport::open(&serial_devices[0].port_name).unwrap();
            serial.set_timeout(time::Duration::from_millis(50)).unwrap();
            serial.set_baud_rate(9600).unwrap();
            serial.set_parity(serialport::Parity::None).unwrap();
            serial.set_stop_bits(serialport::StopBits::One).unwrap();
            Ok(serial)
        }
        _ => Err(anyhow::anyhow!("Multiple Power Supplies Found!")),
    }
}

impl std::convert::From<Command> for String {
    fn from(c: Command) -> Self {
        match c {
            Command::Power(s) => match s {
                Switch::On => String::from("OUT1"),
                Switch::Off => String::from("OUT0"),
            },
            Command::Ovp(s) => match s {
                Switch::On => String::from("OVP1"),
                Switch::Off => String::from("OVP0"),
            },
            Command::Ocp(s) => match s {
                Switch::On => String::from("OCP1"),
                Switch::Off => String::from("OCP0"),
            },
            Command::Beep(s) => match s {
                Switch::On => String::from("BEEP1"),
                Switch::Off => String::from("BEEP0"),
            },
            Command::Save(id) => format!("SAV{}", id),
            Command::Load(id) => format!("RCL{}", id),
            Command::Voltage(v) => format!("VSET1:{}.{}", v.volts, v.milli_volts),
            Command::Current(i) => format!("ISET1:{}.{}", i.ampere, i.milli_ampere),
        }
    }
}

pub fn execute(serial: &mut dyn serialport::SerialPort, command: Command) -> anyhow::Result<()> {
    run_command(serial, &String::from(command))?;
    Ok(())
}

/// Retrieve status information from the power supply
pub fn status(serial: &mut dyn serialport::SerialPort) -> anyhow::Result<Status> {
    let flags = Flags::new(run_command(serial, "STATUS?")?[0]);
    let voltage = V::from_str(
        String::from_utf8_lossy(&run_command(serial, "VOUT1?")?)
            .into_owned()
            .as_str(),
    )?;
    let current = I::from_str(
        String::from_utf8_lossy(&run_command(serial, "IOUT1?")?)
            .into_owned()
            .as_str(),
    )?;
    Ok(Status::new(flags, voltage, current))
}

fn run_command(serial: &mut dyn serialport::SerialPort, command: &str) -> anyhow::Result<Vec<u8>> {
    let bytes = command.as_bytes();
    if !serial.write(bytes)? == bytes.len() {
        return Err(anyhow::anyhow!("Could not write command"));
    }
    serial.flush()?;
    let mut result: Vec<u8> = Vec::new();
    let mut is_done = false;
    while !is_done {
        let mut serial_buf: Vec<u8> = vec![0; 512];
        match serial.read(serial_buf.as_mut_slice()) {
            Ok(count) => {
                result.extend(serial_buf.drain(..count));
            }
            Err(ref e) if e.kind() == io::ErrorKind::TimedOut => {
                is_done = true;
            }
            Err(e) => {
                return Err(e).with_context(|| "could not retrieve response from power supply")
            }
        };
    }
    Ok(result)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_channel1_status() {
        assert_eq!(Mode::CC, Flags::new(0).mode(Channel::_1));
        assert_eq!(Mode::CV, Flags::new(1).mode(Channel::_1));
    }

    #[test]
    fn test_channel2_status() {
        assert_eq!(Mode::CC, Flags::new(0).mode(Channel::_2));
        assert_eq!(Mode::CV, Flags::new(2).mode(Channel::_2));
    }

    #[test]
    fn test_beep_status() {
        assert_eq!(Switch::Off, Flags::new(0).beep());
        assert_eq!(Switch::On, Flags::new(16).beep());
    }

    #[test]
    fn test_lock_status() {
        assert_eq!(Lock::Unlocked, Flags::new(0).lock());
        assert_eq!(Lock::Locked, Flags::new(32).lock());
    }

    #[test]
    fn test_output_status() {
        assert_eq!(Switch::Off, Flags::new(0).output());
        assert_eq!(Switch::On, Flags::new(64).output());
    }
    #[test]
    fn test_voltage_from_str() {
        assert_eq!(
            V {
                volts: 10,
                milli_volts: 0
            },
            "10.0".parse::<V>().unwrap()
        );
        assert_eq!(
            V {
                volts: 1,
                milli_volts: 9
            },
            "1.9".parse::<V>().unwrap()
        );
        assert_eq!(
            V {
                volts: 0,
                milli_volts: 9
            },
            "0.9".parse::<V>().unwrap()
        );
    }

    #[test]
    fn test_current_from_str() {
        assert_eq!(
            I {
                ampere: 10,
                milli_ampere: 0
            },
            "10.0".parse::<I>().unwrap()
        );
        assert_eq!(
            I {
                ampere: 1,
                milli_ampere: 9
            },
            "1.9".parse::<I>().unwrap()
        );
        assert_eq!(
            I {
                ampere: 0,
                milli_ampere: 9
            },
            "0.9".parse::<I>().unwrap()
        );
    }
}