braid-triggerbox 0.6.0

camera synchronization trigger box for Braid
Documentation
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Host-side driver for the Braid camera synchronization triggerbox.

mod datetime_conversion;

mod arduino_udev;
use crate::arduino_udev::serial_handshake;

use anyhow::{Context, Result};
use jiff::{SignedDuration, Timestamp};
use log::{debug, error, info, trace, warn};

use nalgebra as na;

use std::collections::BTreeMap;
use tokio::{
    io::{AsyncReadExt, AsyncWriteExt},
    sync::mpsc::{Receiver, Sender},
};

use braid_triggerbox_comms::{DEVICE_FIRMWARE_VERSION, Prescaler, TopAndPrescaler};

// ----- name type handling

/// Length of the device name (in bytes).
pub const DEVICE_NAME_LEN: usize = 8;

/// A device name.
pub type InnerNameType = [u8; DEVICE_NAME_LEN];
/// An optional device name.
pub type NameType = Option<InnerNameType>;

/// Callback called with each new clock model, or `None` when the model is
/// reset.
pub type ClockModelCallback = Box<dyn FnMut(Option<ClockModel>) + Send>;

/// Convert a string to a device name.
///
/// # Errors
///
/// Returns an error if `x` is longer than [`DEVICE_NAME_LEN`] bytes.
pub fn to_name_type(x: &str) -> Result<InnerNameType> {
    let bytes = x.as_bytes();
    if bytes.len() > DEVICE_NAME_LEN {
        anyhow::bail!("Maximum name length ({DEVICE_NAME_LEN} chars) exceeded.");
    }
    let mut name = [0; DEVICE_NAME_LEN];
    for (dest, src) in name.iter_mut().zip(bytes) {
        *dest = *src;
    }
    Ok(name)
}

/// Format a device name for display.
#[must_use]
pub fn name_display(name: &NameType) -> String {
    if let Some(name) = name {
        format!("\"{}\"", String::from_utf8_lossy(name))
    } else {
        "none".into()
    }
}

// ------ clock model types

/// A linear model relating triggerbox pulse number to host time.
#[derive(Debug, PartialEq, Clone)]
pub struct ClockModel {
    /// Seconds per pulse.
    pub gain: f64,
    /// Host time (in seconds since the UNIX epoch) of pulse zero.
    pub offset: f64,
    /// Sum of squared residuals of the fit.
    pub residuals: f64,
    /// Number of measurements used for the fit.
    pub n_measurements: u64,
}

/// A raw clock measurement.
#[derive(Debug)]
pub struct TriggerClockInfoRow {
    /// Host time at which the measurement was requested.
    pub start_timestamp: Timestamp,
    /// Triggerbox pulse number.
    pub framecount: i64,
    /// Fraction of the current pulse elapsed, scaled to 0-255.
    pub tcnt: u8,
    /// Host time at which the measurement was received.
    pub stop_timestamp: Timestamp,
}

/// A Braid Triggerbox device.
pub struct TriggerboxDevice {
    icr1_and_prescaler: Option<TopAndPrescaler>,
    version_check_done: bool,
    qi: u8,
    queries: BTreeMap<u8, Timestamp>,
    ser: tokio_serial::SerialStream,
    outq: Receiver<Cmd>,
    vquery_time: Timestamp,
    last_time: Timestamp,
    past_data: Vec<(f64, f64)>,
    allow_requesting_clock_sync: bool,
    on_new_model_cb: ClockModelCallback,
    triggerbox_data_tx: Option<Sender<TriggerClockInfoRow>>,
    max_acceptable_measurement_error: SignedDuration,
    query_dt: SignedDuration,
}

impl std::fmt::Debug for TriggerboxDevice {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TriggerboxDevice")
            .field("icr1_and_prescaler", &self.icr1_and_prescaler)
            .field("version_check_done", &self.version_check_done)
            .field("ser", &self.ser)
            .finish_non_exhaustive()
    }
}

/// A command for the triggerbox.
#[derive(Debug, Clone)]
pub enum Cmd {
    /// Set the trigger rate.
    TopAndPrescaler(TopAndPrescaler),
    /// Stop the trigger pulses and reset the pulse number.
    StopPulsesAndReset,
    /// Start the trigger pulses.
    StartPulses,
    /// Store a device name on the device.
    SetDeviceName(InnerNameType),
    /// Set the analog outputs (in volts).
    SetAOut((f64, f64)),
}

