|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2020 Jeff Epler for Adafruit Industries |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "shared-bindings/canio/RemoteTransmissionRequest.h" |
| 28 | + |
| 29 | +#include "py/obj.h" |
| 30 | +#include "py/objproperty.h" |
| 31 | +#include "py/runtime.h" |
| 32 | + |
| 33 | +//| class RemoteTransmissionRequest: |
| 34 | +//| def __init__(self, id: int, length: int, *, extended: bool = False): |
| 35 | +//| """Construct a RemoteTransmissionRequest to send on a CAN bus. |
| 36 | +//| |
| 37 | +//| :param int id: The numeric ID of the requested message |
| 38 | +//| :param int length: The length of the requested message |
| 39 | +//| :param bool extended: True if the message has an extended identifier, False if it has a standard identifier |
| 40 | +//| |
| 41 | +//| In CAN, messages can have a length from 0 to 8 bytes. |
| 42 | +//| """ |
| 43 | +//| ... |
| 44 | +//| |
| 45 | +STATIC mp_obj_t canio_remote_transmission_request_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 46 | + enum { ARG_id, ARG_length, ARG_extended, NUM_ARGS }; |
| 47 | + static const mp_arg_t allowed_args[] = { |
| 48 | + { MP_QSTR_id, MP_ARG_INT | MP_ARG_REQUIRED }, |
| 49 | + { MP_QSTR_length, MP_ARG_INT | MP_ARG_REQUIRED }, |
| 50 | + { MP_QSTR_extended, MP_ARG_BOOL, {.u_bool = false} }, |
| 51 | + }; |
| 52 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 53 | + MP_STATIC_ASSERT( MP_ARRAY_SIZE(allowed_args) == NUM_ARGS ); |
| 54 | + |
| 55 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 56 | + |
| 57 | + int length = args[ARG_length].u_int; |
| 58 | + if (length < 0 || length > 8) { |
| 59 | + mp_raise_ValueError(translate("RemoteTransmissionRequests limited to 8 bytes")); |
| 60 | + } |
| 61 | + |
| 62 | + canio_remote_transmission_request_obj_t *self = m_new_obj(canio_remote_transmission_request_obj_t); |
| 63 | + self->base.type = &canio_remote_transmission_request_type; |
| 64 | + common_hal_canio_remote_transmission_request_construct(self, args[ARG_id].u_int, length, args[ARG_extended].u_bool); |
| 65 | + return self; |
| 66 | +} |
| 67 | + |
| 68 | + |
| 69 | +//| id: int |
| 70 | +//| """The numeric ID of the message""" |
| 71 | +//| |
| 72 | +STATIC mp_obj_t canio_remote_transmission_request_id_get(const mp_obj_t self_in) { |
| 73 | + canio_remote_transmission_request_obj_t *self = self_in; |
| 74 | + return MP_OBJ_NEW_SMALL_INT(common_hal_canio_remote_transmission_request_get_id(self)); |
| 75 | +} |
| 76 | +MP_DEFINE_CONST_FUN_OBJ_1(canio_remote_transmission_request_id_get_obj, canio_remote_transmission_request_id_get); |
| 77 | + |
| 78 | +STATIC mp_obj_t canio_remote_transmission_request_id_set(const mp_obj_t self_in, const mp_obj_t id) { |
| 79 | + canio_remote_transmission_request_obj_t *self = self_in; |
| 80 | + common_hal_canio_remote_transmission_request_set_id(self, mp_obj_get_int(id)); |
| 81 | + return mp_const_none; |
| 82 | +} |
| 83 | +MP_DEFINE_CONST_FUN_OBJ_2(canio_remote_transmission_request_id_set_obj, canio_remote_transmission_request_id_set); |
| 84 | + |
| 85 | +STATIC const mp_obj_property_t canio_remote_transmission_request_id_obj = { |
| 86 | + .base.type = &mp_type_property, |
| 87 | + .proxy = {(mp_obj_t)&canio_remote_transmission_request_id_get_obj, |
| 88 | + (mp_obj_t)&canio_remote_transmission_request_id_set_obj, |
| 89 | + (mp_obj_t)&mp_const_none_obj}, |
| 90 | +}; |
| 91 | + |
| 92 | +//| extended: bool |
| 93 | +//| """True if the message's id is an extended id""" |
| 94 | +//| |
| 95 | +STATIC mp_obj_t canio_remote_transmission_request_extended_get(const mp_obj_t self_in) { |
| 96 | + canio_remote_transmission_request_obj_t *self = self_in; |
| 97 | + return mp_obj_new_bool(common_hal_canio_remote_transmission_request_get_extended(self)); |
| 98 | +} |
| 99 | +MP_DEFINE_CONST_FUN_OBJ_1(canio_remote_transmission_request_extended_get_obj, canio_remote_transmission_request_extended_get); |
| 100 | + |
| 101 | +STATIC mp_obj_t canio_remote_transmission_request_extended_set(const mp_obj_t self_in, const mp_obj_t extended) { |
| 102 | + canio_remote_transmission_request_obj_t *self = self_in; |
| 103 | + common_hal_canio_remote_transmission_request_set_extended(self, mp_obj_is_true(extended)); |
| 104 | + return mp_const_none; |
| 105 | +} |
| 106 | +MP_DEFINE_CONST_FUN_OBJ_2(canio_remote_transmission_request_extended_set_obj, canio_remote_transmission_request_extended_set); |
| 107 | + |
| 108 | + |
| 109 | +STATIC const mp_obj_property_t canio_remote_transmission_request_extended_obj = { |
| 110 | + .base.type = &mp_type_property, |
| 111 | + .proxy = {(mp_obj_t)&canio_remote_transmission_request_extended_get_obj, |
| 112 | + (mp_obj_t)&canio_remote_transmission_request_extended_set_obj, |
| 113 | + (mp_obj_t)&mp_const_none_obj}, |
| 114 | +}; |
| 115 | + |
| 116 | +//| extended: bool |
| 117 | +//| """True if the message's id is an extended id""" |
| 118 | +//| |
| 119 | + |
| 120 | +//| id: int |
| 121 | +//| """The numeric ID of the message""" |
| 122 | +//| |
| 123 | + |
| 124 | +//| length: int |
| 125 | +//| """The length of the requested message.""" |
| 126 | +//| |
| 127 | +STATIC mp_obj_t canio_remote_transmission_request_length_get(const mp_obj_t self_in) { |
| 128 | + canio_remote_transmission_request_obj_t *self = self_in; |
| 129 | + return MP_OBJ_NEW_SMALL_INT(common_hal_canio_remote_transmission_request_get_length(self)); |
| 130 | +} |
| 131 | +MP_DEFINE_CONST_FUN_OBJ_1(canio_remote_transmission_request_length_get_obj, canio_remote_transmission_request_length_get); |
| 132 | + |
| 133 | +STATIC mp_obj_t canio_remote_transmission_request_length_set(const mp_obj_t self_in, const mp_obj_t length_in) { |
| 134 | + canio_remote_transmission_request_obj_t *self = self_in; |
| 135 | + int length = mp_obj_get_int(length_in); |
| 136 | + if (length < 0 || length > 8) { |
| 137 | + mp_raise_ValueError(translate("RemoteTransmissionRequests limited to 8 bytes")); |
| 138 | + } |
| 139 | + common_hal_canio_remote_transmission_request_set_length(self, length); |
| 140 | + return mp_const_none; |
| 141 | +} |
| 142 | +MP_DEFINE_CONST_FUN_OBJ_2(canio_remote_transmission_request_length_set_obj, canio_remote_transmission_request_length_set); |
| 143 | + |
| 144 | + |
| 145 | +STATIC const mp_obj_property_t canio_remote_transmission_request_length_obj = { |
| 146 | + .base.type = &mp_type_property, |
| 147 | + .proxy = {(mp_obj_t)&canio_remote_transmission_request_length_get_obj, |
| 148 | + (mp_obj_t)&canio_remote_transmission_request_length_set_obj, |
| 149 | + (mp_obj_t)&mp_const_none_obj}, |
| 150 | +}; |
| 151 | + |
| 152 | +STATIC const mp_rom_map_elem_t canio_remote_transmission_request_locals_dict_table[] = { |
| 153 | + { MP_ROM_QSTR(MP_QSTR_id), MP_ROM_PTR(&canio_remote_transmission_request_id_obj) }, |
| 154 | + { MP_ROM_QSTR(MP_QSTR_length), MP_ROM_PTR(&canio_remote_transmission_request_length_obj) }, |
| 155 | + { MP_ROM_QSTR(MP_QSTR_extended), MP_ROM_PTR(&canio_remote_transmission_request_extended_obj) }, |
| 156 | +}; |
| 157 | +STATIC MP_DEFINE_CONST_DICT(canio_remote_transmission_request_locals_dict, canio_remote_transmission_request_locals_dict_table); |
| 158 | + |
| 159 | +const mp_obj_type_t canio_remote_transmission_request_type = { |
| 160 | + { &mp_type_type }, |
| 161 | + .name = MP_QSTR_RemoteTransmissionRequest, |
| 162 | + .make_new = canio_remote_transmission_request_make_new, |
| 163 | + .locals_dict = (mp_obj_t)&canio_remote_transmission_request_locals_dict, |
| 164 | +}; |
0 commit comments