2020-08-20 12:08:00 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2020 Jeff Epler for Adafruit Industries
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2020-09-21 17:42:12 -04:00
|
|
|
#include "shared-bindings/canio/Message.h"
|
2020-08-20 12:08:00 -04:00
|
|
|
|
|
|
|
#include "py/obj.h"
|
|
|
|
#include "py/objproperty.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
|
|
|
|
//| class Message:
|
2020-10-29 20:15:34 -04:00
|
|
|
//| def __init__(self, id: int, data: bytes, *, extended: bool = False) -> None:
|
2020-09-24 16:25:06 -04:00
|
|
|
//| """Construct a Message to send on a CAN bus.
|
2020-08-20 12:08:00 -04:00
|
|
|
//|
|
|
|
|
//| :param int id: The numeric ID of the message
|
|
|
|
//| :param bytes data: The content of the message
|
2020-09-17 11:57:16 -04:00
|
|
|
//| :param bool extended: True if the message has an extended identifier, False if it has a standard identifier
|
2020-08-20 12:08:00 -04:00
|
|
|
//|
|
2020-09-24 16:25:06 -04:00
|
|
|
//| In CAN, messages can have a length from 0 to 8 bytes.
|
2020-08-20 12:08:00 -04:00
|
|
|
//| """
|
|
|
|
//| ...
|
2021-10-15 14:43:12 -04:00
|
|
|
STATIC mp_obj_t canio_message_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
|
2020-09-24 16:25:06 -04:00
|
|
|
enum { ARG_id, ARG_data, ARG_extended, NUM_ARGS };
|
2020-08-20 12:08:00 -04:00
|
|
|
static const mp_arg_t allowed_args[] = {
|
2020-09-24 16:25:06 -04:00
|
|
|
{ MP_QSTR_id, MP_ARG_INT | MP_ARG_REQUIRED },
|
|
|
|
{ MP_QSTR_data, MP_ARG_OBJ | MP_ARG_REQUIRED },
|
2020-09-17 11:57:16 -04:00
|
|
|
{ MP_QSTR_extended, MP_ARG_BOOL, {.u_bool = false} },
|
2020-08-20 12:08:00 -04:00
|
|
|
};
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
2021-03-15 09:57:36 -04:00
|
|
|
MP_STATIC_ASSERT(MP_ARRAY_SIZE(allowed_args) == NUM_ARGS);
|
2020-08-20 12:08:00 -04:00
|
|
|
|
2021-10-15 14:43:12 -04:00
|
|
|
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
2020-08-20 12:08:00 -04:00
|
|
|
|
|
|
|
mp_buffer_info_t data;
|
2020-09-24 16:25:06 -04:00
|
|
|
mp_get_buffer_raise(args[ARG_data].u_obj, &data, MP_BUFFER_READ);
|
2020-08-20 12:08:00 -04:00
|
|
|
|
2022-05-13 15:33:43 -04:00
|
|
|
mp_arg_validate_length_range(data.len, 0, 8, MP_QSTR_data);
|
2020-08-20 12:08:00 -04:00
|
|
|
|
|
|
|
canio_message_obj_t *self = m_new_obj(canio_message_obj_t);
|
|
|
|
self->base.type = &canio_message_type;
|
2020-09-24 16:25:06 -04:00
|
|
|
common_hal_canio_message_construct(self, args[ARG_id].u_int, data.buf, data.len, args[ARG_extended].u_bool);
|
2020-08-20 12:08:00 -04:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
//| id: int
|
|
|
|
//| """The numeric ID of the message"""
|
|
|
|
STATIC mp_obj_t canio_message_id_get(const mp_obj_t self_in) {
|
|
|
|
canio_message_obj_t *self = self_in;
|
2020-09-17 13:44:40 -04:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(common_hal_canio_message_get_id(self));
|
2020-08-20 12:08:00 -04:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(canio_message_id_get_obj, canio_message_id_get);
|
|
|
|
|
|
|
|
STATIC mp_obj_t canio_message_id_set(const mp_obj_t self_in, const mp_obj_t id) {
|
|
|
|
canio_message_obj_t *self = self_in;
|
2020-09-17 13:44:40 -04:00
|
|
|
common_hal_canio_message_set_id(self, mp_obj_get_int(id));
|
2020-08-20 12:08:00 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(canio_message_id_set_obj, canio_message_id_set);
|
|
|
|
|
2022-05-01 12:24:05 -04:00
|
|
|
MP_PROPERTY_GETSET(canio_message_id_obj,
|
2022-05-01 11:16:53 -04:00
|
|
|
(mp_obj_t)&canio_message_id_get_obj,
|
|
|
|
(mp_obj_t)&canio_message_id_set_obj);
|
2020-08-20 12:08:00 -04:00
|
|
|
|
|
|
|
//| data: bytes
|
2020-09-24 16:25:06 -04:00
|
|
|
//| """The content of the message"""
|
2020-08-20 12:08:00 -04:00
|
|
|
STATIC mp_obj_t canio_message_data_get(const mp_obj_t self_in) {
|
|
|
|
canio_message_obj_t *self = self_in;
|
2021-03-15 09:57:36 -04:00
|
|
|
return mp_obj_new_bytes((const byte *)common_hal_canio_message_get_data(self), common_hal_canio_message_get_length(self));
|
2020-08-20 12:08:00 -04:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(canio_message_data_get_obj, canio_message_data_get);
|
|
|
|
|
|
|
|
STATIC mp_obj_t canio_message_data_set(const mp_obj_t self_in, const mp_obj_t data_in) {
|
|
|
|
canio_message_obj_t *self = self_in;
|
|
|
|
mp_buffer_info_t data;
|
|
|
|
mp_get_buffer_raise(data_in, &data, MP_BUFFER_READ);
|
2022-05-13 15:33:43 -04:00
|
|
|
|
|
|
|
mp_arg_validate_length_range(data.len, 0, 8, MP_QSTR_data);
|
|
|
|
|
2020-09-17 13:44:40 -04:00
|
|
|
common_hal_canio_message_set_data(self, data.buf, data.len);
|
2020-08-20 12:08:00 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(canio_message_data_set_obj, canio_message_data_set);
|
|
|
|
|
|
|
|
|
2022-05-01 12:24:05 -04:00
|
|
|
MP_PROPERTY_GETSET(canio_message_data_obj,
|
2022-05-01 11:16:53 -04:00
|
|
|
(mp_obj_t)&canio_message_data_get_obj,
|
|
|
|
(mp_obj_t)&canio_message_data_set_obj);
|
2020-08-20 12:08:00 -04:00
|
|
|
|
|
|
|
|
2020-09-17 11:57:16 -04:00
|
|
|
//| extended: bool
|
2020-09-24 16:25:06 -04:00
|
|
|
//| """True if the message's id is an extended id"""
|
2020-09-17 11:57:16 -04:00
|
|
|
STATIC mp_obj_t canio_message_extended_get(const mp_obj_t self_in) {
|
|
|
|
canio_message_obj_t *self = self_in;
|
2020-09-17 13:44:40 -04:00
|
|
|
return mp_obj_new_bool(common_hal_canio_message_get_extended(self));
|
2020-09-17 11:57:16 -04:00
|
|
|
}
|
|
|
|
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;
|
2020-09-17 13:44:40 -04:00
|
|
|
common_hal_canio_message_set_extended(self, mp_obj_is_true(extended));
|
2020-09-17 11:57:16 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(canio_message_extended_set_obj, canio_message_extended_set);
|
|
|
|
|
|
|
|
|
2022-05-01 12:24:05 -04:00
|
|
|
MP_PROPERTY_GETSET(canio_message_extended_obj,
|
2022-05-01 11:16:53 -04:00
|
|
|
(mp_obj_t)&canio_message_extended_get_obj,
|
|
|
|
(mp_obj_t)&canio_message_extended_set_obj);
|
2020-09-17 11:57:16 -04:00
|
|
|
|
2020-08-20 12:08:00 -04:00
|
|
|
STATIC const mp_rom_map_elem_t canio_message_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_id), MP_ROM_PTR(&canio_message_id_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_data), MP_ROM_PTR(&canio_message_data_obj) },
|
2020-09-17 11:57:16 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_extended), MP_ROM_PTR(&canio_message_extended_obj) },
|
2020-08-20 12:08:00 -04:00
|
|
|
};
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(canio_message_locals_dict, canio_message_locals_dict_table);
|
|
|
|
|
|
|
|
const mp_obj_type_t canio_message_type = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_Message,
|
|
|
|
.make_new = canio_message_make_new,
|
|
|
|
.locals_dict = (mp_obj_t)&canio_message_locals_dict,
|
|
|
|
};
|