sts3215-controller 0.1.1

A Rust library for controlling ST3215 servos
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
use crate::port_handler::PortHandler;
use crate::values::*;

pub struct ProtocolPacketHandler<'a> {
    port_handler: &'a mut PortHandler,
    sts_end: u8,
}

impl<'a> ProtocolPacketHandler<'a> {
    pub fn new(port_handler: &'a mut PortHandler) -> Self {
        Self {
            port_handler,
            sts_end: 0,
        }
    }

    // Fonctions utilitaires de manipulation de bytes
    pub fn sts_makeword(&self, a: u8, b: u8) -> u16 {
        if self.sts_end == 0 {
            (a as u16) | ((b as u16) << 8)
        } else {
            (b as u16) | ((a as u16) << 8)
        }
    }

    pub fn sts_makedword(&self, a: u16, b: u16) -> u32 {
        (a as u32) | ((b as u32) << 16)
    }

    pub fn sts_lobyte(&self, w: u16) -> u8 {
        if self.sts_end == 0 {
            (w & 0xFF) as u8
        } else {
            ((w >> 8) & 0xFF) as u8
        }
    }

    pub fn sts_hibyte(&self, w: u16) -> u8 {
        if self.sts_end == 0 {
            ((w >> 8) & 0xFF) as u8
        } else {
            (w & 0xFF) as u8
        }
    }

    pub fn sts_loword(&self, l: u32) -> u16 {
        (l & 0xFFFF) as u16
    }

    pub fn sts_hiword(&self, h: u32) -> u16 {
        ((h >> 16) & 0xFFFF) as u16
    }

    pub fn sts_tohost(&self, a: u16, b: u8) -> i16 {
        if (a & (1 << b)) != 0 {
            -((a & !(1 << b)) as i16)
        } else {
            a as i16
        }
    }

    // Transmission de paquet
    pub fn tx_packet(&mut self, txpacket: &mut Vec<u8>) -> CommResult {
        let total_packet_length = txpacket[PKT_LENGTH] as usize + 4;

        if self.port_handler.is_using {
            return CommResult::PortBusy;
        }
        self.port_handler.is_using = true;

        if total_packet_length > TXPACKET_MAX_LEN {
            self.port_handler.is_using = false;
            return CommResult::TxError;
        }

        // En-tête de paquet
        txpacket[PKT_HEADER_0] = 0xFF;
        txpacket[PKT_HEADER_1] = 0xFF;

        // Calcul de la somme de contrôle
        let mut checksum: u8 = 0;
        for i in 2..total_packet_length - 1 {
            checksum = checksum.wrapping_add(txpacket[i]);
        }
        txpacket[total_packet_length - 1] = !checksum;

        // Envoi du paquet
        let _ = self.port_handler.clear_port();
        match self.port_handler.write_port(&txpacket[..total_packet_length]) {
            Ok(written) if written == total_packet_length => CommResult::Success,
            _ => {
                self.port_handler.is_using = false;
                CommResult::TxFail
            }
        }
    }

    // Réception de paquet
    pub fn rx_packet(&mut self) -> (Vec<u8>, CommResult) {
        let mut rxpacket = Vec::new();
        let mut wait_length = 6;

        loop {
            match self.port_handler.read_port(wait_length - rxpacket.len()) {
                Ok(mut data) => rxpacket.append(&mut data),
                Err(_) => break,
            }

            let rx_length = rxpacket.len();
            if rx_length >= wait_length {
                // Recherche de l'en-tête du paquet
                let mut idx = 0;
                for i in 0..rx_length - 1 {
                    if rxpacket[i] == 0xFF && rxpacket[i + 1] == 0xFF {
                        idx = i;
                        break;
                    }
                }

                if idx == 0 {
                    if rx_length >= PKT_LENGTH + 1 {
                        let id = rxpacket[PKT_ID];
                        let length = rxpacket[PKT_LENGTH];
                        let error = rxpacket[PKT_ERROR];

                        if id > 0xFD || length as usize > RXPACKET_MAX_LEN || error > 0x7F {
                            rxpacket.remove(0);
                            continue;
                        }

                        let new_wait_length = length as usize + PKT_LENGTH + 1;
                        if wait_length != new_wait_length {
                            wait_length = new_wait_length;
                            continue;
                        }

                        if rx_length < wait_length {
                            if self.port_handler.is_packet_timeout() {
                                self.port_handler.is_using = false;
                                return (
                                    rxpacket,
                                    if rx_length == 0 {
                                        CommResult::RxTimeout
                                    } else {
                                        CommResult::RxCorrupt
                                    },
                                );
                            }
                            continue;
                        }

                        // Vérification de la somme de contrôle
                        let mut checksum: u8 = 0;
                        for i in 2..wait_length - 1 {
                            checksum = checksum.wrapping_add(rxpacket[i]);
                        }
                        checksum = !checksum;

                        self.port_handler.is_using = false;
                        if rxpacket[wait_length - 1] == checksum {
                            return (rxpacket, CommResult::Success);
                        } else {
                            return (rxpacket, CommResult::RxCorrupt);
                        }
                    }
                } else {
                    rxpacket.drain(0..idx);
                }
            } else {
                if self.port_handler.is_packet_timeout() {
                    self.port_handler.is_using = false;
                    return (
                        rxpacket,
                        if rx_length == 0 {
                            CommResult::RxTimeout
                        } else {
                            CommResult::RxCorrupt
                        },
                    );
                }
            }
        }

        self.port_handler.is_using = false;
        (rxpacket, CommResult::RxFail)
    }

