ptouch 0.2.2

Brother PTouch label maker driver and utility
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
//! PTouch printer device definitions
// Rust PTouch Driver / Utility
//
// https://github.com/ryankurte/rust-ptouch
// Copyright 2021 Ryan Kurte

use crate::Error;

use bitflags::bitflags;

#[cfg(feature = "strum")]
use strum_macros::{Display, EnumString, EnumVariantNames};

bitflags::bitflags! {
    /// First error byte
    pub struct Error1: u8 {
        const NO_MEDIA = 0x01;
        const CUTTER_JAM = 0x04;
        const WEAK_BATT = 0x08;
        const HIGH_VOLT = 0x40;
    }
}

bitflags::bitflags! {
    /// Second device error type
    pub struct Error2: u8 {
        const WRONG_MEDIA = 0x01;
        const COVER_OPEN = 0x10;
        const OVERHEAT = 0x20;
    }
}

/// PTouch device type.
/// Note that only the p710bt has been tested
#[derive(Copy, Clone, PartialEq, Debug)]
#[cfg_attr(feature = "strum", derive(Display, EnumString, EnumVariantNames))]
#[cfg_attr(feature = "strum", strum(serialize_all = "snake_case"))]
pub enum PTouchDevice {
    #[cfg_attr(feature = "strum", strum(serialize = "pt-e550w"))]
    PtE550W = 0x2060,
    #[cfg_attr(feature = "strum", strum(serialize = "pt-p750w"))]
    PtP750W = 0x2062,
    #[cfg_attr(feature = "strum", strum(serialize = "pt-p710bt"))]
    PtP710Bt = 0x20af,
}


/// Media width encoding for Status message
#[derive(Copy, Clone, PartialEq, Debug)]
#[cfg_attr(feature = "strum", derive(Display, EnumString, EnumVariantNames))]
#[cfg_attr(feature = "strum", strum(serialize_all = "snake_case"))]
pub enum Media {
    /// 6mm TZe Tape
    Tze6mm = 257,
    /// 9mm TZe Tape
    Tze9mm = 258,
    /// 12mm TZe Tape
    Tze12mm = 259,
    /// 18mm TZe Tape
    Tze18mm = 260,
    /// 24mm TZe Tape
    Tze24mm = 261,

    /// 6mm HeatShrink Tube
    Hs6mm = 415,
    /// 9mm HeatShrink Tube
    Hs9mm = 416,
    /// 12mm HeatShrink Tube
    Hs12mm = 417,
    /// 18mm HeatShrink Tube
    Hs18mm = 418,
    /// 24mm HeatShrink Tube
    Hs24mm = 419,

    /// Unknown media width
    Unknown = 0xFFFF,
}

/// Generate a MediaWidth from provided MediaKind and u8 width
impl From<(MediaKind, u8)> for Media {
    fn from(v: (MediaKind, u8)) -> Self {
        use MediaKind::*;
        use Media::*;

        match v {
            (LaminatedTape, 6) | (NonLaminatedTape, 6) | (FlexibleTape, 6) => Tze6mm,
            (LaminatedTape, 9) | (NonLaminatedTape, 9) | (FlexibleTape, 9) => Tze9mm,
            (LaminatedTape, 12) | (NonLaminatedTape, 12) | (FlexibleTape, 12) => Tze12mm,
            (LaminatedTape, 18) | (NonLaminatedTape, 18) | (FlexibleTape, 18) => Tze18mm,
            (LaminatedTape, 24) | (NonLaminatedTape, 24) | (FlexibleTape, 24) => Tze24mm,
            (HeatShrinkTube, 6) => Hs6mm,
            (HeatShrinkTube, 9) => Hs9mm,
            (HeatShrinkTube, 12)  => Hs12mm,
            (HeatShrinkTube, 18)  => Hs18mm,
            (HeatShrinkTube, 24)  => Hs24mm,
            _ => Unknown,
        }
    }
}

impl Media {
    /// Fetch media print area (left margin, print area, right margin)
    pub fn area(&self) -> (usize, usize, usize) {
        use Media::*;

        match self {
            Tze6mm => (52, 32, 52),
            Tze9mm => (39, 50, 39),
            Tze12mm => (29, 70, 29),
            Tze18mm => (8, 112, 8),
            Tze24mm => (0, 128, 0),

            Hs6mm => (50, 28, 50),
            Hs9mm => (40, 48, 40),
            Hs12mm => (31, 66, 31),
            Hs18mm => (11, 106, 11),
            Hs24mm => (0, 128, 0),

            Unknown => (0, 0, 0)
        }
    }