/// A sample returned by the device.
#[derive(Debug, PartialEq, Eq)]
struct TimedSample {
    value: u8,
    pulsenumber: u32,
    count: u16,
}

/// A packet sent by the device.
#[derive(Debug, PartialEq, Eq)]
enum DevicePacket {
    Timestamp(TimedSample),
    Version(TimedSample),
    /// Confirmation of new analog output values.
    AOutConfirm(TimedSample),
    /// A byte which does not start a valid packet was dropped.
    Skipped(SkipReason),
}

/// Why a byte received from the device was dropped.
#[derive(Debug, PartialEq, Eq)]
enum SkipReason {
    UnknownPacketType(u8),
    BadPayloadLength { packet_type: u8, payload_len: u8 },
    ChecksumMismatch,
}

/// Every packet sent by the device has a 7 byte payload.
const PAYLOAD_LEN: u8 = 7;

/// Remove the first packet from `buf` and return it.
///
/// If `buf` does not start with a valid packet, the first byte is removed and
/// [`DevicePacket::Skipped`] is returned, so that the next call can find the
/// start of the next packet. Returns `None` if `buf` does not yet hold a
/// complete packet.
fn take_packet(buf: &mut Vec<u8>) -> Option<DevicePacket> {
    // A packet is a header (type and payload length), the payload and a
    // checksum.
    let [packet_type, payload_len, rest @ ..] = buf.as_slice() else {
        return None;
    };
    let (packet_type, payload_len) = (*packet_type, *payload_len);

    let skip_reason = if !matches!(packet_type, b'P' | b'V' | b'O') {
        Some(SkipReason::UnknownPacketType(packet_type))
    } else if payload_len != PAYLOAD_LEN {
        Some(SkipReason::BadPayloadLength {
            packet_type,
            payload_len,
        })
    } else {
        let (payload, tail) = rest.split_first_chunk::<7>()?;
        let [expected_chksum, ..] = tail else {
            return None;
        };
        let actual_chksum = payload.iter().fold(0u8, |acc, x| acc.wrapping_add(*x));
        if actual_chksum == *expected_chksum {
            let [value, p0, p1, p2, p3, c0, c1] = *payload;
            let sample = TimedSample {
                value,
                pulsenumber: u32::from_le_bytes([p0, p1, p2, p3]),
                count: u16::from_le_bytes([c0, c1]),
            };
            let packet = match packet_type {
                b'P' => DevicePacket::Timestamp(sample),
                b'V' => DevicePacket::Version(sample),
                _ => DevicePacket::AOutConfirm(sample),
            };
            // header (2) + payload + checksum (1)
            buf.drain(..usize::from(PAYLOAD_LEN).saturating_add(3));
            return Some(packet);
        }
        Some(SkipReason::ChecksumMismatch)
    };

    buf.drain(..1);
    skip_reason.map(DevicePacket::Skipped)
}

/// Truncate `x` towards zero to a `u16`, or `None` if out of range.
fn f64_to_u16_checked(x: f64) -> Option<u16> {
    if (0.0..65536.0).contains(&x) {
        #[expect(
            clippy::cast_possible_truncation,
            clippy::cast_sign_loss,
            reason = "range checked above, truncation is intended"
        )]
        Some(x as u16)
    } else {
        None
    }
}

/// Truncate `x` towards zero to a `u8`, or `None` if out of range.
fn f64_to_u8_checked(x: f64) -> Option<u8> {
    f64_to_u16_checked(x).and_then(|x| u8::try_from(x).ok())
}

/// Convert a voltage to a 12-bit DAC value, clamping to the DAC range.
fn volts_to_dac(volts: f64) -> u16 {
    // Convert voltage to fraction and clamp.
    let frac = (volts / 4.096).clamp(0.0, 1.0);
    // Compute integer DAC value. (A NaN voltage gives zero.)
    f64_to_u16_checked((frac * 4095.0).round()).unwrap_or(0)
}

