Skip to content

Commit 2cb4707

Browse files
committed
Message: add extended address flag
1 parent ca32a81 commit 2cb4707

3 files changed

Lines changed: 50 additions & 7 deletions

File tree

shared-bindings/_canio/Message.c

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@
3232
#include "py/runtime.h"
3333

3434
//| class Message:
35-
//| def __init__(self, id: int=0, data: Optional[bytes] = None, *, size: Optional[int] = None, rtr: bool = False):
35+
//| def __init__(self, id: int=0, data: Optional[bytes] = None, *, size: Optional[int] = None, rtr: bool = False, extended: bool = False):
3636
//| """Construct a Message to send on a CAN bus
3737
//|
3838
//| :param int id: The numeric ID of the message
3939
//| :param bytes data: The content of the message
4040
//| :param int size: The amount of data requested, for an rtr
4141
//| :param bool rtr: True if the message represents an rtr (Remote Transmission Request)
42+
//| :param bool extended: True if the message has an extended identifier, False if it has a standard identifier
4243
//|
4344
//| In CAN, messages can have a size from 0 to 8 bytes.
4445
//|
@@ -47,19 +48,21 @@
4748
//| ...
4849
//|
4950
STATIC mp_obj_t canio_message_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
50-
enum { ARG_id, ARG_data, ARG_size, ARG_rtr, NUM_ARGS };
51+
enum { ARG_id, ARG_data, ARG_size, ARG_rtr, ARG_extended, NUM_ARGS };
5152
static const mp_arg_t allowed_args[] = {
5253
{ MP_QSTR_id, MP_ARG_INT, {.u_obj = 0} },
5354
{ MP_QSTR_data, MP_ARG_OBJ, {.u_obj = 0} },
5455
{ MP_QSTR_size, MP_ARG_INT, {.u_int = -1} },
5556
{ MP_QSTR_rtr, MP_ARG_BOOL, {.u_bool = false} },
57+
{ MP_QSTR_extended, MP_ARG_BOOL, {.u_bool = false} },
5658
};
5759
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
5860
MP_STATIC_ASSERT( MP_ARRAY_SIZE(allowed_args) == NUM_ARGS );
5961

6062
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
6163

6264
bool rtr = args[ARG_rtr].u_bool;
65+
bool extended = args[ARG_extended].u_bool;
6366
size_t size = (size_t)args[ARG_size].u_int;
6467
bool specified_size = (size != (size_t)-1);
6568
bool specified_data = (args[ARG_data].u_obj != NULL);
@@ -85,7 +88,7 @@ STATIC mp_obj_t canio_message_make_new(const mp_obj_type_t *type, size_t n_args,
8588

8689
canio_message_obj_t *self = m_new_obj(canio_message_obj_t);
8790
self->base.type = &canio_message_type;
88-
common_hal_canio_message_construct(self, args[ARG_id].u_int, data.buf, data.len, rtr);
91+
common_hal_canio_message_construct(self, args[ARG_id].u_int, data.buf, data.len, rtr, extended);
8992
return self;
9093
}
9194

@@ -174,6 +177,31 @@ STATIC const mp_obj_property_t canio_message_size_obj = {
174177
(mp_obj_t)&mp_const_none_obj},
175178
};
176179