    /// Check if a media type is _tape_
    pub fn is_tape(&self) -> bool {
        use Media::*;

        match self {
            Tze6mm | Tze9mm | Tze12mm | Tze18mm | Tze24mm => true,
            _ => false,
        }
    }

    /// Fetch the (approximate) media width in mm
    pub fn width(&self) -> usize {
        use Media::*;

        match self {
            Tze6mm => 6,
            Tze9mm => 9,
            Tze12mm => 12,
            Tze18mm => 18,
            Tze24mm => 24,
            Hs6mm => 6,
            Hs9mm => 9,
            Hs12mm => 12,
            Hs18mm => 18,
            Hs24mm => 24,
            _ => panic!("Unknown media width"),
        }
    }
}

/// Kind of media loaded in printer
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum MediaKind {
    None = 0x00,
    LaminatedTape = 0x01,
    NonLaminatedTape = 0x03,
    HeatShrinkTube = 0x11,
    FlexibleTape = 0x14,
    IncompatibleTape = 0xFF,
}

/// Device operating phase
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum Phase {
    Editing,
    Printing,
    Unknown,
}

impl From<u8> for Phase {
    fn from(v: u8) -> Self {
        use Phase::*;

        match v {
            0 => Editing,
            1 => Printing,
            _ => Unknown
        }
    }
}

/// Create media kind from status values
impl From<u8> for MediaKind {
    fn from(v: u8) -> Self {
        match v {
           0x00 => MediaKind::None,
           0x01 => MediaKind::LaminatedTape,
           0x03 => MediaKind::NonLaminatedTape,
           0x11 => MediaKind::HeatShrinkTube,
           0x14 => MediaKind::FlexibleTape,
           0xFF => MediaKind::IncompatibleTape,
           _ => MediaKind::IncompatibleTape,
       }
    }
}

/// Device state enumeration
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum DeviceStatus {
    Reply = 0x00,
    Completed = 0x01,
    Error = 0x02,
    ExitIF = 0x03,
    TurnedOff = 0x04,
    Notification = 0x05,
    PhaseChange = 0x06,

    Unknown = 0xFF,
}

impl From<u8> for DeviceStatus {
    fn from(v: u8) -> Self {
        use DeviceStatus::*;

        match v {
            0x00 => Reply,
            0x01 => Completed,
            0x02 => Error,
            0x03 => ExitIF,
            0x04 => TurnedOff,
            0x05 => Notification,
            0x06 => PhaseChange,
            _ => Unknown,
       }
    }
}

/// Device mode for set_mode command
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum Mode {
    /// Not sure tbqh?
    EscP = 0x00,
    /// Raster mode, what this driver uses
    Raster = 0x01,
    /// Note PTouchTemplate is not available on most devices
    PTouchTemplate = 0x03,
}

bitflags! {
    /// Various mode flags
    pub struct VariousMode: u8 {
        const AUTO_CUT = (1 << 6);
        const MIRROR = (1 << 7);
    }
}

bitflags! {
    /// Advanced mode flags
    pub struct AdvancedMode: u8 {
        const HALF_CUT = (1 << 2);
        const NO_CHAIN = (1 << 3);
        const SPECIAL_TAPE = (1 << 4);
        const HIGH_RES = (1 << 6);
        const NO_BUFF_CLEAR = (1 << 7);
    }
}

/// Notification enumerations
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum Notification {
    NotAvailable = 0x00,
    CoverOpen = 0x01,
    CoverClosed = 0x02,
}

/// Tape colour enumerations
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum TapeColour {
    White = 0x01,
    Other = 0x02,
    ClearBlack = 0x03,
    Red = 0x04,
    Blue = 0x05,
    Black = 0x08,
    ClearWhite = 0x09,
    MatteWhite = 0x20,
    MatteClear = 0x21,
    MatteSilver = 0x22,
    SatinGold = 0x23,
    SatinSilver = 0x24,
    BlueD = 0x30,
    RedD = 0x31,
    FluroOrange=0x40,
    FluroYellow=0x41,
    BerryPinkS = 0x50,
    LightGrayS = 0x51,
    LimeGreenS = 0x52,
    YellowF = 0x60,
    PinkF = 0x61,
    BlueF = 0x62,
    WhiteHst = 0x70,
    WhiteFlexId = 0x90,
    YellowFlexId = 0x91,
    Cleaning = 0xF0,
    Stencil = 0xF1,
    Incompatible = 0xFF,
}