impl TriggerboxDevice {
    /// Connect to the triggerbox at `opts.device_path`.
    ///
    /// # Errors
    ///
    /// Returns an error if the options are out of range, or if the device
    /// cannot be opened, does not respond or does not have the name
    /// `opts.assert_device_name` (if given).
    pub async fn new(
        on_new_model_cb: ClockModelCallback,
        outq: Receiver<Cmd>,
        triggerbox_data_tx: Option<Sender<TriggerClockInfoRow>>,
        opts: TriggerboxOptions,
    ) -> Result<Self> {
        let TriggerboxOptions {
            device_path,
            query_dt,
            assert_device_name,
            max_acceptable_measurement_error,
            sleep_dur,
        } = opts;
        let baud_rate = 115_200;
        let max_acceptable_measurement_error =
            SignedDuration::try_from(max_acceptable_measurement_error)
                .context("max_acceptable_measurement_error out of range")?;
        let query_dt = SignedDuration::try_from(query_dt).context("query_dt out of range")?;
        let now = Timestamp::now();

        // wait 1 second before first version query
        let vquery_time = add_duration(now, SignedDuration::from_secs(1))?;
        let last_time = add_duration(vquery_time, SignedDuration::from_secs(1))?;

        debug!("Opening device at path {device_path}");

        let (ser, name) = match tokio::time::timeout(
            std::time::Duration::from_secs(15),
            serial_handshake(&device_path, baud_rate, sleep_dur),
        )
        .await
        {
            Ok(r) => r,
            Err(elapsed) => Err(anyhow::Error::from(elapsed)),
        }
        .with_context(|| format!("opening device {device_path}"))?;

        if let Some(name) = &name {
            let name_str = String::from_utf8_lossy(name);
            debug!("Connected to device named \"{name_str}\".");
        } else {
            debug!("Connected to unnamed device.");
        }

        if assert_device_name.is_some() && name != assert_device_name {
            anyhow::bail!(
                "Found name {}, but expected {}. ({:?} vs {:?}.)",
                name_display(&name),
                name_display(&assert_device_name),
                name,
                assert_device_name,
            );
        }

        Ok(Self {
            icr1_and_prescaler: None,
            version_check_done: false,
            qi: 0,
            queries: BTreeMap::new(),
            ser,
            outq,
            vquery_time,
            last_time,
            past_data: Vec::new(),
            allow_requesting_clock_sync: false,
            on_new_model_cb,
            triggerbox_data_tx,
            max_acceptable_measurement_error,
            query_dt,
        })
    }

    async fn write(&mut self, buf: &[u8]) -> tokio::io::Result<()> {
        trace!("sending: \"{}\"", String::from_utf8_lossy(buf));
        for byte in buf {
            trace!("sending byte: {byte}");
        }
        AsyncWriteExt::write_all(&mut self.ser, buf).await?;
        Ok(())
    }

    async fn handle_host_command(&mut self, cmd: Cmd) -> Result<()> {
        debug!("got command {cmd:?}");
        match cmd {
            Cmd::TopAndPrescaler(new_value) => {
                self.set_top_and_prescaler(new_value).await?;
            }
            Cmd::StopPulsesAndReset => {
                debug!("will reset counters. dropping outstanding info requests.");
                self.allow_requesting_clock_sync = false;
                self.queries.clear();
                self.past_data.clear();
                (self.on_new_model_cb)(None);
                self.write(b"S0").await?;
            }
            Cmd::StartPulses => {
                self.allow_requesting_clock_sync = true;
                self.write(b"S1").await?;
            }
            Cmd::SetDeviceName(name) => {
                // The device reads exactly two hex digits.
                let computed_crc = format!("{:02X}", arduino_udev::CRC_MAXIM.checksum(&name));
                trace!("computed CRC: {computed_crc:?}");

                self.write(b"N=").await?;
                self.write(&name).await?;
                self.write(computed_crc.as_bytes()).await?;
            }
            Cmd::SetAOut((volts1, volts2)) => {
                let val1 = volts_to_dac(volts1);
                let val2 = volts_to_dac(volts2);

                self.write(b"O=").await?;
                self.write(&val1.to_le_bytes()).await?;
                self.write(&val2.to_le_bytes()).await?;
                self.write(b"x").await?;

                // Now wait for return value.
                tokio::time::sleep(std::time::Duration::from_millis(50)).await;

                let mut buf = vec![0; 100];
                let len = self.ser.read(&mut buf).await?;
                buf.truncate(len);
                debug!("AOUT ignoring values: {buf:?}");
            }
        }
        Ok(())
    }

