dynamixel_reader.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 Inria
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser General
5  * Public License v2.1. See the file LICENSE in the top level directory for more
6  * details.
7  */
8 
20 #ifndef DYNAMIXEL_READER_H
21 #define DYNAMIXEL_READER_H
22 
23 #include <stdlib.h>
24 #include <stdbool.h>
25 
26 #include "dynamixel_protocol.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 #define DXL_PING_SIZE (10)
33 #define DXL_STATUS_SIZE(len) (11+len)
34 #define DXL_READ_SIZE (14)
35 #define DXL_WRITE_SIZE(len) (12+len)
36 
40 typedef struct {
41  const uint8_t *buffer;
42  size_t size;
44 
52 static inline void dynamixel_reader_init(dynamixel_reader_t *reader, const uint8_t *buffer, size_t size)
53 {
54  reader->buffer = buffer;
55  reader->size = size;
56 }
57 
58 
68 
76 static inline uint8_t dynamixel_reader_get_id(const dynamixel_reader_t *reader)
77 {
78  return reader->buffer[4];
79 }
80 
88 static inline uint8_t dynamixel_reader_get_instr(const dynamixel_reader_t *reader)
89 {
90  return reader->buffer[7];
91 }
92 
100 static inline uint16_t dynamixel_reader_get_length(const dynamixel_reader_t *reader)
101 {
102  return
103  (((uint16_t)reader->buffer[5]) & 0xFF) |
104  ((((uint16_t)reader->buffer[6]) & 0xFF) << 8);
105 }
106 
114 static inline uint16_t dynamixel_reader_get_crc(const dynamixel_reader_t *reader)
115 {
116  return
117  (((uint16_t)reader->buffer[reader->size - 2]) & 0xFF) |
118  ((((uint16_t)reader->buffer[reader->size - 1]) & 0xFF) << 8);
119 }
120 
128 static inline const uint8_t *dynamixel_reader_status_get_payload(const dynamixel_reader_t *reader)
129 {
130  return &reader->buffer[9];
131 }
132 
141 {
142  return dynamixel_reader_get_length(reader) - 4;
143 }
144 
145 #ifdef __cplusplus
146 }
147 #endif
148 
149 #endif /* DYNAMIXEL_READER_H */
150 
dynamixel_reader_get_length
static uint16_t dynamixel_reader_get_length(const dynamixel_reader_t *reader)
Get the packet's length field.
Definition: dynamixel_reader.h:100
dynamixel_reader_init
static void dynamixel_reader_init(dynamixel_reader_t *reader, const uint8_t *buffer, size_t size)
Initialize the Dynamixel packet reader.
Definition: dynamixel_reader.h:52
dynamixel_reader_t
Dynamixel packet reader struct.
Definition: dynamixel_reader.h:40
dynamixel_reader_t::buffer
const uint8_t * buffer
data buffer
Definition: dynamixel_reader.h:41
dynamixel_protocol.h
Dynamixel protocol definitions.
dynamixel_reader_get_instr
static uint8_t dynamixel_reader_get_instr(const dynamixel_reader_t *reader)
Get the packet's instruction code.
Definition: dynamixel_reader.h:88
dynamixel_reader_status_get_payload_size
static size_t dynamixel_reader_status_get_payload_size(const dynamixel_reader_t *reader)
Get the packet's payload size (response)
Definition: dynamixel_reader.h:140
dynamixel_reader_status_get_payload
static const uint8_t * dynamixel_reader_status_get_payload(const dynamixel_reader_t *reader)
Get the packet's payload (response)
Definition: dynamixel_reader.h:128
dynamixel_reader_is_valid
bool dynamixel_reader_is_valid(const dynamixel_reader_t *reader)
Check if the packet is valid.
dynamixel_reader_get_crc
static uint16_t dynamixel_reader_get_crc(const dynamixel_reader_t *reader)
Get the packet's crc.
Definition: dynamixel_reader.h:114
dynamixel_reader_t::size
size_t size
data buffer's size
Definition: dynamixel_reader.h:42
dynamixel_reader_get_id
static uint8_t dynamixel_reader_get_id(const dynamixel_reader_t *reader)
Get the packet's device id.
Definition: dynamixel_reader.h:76