    // Transmission et réception
    pub fn tx_rx_packet(&mut self, txpacket: &mut Vec<u8>) -> (Option<Vec<u8>>, CommResult, u8) {
        let result = self.tx_packet(txpacket);
        if !result.is_success() {
            return (None, result, 0);
        }

        if txpacket[PKT_ID] == BROADCAST_ID {
            self.port_handler.is_using = false;
            return (None, result, 0);
        }

        // Définition du délai d'attente
        if txpacket[PKT_INSTRUCTION] == INST_READ {
            self.port_handler
                .set_packet_timeout(txpacket[PKT_PARAMETER0 + 1] as usize + 6);
        } else {
            self.port_handler.set_packet_timeout(6);
        }

        loop {
            let (rxpacket, rx_result) = self.rx_packet();
            if !rx_result.is_success() || rxpacket.get(PKT_ID) == Some(&txpacket[PKT_ID]) {
                let error = if rx_result.is_success() && !rxpacket.is_empty() {
                    rxpacket[PKT_ERROR]
                } else {
                    0
                };
                return (Some(rxpacket), rx_result, error);
            }
        }
    }

    // Ping
    pub fn ping(&mut self, sts_id: u8) -> (u16, CommResult, u8) {
        if sts_id >= BROADCAST_ID {
            return (0, CommResult::NotAvailable, 0);
        }

        let mut txpacket = vec![0u8; 6];
        txpacket[PKT_ID] = sts_id;
        txpacket[PKT_LENGTH] = 2;
        txpacket[PKT_INSTRUCTION] = INST_PING;

        let (_rxpacket, result, error) = self.tx_rx_packet(&mut txpacket);

        if result.is_success() {
            let (data, read_result, read_error) = self.read_tx_rx(sts_id, 3, 2);
            if read_result.is_success() && data.len() >= 2 {
                let model_number = self.sts_makeword(data[0], data[1]);
                return (model_number, read_result, read_error);
            }
        }

        (0, result, error)
    }

    // Lecture
    pub fn read_tx_rx(&mut self, sts_id: u8, address: u8, length: u8) -> (Vec<u8>, CommResult, u8) {
        if sts_id >= BROADCAST_ID {
            return (Vec::new(), CommResult::NotAvailable, 0);
        }

        let mut txpacket = vec![0u8; 8];
        txpacket[PKT_ID] = sts_id;
        txpacket[PKT_LENGTH] = 4;
        txpacket[PKT_INSTRUCTION] = INST_READ;
        txpacket[PKT_PARAMETER0] = address;
        txpacket[PKT_PARAMETER0 + 1] = length;

        let (rxpacket, result, error) = self.tx_rx_packet(&mut txpacket);

        if result.is_success() {
            if let Some(packet) = rxpacket {
                let start = PKT_PARAMETER0;
                let end = PKT_PARAMETER0 + length as usize;
                if packet.len() >= end {
                    return (packet[start..end].to_vec(), result, error);
                }
            }
        }

        (Vec::new(), result, error)
    }

    pub fn read_1byte_tx_rx(&mut self, sts_id: u8, address: u8) -> (u8, CommResult, u8) {
        let (data, result, error) = self.read_tx_rx(sts_id, address, 1);
        let data_read = if result.is_success() && !data.is_empty() {
            data[0]
        } else {
            0
        };
        (data_read, result, error)
    }

    pub fn read_2byte_tx_rx(&mut self, sts_id: u8, address: u8) -> (u16, CommResult, u8) {
        let (data, result, error) = self.read_tx_rx(sts_id, address, 2);
        let data_read = if result.is_success() && data.len() >= 2 {
            self.sts_makeword(data[0], data[1])
        } else {
            0
        };
        (data_read, result, error)
    }