impl From<u8> for TapeColour {
    fn from(v: u8) -> TapeColour {
        use TapeColour::*;

        match v {
            0x01 => White,
            0x02 => Other,
            0x03 => ClearBlack,
            0x04 => Red,
            0x05 => Blue,
            0x08 => Black,
            0x09 => ClearWhite,
            0x20 => MatteWhite,
            0x21 => MatteClear,
            0x22 => MatteSilver,
            0x23 => SatinGold,
            0x24 => SatinSilver,
            0x30 => BlueD,
            0x31 => RedD,
            0x40 => FluroOrange,
            0x41 => FluroYellow,
            0x50 => BerryPinkS,
            0x51 => LightGrayS,
            0x52 => LimeGreenS,
            0x60 => YellowF,
            0x61 => PinkF,
            0x62 => BlueF,
            0x70 => WhiteHst,
            0x90 => WhiteFlexId,
            0x91 => YellowFlexId,
            0xF0 => Cleaning,
            0xF1 => Stencil,
            0xFF | _ => Incompatible,
        }
    }
}

/// Text colour enumerations
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum TextColour {
    White = 0x01,
    Red = 0x04,
    Blue = 0x05,
    Black = 0x08,
    Gold = 0x0A,
    BlueF = 0x62,
    Cleaning = 0xf0,
    Stencil = 0xF1,
    Other = 0x02,
    Incompatible = 0xFF,
}

impl From<u8> for TextColour {
    fn from(v: u8) -> TextColour {
        use TextColour::*;
        
        match v {
            0x01 => White,
            0x04 => Red,
            0x05 => Blue,
            0x08 => Black,
            0x0A => Gold,
            0x62 => BlueF,
            0xf0 => Cleaning,
            0xF1 => Stencil,
            0x02 => Other,
            0xFF | _=> Incompatible,
        }
    }
}

/// Device status message
#[derive(Clone, PartialEq, Debug)]
pub struct Status {
    pub model: u8,

    pub error1: Error1,
    pub error2: Error2,

    pub media_width: u8,
    pub media_kind: MediaKind,

    pub status_type: DeviceStatus,
    pub phase: Phase,

    pub tape_colour: TapeColour,
    pub text_colour: TextColour,
}

impl Status {
    // This function is gonna be called if the --no-status-fetch flag is enabled.
    // It returns a default status, which is assumed to be correct to then print.
    pub fn new(media: &Media) -> Result<Status, Error> {
        Ok(Status {
            model: 0,                                   // The model is not that important, and also the manual only shows the model ID of E550W and E750W
            error1: Error1::empty(),                    // Assuming there's no error
            error2: Error2::empty(),                    // Assuming there's no error
            media_width: media.width() as u8,           // Width given by user in command
            media_kind: match media.is_tape() {         // Not sure if this is really important, but this is an easy way to detect if it is tape (can't know if laminated or not) or not
                true => MediaKind::LaminatedTape,
                false => MediaKind::HeatShrinkTube
            },
            status_type: DeviceStatus::Completed,       // Assuming the printer is ready to print
            phase: Phase::Editing,                      // Assuming the printer is not printing
            tape_colour: TapeColour::White,             // By default, assuming the tape is white...
            text_colour: TextColour::Black,             // ...and the text colour is black. Would maybe be good to let the user change it in the command
        })
    }
}

impl From<[u8; 32]> for Status {

    fn from(r: [u8; 32]) -> Self {
        Self {
            model: r[0],
            error1: Error1::from_bits_truncate(r[8]),
            error2: Error2::from_bits_truncate(r[9]),
            media_width: r[10],
            media_kind: MediaKind::from(r[11]),

            status_type: DeviceStatus::from(r[18]),
            phase: Phase::from(r[20]),
            tape_colour: TapeColour::from(r[24]),
            text_colour: TextColour::from(r[25]),
        }
    }
}

/// Print information command
#[derive(Clone, PartialEq, Debug)]
pub struct PrintInfo {
    /// Media kind
    pub kind: Option<MediaKind>,
    /// Tape width in mm
    pub width: Option<u8>,
    /// Tape length, always set to 0
    pub length: Option<u8>,
    /// Raster number (??)
    pub raster_no: u32,
    /// Enable print recovery
    pub recover: bool,
}

impl Default for PrintInfo {
    fn default() -> Self {
        Self {
            kind: None,
            width: None,
            length: Some(0),
            raster_no: 0,
            recover: true,
        }
    }
}

/// Compression mode enumeration
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum CompressionMode {
    None = 0x00,
    Tiff = 0x02,
}