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
use gmt_dos_clients_transceiver::{Monitor, On, Transceiver, Transmitter, TransmitterBuilder};
use interface::{Data, Read, UniqueIdentifier};

use crate::{
    payload::{Payload, ScopeData},
    GmtScope, ImageScope, ImageScopeKind,
};

use super::XScope;

/// Server for image display scope
pub type Shot<FU> = XScope<FU, ImageScope>;
/// Server for GMT scope
pub type GmtShot<FU> = XScope<FU, GmtScope>;

impl<'a, FU, K> super::Builder<'a, FU, K>
where
    FU: UniqueIdentifier + 'static,
    K: ImageScopeKind,
{
    /// Build the [Shot]
    pub fn build(self) -> Result<XScope<FU, K>, super::ServerError> {
        Ok(XScope {
            tx: if self.frame_by_frame {
                TransmitterBuilder::new(self.address).capacity(0).build()?
            } else {
                Transceiver::transmitter(self.address)?
            }
            .run(self.monitor.unwrap()),
            size: self.size.unwrap(),
            minmax: self.minmax,
            scale: self.scale,
            tau: self.tau.unwrap_or(1f64),
            idx: 0,
            kind: std::marker::PhantomData,
        })
    }
    /// Sends images one by one
    ///
    /// The default behavior is to send a buffer of images
    /// whenever possible
    pub fn frame_by_frame(mut self) -> Self {
        self.frame_by_frame = true;
        self
    }
    /// Sets the minimum and maximum values of the image colormap
    pub fn minmax(mut self, minmax: (f64, f64)) -> Self {
        self.minmax = Some(minmax);
        self
    }
}

/* /// [Shot](crate::Shot) server
///
/// Wraps a signal into the scope payload before sending it to a [XScope](crate::XScope)
#[derive(Debug)]
pub struct Shot<FU>
where
    FU: UniqueIdentifier,
{
    tx: Transceiver<ScopeData<FU>, Transmitter, On>,
    size: [usize; 2],
    minmax: Option<(f64, f64)>,
    scale: Option<f64>,
}
 */
impl<FU, K> XScope<FU, K>
where
    FU: UniqueIdentifier + 'static,
    <FU as UniqueIdentifier>::DataType: Send + Sync + serde::Serialize,
    K: ImageScopeKind,
{
    /// Creates a [Builder](super::Builder)
    pub fn builder(
        address: impl Into<String>,
        monitor: &mut Monitor,
        size: [usize; 2],
    ) -> super::Builder<FU, K> {
        super::Builder {
            address: address.into(),
            monitor: Some(monitor),
            size: Some(size),
            ..Default::default()
        }
    }
}

impl<T, FU> Read<FU> for Shot<FU>
where
    FU: UniqueIdentifier<DataType = Vec<T>>,
    T: Copy,
    f64: From<T>,
{
    fn read(&mut self, data: Data<FU>) {
        let payload = Payload::image(data, self.tau, self.size, self.minmax, self.scale)
            .expect("failed to create payload from data");
        <Transceiver<ScopeData<FU>, Transmitter, On> as Read<ScopeData<FU>>>::read(
            &mut self.tx,
            Data::new(payload),
        );
    }
}

impl<T, FU> Read<FU> for GmtShot<FU>
where
    FU: UniqueIdentifier<DataType = (Vec<T>, Vec<bool>)>,
    T: Copy,
    f64: From<T>,
{
    fn read(&mut self, data: Data<FU>) {
        let payload = Payload::gmt(data, self.tau, self.size, self.minmax, self.scale)
            .expect("failed to create payload from data");
        <Transceiver<ScopeData<FU>, Transmitter, On> as Read<ScopeData<FU>>>::read(
            &mut self.tx,
            Data::new(payload),
        );
    }
}