    /// Run forever, handling interaction with the triggerbox hardware device.
    ///
    /// Drop all instances of the `Sender<Cmd>` which could send messages to the
    /// `Receiver<Cmd>` passed to [`Self::new`] to exit.
    ///
    /// # Errors
    ///
    /// Returns an error if communication with the device fails.
    pub async fn run_forever(mut self: TriggerboxDevice) -> Result<()> {
        let query_dt = self.query_dt;

        let connect_time = Timestamp::now();

        let mut buf: Vec<u8> = Vec::new();
        let mut read_buf: Vec<u8> = vec![0; 100];
        let mut version_check_started = false;
        let mut new_data = false;
        let mut interval = tokio::time::interval(std::time::Duration::from_millis(100));

        loop {
            if self.version_check_done {
                tokio::select! {
                    // Handle command queue iff version check done.
                    opt_cmd_tup = self.outq.recv() => {
                        if let Some(cmd) = opt_cmd_tup {
                            self.handle_host_command(cmd).await?;
                        } else {
                            // no more commands, sender hung up
                            info!("exiting run loop");
                            return Ok(());
                        }
                    },
                    res_r = self.ser.read(&mut read_buf) => {
                        let n_bytes_read = res_r?;
                        if n_bytes_read > 0 {
                            update_read_buffer(n_bytes_read, &read_buf, &mut buf);
                            new_data = true;
                        }
                    },
                    _ = interval.tick() => {}
                }
            } else {
                // Same as above except `self.outq` is not checked. This is done
                // at startup before the version number is confirmed.
                tokio::select! {
                    res_r = self.ser.read(&mut read_buf) => {
                        let n_bytes_read = res_r?;
                        if n_bytes_read > 0 {
                            update_read_buffer(n_bytes_read, &read_buf, &mut buf);
                            new_data = true;
                        }
                    },
                    _ = interval.tick() => {}
                }
            }

            // handle pending data, if any
            if new_data {
                self.handle_data_from_device(&mut buf).await?;
                new_data = false;
            }

            let now = Timestamp::now();

            if self.version_check_done {
                if self.allow_requesting_clock_sync
                    && (now.duration_since(self.last_time) > query_dt)
                {
                    // request sample
                    debug!("making clock sample request. qi: {}, now: {now}", self.qi);
                    self.queries.insert(self.qi, now);
                    let send_buf = [b'P', self.qi];
                    self.write(&send_buf).await?;
                    self.qi = self.qi.wrapping_add(1);
                    self.last_time = now;
                }
            } else {
                // request firmware version
                if !version_check_started && now >= self.vquery_time {
                    info!("checking firmware version");
                    self.write(b"V?").await?;
                    version_check_started = true;
                    self.vquery_time = now;
                }

                // retry every second
                if now.duration_since(self.vquery_time) > SignedDuration::from_secs(1) {
                    version_check_started = false;
                }
                // give up after 20 seconds
                if now.duration_since(connect_time) > SignedDuration::from_secs(20) {
                    return Err(anyhow::anyhow!("no version response"));
                }
            }
        }
    }

    async fn set_top_and_prescaler(&mut self, new_value: TopAndPrescaler) -> Result<()> {
        let [icr1_lo, icr1_hi] = new_value.avr_icr1().to_le_bytes();
        let buf = [icr1_lo, icr1_hi, new_value.prescaler_key()];

        self.icr1_and_prescaler = Some(new_value);

        self.write(b"T=").await?;
        self.write(&buf).await?;
        Ok(())
    }

