Message: add extended address flag

This commit is contained in:
Jeff Epler 2020-09-17 10:57:16 -05:00
parent ca32a81bf1
commit 2cb4707f92
3 changed files with 50 additions and 7 deletions

View File

@ -32,13 +32,14 @@
#include "py/runtime.h" #include "py/runtime.h"
//| class Message: //| class Message:
//| def __init__(self, id: int=0, data: Optional[bytes] = None, *, size: Optional[int] = None, rtr: bool = False): //| def __init__(self, id: int=0, data: Optional[bytes] = None, *, size: Optional[int] = None, rtr: bool = False, extended: bool = False):
//| """Construct a Message to send on a CAN bus //| """Construct a Message to send on a CAN bus
//| //|
//| :param int id: The numeric ID of the message //| :param int id: The numeric ID of the message
//| :param bytes data: The content of the message //| :param bytes data: The content of the message
//| :param int size: The amount of data requested, for an rtr //| :param int size: The amount of data requested, for an rtr
//| :param bool rtr: True if the message represents an rtr (Remote Transmission Request) //| :param bool rtr: True if the message represents an rtr (Remote Transmission Request)
//| :param bool extended: True if the message has an extended identifier, False if it has a standard identifier
//| //|
//| In CAN, messages can have a size from 0 to 8 bytes. //| In CAN, messages can have a size from 0 to 8 bytes.
//| //|
@ -47,12 +48,13 @@
//| ... //| ...
//| //|
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) { 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) {
enum { ARG_id, ARG_data, ARG_size, ARG_rtr, NUM_ARGS }; enum { ARG_id, ARG_data, ARG_size, ARG_rtr, ARG_extended, NUM_ARGS };
static const mp_arg_t allowed_args[] = { static const mp_arg_t allowed_args[] = {
{ MP_QSTR_id, MP_ARG_INT, {.u_obj = 0} }, { MP_QSTR_id, MP_ARG_INT, {.u_obj = 0} },
{ MP_QSTR_data, MP_ARG_OBJ, {.u_obj = 0} }, { MP_QSTR_data, MP_ARG_OBJ, {.u_obj = 0} },
{ MP_QSTR_size, MP_ARG_INT, {.u_int = -1} }, { MP_QSTR_size, MP_ARG_INT, {.u_int = -1} },
{ MP_QSTR_rtr, MP_ARG_BOOL, {.u_bool = false} }, { MP_QSTR_rtr, MP_ARG_BOOL, {.u_bool = false} },
{ MP_QSTR_extended, MP_ARG_BOOL, {.u_bool = false} },
}; };
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
MP_STATIC_ASSERT( MP_ARRAY_SIZE(allowed_args) == NUM_ARGS ); MP_STATIC_ASSERT( MP_ARRAY_SIZE(allowed_args) == NUM_ARGS );
@ -60,6 +62,7 @@ STATIC mp_obj_t canio_message_make_new(const mp_obj_type_t *type, size_t n_args,
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
bool rtr = args[ARG_rtr].u_bool; bool rtr = args[ARG_rtr].u_bool;
bool extended = args[ARG_extended].u_bool;
size_t size = (size_t)args[ARG_size].u_int; size_t size = (size_t)args[ARG_size].u_int;
bool specified_size = (size != (size_t)-1); bool specified_size = (size != (size_t)-1);
bool specified_data = (args[ARG_data].u_obj != NULL); 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,
canio_message_obj_t *self = m_new_obj(canio_message_obj_t); canio_message_obj_t *self = m_new_obj(canio_message_obj_t);
self->base.type = &canio_message_type; self->base.type = &canio_message_type;
common_hal_canio_message_construct(self, args[ARG_id].u_int, data.buf, data.len, rtr); common_hal_canio_message_construct(self, args[ARG_id].u_int, data.buf, data.len, rtr, extended);
return self; return self;
} }
@ -174,6 +177,31 @@ STATIC const mp_obj_property_t canio_message_size_obj = {
(mp_obj_t)&mp_const_none_obj}, (mp_obj_t)&mp_const_none_obj},
}; };
//| extended: bool
//| """True if the message represents a remote transmission request (RTR)"""
//|
STATIC mp_obj_t canio_message_extended_get(const mp_obj_t self_in) {
canio_message_obj_t *self = self_in;
return mp_obj_new_bool(common_hal_canio_message_extended_get(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(canio_message_extended_get_obj, canio_message_extended_get);
STATIC mp_obj_t canio_message_extended_set(const mp_obj_t self_in, const mp_obj_t extended) {
canio_message_obj_t *self = self_in;
common_hal_canio_message_size_set(self, mp_obj_is_true(extended));
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(canio_message_extended_set_obj, canio_message_extended_set);
STATIC const mp_obj_property_t canio_message_extended_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&canio_message_extended_get_obj,
(mp_obj_t)&canio_message_extended_set_obj,
(mp_obj_t)&mp_const_none_obj},
};
//| rtr: bool //| rtr: bool
//| """True if the message represents a remote transmission request (RTR)""" //| """True if the message represents a remote transmission request (RTR)"""
//| //|
@ -204,6 +232,7 @@ STATIC const mp_rom_map_elem_t canio_message_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_data), MP_ROM_PTR(&canio_message_data_obj) }, { MP_ROM_QSTR(MP_QSTR_data), MP_ROM_PTR(&canio_message_data_obj) },
{ MP_ROM_QSTR(MP_QSTR_size), MP_ROM_PTR(&canio_message_size_obj) }, { MP_ROM_QSTR(MP_QSTR_size), MP_ROM_PTR(&canio_message_size_obj) },
{ MP_ROM_QSTR(MP_QSTR_rtr), MP_ROM_PTR(&canio_message_rtr_obj) }, { MP_ROM_QSTR(MP_QSTR_rtr), MP_ROM_PTR(&canio_message_rtr_obj) },
{ MP_ROM_QSTR(MP_QSTR_extended), MP_ROM_PTR(&canio_message_extended_obj) },
}; };
STATIC MP_DEFINE_CONST_DICT(canio_message_locals_dict, canio_message_locals_dict_table); STATIC MP_DEFINE_CONST_DICT(canio_message_locals_dict, canio_message_locals_dict_table);

