Skip to main content

printobserver_printer_api/
push.rs

1//! What a printer pushes on its own, and the closed set of commands it may be
2//! sent.
3//!
4//! A poll reads the printer's state; some of what matters never shows in one.
5//! A firmware reset that lands the machine idle reads exactly like a cancel,
6//! and a reset while idle changes nothing a read reports at all. The printer's
7//! own push channel says so, and [`PrinterPush`] is that channel's vocabulary
8//! here: what [`PrinterPort::push_events`](crate::PrinterPort::push_events)
9//! answers.
10//!
11//! [`PrinterCommand`] is the other direction: the only commands
12//! [`PrinterPort::send_command`](crate::PrinterPort::send_command) takes. It is
13//! a closed vocabulary rather than a line, so nothing a caller writes reaches a
14//! machine through it; each adapter renders a variant to its own line.
15//! [`AxisPosition`] is what
16//! [`PrinterPort::read_position`](crate::PrinterPort::read_position) answers.
17
18use core::pin::Pin;
19use core::task::{Context, Poll};
20
21/// A stream of values that arrive one at a time, asynchronously.
22///
23/// The one shape [`PrinterPort::push_events`](crate::PrinterPort::push_events)
24/// answers in, declared here so that this port depends on nothing but the type
25/// crate. It is the same contract as the ecosystem's own `Stream`: `Ready(None)`
26/// is the end, after which nothing more arrives.
27pub trait Stream {
28    /// What the stream yields.
29    type Item;
30
31    /// Take the next value when one has arrived, `Ready(None)` at the end, or
32    /// `Pending` — having arranged for the context's waker to be woken when
33    /// either is ready.
34    fn poll_next(self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Option<Self::Item>>;
35}
36
37/// A stream this port answers, in the one shape a trait object can carry.
38pub type BoxStream<'a, T> = Pin<Box<dyn Stream<Item = T> + Send + 'a>>;
39
40/// One thing a printer pushed on its own.
41#[derive(Debug, Clone, PartialEq, Eq)]
42pub enum PrinterPush {
43    /// The printer reset: its firmware restarted, and every state it held —
44    /// the job, the position, the heaters — is gone.
45    Reset,
46    /// The printer reported an error.
47    Error {
48        /// What kind of error the source reported.
49        reason: PrinterErrorReason,
50    },
51    /// One line of the printer's terminal, as the source shows it.
52    Log {
53        /// The line.
54        line: String,
55    },
56}
57
58/// What kind of error a printer pushed.
59#[derive(Debug, Clone, PartialEq, Eq)]
60pub enum PrinterErrorReason {
61    /// The printer's own firmware reported it: a thermal runaway, a kill, a
62    /// halt. What ends a print `failed`.
63    Firmware,
64    /// Anything else the source reported, in its own word for it: a resend it
65    /// could not satisfy, the host losing the line.
66    Other(String),
67}
68
69/// A command a printer may be sent: a closed vocabulary, each variant rendered
70/// by the adapter to its own line.
71///
72/// Every number a variant carries is a distance or a place in millimetres that
73/// policy has already ruled on; no variant carries text.
74#[derive(Debug, Clone, Copy, PartialEq)]
75pub enum PrinterCommand {
76    /// Report where the print head is. The answer arrives on the push channel.
77    ReportPosition,
78    /// Move the print head along Z by a distance from where it is, in
79    /// millimetres: positive moves Z up the axis, negative down it.
80    MoveZ {
81        /// How far, in millimetres, signed along the axis.
82        mm: f64,
83    },
84    /// Move the print head to a place in X and Y, in millimetres, leaving Z
85    /// where it is.
86    MoveXy {
87        /// Where in X.
88        x_mm: f64,
89        /// Where in Y.
90        y_mm: f64,
91    },
92    /// Home the axes named.
93    Home {
94        /// Which axes.
95        axes: HomeAxes,
96    },
97}
98
99/// Which axes a [`PrinterCommand::Home`] homes.
100#[derive(Debug, Clone, Copy, PartialEq, Eq)]
101pub enum HomeAxes {
102    /// X and Y, leaving Z where it is.
103    Xy,
104    /// Z alone.
105    Z,
106}
107
108/// Where the print head is, in millimetres, as the printer reported it.
109#[derive(Debug, Clone, Copy, PartialEq)]
110pub struct AxisPosition {
111    /// X.
112    pub x: f64,
113    /// Y.
114    pub y: f64,
115    /// Z.
116    pub z: f64,
117}