    async fn handle_returned_timestamp(&mut self, sample: TimedSample) -> Result<()> {
        let TimedSample {
            value: qi,
            pulsenumber,
            count,
        } = sample;
        debug!("got returned timestamp with qi: {qi}, pulsenumber: {pulsenumber}, count: {count}");
        let now = Timestamp::now();
        if self.queries.len() > 50 {
            self.queries.clear();
            error!("too many outstanding queries");
        }

        let Some(send_timestamp) = self.queries.remove(&qi) else {
            warn!("could not find original data for query {qi:?}");
            return Ok(());
        };
        trace!("this query has send_timestamp: {send_timestamp}");

        let max_error = now.duration_since(send_timestamp);
        if max_error > self.max_acceptable_measurement_error {
            debug!("clock sample took {max_error:?}. Ignoring value.");
            return Ok(());
        }

        trace!("max_error: {max_error:?}");

        let half_max_error = max_error
            .checked_div(2)
            .context("measurement error out of range")?;
        let ino_time_estimate = add_duration(send_timestamp, half_max_error)?;

        let Some(s) = &self.icr1_and_prescaler else {
            warn!("No clock measurements until framerate set.");
            return Ok(());
        };

        let frac = f64::from(count) / f64::from(s.avr_icr1());
        if !(0.0..=1.0).contains(&frac) {
            warn!("ignoring clock sample with invalid count {count}");
            return Ok(());
        }
        let ino_stamp = f64::from(pulsenumber) + frac;

        if let Some(tbox_tx) = &self.triggerbox_data_tx {
            // send our newly acquired data to be saved to disk
            let to_save = TriggerClockInfoRow {
                start_timestamp: send_timestamp,
                framecount: i64::from(pulsenumber),
                tcnt: f64_to_u8_checked(frac * 255.0).unwrap_or(u8::MAX),
                stop_timestamp: now,
            };
            if let Err(e) = tbox_tx.send(to_save).await {
                warn!("ignoring {e}");
            }
        }

        // delete old data
        while self.past_data.len() >= 100 {
            self.past_data.remove(0);
        }

        self.past_data.push((
            ino_stamp,
            datetime_conversion::timestamp_to_f64(ino_time_estimate),
        ));

        if self.past_data.len() >= 5 {
            let (gain, offset, residuals) =
                fit_time_model(&self.past_data).map_err(|e| anyhow::anyhow!("lstsq err: {e}"))?;

            let n_measurements = u32::try_from(self.past_data.len())?;
            let per_point_residual = residuals / f64::from(n_measurements);
            // TODO only accept this if residuals less than some amount?
            debug!(
                "new: ClockModel{{gain: {gain}, offset: {offset}}}, per_point_residual: {per_point_residual}"
            );
            (self.on_new_model_cb)(Some(ClockModel {
                gain,
                offset,
                residuals,
                n_measurements: u64::from(n_measurements),
            }));
        }
        Ok(())
    }

    fn handle_version(&mut self, sample: &TimedSample) -> Result<()> {
        let value = sample.value;
        trace!("got returned version with value: {value}");
        if value != DEVICE_FIRMWARE_VERSION {
            anyhow::bail!(
                "triggerbox firmware version {value} is not supported (expected version \
                {DEVICE_FIRMWARE_VERSION})"
            );
        }
        self.vquery_time = Timestamp::now();
        self.version_check_done = true;
        info!("connected to triggerbox firmware version {value}");
        Ok(())
    }

    async fn handle_data_from_device(&mut self, buf: &mut Vec<u8>) -> Result<()> {
        while let Some(packet) = take_packet(buf) {
            match packet {
                DevicePacket::Timestamp(sample) => self.handle_returned_timestamp(sample).await?,
                DevicePacket::Version(sample) => self.handle_version(&sample)?,
                DevicePacket::AOutConfirm(sample) => {
                    debug!("ignoring AOUT confirmation {}", sample.value);
                }
                DevicePacket::Skipped(reason) => {
                    warn!("dropped a byte received from the device: {reason:?}");
                }
            }
        }
        Ok(())
    }
}

fn update_read_buffer(n_bytes_read: usize, read_buf: &[u8], buf: &mut Vec<u8>) {
    for &byte in read_buf.iter().take(n_bytes_read) {
        trace!("read byte {byte} (char {})", char::from(byte));
        buf.push(byte);
    }
}

fn add_duration(t: Timestamp, d: SignedDuration) -> Result<Timestamp> {
    t.checked_add(d).context("time out of range")
}