View File

@ -28,11 +28,12 @@
#include <string.h> #include <string.h>
void common_hal_canio_message_construct(canio_message_obj_t *self, int id, void *data, size_t size, bool rtr) void common_hal_canio_message_construct(canio_message_obj_t *self, int id, void *data, size_t size, bool rtr, bool extended)
{ {
self->id = id; self->id = id;
self->size = size; self->size = size;
self->rtr = rtr; self->rtr = rtr;
self->extended = extended;
if (data) { if (data) {
memcpy(self->data, data, size); memcpy(self->data, data, size);
} else { } else {
@ -84,3 +85,13 @@ void common_hal_canio_message_rtr_set(canio_message_obj_t *self, bool rtr)
{ {
self->rtr = rtr; self->rtr = rtr;
} }
bool common_hal_canio_message_extended_get(const canio_message_obj_t *self)
{
return self->extended;
}
void common_hal_canio_message_extended_set(canio_message_obj_t *self, bool extended)
{
self->extended = extended;
}

View File

@ -31,17 +31,20 @@
typedef struct { typedef struct {
mp_obj_base_t base; mp_obj_base_t base;
int id; int id;
size_t size;
uint8_t data[8]; uint8_t data[8];
bool rtr; size_t size:4;
bool rtr:1;
bool extended:1;
} canio_message_obj_t; } canio_message_obj_t;
void common_hal_canio_message_construct(canio_message_obj_t *self, int id, void *data, size_t size, bool rtr); void common_hal_canio_message_construct(canio_message_obj_t *self, int id, void *data, size_t size, bool rtr, bool extended);
bool common_hal_canio_message_rtr_get(const canio_message_obj_t *self); bool common_hal_canio_message_rtr_get(const canio_message_obj_t *self);
bool common_hal_canio_message_extended_get(const canio_message_obj_t *self);
int common_hal_canio_message_id_get(const canio_message_obj_t *self); int common_hal_canio_message_id_get(const canio_message_obj_t *self);
const void *common_hal_canio_message_data_get(const canio_message_obj_t *self); const void *common_hal_canio_message_data_get(const canio_message_obj_t *self);
size_t common_hal_canio_message_size_get(const canio_message_obj_t *self); size_t common_hal_canio_message_size_get(const canio_message_obj_t *self);
void common_hal_canio_message_rtr_set(canio_message_obj_t *self, bool rtr); void common_hal_canio_message_rtr_set(canio_message_obj_t *self, bool rtr);
void common_hal_canio_message_extended_set(canio_message_obj_t *self, bool extended);
void common_hal_canio_message_id_set(canio_message_obj_t *self, int id); void common_hal_canio_message_id_set(canio_message_obj_t *self, int id);
void common_hal_canio_message_data_set(canio_message_obj_t *self, const void *data, size_t size); void common_hal_canio_message_data_set(canio_message_obj_t *self, const void *data, size_t size);
void common_hal_canio_message_size_set(canio_message_obj_t *self, size_t size); void common_hal_canio_message_size_set(canio_message_obj_t *self, size_t size);