180+
//| extended: bool
181+
//| """True if the message represents a remote transmission request (RTR)"""
182+
//|
183+
STATIC mp_obj_t canio_message_extended_get(const mp_obj_t self_in) {
184+
canio_message_obj_t *self = self_in;
185+
return mp_obj_new_bool(common_hal_canio_message_extended_get(self));
186+
}
187+
MP_DEFINE_CONST_FUN_OBJ_1(canio_message_extended_get_obj, canio_message_extended_get);
188+
189+
STATIC mp_obj_t canio_message_extended_set(const mp_obj_t self_in, const mp_obj_t extended) {
190+
canio_message_obj_t *self = self_in;
191+
common_hal_canio_message_size_set(self, mp_obj_is_true(extended));
192+
return mp_const_none;
193+
}
194+
MP_DEFINE_CONST_FUN_OBJ_2(canio_message_extended_set_obj, canio_message_extended_set);
195+
196+
197+
STATIC const mp_obj_property_t canio_message_extended_obj = {
198+
.base.type = &mp_type_property,
199+
.proxy = {(mp_obj_t)&canio_message_extended_get_obj,
200+
(mp_obj_t)&canio_message_extended_set_obj,
201+
(mp_obj_t)&mp_const_none_obj},
202+
};
203+
204+
177205
//| rtr: bool
178206
//| """True if the message represents a remote transmission request (RTR)"""
179207
//|
@@ -204,6 +232,7 @@ STATIC const mp_rom_map_elem_t canio_message_locals_dict_table[] = {
204232
{ MP_ROM_QSTR(MP_QSTR_data), MP_ROM_PTR(&canio_message_data_obj) },
205233
{ MP_ROM_QSTR(MP_QSTR_size), MP_ROM_PTR(&canio_message_size_obj) },
206234
{ MP_ROM_QSTR(MP_QSTR_rtr), MP_ROM_PTR(&canio_message_rtr_obj) },
235+
{ MP_ROM_QSTR(MP_QSTR_extended), MP_ROM_PTR(&canio_message_extended_obj) },
207236
};
208237
STATIC MP_DEFINE_CONST_DICT(canio_message_locals_dict, canio_message_locals_dict_table);
209238

shared-module/_canio/Message.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@
2828

2929
#include <string.h>
3030

31-
void common_hal_canio_message_construct(canio_message_obj_t *self, int id, void *data, size_t size, bool rtr)
31+
void common_hal_canio_message_construct(canio_message_obj_t *self, int id, void *data, size_t size, bool rtr, bool extended)
3232
{
3333
self->id = id;
3434
self->size = size;
3535
self->rtr = rtr;
36+
self->extended = extended;
3637
if (data) {
3738
memcpy(self->data, data, size);
3839
} else {
@@ -84,3 +85,13 @@ void common_hal_canio_message_rtr_set(canio_message_obj_t *self, bool rtr)
8485
{
8586
self->rtr = rtr;
8687
}
88+
89+
bool common_hal_canio_message_extended_get(const canio_message_obj_t *self)
90+
{
91+
return self->extended;
92+
}
93+
94+
void common_hal_canio_message_extended_set(canio_message_obj_t *self, bool extended)
95+
{
96+
self->extended = extended;
97+
}

shared-module/_canio/Message.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,20 @@
3131
typedef struct {
3232
mp_obj_base_t base;
3333
int id;
34-
size_t size;
3534
uint8_t data[8];
36-
bool rtr;
35+
size_t size:4;
36+
bool rtr:1;
37+
bool extended:1;
3738
} canio_message_obj_t;
3839

39-
void common_hal_canio_message_construct(canio_message_obj_t *self, int id, void *data, size_t size, bool rtr);
40+
void common_hal_canio_message_construct(canio_message_obj_t *self, int id, void *data, size_t size, bool rtr, bool extended);
4041
bool common_hal_canio_message_rtr_get(const canio_message_obj_t *self);
42+
bool common_hal_canio_message_extended_get(const canio_message_obj_t *self);
4143
int common_hal_canio_message_id_get(const canio_message_obj_t *self);
4244
const void *common_hal_canio_message_data_get(const canio_message_obj_t *self);
4345
size_t common_hal_canio_message_size_get(const canio_message_obj_t *self);
4446
void common_hal_canio_message_rtr_set(canio_message_obj_t *self, bool rtr);
47+
void common_hal_canio_message_extended_set(canio_message_obj_t *self, bool extended);
4548
void common_hal_canio_message_id_set(canio_message_obj_t *self, int id);
4649
void common_hal_canio_message_data_set(canio_message_obj_t *self, const void *data, size_t size);
4750
void common_hal_canio_message_size_set(canio_message_obj_t *self, size_t size);

0 commit comments

Comments
 (0)