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
use byteorder::{WriteBytesExt, BigEndian};
use crate::util::to_u8_vec;
#[derive(Clone)]
pub enum UdpTag {
Countdown(Countdown),
Joysticks(Joysticks),
DateTime(DateTime),
Timezone(Timezone),
}
pub(crate) trait Tag {
fn id(&self) -> usize;
fn data(&self) -> Vec<u8>;
fn construct(&self) -> Vec<u8> {
let mut buf = vec![self.id() as u8];
buf.extend(self.data());
buf.insert(0, buf.len() as u8);
buf
}
}
#[derive(Clone)]
pub struct Countdown {
seconds_remaining: f32,
}
impl Countdown {
pub fn new(seconds: f32) -> Countdown {
Countdown {
seconds_remaining: seconds
}
}
}
impl Tag for Countdown {
fn id(&self) -> usize {
0x07
}
fn data(&self) -> Vec<u8> {
let mut buf = Vec::with_capacity(2);
buf.write_f32::<BigEndian>(self.seconds_remaining).unwrap();
buf
}
}
#[derive(Clone)]
pub struct Joysticks {
axes: Vec<i8>,
buttons: Vec<bool>,
povs: Vec<i16>,
}
impl Joysticks {
pub fn new(axes: Vec<i8>, buttons: Vec<bool>, povs: Vec<i16>) -> Joysticks {
Joysticks {
axes,
buttons,
povs
}
}
}
impl Tag for Joysticks {
fn id(&self) -> usize {
0x0c
}
fn data(&self) -> Vec<u8> {
let mut buf = vec![];
buf.write_u8(self.axes.len() as u8).unwrap();
for axis in &self.axes {
buf.write_i8(*axis).unwrap();
}
let buttons = to_u8_vec(&self.buttons);
buf.push(10);
buf.extend(buttons);
buf.push(self.povs.len() as u8);
for pov in &self.povs {
buf.write_i16::<BigEndian>(*pov).unwrap();
}
buf
}
}
#[derive(Clone)]
pub struct DateTime {
micros: u32,
second: u8,
minute: u8,
hour: u8,
day: u8,
month: u8,
year: u8
}
impl DateTime {
pub fn new(micros: u32, second: u8, minute: u8, hour: u8, day: u8, month: u8, year: u8) -> DateTime {
DateTime {
micros,
second,
minute,
hour,
day,
month,
year
}
}
}
impl Tag for DateTime {
fn id(&self) -> usize {
0x0f
}
fn data(&self) -> Vec<u8> {
let mut buf = Vec::new();
buf.write_u32::<BigEndian>(self.micros).unwrap();
buf.push(self.second);
buf.push(self.minute);
buf.push(self.hour);
buf.push(self.day);
buf.push(self.month);
buf.push(self.year);
buf
}
}
#[derive(Clone)]
pub struct Timezone {
tz: String,
}
impl Timezone {
pub fn new(tz: &str) -> Timezone {
Timezone {
tz: tz.to_string()
}
}
}
impl Tag for Timezone {
fn id(&self) -> usize {
0x10
}
fn data(&self) -> Vec<u8> {
let mut buf = Vec::new();
buf.extend_from_slice(self.tz.as_bytes());
buf
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn verify_format() {
let countdown = Countdown { seconds_remaining: 2f32 };
let buf = countdown.construct();
assert_eq!(buf, &[0x05, 0x07, 0x040, 0x0, 0x0, 0x0]);
}
#[test]
fn verify_joysticks() {
let joysticks = Joysticks {
axes: vec![],
buttons: vec![true, true, false, false, false, true, false],
povs: vec![]
};
let buf = joysticks.construct();
println!("{:?}", buf);
}
}