fn fit_time_model(past_data: &[(f64, f64)]) -> Result<(f64, f64, f64), &'static str> {
    use na::{OMatrix, OVector, U2};

    let mut a: Vec<f64> = Vec::with_capacity(past_data.len().saturating_mul(2));
    let mut b: Vec<f64> = Vec::with_capacity(past_data.len());

    for row in past_data {
        a.push(row.0);
        a.push(1.0);
        b.push(row.1);
    }
    let a = OMatrix::<f64, na::Dyn, U2>::from_row_slice(&a);
    let b = OVector::<f64, na::Dyn>::from_row_slice(&b);

    let epsilon = 1e-10;
    let results = lstsq::lstsq(&a, &b, epsilon)?;

    let gain = results.solution.x;
    let offset = results.solution.y;
    let residuals = results.residuals;

    Ok((gain, offset, residuals))
}

/// Options for [`run_triggerbox`].
#[derive(Clone, Debug)]
pub struct TriggerboxOptions {
    /// Path of the serial device.
    pub device_path: String,
    /// Interval between clock measurements.
    pub query_dt: std::time::Duration,
    /// If given, the required device name.
    pub assert_device_name: NameType,
    /// Clock measurements which take longer than this are discarded.
    pub max_acceptable_measurement_error: std::time::Duration,
    /// Time to wait for the device to reset after opening it.
    pub sleep_dur: std::time::Duration,
}

/// Connect to a triggerbox and run until `outq` is closed.
///
/// # Errors
///
/// Returns an error if connecting to or communicating with the device fails.
pub async fn run_triggerbox(
    on_new_model_cb: ClockModelCallback,
    outq: Receiver<Cmd>,
    triggerbox_data_tx: Option<Sender<TriggerClockInfoRow>>,
    opts: TriggerboxOptions,
) -> Result<()> {
    let triggerbox = TriggerboxDevice::new(on_new_model_cb, outq, triggerbox_data_tx, opts).await?;
    triggerbox.run_forever().await
}

fn get_rate(rate_ideal: f64, prescaler: Prescaler) -> (u16, f64) {
    let xtal = 16e6; // 16 MHz clock
    let base_clock = xtal / prescaler.as_f64();
    let new_top_ideal = base_clock / rate_ideal;
    let new_icr1_f64 = new_top_ideal.round().clamp(0.0, f64::from(u16::MAX));
    // A NaN rate gives zero.
    let new_icr1 = f64_to_u16_checked(new_icr1_f64).unwrap_or(0);
    let rate_actual = base_clock / f64::from(new_icr1);
    (new_icr1, rate_actual)
}