    // Écriture
    pub fn write_tx_rx(
        &mut self,
        sts_id: u8,
        address: u8,
        data: &[u8],
    ) -> (CommResult, u8) {
        let length = data.len();
        let mut txpacket = vec![0u8; length + 7];

        txpacket[PKT_ID] = sts_id;
        txpacket[PKT_LENGTH] = (length + 3) as u8;
        txpacket[PKT_INSTRUCTION] = INST_WRITE;
        txpacket[PKT_PARAMETER0] = address;

        txpacket[PKT_PARAMETER0 + 1..PKT_PARAMETER0 + 1 + length].copy_from_slice(data);

        let (_, result, error) = self.tx_rx_packet(&mut txpacket);
        (result, error)
    }

    pub fn write_tx_only(&mut self, sts_id: u8, address: u8, data: &[u8]) -> CommResult {
        let length = data.len();
        let mut txpacket = vec![0u8; length + 7];

        txpacket[PKT_ID] = sts_id;
        txpacket[PKT_LENGTH] = (length + 3) as u8;
        txpacket[PKT_INSTRUCTION] = INST_WRITE;
        txpacket[PKT_PARAMETER0] = address;

        txpacket[PKT_PARAMETER0 + 1..PKT_PARAMETER0 + 1 + length].copy_from_slice(data);

        let result = self.tx_packet(&mut txpacket);
        self.port_handler.is_using = false;
        result
    }

    pub fn write_1byte_tx_only(&mut self, sts_id: u8, address: u8, data: u8) -> CommResult {
        self.write_tx_only(sts_id, address, &[data])
    }

    pub fn write_1byte_tx_rx(&mut self, sts_id: u8, address: u8, data: u8) -> (CommResult, u8) {
        self.write_tx_rx(sts_id, address, &[data])
    }

    pub fn write_2byte_tx_only(&mut self, sts_id: u8, address: u8, data: u16) -> CommResult {
        let bytes = [self.sts_lobyte(data), self.sts_hibyte(data)];
        self.write_tx_only(sts_id, address, &bytes)
    }

    pub fn write_2byte_tx_rx(&mut self, sts_id: u8, address: u8, data: u16) -> (CommResult, u8) {
        let bytes = [self.sts_lobyte(data), self.sts_hibyte(data)];
        self.write_tx_rx(sts_id, address, &bytes)
    }

    // Sync Write
    pub fn sync_write_tx_only(
        &mut self,
        start_address: u8,
        data_length: u8,
        param: &[u8],
    ) -> CommResult {
        let param_length = param.len();
        let mut txpacket = vec![0u8; param_length + 8];

        txpacket[PKT_ID] = BROADCAST_ID;
        txpacket[PKT_LENGTH] = (param_length + 4) as u8;
        txpacket[PKT_INSTRUCTION] = INST_SYNC_WRITE;
        txpacket[PKT_PARAMETER0] = start_address;
        txpacket[PKT_PARAMETER0 + 1] = data_length;

        txpacket[PKT_PARAMETER0 + 2..PKT_PARAMETER0 + 2 + param_length].copy_from_slice(param);

        let (_, result, _) = self.tx_rx_packet(&mut txpacket);
        result
    }

    // Sync Read
    pub fn sync_read_tx(
        &mut self,
        start_address: u8,
        data_length: u8,
        param: &[u8],
    ) -> CommResult {
        let param_length = param.len();
        let mut txpacket = vec![0u8; param_length + 8];

        txpacket[PKT_ID] = BROADCAST_ID;
        txpacket[PKT_LENGTH] = (param_length + 4) as u8;
        txpacket[PKT_INSTRUCTION] = INST_SYNC_READ;
        txpacket[PKT_PARAMETER0] = start_address;
        txpacket[PKT_PARAMETER0 + 1] = data_length;

        txpacket[PKT_PARAMETER0 + 2..PKT_PARAMETER0 + 2 + param_length].copy_from_slice(param);

        self.tx_packet(&mut txpacket)
    }

    pub fn sync_read_rx(&mut self, data_length: usize, param_length: usize) -> (CommResult, Vec<u8>) {
        let wait_length = (6 + data_length) * param_length;
        self.port_handler.set_packet_timeout(wait_length);
        
        let mut rxpacket = Vec::new();

        loop {
            match self.port_handler.read_port(wait_length - rxpacket.len()) {
                Ok(mut data) => rxpacket.append(&mut data),
                Err(_) => break,
            }

            let rx_length = rxpacket.len();
            if rx_length >= wait_length {
                self.port_handler.is_using = false;
                return (CommResult::Success, rxpacket);
            } else {
                if self.port_handler.is_packet_timeout() {
                    self.port_handler.is_using = false;
                    return (
                        if rx_length == 0 {
                            CommResult::RxTimeout
                        } else {
                            CommResult::RxCorrupt
                        },
                        rxpacket,
                    );
                }
            }
        }

        self.port_handler.is_using = false;
        (CommResult::RxFail, rxpacket)
    }
}