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
//! Direction byte shared by `LOCK`, `CLIP` and `CATCH`.
use crate::protocol::opcode::{
LOCK_DIR_AGAINST, LOCK_DIR_BOTH, LOCK_DIR_NEG, LOCK_DIR_POS, LOCK_DIR_WITH,
};
/// Direction byte carried by `LOCK`, `CLIP` and `CATCH`.
///
/// [`Positive`](Direction::Positive) and [`Negative`](Direction::Negative) name a fixed sign or edge.
/// [`With`](Direction::With) and [`Against`](Direction::Against) name a sign relative to the bearing
/// (the direction the box is injecting): their sign follows the injection, not the axis. See
/// [`Device::scale`](crate::Device::scale) and [`Device::set_bearing`](crate::Device::set_bearing).
///
/// Variants are named for the axis reading; the constants below alias them for the edge and flow
/// readings. The class picks the reading, and no class has two.
///
/// | Constant | Same as | Classes |
/// |---|---|---|
/// | [`Direction::PRESS`] | `Positive` | button, key, media |
/// | [`Direction::RELEASE`] | `Negative` | button, key, media |
/// | [`Direction::IN`] | `Positive` | traffic: device to PC |
/// | [`Direction::OUT`] | `Negative` | traffic: PC to device |
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
pub enum Direction {
/// Both edges, signs or flows. A `LOCK` scale on `Both` goes to the two fixed signs, and the
/// relative pair gets a full pass. Only an unlock (a full pass) reaches the relative pair as sent.
#[default]
Both = LOCK_DIR_BOTH,
/// Press edge, positive sign or IN flow.
Positive = LOCK_DIR_POS,
/// Release edge, negative sign or OUT flow.
Negative = LOCK_DIR_NEG,
/// Axis sign along the bearing (the injected direction), not a fixed axis sign. Inert while no
/// bearing is live. Axes only.
With = LOCK_DIR_WITH,
/// Axis sign opposing the bearing (the injected direction), not a fixed axis sign. Inert while no
/// bearing is live. Axes only.
Against = LOCK_DIR_AGAINST,
}
impl Direction {
/// A momentary usage going down.
pub const PRESS: Direction = Direction::Positive;
/// A momentary usage coming up.
pub const RELEASE: Direction = Direction::Negative;
/// Device-to-PC traffic.
pub const IN: Direction = Direction::Positive;
/// PC-to-device traffic.
pub const OUT: Direction = Direction::Negative;
/// Wire `direction` byte.
pub fn as_u8(self) -> u8 {
self as u8
}
/// Decodes a wire `direction` byte; `None` if unknown.
pub fn from_u8(v: u8) -> Option<Direction> {
Some(match v {
LOCK_DIR_BOTH => Direction::Both,
LOCK_DIR_POS => Direction::Positive,
LOCK_DIR_NEG => Direction::Negative,
LOCK_DIR_WITH => Direction::With,
LOCK_DIR_AGAINST => Direction::Against,
_ => return None,
})
}
/// Whether this direction is relative to the bearing.
///
/// Only the relative-axis class reads one. These refuse one with
/// [`Error::RelativeDirection`](crate::Error::RelativeDirection): a catch subscription (addressed
/// before any injection), a button, key or media lock (no bearing), and a clip packet trigger. A
/// clip input trigger's edge is the three-variant [`Edge`](crate::Edge).
pub fn is_relative(self) -> bool {
matches!(self, Direction::With | Direction::Against)
}
/// Whether an event on `other` matches this direction. `Both` on either side matches, as on the
/// box.
pub fn admits(self, other: Direction) -> bool {
self == Direction::Both || other == Direction::Both || self == other
}
/// Sign of a delta; `Both` for zero.
pub fn of_delta(delta: i16) -> Direction {
match delta {
d if d > 0 => Direction::Positive,
d if d < 0 => Direction::Negative,
_ => Direction::Both,
}
}
}