/// Given an ideal frame rate (in frames per second), compute the triggerbox
/// command which best approximates this frame rate.
///
/// Returns the triggerbox command and the expected actual frame rate (in frames
/// per second).
#[must_use]
pub fn make_trig_fps_cmd(rate_ideal: f64) -> (Cmd, f64) {
    let (top_8, rate_actual_8) = get_rate(rate_ideal, Prescaler::Scale8);
    let (top_64, rate_actual_64) = get_rate(rate_ideal, Prescaler::Scale64);

    let error_8 = (rate_ideal - rate_actual_8).abs();
    let error_64 = (rate_ideal - rate_actual_64).abs();

    let (top, rate_actual, prescaler) = if error_8 < error_64 {
        (top_8, rate_actual_8, Prescaler::Scale8)
    } else {
        (top_64, rate_actual_64, Prescaler::Scale64)
    };

    (
        Cmd::TopAndPrescaler(TopAndPrescaler::new_avr(top, prescaler)),
        rate_actual,
    )
}

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

    fn packet(packet_type: u8, payload: &[u8]) -> Vec<u8> {
        let mut buf = vec![packet_type, u8::try_from(payload.len()).unwrap()];
        buf.extend_from_slice(payload);
        buf.push(payload.iter().fold(0u8, |acc, x| acc.wrapping_add(*x)));
        buf
    }

    #[test]
    fn test_fit_time_model() {
        let epsilon = 1e-12;

        let data = vec![(0.0, 0.0), (1.0, 1.0), (2.0, 2.0), (3.0, 3.0)];
        let (gain, offset, _residuals) = fit_time_model(&data).unwrap();
        assert!((gain - 1.0).abs() < epsilon);
        assert!((offset - 0.0).abs() < epsilon);

        let data = vec![(0.0, 12.0), (1.0, 22.0), (2.0, 32.0), (3.0, 42.0)];
        let (gain, offset, _residuals) = fit_time_model(&data).unwrap();
        assert!((gain - 10.0).abs() < epsilon);
        assert!((offset - 12.0).abs() < epsilon);
    }

    fn sample(value: u8, pulsenumber: u32, count: u16) -> TimedSample {
        TimedSample {
            value,
            pulsenumber,
            count,
        }
    }

    #[test]
    fn take_packets() {
        let mut buf = packet(b'P', &[3, 1, 0, 0, 0, 2, 0]);
        buf.extend(packet(b'V', &[14, 0, 0, 0, 0, 0, 0]));
        buf.extend(packet(b'O', &[b'x', 0, 0, 0, 0, 0, 0]));
        // A partial packet.
        buf.extend(&[b'P', 7, 0]);

        assert_eq!(
            take_packet(&mut buf),
            Some(DevicePacket::Timestamp(sample(3, 1, 2)))
        );
        assert_eq!(
            take_packet(&mut buf),
            Some(DevicePacket::Version(sample(14, 0, 0)))
        );
        assert_eq!(
            take_packet(&mut buf),
            Some(DevicePacket::AOutConfirm(sample(b'x', 0, 0)))
        );
        assert_eq!(take_packet(&mut buf), None);
        assert_eq!(buf, [b'P', 7, 0]);
    }

    /// Take packets until `buf` is exhausted, returning the valid ones and the
    /// number of skipped bytes.
    fn take_all(buf: &mut Vec<u8>) -> (Vec<DevicePacket>, usize) {
        let (skipped, packets): (Vec<_>, Vec<_>) = std::iter::from_fn(|| take_packet(buf))
            .partition(|packet| matches!(packet, DevicePacket::Skipped(_)));
        (packets, skipped.len())
    }

    #[test]
    fn take_packet_resynchronizes() {
        let good = packet(b'P', &[9, 8, 0, 0, 0, 6, 0]);

        // A packet with a bad checksum, then a good packet.
        let mut buf = packet(b'P', &[1, 2, 3, 4, 5, 6, 7]);
        *buf.last_mut().unwrap() ^= 0xFF;
        assert_eq!(
            take_packet(&mut buf.clone()),
            Some(DevicePacket::Skipped(SkipReason::ChecksumMismatch))
        );
        buf.extend(&good);
        let (packets, n_skipped) = take_all(&mut buf);
        assert_eq!(packets, [DevicePacket::Timestamp(sample(9, 8, 6))]);
        assert_eq!(n_skipped, 10);
        assert_eq!(buf, Vec::<u8>::new());

        // A packet with a bad payload length, then a good packet.
        let mut buf = packet(b'V', &[1, 2, 3]);
        assert_eq!(
            take_packet(&mut buf.clone()),
            Some(DevicePacket::Skipped(SkipReason::BadPayloadLength {
                packet_type: b'V',
                payload_len: 3
            }))
        );
        buf.extend(&good);
        let (packets, _) = take_all(&mut buf);
        assert_eq!(packets, [DevicePacket::Timestamp(sample(9, 8, 6))]);

        // Garbage, then a good packet.
        let mut buf = b"garbage".to_vec();
        buf.extend(&good);
        let (packets, n_skipped) = take_all(&mut buf);
        assert_eq!(packets, [DevicePacket::Timestamp(sample(9, 8, 6))]);
        assert_eq!(n_skipped, 7);
    }

    #[test]
    fn name_type() {
        assert_eq!(to_name_type("abc").unwrap(), *b"abc\0\0\0\0\0");
        assert_eq!(to_name_type("abcdefgh").unwrap(), *b"abcdefgh");
        assert!(to_name_type("abcdefghi").is_err());
    }

    #[test]
    fn dac_values() {
        assert_eq!(volts_to_dac(-1.0), 0);
        assert_eq!(volts_to_dac(0.0), 0);
        assert_eq!(volts_to_dac(4.096), 4095);
        assert_eq!(volts_to_dac(100.0), 4095);
        assert_eq!(volts_to_dac(f64::NAN), 0);
    }

    #[test]
    fn trig_fps_cmd() {
        let (_cmd, rate_actual) = make_trig_fps_cmd(100.0);
        assert!((rate_actual - 100.0).abs() < 1e-6);
        // Extreme requests are clamped rather than panicking.
        for rate in [0.0, 1e-6, 1e12, f64::NAN, f64::INFINITY] {
            let _ = make_trig_fps_cmd(rate);
        }
    }
}