From 8b82c239b825b058efcbe4e3749a6e7d023bdd17 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 24 Sep 2020 11:20:02 -0500 Subject: [PATCH 01/19] canio: doc improvements --- shared-bindings/canio/Message.c | 2 +- shared-bindings/canio/__init__.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/shared-bindings/canio/Message.c b/shared-bindings/canio/Message.c index 23645d2e5f..8d1828fba0 100644 --- a/shared-bindings/canio/Message.c +++ b/shared-bindings/canio/Message.c @@ -117,7 +117,7 @@ STATIC const mp_obj_property_t canio_message_id_obj = { //| data: bytes //| """The content of the message, or dummy content in the case of an rtr. //| -//| Assigning to data also sets the length and clears the rtr flag.""" +//| Assigning to data also clears the rtr flag, if it was set.""" //| STATIC mp_obj_t canio_message_data_get(const mp_obj_t self_in) { canio_message_obj_t *self = self_in; diff --git a/shared-bindings/canio/__init__.c b/shared-bindings/canio/__init__.c index b0b982c950..409cc20cfe 100644 --- a/shared-bindings/canio/__init__.c +++ b/shared-bindings/canio/__init__.c @@ -40,8 +40,8 @@ //| from board import * //| //| can = canio.CAN(board.CAN_RX, board.CAN_TX, baudrate=1000000) -//| message = canio.Message(id=0x0408, data="adafruit" -//| can.write(message)) +//| message = canio.Message(id=0x0408, data=b"adafruit") +//| can.send(message) //| can.deinit() //| //| This example will write the data 'adafruit' onto the CAN bus to any From 79ca430ddf25a71c73d6219fb68160fb405c3777 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 24 Sep 2020 13:58:45 -0500 Subject: [PATCH 02/19] Match: address -> id --- ports/atmel-samd/common-hal/canio/Listener.c | 60 ++++++++++---------- shared-bindings/canio/Match.c | 46 +++++++-------- shared-bindings/canio/Match.h | 4 +- shared-module/canio/Match.c | 8 +-- shared-module/canio/Match.h | 2 +- 5 files changed, 60 insertions(+), 60 deletions(-) diff --git a/ports/atmel-samd/common-hal/canio/Listener.c b/ports/atmel-samd/common-hal/canio/Listener.c index 02cfde9cc8..d21ebbb313 100644 --- a/ports/atmel-samd/common-hal/canio/Listener.c +++ b/ports/atmel-samd/common-hal/canio/Listener.c @@ -58,8 +58,8 @@ STATIC void static_assertions(void) { MP_STATIC_ASSERT(CAN_XIDFE_0_EFEC_STF0M_Val + 1 == CAN_XIDFE_0_EFEC_STF1M_Val); } -STATIC bool single_address_filter(canio_match_obj_t *match) { - return match->mask == 0 || match->mask == match->address; +STATIC bool single_id_filter(canio_match_obj_t *match) { + return match->mask == 0 || match->mask == match->id; } STATIC bool standard_filter_in_use(CanMramSidfe *filter) { @@ -76,7 +76,7 @@ STATIC size_t num_filters_needed(size_t nmatch, canio_match_obj_t **matches, boo if (extended != matches[i]->extended) { continue; } - if (single_address_filter(matches[i])) { + if (single_id_filter(matches[i])) { num_half_filters_needed += 1; } else { num_half_filters_needed += 2; @@ -191,7 +191,7 @@ STATIC void install_extended_filter(CanMramXidfe *extended, int id1, int id2, in } -#define NO_ADDRESS (-1) +#define NO_ID (-1) void set_filters(canio_listener_obj_t *self, size_t nmatch, canio_match_obj_t **matches) { int fifo = self->fifo_idx; @@ -207,31 +207,31 @@ void set_filters(canio_listener_obj_t *self, size_t nmatch, canio_match_obj_t ** CanMramSidfe *standard = next_standard_filter(self, NULL); CanMramXidfe *extended = next_extended_filter(self, NULL); - int first_address = NO_ADDRESS; + int first_id = NO_ID; - // step 1: single address standard matches + // step 1: single id standard matches // we have to gather up pairs and stuff them in a single filter entry for(size_t i = 0; iextended) { continue; } - if (!single_address_filter(match)) { + if (!single_id_filter(match)) { continue; } - if (first_address != NO_ADDRESS) { - install_standard_filter(standard, first_address, match->address, CAN_SIDFE_0_SFEC_STF0M_Val + fifo, CAN_SIDFE_0_SFT_DUAL_Val); - first_address = NO_ADDRESS; + if (first_id != NO_ID) { + install_standard_filter(standard, first_id, match->id, CAN_SIDFE_0_SFEC_STF0M_Val + fifo, CAN_SIDFE_0_SFT_DUAL_Val); + first_id = NO_ID; standard = next_standard_filter(self, standard); } else { - first_address = match->address; + first_id = match->id; } } - // step 1.5. odd single address standard match - if (first_address != NO_ADDRESS) { - install_standard_filter(standard, first_address, first_address, CAN_SIDFE_0_SFEC_STF0M_Val + fifo, CAN_SIDFE_0_SFT_DUAL_Val); + // step 1.5. odd single id standard match + if (first_id != NO_ID) { + install_standard_filter(standard, first_id, first_id, CAN_SIDFE_0_SFEC_STF0M_Val + fifo, CAN_SIDFE_0_SFT_DUAL_Val); standard = next_standard_filter(self, standard); - first_address = NO_ADDRESS; + first_id = NO_ID; } // step 2: standard mask filter @@ -240,36 +240,36 @@ void set_filters(canio_listener_obj_t *self, size_t nmatch, canio_match_obj_t ** if (match->extended) { continue; } - if (single_address_filter(match)) { + if (single_id_filter(match)) { continue; } - install_standard_filter(standard, match->address, match->mask, CAN_SIDFE_0_SFEC_STF0M_Val + fifo, CAN_SIDFE_0_SFT_CLASSIC_Val); + install_standard_filter(standard, match->id, match->mask, CAN_SIDFE_0_SFEC_STF0M_Val + fifo, CAN_SIDFE_0_SFT_CLASSIC_Val); standard = next_standard_filter(self, standard); } - // step 3: single address extended matches + // step 3: single id extended matches // we have to gather up pairs and stuff them in a single filter entry for(size_t i = 0; iextended) { continue; } - if (!single_address_filter(match)) { + if (!single_id_filter(match)) { continue; } - if (first_address != NO_ADDRESS) { - install_extended_filter(extended, first_address, match->address, CAN_XIDFE_0_EFEC_STF0M_Val + fifo, CAN_XIDFE_1_EFT_DUAL_Val); - first_address = NO_ADDRESS; + if (first_id != NO_ID) { + install_extended_filter(extended, first_id, match->id, CAN_XIDFE_0_EFEC_STF0M_Val + fifo, CAN_XIDFE_1_EFT_DUAL_Val); + first_id = NO_ID; extended = next_extended_filter(self, extended); } else { - first_address = match->address; + first_id = match->id; } } - // step 3.5. odd single address standard match - if (first_address != NO_ADDRESS) { - install_extended_filter(extended, first_address, first_address, CAN_XIDFE_0_EFEC_STF0M_Val + fifo, CAN_XIDFE_1_EFT_DUAL_Val); + // step 3.5. odd single id standard match + if (first_id != NO_ID) { + install_extended_filter(extended, first_id, first_id, CAN_XIDFE_0_EFEC_STF0M_Val + fifo, CAN_XIDFE_1_EFT_DUAL_Val); extended = next_extended_filter(self, extended); - first_address = NO_ADDRESS; + first_id = NO_ID; } // step 4: extended mask filters @@ -278,10 +278,10 @@ void set_filters(canio_listener_obj_t *self, size_t nmatch, canio_match_obj_t ** if (!match->extended) { continue; } - if (single_address_filter(match)) { + if (single_id_filter(match)) { continue; } - install_extended_filter(extended, match->address, match->mask, CAN_XIDFE_0_EFEC_STF0M_Val + fifo, CAN_XIDFE_1_EFT_CLASSIC_Val); + install_extended_filter(extended, match->id, match->mask, CAN_XIDFE_0_EFEC_STF0M_Val + fifo, CAN_XIDFE_1_EFT_CLASSIC_Val); extended = next_extended_filter(self, extended); } @@ -363,7 +363,7 @@ bool common_hal_canio_listener_readinto(canio_listener_obj_t *self, canio_messag if (message->extended) { message->id = hw_message->rxf0.bit.ID; } else { - message->id = hw_message->rxf0.bit.ID >> 18; // short addresses are left-justified + message->id = hw_message->rxf0.bit.ID >> 18; // short ids are left-justified } message->rtr = hw_message->rxf0.bit.RTR; message->size = hw_message->rxf1.bit.DLC; diff --git a/shared-bindings/canio/Match.c b/shared-bindings/canio/Match.c index ff5d028fad..91a9dd02a3 100644 --- a/shared-bindings/canio/Match.c +++ b/shared-bindings/canio/Match.c @@ -33,19 +33,19 @@ //| """Describe CAN bus messages to match""" //| //| -//| def __init__(self, address: int, *, mask: int = 0, extended: bool = False): +//| def __init__(self, id: int, *, mask: int = 0, extended: bool = False): //| """Construct a Match with the given properties. //| -//| If mask is nonzero, then the filter is for any sender which matches all -//| the nonzero bits in mask. Otherwise, it matches exactly the given address. -//| If extended is true then only extended addresses are matched, otherwise -//| only standard addresses are matched.""" +//| If mask is nonzero, then the filter is for any id which matches all +//| the nonzero bits in mask. Otherwise, it matches exactly the given id. +//| If extended is true then only extended ids are matched, otherwise +//| only standard ids are matched.""" //| STATIC mp_obj_t canio_match_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_address, ARG_mask, ARG_extended, NUM_ARGS }; + enum { ARG_id, ARG_mask, ARG_extended, NUM_ARGS }; static const mp_arg_t allowed_args[] = { - { MP_QSTR_address, MP_ARG_INT | MP_ARG_REQUIRED }, + { MP_QSTR_id, MP_ARG_INT | MP_ARG_REQUIRED }, { MP_QSTR_mask, MP_ARG_INT, {.u_int = 0} }, { MP_QSTR_extended, MP_ARG_BOOL, {.u_bool = false} }, }; @@ -54,44 +54,44 @@ STATIC mp_obj_t canio_match_make_new(const mp_obj_type_t *type, size_t n_args, c mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - int address_bits = args[ARG_extended].u_bool ? 0x1fffffff : 0x7ff; - int address = args[ARG_address].u_int; + int id_bits = args[ARG_extended].u_bool ? 0x1fffffff : 0x7ff; + int id = args[ARG_id].u_int; int mask = args[ARG_mask].u_int; - if (address & ~address_bits) { - mp_raise_ValueError_varg(translate("%q out of range"), MP_QSTR_address); + if (id & ~id_bits) { + mp_raise_ValueError_varg(translate("%q out of range"), MP_QSTR_id); } - if (mask & ~address_bits) { + if (mask & ~id_bits) { mp_raise_ValueError_varg(translate("%q out of range"), MP_QSTR_mask); } canio_match_obj_t *self = m_new_obj(canio_match_obj_t); self->base.type = &canio_match_type; - common_hal_canio_match_construct(self, args[ARG_address].u_int, args[ARG_mask].u_int, args[ARG_extended].u_bool); + common_hal_canio_match_construct(self, args[ARG_id].u_int, args[ARG_mask].u_int, args[ARG_extended].u_bool); return self; } -//| address: int -//| """The address to match""" +//| id: int +//| """The id to match""" //| -STATIC mp_obj_t canio_match_address_get(mp_obj_t self_in) { +STATIC mp_obj_t canio_match_id_get(mp_obj_t self_in) { canio_match_obj_t *self = self_in; - return MP_OBJ_NEW_SMALL_INT(common_hal_canio_match_get_address(self)); + return MP_OBJ_NEW_SMALL_INT(common_hal_canio_match_get_id(self)); } -MP_DEFINE_CONST_FUN_OBJ_1(canio_match_address_get_obj, canio_match_address_get); +MP_DEFINE_CONST_FUN_OBJ_1(canio_match_id_get_obj, canio_match_id_get); -const mp_obj_property_t canio_match_address_obj = { +const mp_obj_property_t canio_match_id_obj = { .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&canio_match_address_get_obj, + .proxy = {(mp_obj_t)&canio_match_id_get_obj, (mp_obj_t)&mp_const_none_obj, (mp_obj_t)&mp_const_none_obj}, }; //| //| mask: int -//| """The optional mask of addresses to match""" +//| """The optional mask of ids to match""" //| STATIC mp_obj_t canio_match_mask_get(mp_obj_t self_in) { @@ -108,7 +108,7 @@ const mp_obj_property_t canio_match_mask_obj = { }; //| extended: bool -//| """True to match extended addresses, False to match standard addresses""" +//| """True to match extended ids, False to match standard ides""" //| STATIC mp_obj_t canio_match_extended_get(mp_obj_t self_in) { @@ -125,7 +125,7 @@ const mp_obj_property_t canio_match_extended_obj = { }; STATIC const mp_rom_map_elem_t canio_match_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_address), MP_ROM_PTR(&canio_match_address_obj) }, + { MP_ROM_QSTR(MP_QSTR_id), MP_ROM_PTR(&canio_match_id_obj) }, { MP_ROM_QSTR(MP_QSTR_mask), MP_ROM_PTR(&canio_match_mask_obj) }, { MP_ROM_QSTR(MP_QSTR_extended), MP_ROM_PTR(&canio_match_extended_obj) }, }; diff --git a/shared-bindings/canio/Match.h b/shared-bindings/canio/Match.h index 88996d730f..01a75fd1c6 100644 --- a/shared-bindings/canio/Match.h +++ b/shared-bindings/canio/Match.h @@ -31,7 +31,7 @@ extern const mp_obj_type_t canio_match_type; -void common_hal_canio_match_construct(canio_match_obj_t *self, int address, int mask, bool extended); -int common_hal_canio_match_get_address(const canio_match_obj_t *self); +void common_hal_canio_match_construct(canio_match_obj_t *self, int id, int mask, bool extended); +int common_hal_canio_match_get_id(const canio_match_obj_t *self); int common_hal_canio_match_get_mask(const canio_match_obj_t *self); bool common_hal_canio_match_get_extended(const canio_match_obj_t *self); diff --git a/shared-module/canio/Match.c b/shared-module/canio/Match.c index 9e33b956f6..b4e8616e9b 100644 --- a/shared-module/canio/Match.c +++ b/shared-module/canio/Match.c @@ -26,14 +26,14 @@ #include "shared-module/canio/Match.h" -void common_hal_canio_match_construct(canio_match_obj_t *self, int address, int mask, bool extended) { - self->address = address; +void common_hal_canio_match_construct(canio_match_obj_t *self, int id, int mask, bool extended) { + self->id = id; self->mask = mask; self->extended = extended; } -int common_hal_canio_match_get_address(const canio_match_obj_t *self) { - return self->address; +int common_hal_canio_match_get_id(const canio_match_obj_t *self) { + return self->id; } int common_hal_canio_match_get_mask(const canio_match_obj_t *self) { return self->mask; diff --git a/shared-module/canio/Match.h b/shared-module/canio/Match.h index 25f047f37e..b52191d7c4 100644 --- a/shared-module/canio/Match.h +++ b/shared-module/canio/Match.h @@ -30,7 +30,7 @@ typedef struct { mp_obj_base_t base; - int address; + int id; int mask; bool extended; } canio_match_obj_t; From 48bda589b81acad03bfe088c4c754040e9eb0783 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 24 Sep 2020 14:05:16 -0500 Subject: [PATCH 03/19] Listerner: read -> receive, drop readinto This prepares for creating a separate RemoteTransmissionRequest class --- ports/atmel-samd/common-hal/canio/Listener.c | 2 +- ports/atmel-samd/common-hal/canio/Listener.h | 4 +-- shared-bindings/canio/CAN.h | 1 + shared-bindings/canio/Listener.c | 29 ++++---------------- shared-bindings/canio/Listener.h | 12 ++++++++ 5 files changed, 22 insertions(+), 26 deletions(-) diff --git a/ports/atmel-samd/common-hal/canio/Listener.c b/ports/atmel-samd/common-hal/canio/Listener.c index d21ebbb313..7137f806bf 100644 --- a/ports/atmel-samd/common-hal/canio/Listener.c +++ b/ports/atmel-samd/common-hal/canio/Listener.c @@ -348,7 +348,7 @@ int common_hal_canio_listener_in_waiting(canio_listener_obj_t *self) { return self->hw->RXFS.bit.F0FL; } -bool common_hal_canio_listener_readinto(canio_listener_obj_t *self, canio_message_obj_t *message) { +bool common_hal_canio_listener_receiveinto(canio_listener_obj_t *self, canio_message_obj_t *message) { if (!common_hal_canio_listener_in_waiting(self)) { uint64_t deadline = supervisor_ticks_ms64() + self->timeout_ms; do { diff --git a/ports/atmel-samd/common-hal/canio/Listener.h b/ports/atmel-samd/common-hal/canio/Listener.h index 1b81d82aa6..10e1a5f8ad 100644 --- a/ports/atmel-samd/common-hal/canio/Listener.h +++ b/ports/atmel-samd/common-hal/canio/Listener.h @@ -35,7 +35,7 @@ typedef struct { __IO CAN_RXF0A_Type RXFA; /**< \brief (R/W 32) Rx FIFO n Acknowledge */ } canio_rxfifo_reg_t; -typedef struct { +typedef struct canio_listener_obj { mp_obj_base_t base; canio_can_obj_t *can; canio_can_rx_fifo_t *fifo; @@ -47,7 +47,7 @@ typedef struct { void common_hal_canio_listener_construct(canio_listener_obj_t *self, canio_can_obj_t *can, size_t nmatch, canio_match_obj_t **matches, float timeout); void common_hal_canio_listener_check_for_deinit(canio_listener_obj_t *self); void common_hal_canio_listener_deinit(canio_listener_obj_t *self); -bool common_hal_canio_listener_readinto(canio_listener_obj_t *self, canio_message_obj_t *message); +bool common_hal_canio_listener_receiveinto(canio_listener_obj_t *self, canio_message_obj_t *message); int common_hal_canio_listener_in_waiting(canio_listener_obj_t *self); float common_hal_canio_listener_get_timeout(canio_listener_obj_t *self); void common_hal_canio_listener_set_timeout(canio_listener_obj_t *self, float timeout); diff --git a/shared-bindings/canio/CAN.h b/shared-bindings/canio/CAN.h index 66c972c285..08166dfd42 100644 --- a/shared-bindings/canio/CAN.h +++ b/shared-bindings/canio/CAN.h @@ -28,6 +28,7 @@ #include "py/obj.h" #include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/canio/__init__.h" #include "shared-bindings/canio/Message.h" extern const mp_obj_type_t canio_can_type; diff --git a/shared-bindings/canio/Listener.c b/shared-bindings/canio/Listener.c index 1f4176f0c0..5159c8481f 100644 --- a/shared-bindings/canio/Listener.c +++ b/shared-bindings/canio/Listener.c @@ -38,44 +38,28 @@ //| Listen method of a canio.CAN object.""" //| -//| def read(self) -> Optional[Message]: +//| def receive(self) -> Optional[Message]: //| """Reads a message, after waiting up to self.timeout seconds //| //| If no message is received in time, None is returned. Otherwise, //| a Message is returned.""" //| ... //| -STATIC mp_obj_t canio_listener_read(mp_obj_t self_in) { +STATIC mp_obj_t canio_listener_receive(mp_obj_t self_in) { canio_listener_obj_t *self = MP_OBJ_TO_PTR(self_in); common_hal_canio_listener_check_for_deinit(self); canio_message_obj_t *message = m_new_obj(canio_message_obj_t); message->base.type = &canio_message_type; - if (common_hal_canio_listener_readinto(self, message)) { + if (common_hal_canio_listener_receiveinto(self, message)) { return message; } else { m_free(message); // message did not escape into vm } return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(canio_listener_read_obj, canio_listener_read); - -//| def readinto(self, message: Message) -> bool: -//| """Returns True (and modifies message) if a message was received -//| before ``timeout`` seconds elapsed, False otherwise.""" -//| ... -//| -STATIC mp_obj_t canio_listener_readinto(mp_obj_t self_in, mp_obj_t message) { - canio_listener_obj_t *self = MP_OBJ_TO_PTR(self_in); - mp_obj_type_t *type = mp_obj_get_type(message); - if (type != &canio_message_type) { - mp_raise_TypeError_varg(translate("expected '%q' but got '%q'"), MP_QSTR_Message, type->name); - } - common_hal_canio_listener_check_for_deinit(self); - return mp_obj_new_bool(common_hal_canio_listener_readinto(self, message)); -} -STATIC MP_DEFINE_CONST_FUN_OBJ_2(canio_listener_readinto_obj, canio_listener_readinto); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(canio_listener_receive_obj, canio_listener_receive); //| def in_waiting(self) -> int: //| """Returns the number of messages waiting""" @@ -100,7 +84,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(canio_listener_in_waiting_obj, canio_listener_i //| ... //| STATIC mp_obj_t canio_iternext(mp_obj_t self_in) { - mp_obj_t result = canio_listener_read(self_in); + mp_obj_t result = canio_listener_receive(self_in); if (result == mp_const_none) { return MP_OBJ_STOP_ITERATION; } @@ -170,8 +154,7 @@ STATIC const mp_rom_map_elem_t canio_listener_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&canio_listener_exit_obj) }, { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&canio_listener_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR_in_waiting), MP_ROM_PTR(&canio_listener_in_waiting_obj) }, - { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&canio_listener_read_obj) }, - { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&canio_listener_readinto_obj) }, + { MP_ROM_QSTR(MP_QSTR_receive), MP_ROM_PTR(&canio_listener_receive_obj) }, { MP_ROM_QSTR(MP_QSTR_timeout), MP_ROM_PTR(&canio_listener_timeout_obj) }, }; STATIC MP_DEFINE_CONST_DICT(canio_listener_locals_dict, canio_listener_locals_dict_table); diff --git a/shared-bindings/canio/Listener.h b/shared-bindings/canio/Listener.h index eaa3490dff..d925fa2652 100644 --- a/shared-bindings/canio/Listener.h +++ b/shared-bindings/canio/Listener.h @@ -27,5 +27,17 @@ #pragma once #include "py/obj.h" +#include "shared-bindings/canio/CAN.h" +#include "shared-bindings/canio/Match.h" extern const mp_obj_type_t canio_listener_type; + +typedef struct canio_listener_obj canio_listener_obj_t; + +void common_hal_canio_listener_construct(canio_listener_obj_t *self, canio_can_obj_t *can, size_t nmatch, canio_match_obj_t **matches, float timeout); +void common_hal_canio_listener_check_for_deinit(canio_listener_obj_t *self); +void common_hal_canio_listener_deinit(canio_listener_obj_t *self); +bool common_hal_canio_listener_receiveinto(canio_listener_obj_t *self, canio_message_obj_t *message); +int common_hal_canio_listener_in_waiting(canio_listener_obj_t *self); +float common_hal_canio_listener_get_timeout(canio_listener_obj_t *self); +void common_hal_canio_listener_set_timeout(canio_listener_obj_t *self, float timeout); From 8d45be1cd99c6fbfcb69877ab335e010810c54fd Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 24 Sep 2020 15:25:06 -0500 Subject: [PATCH 04/19] canio: Split RemoteTransmissionRequest to its own class It reuses most of canio.Message's implementation, and structure --- shared-bindings/canio/CAN.c | 6 +- shared-bindings/canio/Listener.c | 6 +- shared-bindings/canio/Message.c | 138 +++++++++++++++++++------------ shared-bindings/canio/Message.h | 7 +- shared-bindings/canio/__init__.c | 1 + shared-module/canio/Message.c | 10 +-- 6 files changed, 102 insertions(+), 66 deletions(-) diff --git a/shared-bindings/canio/CAN.c b/shared-bindings/canio/CAN.c index ff27bb2048..6b7a3878eb 100644 --- a/shared-bindings/canio/CAN.c +++ b/shared-bindings/canio/CAN.c @@ -265,7 +265,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(canio_can_restart_obj, canio_can_restart); //| //| An empty filter list causes all messages to be accepted. //| -//| Timeout dictates how long readinto, read and next() will block.""" +//| Timeout dictates how long receive() and next() will block.""" //| ... //| STATIC mp_obj_t canio_can_listen(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { @@ -334,8 +334,8 @@ STATIC mp_obj_t canio_can_send(mp_obj_t self_in, mp_obj_t message_in) { canio_can_obj_t *self = MP_OBJ_TO_PTR(self_in); common_hal_canio_can_check_for_deinit(self); mp_obj_type_t *message_type = mp_obj_get_type(message_in); - if (message_type != &canio_message_type) { - mp_raise_TypeError_varg(translate("expected '%q' but got '%q'"), MP_QSTR_Message, message_type->name); + if (message_type != &canio_message_type && message_type != &canio_remote_transmission_request_type) { + mp_raise_TypeError_varg(translate("expected '%q' or '%q' but got '%q'"), MP_QSTR_Message, MP_QSTR_RemoteTransmissionRequest, message_type->name); } canio_message_obj_t *message = message_in; diff --git a/shared-bindings/canio/Listener.c b/shared-bindings/canio/Listener.c index 5159c8481f..9f3fe05c5d 100644 --- a/shared-bindings/canio/Listener.c +++ b/shared-bindings/canio/Listener.c @@ -50,9 +50,13 @@ STATIC mp_obj_t canio_listener_receive(mp_obj_t self_in) { common_hal_canio_listener_check_for_deinit(self); canio_message_obj_t *message = m_new_obj(canio_message_obj_t); - message->base.type = &canio_message_type; if (common_hal_canio_listener_receiveinto(self, message)) { + if (message->rtr) { + message->base.type = &canio_remote_transmission_request_type; + } else { + message->base.type = &canio_message_type; + } return message; } else { m_free(message); // message did not escape into vm diff --git a/shared-bindings/canio/Message.c b/shared-bindings/canio/Message.c index 8d1828fba0..ae03c25e76 100644 --- a/shared-bindings/canio/Message.c +++ b/shared-bindings/canio/Message.c @@ -31,28 +31,22 @@ #include "py/runtime.h" //| class Message: -//| def __init__(self, id: int=0, data: Optional[bytes] = None, *, size: Optional[int] = None, rtr: bool = False, extended: bool = False): -//| """Construct a Message to use with a CAN bus. Provide arguments to create a message to send. Otherwise, use Listener.readinto() to read a message. +//| def __init__(self, id: int, data: bytes, *, extended: bool = False): +//| """Construct a Message to send on a CAN bus. //| //| :param int id: The numeric ID of the message //| :param bytes data: The content of the message -//| :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 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. -//| -//| For a non-rtr message, specify ``data``. For an rtr-message, specify either ``data`` (a dummy buffer of the requested size) or ``size``. +//| In CAN, messages can have a length from 0 to 8 bytes. //| """ //| ... //| 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, ARG_extended, NUM_ARGS }; + enum { ARG_id, ARG_data, ARG_extended, NUM_ARGS }; static const mp_arg_t allowed_args[] = { - { MP_QSTR_id, MP_ARG_INT, {.u_obj = 0} }, - { MP_QSTR_data, MP_ARG_OBJ, {.u_obj = 0} }, - { MP_QSTR_size, MP_ARG_INT, {.u_int = -1} }, - { MP_QSTR_rtr, MP_ARG_BOOL, {.u_bool = false} }, + { MP_QSTR_id, MP_ARG_INT | MP_ARG_REQUIRED }, + { MP_QSTR_data, MP_ARG_OBJ | MP_ARG_REQUIRED }, { MP_QSTR_extended, MP_ARG_BOOL, {.u_bool = false} }, }; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; @@ -60,26 +54,8 @@ 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); - bool rtr = args[ARG_rtr].u_bool; - bool extended = args[ARG_extended].u_bool; - size_t size = (size_t)args[ARG_size].u_int; - bool specified_size = (size != (size_t)-1); - bool specified_data = (args[ARG_data].u_obj != NULL); - - if (specified_size && specified_data) { - mp_raise_TypeError(translate("specify size or data, but not both")); - } - mp_buffer_info_t data; - if (specified_data) { - mp_get_buffer_raise(args[ARG_data].u_obj, &data, MP_BUFFER_READ); - } else if (specified_size) { - data.buf = 0; - data.len = size; - } else { - data.buf = 0; - data.len = 0; - } + mp_get_buffer_raise(args[ARG_data].u_obj, &data, MP_BUFFER_READ); if (data.len > 8) { mp_raise_ValueError(translate("Messages limited to 8 bytes")); @@ -87,7 +63,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); self->base.type = &canio_message_type; - common_hal_canio_message_construct(self, args[ARG_id].u_int, data.buf, data.len, rtr, extended); + common_hal_canio_message_construct(self, args[ARG_id].u_int, data.buf, data.len, args[ARG_extended].u_bool); return self; } @@ -115,13 +91,11 @@ STATIC const mp_obj_property_t canio_message_id_obj = { }; //| data: bytes -//| """The content of the message, or dummy content in the case of an rtr. -//| -//| Assigning to data also clears the rtr flag, if it was set.""" +//| """The content of the message""" //| STATIC mp_obj_t canio_message_data_get(const mp_obj_t self_in) { canio_message_obj_t *self = self_in; - return mp_obj_new_bytes((const byte*)common_hal_canio_message_get_data(self), common_hal_canio_message_get_size(self)); + return mp_obj_new_bytes((const byte*)common_hal_canio_message_get_data(self), common_hal_canio_message_get_length(self)); } MP_DEFINE_CONST_FUN_OBJ_1(canio_message_data_get_obj, canio_message_data_get); @@ -147,7 +121,7 @@ STATIC const mp_obj_property_t canio_message_data_obj = { //| extended: bool -//| """True if the message represents a remote transmission request (RTR)""" +//| """True if the message's id is an extended id""" //| STATIC mp_obj_t canio_message_extended_get(const mp_obj_t self_in) { canio_message_obj_t *self = self_in; @@ -170,36 +144,80 @@ STATIC const mp_obj_property_t canio_message_extended_obj = { (mp_obj_t)&mp_const_none_obj}, }; - -//| rtr: bool -//| """True if the message represents a remote transmission request (RTR). Setting rtr to true zeros out data""" +//| class RemoteTransmissionRequest: +//| def __init__(self, id: int, length: int, *, extended: bool = False): +//| """Construct a Message to send on a CAN bus. //| -STATIC mp_obj_t canio_message_rtr_get(const mp_obj_t self_in) { - canio_message_obj_t *self = self_in; - return mp_obj_new_bool(common_hal_canio_message_get_rtr(self)); -} -MP_DEFINE_CONST_FUN_OBJ_1(canio_message_rtr_get_obj, canio_message_rtr_get); +//| :param int id: The numeric ID of the requested message +//| :param int length: The length of the requested message +//| :param bool extended: True if the message has an extended identifier, False if it has a standard identifier +//| +//| In CAN, messages can have a length from 0 to 8 bytes. +//| """ +//| ... +//| +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) { + enum { ARG_id, ARG_length, ARG_extended, NUM_ARGS }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_id, MP_ARG_INT | MP_ARG_REQUIRED }, + { MP_QSTR_length, MP_ARG_INT | MP_ARG_REQUIRED }, + { MP_QSTR_extended, MP_ARG_BOOL, {.u_bool = false} }, + }; + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + MP_STATIC_ASSERT( MP_ARRAY_SIZE(allowed_args) == NUM_ARGS ); -STATIC mp_obj_t canio_message_rtr_set(const mp_obj_t self_in, const mp_obj_t rtr) { + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + int length = args[ARG_length].u_int; + if (length < 0 || length > 8) { + mp_raise_ValueError(translate("Messages limited to 8 bytes")); + } + + canio_message_obj_t *self = m_new_obj(canio_message_obj_t); + self->base.type = &canio_remote_transmission_request_type; + common_hal_canio_message_construct(self, args[ARG_id].u_int, NULL, length, args[ARG_extended].u_bool); + return self; +} + +//| extended: bool +//| """True if the message's id is an extended id""" +//| + +//| id: int +//| """The numeric ID of the message""" +//| + +//| length: int +//| """The length of the requested message.""" +//| +STATIC mp_obj_t canio_remote_transmission_request_length_get(const mp_obj_t self_in) { canio_message_obj_t *self = self_in; - common_hal_canio_message_set_rtr(self, mp_obj_is_true(rtr)); + return MP_OBJ_NEW_SMALL_INT(common_hal_canio_message_get_length(self)); +} +MP_DEFINE_CONST_FUN_OBJ_1(canio_remote_transmission_request_length_get_obj, canio_remote_transmission_request_length_get); + +STATIC mp_obj_t canio_remote_transmission_request_length_set(const mp_obj_t self_in, const mp_obj_t length_in) { + canio_message_obj_t *self = self_in; + int length = mp_obj_get_int(length_in); + if (length < 0 || length > 8) { + mp_raise_ValueError(translate("Messages limited to 8 bytes")); + } + common_hal_canio_remote_transmission_request_set_length(self, length); return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_2(canio_message_rtr_set_obj, canio_message_rtr_set); +MP_DEFINE_CONST_FUN_OBJ_2(canio_remote_transmission_request_length_set_obj, canio_remote_transmission_request_length_set); -STATIC const mp_obj_property_t canio_message_rtr_obj = { +STATIC const mp_obj_property_t canio_remote_transmission_request_length_obj = { .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&canio_message_rtr_get_obj, - (mp_obj_t)&canio_message_rtr_set_obj, + .proxy = {(mp_obj_t)&canio_remote_transmission_request_length_get_obj, + (mp_obj_t)&canio_remote_transmission_request_length_set_obj, (mp_obj_t)&mp_const_none_obj}, }; - 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) }, - { 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); @@ -210,3 +228,17 @@ const mp_obj_type_t canio_message_type = { .make_new = canio_message_make_new, .locals_dict = (mp_obj_t)&canio_message_locals_dict, }; + +STATIC const mp_rom_map_elem_t canio_remote_transmission_request_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_id), MP_ROM_PTR(&canio_message_id_obj) }, + { MP_ROM_QSTR(MP_QSTR_length), MP_ROM_PTR(&canio_remote_transmission_request_length_obj) }, + { MP_ROM_QSTR(MP_QSTR_extended), MP_ROM_PTR(&canio_message_extended_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(canio_remote_transmission_request_locals_dict, canio_remote_transmission_request_locals_dict_table); + +const mp_obj_type_t canio_remote_transmission_request_type = { + { &mp_type_type }, + .name = MP_QSTR_RemoteTransmissionRequest, + .make_new = canio_remote_transmission_request_make_new, + .locals_dict = (mp_obj_t)&canio_remote_transmission_request_locals_dict, +}; diff --git a/shared-bindings/canio/Message.h b/shared-bindings/canio/Message.h index 34b632e847..96716864cf 100644 --- a/shared-bindings/canio/Message.h +++ b/shared-bindings/canio/Message.h @@ -30,8 +30,9 @@ #include "shared-module/canio/Message.h" extern const mp_obj_type_t canio_message_type; +extern const mp_obj_type_t canio_remote_transmission_request_type; -void common_hal_canio_message_construct(canio_message_obj_t *self, int id, void *data, size_t size, bool rtr, bool extended); +void common_hal_canio_message_construct(canio_message_obj_t *self, int id, void *data, size_t size, bool extended); const void *common_hal_canio_message_get_data(const canio_message_obj_t *self); void common_hal_canio_message_set_data(canio_message_obj_t *self, const void *data, size_t size); bool common_hal_canio_message_get_extended(const canio_message_obj_t *self); @@ -40,5 +41,5 @@ int common_hal_canio_message_get_id(const canio_message_obj_t *self); void common_hal_canio_message_set_id(canio_message_obj_t *self, int id); bool common_hal_canio_message_get_rtr(const canio_message_obj_t *self); void common_hal_canio_message_set_rtr(canio_message_obj_t *self, bool rtr); -size_t common_hal_canio_message_get_size(const canio_message_obj_t *self); -void common_hal_canio_message_set_size(canio_message_obj_t *self, size_t size); +size_t common_hal_canio_message_get_length(const canio_message_obj_t *self); +void common_hal_canio_remote_transmission_request_set_length(canio_message_obj_t *self, size_t length); diff --git a/shared-bindings/canio/__init__.c b/shared-bindings/canio/__init__.c index 409cc20cfe..f29d3ab8ac 100644 --- a/shared-bindings/canio/__init__.c +++ b/shared-bindings/canio/__init__.c @@ -113,6 +113,7 @@ STATIC const mp_rom_map_elem_t canio_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_Listener), MP_ROM_PTR(&canio_listener_type) }, { MP_ROM_QSTR(MP_QSTR_Match), MP_ROM_PTR(&canio_match_type) }, { MP_ROM_QSTR(MP_QSTR_Message), MP_ROM_PTR(&canio_message_type) }, + { MP_ROM_QSTR(MP_QSTR_RemoteTransmissionRequest), MP_ROM_PTR(&canio_remote_transmission_request_type) }, { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR__canio) }, }; diff --git a/shared-module/canio/Message.c b/shared-module/canio/Message.c index b8ebb78596..86a3b71069 100644 --- a/shared-module/canio/Message.c +++ b/shared-module/canio/Message.c @@ -28,16 +28,14 @@ #include -void common_hal_canio_message_construct(canio_message_obj_t *self, int id, void *data, size_t size, bool rtr, bool extended) +void common_hal_canio_message_construct(canio_message_obj_t *self, int id, void *data, size_t size, bool extended) { self->id = id; self->size = size; - self->rtr = rtr; + self->rtr = !data; self->extended = extended; if (data) { memcpy(self->data, data, size); - } else { - memset(self->data, 0, size); } } @@ -65,12 +63,12 @@ const void common_hal_canio_message_set_data(canio_message_obj_t *self, const vo } -size_t common_hal_canio_message_get_size(const canio_message_obj_t *self) +size_t common_hal_canio_message_get_length(const canio_message_obj_t *self) { return self->size; } -void common_hal_canio_message_set_size(canio_message_obj_t *self, size_t size) +void common_hal_canio_remote_transmission_request_set_length(canio_message_obj_t *self, size_t size) { memset(self->data, 0, size); self->size = size; From f4fd5bbfc48c162647705aa68d2dded71a38c959 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Sat, 26 Sep 2020 09:46:26 -0500 Subject: [PATCH 05/19] canio: docs: fix how we refer to 'the listen method' --- shared-bindings/canio/Listener.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shared-bindings/canio/Listener.c b/shared-bindings/canio/Listener.c index 9f3fe05c5d..1aaa092178 100644 --- a/shared-bindings/canio/Listener.c +++ b/shared-bindings/canio/Listener.c @@ -34,8 +34,8 @@ //| class Listener: //| """Listens for CAN message //| -//| canio.Listener is not constructed directly, but instead by calling the -//| Listen method of a canio.CAN object.""" +//| canio.Listener is not constructed directly, but instead by calling +//| `~canio.CAN.listen`.""" //| //| def receive(self) -> Optional[Message]: From ea2f5b63963f7bf281fad6f2d29b2b673826610e Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Sat, 26 Sep 2020 10:04:30 -0500 Subject: [PATCH 06/19] canio: Correct type annotations of CAN.send, Listener.receive --- shared-bindings/canio/CAN.c | 2 +- shared-bindings/canio/Listener.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/shared-bindings/canio/CAN.c b/shared-bindings/canio/CAN.c index 6b7a3878eb..b1fe7d28ba 100644 --- a/shared-bindings/canio/CAN.c +++ b/shared-bindings/canio/CAN.c @@ -324,7 +324,7 @@ STATIC const mp_obj_property_t canio_can_loopback_obj = { }; -//| def send(message: Message) -> None: +//| def send(message: Union[RemoteTransmissionRequest, Message]) -> None: //| """Send a message on the bus with the given data and id. //| If the message could not be sent due to a full fifo or a bus error condition, RuntimeError is raised. //| """ diff --git a/shared-bindings/canio/Listener.c b/shared-bindings/canio/Listener.c index 1aaa092178..7b0e59063c 100644 --- a/shared-bindings/canio/Listener.c +++ b/shared-bindings/canio/Listener.c @@ -38,7 +38,7 @@ //| `~canio.CAN.listen`.""" //| -//| def receive(self) -> Optional[Message]: +//| def receive(self) -> Optional[Union[RemoteTransmissionRequest,Message]]: //| """Reads a message, after waiting up to self.timeout seconds //| //| If no message is received in time, None is returned. Otherwise, From 3e97e9c4be9798c5cd6d4a8a633bdadd3246577c Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Sat, 26 Sep 2020 09:35:48 -0500 Subject: [PATCH 07/19] canio: Listener: shuffle function declarations around --- ports/atmel-samd/common-hal/canio/Listener.c | 1 + ports/atmel-samd/common-hal/canio/Listener.h | 8 -------- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/ports/atmel-samd/common-hal/canio/Listener.c b/ports/atmel-samd/common-hal/canio/Listener.c index 7137f806bf..483802fa4a 100644 --- a/ports/atmel-samd/common-hal/canio/Listener.c +++ b/ports/atmel-samd/common-hal/canio/Listener.c @@ -32,6 +32,7 @@ #include "common-hal/canio/__init__.h" #include "common-hal/canio/Listener.h" +#include "shared-bindings/canio/Listener.h" #include "shared-bindings/util.h" #include "supervisor/shared/tick.h" #include "component/can.h" diff --git a/ports/atmel-samd/common-hal/canio/Listener.h b/ports/atmel-samd/common-hal/canio/Listener.h index 10e1a5f8ad..237dca870d 100644 --- a/ports/atmel-samd/common-hal/canio/Listener.h +++ b/ports/atmel-samd/common-hal/canio/Listener.h @@ -43,11 +43,3 @@ typedef struct canio_listener_obj { uint32_t timeout_ms; uint8_t fifo_idx; } canio_listener_obj_t; - -void common_hal_canio_listener_construct(canio_listener_obj_t *self, canio_can_obj_t *can, size_t nmatch, canio_match_obj_t **matches, float timeout); -void common_hal_canio_listener_check_for_deinit(canio_listener_obj_t *self); -void common_hal_canio_listener_deinit(canio_listener_obj_t *self); -bool common_hal_canio_listener_receiveinto(canio_listener_obj_t *self, canio_message_obj_t *message); -int common_hal_canio_listener_in_waiting(canio_listener_obj_t *self); -float common_hal_canio_listener_get_timeout(canio_listener_obj_t *self); -void common_hal_canio_listener_set_timeout(canio_listener_obj_t *self, float timeout); From a4cc3ad6cb75845e0dc75ede97b8287bb1af0317 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Sat, 26 Sep 2020 09:44:11 -0500 Subject: [PATCH 08/19] canio: RemoteTransmissionRequest: Split implementation, keep one structure This already begins obscuring things, because now there are two sets of shared-module functions for manipulating the same structure, e.g., common_hal_canio_remote_transmission_request_get_id and common_hal_canio_message_get_id --- py/circuitpy_defns.mk | 1 + shared-bindings/canio/Message.c | 85 --------- shared-bindings/canio/Message.h | 2 - .../canio/RemoteTransmissionRequest.c | 164 ++++++++++++++++++ .../canio/RemoteTransmissionRequest.h | 42 +++++ shared-module/canio/Message.c | 20 --- .../canio/RemoteTransmissionRequest.c | 68 ++++++++ .../canio/RemoteTransmissionRequest.h | 33 ++++ 8 files changed, 308 insertions(+), 107 deletions(-) create mode 100644 shared-bindings/canio/RemoteTransmissionRequest.c create mode 100644 shared-bindings/canio/RemoteTransmissionRequest.h create mode 100644 shared-module/canio/RemoteTransmissionRequest.c create mode 100644 shared-module/canio/RemoteTransmissionRequest.h diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index 98fa7a5f2c..76c0a4db67 100644 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -411,6 +411,7 @@ SRC_SHARED_MODULE_ALL = \ _bleio/ScanResults.c \ canio/Match.c \ canio/Message.c \ + canio/RemoteTransmissionRequest.c \ _eve/__init__.c \ _pixelbuf/PixelBuf.c \ _pixelbuf/__init__.c \ diff --git a/shared-bindings/canio/Message.c b/shared-bindings/canio/Message.c index ae03c25e76..e47e997c70 100644 --- a/shared-bindings/canio/Message.c +++ b/shared-bindings/canio/Message.c @@ -144,77 +144,6 @@ STATIC const mp_obj_property_t canio_message_extended_obj = { (mp_obj_t)&mp_const_none_obj}, }; -//| class RemoteTransmissionRequest: -//| def __init__(self, id: int, length: int, *, extended: bool = False): -//| """Construct a Message to send on a CAN bus. -//| -//| :param int id: The numeric ID of the requested message -//| :param int length: The length of the requested message -//| :param bool extended: True if the message has an extended identifier, False if it has a standard identifier -//| -//| In CAN, messages can have a length from 0 to 8 bytes. -//| """ -//| ... -//| -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) { - enum { ARG_id, ARG_length, ARG_extended, NUM_ARGS }; - static const mp_arg_t allowed_args[] = { - { MP_QSTR_id, MP_ARG_INT | MP_ARG_REQUIRED }, - { MP_QSTR_length, MP_ARG_INT | MP_ARG_REQUIRED }, - { MP_QSTR_extended, MP_ARG_BOOL, {.u_bool = false} }, - }; - mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; - MP_STATIC_ASSERT( MP_ARRAY_SIZE(allowed_args) == NUM_ARGS ); - - mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - - int length = args[ARG_length].u_int; - if (length < 0 || length > 8) { - mp_raise_ValueError(translate("Messages limited to 8 bytes")); - } - - canio_message_obj_t *self = m_new_obj(canio_message_obj_t); - self->base.type = &canio_remote_transmission_request_type; - common_hal_canio_message_construct(self, args[ARG_id].u_int, NULL, length, args[ARG_extended].u_bool); - return self; -} - -//| extended: bool -//| """True if the message's id is an extended id""" -//| - -//| id: int -//| """The numeric ID of the message""" -//| - -//| length: int -//| """The length of the requested message.""" -//| -STATIC mp_obj_t canio_remote_transmission_request_length_get(const mp_obj_t self_in) { - canio_message_obj_t *self = self_in; - return MP_OBJ_NEW_SMALL_INT(common_hal_canio_message_get_length(self)); -} -MP_DEFINE_CONST_FUN_OBJ_1(canio_remote_transmission_request_length_get_obj, canio_remote_transmission_request_length_get); - -STATIC mp_obj_t canio_remote_transmission_request_length_set(const mp_obj_t self_in, const mp_obj_t length_in) { - canio_message_obj_t *self = self_in; - int length = mp_obj_get_int(length_in); - if (length < 0 || length > 8) { - mp_raise_ValueError(translate("Messages limited to 8 bytes")); - } - common_hal_canio_remote_transmission_request_set_length(self, length); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_2(canio_remote_transmission_request_length_set_obj, canio_remote_transmission_request_length_set); - - -STATIC const mp_obj_property_t canio_remote_transmission_request_length_obj = { - .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&canio_remote_transmission_request_length_get_obj, - (mp_obj_t)&canio_remote_transmission_request_length_set_obj, - (mp_obj_t)&mp_const_none_obj}, -}; - 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) }, @@ -228,17 +157,3 @@ const mp_obj_type_t canio_message_type = { .make_new = canio_message_make_new, .locals_dict = (mp_obj_t)&canio_message_locals_dict, }; - -STATIC const mp_rom_map_elem_t canio_remote_transmission_request_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_id), MP_ROM_PTR(&canio_message_id_obj) }, - { MP_ROM_QSTR(MP_QSTR_length), MP_ROM_PTR(&canio_remote_transmission_request_length_obj) }, - { MP_ROM_QSTR(MP_QSTR_extended), MP_ROM_PTR(&canio_message_extended_obj) }, -}; -STATIC MP_DEFINE_CONST_DICT(canio_remote_transmission_request_locals_dict, canio_remote_transmission_request_locals_dict_table); - -const mp_obj_type_t canio_remote_transmission_request_type = { - { &mp_type_type }, - .name = MP_QSTR_RemoteTransmissionRequest, - .make_new = canio_remote_transmission_request_make_new, - .locals_dict = (mp_obj_t)&canio_remote_transmission_request_locals_dict, -}; diff --git a/shared-bindings/canio/Message.h b/shared-bindings/canio/Message.h index 96716864cf..adcdb095ce 100644 --- a/shared-bindings/canio/Message.h +++ b/shared-bindings/canio/Message.h @@ -39,7 +39,5 @@ bool common_hal_canio_message_get_extended(const canio_message_obj_t *self); void common_hal_canio_message_set_extended(canio_message_obj_t *self, bool extended); int common_hal_canio_message_get_id(const canio_message_obj_t *self); void common_hal_canio_message_set_id(canio_message_obj_t *self, int id); -bool common_hal_canio_message_get_rtr(const canio_message_obj_t *self); -void common_hal_canio_message_set_rtr(canio_message_obj_t *self, bool rtr); size_t common_hal_canio_message_get_length(const canio_message_obj_t *self); void common_hal_canio_remote_transmission_request_set_length(canio_message_obj_t *self, size_t length); diff --git a/shared-bindings/canio/RemoteTransmissionRequest.c b/shared-bindings/canio/RemoteTransmissionRequest.c new file mode 100644 index 0000000000..4e3f674174 --- /dev/null +++ b/shared-bindings/canio/RemoteTransmissionRequest.c @@ -0,0 +1,164 @@ +/* + * 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. + */ + +#include "shared-bindings/canio/RemoteTransmissionRequest.h" + +#include "py/obj.h" +#include "py/objproperty.h" +#include "py/runtime.h" + +//| class RemoteTransmissionRequest: +//| def __init__(self, id: int, length: int, *, extended: bool = False): +//| """Construct a RemoteTransmissionRequest to send on a CAN bus. +//| +//| :param int id: The numeric ID of the requested message +//| :param int length: The length of the requested message +//| :param bool extended: True if the message has an extended identifier, False if it has a standard identifier +//| +//| In CAN, messages can have a length from 0 to 8 bytes. +//| """ +//| ... +//| +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) { + enum { ARG_id, ARG_length, ARG_extended, NUM_ARGS }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_id, MP_ARG_INT | MP_ARG_REQUIRED }, + { MP_QSTR_length, MP_ARG_INT | MP_ARG_REQUIRED }, + { MP_QSTR_extended, MP_ARG_BOOL, {.u_bool = false} }, + }; + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + MP_STATIC_ASSERT( MP_ARRAY_SIZE(allowed_args) == NUM_ARGS ); + + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + int length = args[ARG_length].u_int; + if (length < 0 || length > 8) { + mp_raise_ValueError(translate("RemoteTransmissionRequests limited to 8 bytes")); + } + + canio_remote_transmission_request_obj_t *self = m_new_obj(canio_remote_transmission_request_obj_t); + self->base.type = &canio_remote_transmission_request_type; + common_hal_canio_remote_transmission_request_construct(self, args[ARG_id].u_int, length, args[ARG_extended].u_bool); + return self; +} + + +//| id: int +//| """The numeric ID of the message""" +//| +STATIC mp_obj_t canio_remote_transmission_request_id_get(const mp_obj_t self_in) { + canio_remote_transmission_request_obj_t *self = self_in; + return MP_OBJ_NEW_SMALL_INT(common_hal_canio_remote_transmission_request_get_id(self)); +} +MP_DEFINE_CONST_FUN_OBJ_1(canio_remote_transmission_request_id_get_obj, canio_remote_transmission_request_id_get); + +STATIC mp_obj_t canio_remote_transmission_request_id_set(const mp_obj_t self_in, const mp_obj_t id) { + canio_remote_transmission_request_obj_t *self = self_in; + common_hal_canio_remote_transmission_request_set_id(self, mp_obj_get_int(id)); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(canio_remote_transmission_request_id_set_obj, canio_remote_transmission_request_id_set); + +STATIC const mp_obj_property_t canio_remote_transmission_request_id_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&canio_remote_transmission_request_id_get_obj, + (mp_obj_t)&canio_remote_transmission_request_id_set_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + +//| extended: bool +//| """True if the message's id is an extended id""" +//| +STATIC mp_obj_t canio_remote_transmission_request_extended_get(const mp_obj_t self_in) { + canio_remote_transmission_request_obj_t *self = self_in; + return mp_obj_new_bool(common_hal_canio_remote_transmission_request_get_extended(self)); +} +MP_DEFINE_CONST_FUN_OBJ_1(canio_remote_transmission_request_extended_get_obj, canio_remote_transmission_request_extended_get); + +STATIC mp_obj_t canio_remote_transmission_request_extended_set(const mp_obj_t self_in, const mp_obj_t extended) { + canio_remote_transmission_request_obj_t *self = self_in; + common_hal_canio_remote_transmission_request_set_extended(self, mp_obj_is_true(extended)); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(canio_remote_transmission_request_extended_set_obj, canio_remote_transmission_request_extended_set); + + +STATIC const mp_obj_property_t canio_remote_transmission_request_extended_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&canio_remote_transmission_request_extended_get_obj, + (mp_obj_t)&canio_remote_transmission_request_extended_set_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + +//| extended: bool +//| """True if the message's id is an extended id""" +//| + +//| id: int +//| """The numeric ID of the message""" +//| + +//| length: int +//| """The length of the requested message.""" +//| +STATIC mp_obj_t canio_remote_transmission_request_length_get(const mp_obj_t self_in) { + canio_remote_transmission_request_obj_t *self = self_in; + return MP_OBJ_NEW_SMALL_INT(common_hal_canio_remote_transmission_request_get_length(self)); +} +MP_DEFINE_CONST_FUN_OBJ_1(canio_remote_transmission_request_length_get_obj, canio_remote_transmission_request_length_get); + +STATIC mp_obj_t canio_remote_transmission_request_length_set(const mp_obj_t self_in, const mp_obj_t length_in) { + canio_remote_transmission_request_obj_t *self = self_in; + int length = mp_obj_get_int(length_in); + if (length < 0 || length > 8) { + mp_raise_ValueError(translate("RemoteTransmissionRequests limited to 8 bytes")); + } + common_hal_canio_remote_transmission_request_set_length(self, length); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(canio_remote_transmission_request_length_set_obj, canio_remote_transmission_request_length_set); + + +STATIC const mp_obj_property_t canio_remote_transmission_request_length_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&canio_remote_transmission_request_length_get_obj, + (mp_obj_t)&canio_remote_transmission_request_length_set_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + +STATIC const mp_rom_map_elem_t canio_remote_transmission_request_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_id), MP_ROM_PTR(&canio_remote_transmission_request_id_obj) }, + { MP_ROM_QSTR(MP_QSTR_length), MP_ROM_PTR(&canio_remote_transmission_request_length_obj) }, + { MP_ROM_QSTR(MP_QSTR_extended), MP_ROM_PTR(&canio_remote_transmission_request_extended_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(canio_remote_transmission_request_locals_dict, canio_remote_transmission_request_locals_dict_table); + +const mp_obj_type_t canio_remote_transmission_request_type = { + { &mp_type_type }, + .name = MP_QSTR_RemoteTransmissionRequest, + .make_new = canio_remote_transmission_request_make_new, + .locals_dict = (mp_obj_t)&canio_remote_transmission_request_locals_dict, +}; diff --git a/shared-bindings/canio/RemoteTransmissionRequest.h b/shared-bindings/canio/RemoteTransmissionRequest.h new file mode 100644 index 0000000000..8956587b3a --- /dev/null +++ b/shared-bindings/canio/RemoteTransmissionRequest.h @@ -0,0 +1,42 @@ +/* + * 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. + */ + +#pragma once + +#include "py/obj.h" +#include "shared-module/canio/RemoteTransmissionRequest.h" + +extern const mp_obj_type_t canio_remote_transmission_request_type; + +void common_hal_canio_remote_transmission_request_construct(canio_remote_transmission_request_obj_t *self, int id, size_t size, bool extended); +const void *common_hal_canio_remote_transmission_request_get_data(const canio_remote_transmission_request_obj_t *self); +void common_hal_canio_remote_transmission_request_set_data(canio_remote_transmission_request_obj_t *self, const void *data, size_t size); +bool common_hal_canio_remote_transmission_request_get_extended(const canio_remote_transmission_request_obj_t *self); +void common_hal_canio_remote_transmission_request_set_extended(canio_remote_transmission_request_obj_t *self, bool extended); +int common_hal_canio_remote_transmission_request_get_id(const canio_remote_transmission_request_obj_t *self); +void common_hal_canio_remote_transmission_request_set_id(canio_remote_transmission_request_obj_t *self, int id); +size_t common_hal_canio_remote_transmission_request_get_length(const canio_remote_transmission_request_obj_t *self); +void common_hal_canio_remote_transmission_request_set_length(canio_remote_transmission_request_obj_t *self, size_t length); diff --git a/shared-module/canio/Message.c b/shared-module/canio/Message.c index 86a3b71069..34c2e61f76 100644 --- a/shared-module/canio/Message.c +++ b/shared-module/canio/Message.c @@ -68,26 +68,6 @@ size_t common_hal_canio_message_get_length(const canio_message_obj_t *self) return self->size; } -void common_hal_canio_remote_transmission_request_set_length(canio_message_obj_t *self, size_t size) -{ - memset(self->data, 0, size); - self->size = size; -} - - -bool common_hal_canio_message_get_rtr(const canio_message_obj_t *self) -{ - return self->rtr; -} - -void common_hal_canio_message_set_rtr(canio_message_obj_t *self, bool rtr) -{ - self->rtr = rtr; - if (rtr) { - memset(self->data, 0, self->size); - } -} - bool common_hal_canio_message_get_extended(const canio_message_obj_t *self) { return self->extended; diff --git a/shared-module/canio/RemoteTransmissionRequest.c b/shared-module/canio/RemoteTransmissionRequest.c new file mode 100644 index 0000000000..00c9334b0a --- /dev/null +++ b/shared-module/canio/RemoteTransmissionRequest.c @@ -0,0 +1,68 @@ +/* + * 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. + */ + +#include "shared-module/canio/RemoteTransmissionRequest.h" +#include "shared-bindings/canio/RemoteTransmissionRequest.h" + +#include + +void common_hal_canio_remote_transmission_request_construct(canio_remote_transmission_request_obj_t *self, int id, size_t size, bool extended) +{ + self->id = id; + self->size = size; + self->rtr = true; + self->extended = extended; +} + +int common_hal_canio_remote_transmission_request_get_id(const canio_remote_transmission_request_obj_t *self) +{ + return self->id; +} + +void common_hal_canio_remote_transmission_request_set_id(canio_remote_transmission_request_obj_t *self, int id) +{ + self->id = id; +} + +size_t common_hal_canio_remote_transmission_request_get_length(const canio_remote_transmission_request_obj_t *self) +{ + return self->size; +} + +void common_hal_canio_remote_transmission_request_set_length(canio_remote_transmission_request_obj_t *self, size_t size) +{ + self->size = size; +} + +bool common_hal_canio_remote_transmission_request_get_extended(const canio_remote_transmission_request_obj_t *self) +{ + return self->extended; +} + +void common_hal_canio_remote_transmission_request_set_extended(canio_remote_transmission_request_obj_t *self, bool extended) +{ + self->extended = extended; +} diff --git a/shared-module/canio/RemoteTransmissionRequest.h b/shared-module/canio/RemoteTransmissionRequest.h new file mode 100644 index 0000000000..2f09b19c6f --- /dev/null +++ b/shared-module/canio/RemoteTransmissionRequest.h @@ -0,0 +1,33 @@ +/* + * 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. + */ + +#pragma once + +#include "py/obj.h" + +#include "shared-bindings/canio/Message.h" + +typedef canio_message_obj_t canio_remote_transmission_request_obj_t; From 4f7f1e810681f2956598acced5c09c05695967f9 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Mon, 28 Sep 2020 15:51:46 -0500 Subject: [PATCH 09/19] canio: CAN.listen: rename argument to 'matches', reflecting that it is a sequence --- shared-bindings/canio/CAN.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/shared-bindings/canio/CAN.c b/shared-bindings/canio/CAN.c index b1fe7d28ba..925b248fa6 100644 --- a/shared-bindings/canio/CAN.c +++ b/shared-bindings/canio/CAN.c @@ -252,7 +252,7 @@ STATIC mp_obj_t canio_can_restart(mp_obj_t self_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(canio_can_restart_obj, canio_can_restart); -//| def listen(self, match: Optional[Sequence[Match]]=None, *, timeout: float=10) -> Listener: +//| def listen(self, matches: Optional[Sequence[Match]]=None, *, timeout: float=10) -> Listener: //| """Start receiving messages that match any one of the filters. //| //| Creating a listener is an expensive operation and can interfere with reception of messages by other listeners. @@ -272,9 +272,9 @@ STATIC mp_obj_t canio_can_listen(size_t n_args, const mp_obj_t *pos_args, mp_map canio_can_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); common_hal_canio_can_check_for_deinit(self); - enum { ARG_match, ARG_timeout, NUM_ARGS }; + enum { ARG_matches, ARG_timeout, NUM_ARGS }; static const mp_arg_t allowed_args[] = { - { MP_QSTR_match, MP_ARG_OBJ, {.u_obj = 0} }, + { MP_QSTR_matches, MP_ARG_OBJ, {.u_obj = 0} }, { MP_QSTR_timeout, MP_ARG_OBJ, {.u_obj = 0} }, }; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; @@ -285,8 +285,8 @@ STATIC mp_obj_t canio_can_listen(size_t n_args, const mp_obj_t *pos_args, mp_map size_t nmatch = 0; mp_obj_t *match_objects = NULL; - if (args[ARG_match].u_obj) { - mp_obj_get_array(args[ARG_match].u_obj, &nmatch, &match_objects); + if (args[ARG_matches].u_obj) { + mp_obj_get_array(args[ARG_matches].u_obj, &nmatch, &match_objects); } canio_match_obj_t *matches[nmatch]; From 13493730a15d5c75b50f2568387f2ddc88c412c7 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Mon, 28 Sep 2020 16:06:54 -0500 Subject: [PATCH 10/19] canio: CAN.Match: improve how an unspecified `mask` is implemented 0 should actually indicate a "match everything" filter which you otherwise couldn't indicate with any single Match object, and an all-address-bits-set number should indicate a "match single address" filter. Use an optional/default None argument to do the job. --- shared-bindings/canio/Match.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/shared-bindings/canio/Match.c b/shared-bindings/canio/Match.c index 91a9dd02a3..3fbc1773e8 100644 --- a/shared-bindings/canio/Match.c +++ b/shared-bindings/canio/Match.c @@ -33,10 +33,10 @@ //| """Describe CAN bus messages to match""" //| //| -//| def __init__(self, id: int, *, mask: int = 0, extended: bool = False): +//| def __init__(self, id: int, *, mask: Optional[int] = None, extended: bool = False): //| """Construct a Match with the given properties. //| -//| If mask is nonzero, then the filter is for any id which matches all +//| If mask is not None, then the filter is for any id which matches all //| the nonzero bits in mask. Otherwise, it matches exactly the given id. //| If extended is true then only extended ids are matched, otherwise //| only standard ids are matched.""" @@ -46,7 +46,7 @@ STATIC mp_obj_t canio_match_make_new(const mp_obj_type_t *type, size_t n_args, c enum { ARG_id, ARG_mask, ARG_extended, NUM_ARGS }; static const mp_arg_t allowed_args[] = { { MP_QSTR_id, MP_ARG_INT | MP_ARG_REQUIRED }, - { MP_QSTR_mask, MP_ARG_INT, {.u_int = 0} }, + { MP_QSTR_mask, MP_ARG_OBJ, {.u_obj = mp_const_none } }, { MP_QSTR_extended, MP_ARG_BOOL, {.u_bool = false} }, }; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; @@ -56,7 +56,7 @@ STATIC mp_obj_t canio_match_make_new(const mp_obj_type_t *type, size_t n_args, c int id_bits = args[ARG_extended].u_bool ? 0x1fffffff : 0x7ff; int id = args[ARG_id].u_int; - int mask = args[ARG_mask].u_int; + int mask = args[ARG_mask].u_obj == mp_const_none ? id_bits : mp_obj_get_int(args[ARG_mask].u_obj); if (id & ~id_bits) { mp_raise_ValueError_varg(translate("%q out of range"), MP_QSTR_id); @@ -68,7 +68,7 @@ STATIC mp_obj_t canio_match_make_new(const mp_obj_type_t *type, size_t n_args, c canio_match_obj_t *self = m_new_obj(canio_match_obj_t); self->base.type = &canio_match_type; - common_hal_canio_match_construct(self, args[ARG_id].u_int, args[ARG_mask].u_int, args[ARG_extended].u_bool); + common_hal_canio_match_construct(self, id, mask, args[ARG_extended].u_bool); return self; } From f4e36fc7f6719cb938b6e11b3e5242b149600d7d Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Mon, 28 Sep 2020 17:14:39 -0500 Subject: [PATCH 11/19] CAN: Use mp_obj_t insteaed of canio_message_obj_t, get rid of rtr field --- ports/atmel-samd/common-hal/canio/CAN.c | 8 +++++--- ports/atmel-samd/common-hal/canio/Listener.c | 12 +++++++----- shared-bindings/canio/CAN.h | 2 +- shared-bindings/canio/Listener.c | 12 +++--------- shared-bindings/canio/Listener.h | 2 +- shared-module/canio/Message.c | 2 -- shared-module/canio/Message.h | 1 - shared-module/canio/RemoteTransmissionRequest.c | 1 - 8 files changed, 17 insertions(+), 23 deletions(-) diff --git a/ports/atmel-samd/common-hal/canio/CAN.c b/ports/atmel-samd/common-hal/canio/CAN.c index 4f28698fba..ec6b759510 100644 --- a/ports/atmel-samd/common-hal/canio/CAN.c +++ b/ports/atmel-samd/common-hal/canio/CAN.c @@ -328,16 +328,18 @@ static void maybe_auto_restart(canio_can_obj_t *self) { } } -void common_hal_canio_can_send(canio_can_obj_t *self, canio_message_obj_t *message) +void common_hal_canio_can_send(canio_can_obj_t *self, mp_obj_t message_in) { maybe_auto_restart(self); + canio_message_obj_t *message = message_in;; // We have just one dedicated TX buffer, use it! canio_can_tx_buffer_t *ent = &self->state->tx_buffer[0]; + bool rtr = message->base.type == &canio_remote_transmission_request_type; ent->txb0.bit.ESI = false; ent->txb0.bit.XTD = message->extended; - ent->txb0.bit.RTR = message->rtr; + ent->txb0.bit.RTR = rtr; if (message->extended) { ent->txb0.bit.ID = message->id; } else { @@ -350,7 +352,7 @@ void common_hal_canio_can_send(canio_can_obj_t *self, canio_message_obj_t *messa ent->txb1.bit.BRS = 0; // No bit rate switching ent->txb1.bit.DLC = message->size; - if (!message->rtr) { + if (!rtr) { memcpy(ent->data, message->data, message->size); } diff --git a/ports/atmel-samd/common-hal/canio/Listener.c b/ports/atmel-samd/common-hal/canio/Listener.c index 483802fa4a..b27c8cd1b8 100644 --- a/ports/atmel-samd/common-hal/canio/Listener.c +++ b/ports/atmel-samd/common-hal/canio/Listener.c @@ -349,30 +349,32 @@ int common_hal_canio_listener_in_waiting(canio_listener_obj_t *self) { return self->hw->RXFS.bit.F0FL; } -bool common_hal_canio_listener_receiveinto(canio_listener_obj_t *self, canio_message_obj_t *message) { +mp_obj_t common_hal_canio_listener_receive(canio_listener_obj_t *self) { if (!common_hal_canio_listener_in_waiting(self)) { uint64_t deadline = supervisor_ticks_ms64() + self->timeout_ms; do { if (supervisor_ticks_ms64() > deadline) { - return false; + return NULL; } } while (!common_hal_canio_listener_in_waiting(self)); } int index = self->hw->RXFS.bit.F0GI; canio_can_rx_fifo_t *hw_message = &self->fifo[index]; + bool rtr = hw_message->rxf0.bit.RTR; + canio_message_obj_t *message = m_new_obj(canio_message_obj_t); + message->base.type = rtr ? &canio_remote_transmission_request_type : &canio_message_type; message->extended = hw_message->rxf0.bit.XTD; if (message->extended) { message->id = hw_message->rxf0.bit.ID; } else { message->id = hw_message->rxf0.bit.ID >> 18; // short ids are left-justified } - message->rtr = hw_message->rxf0.bit.RTR; message->size = hw_message->rxf1.bit.DLC; - if (!message->rtr) { + if (!rtr) { memcpy(message->data, hw_message->data, message->size); } self->hw->RXFA.bit.F0AI = index; - return true; + return message; } void common_hal_canio_listener_deinit(canio_listener_obj_t *self) { diff --git a/shared-bindings/canio/CAN.h b/shared-bindings/canio/CAN.h index 08166dfd42..8d61bc8ed5 100644 --- a/shared-bindings/canio/CAN.h +++ b/shared-bindings/canio/CAN.h @@ -51,5 +51,5 @@ void common_hal_canio_can_auto_restart_set(canio_can_obj_t *self, bool auto_rest void common_hal_canio_can_check_for_deinit(canio_can_obj_t *self); void common_hal_canio_can_deinit(canio_can_obj_t *self); void common_hal_canio_can_restart(canio_can_obj_t *self); -void common_hal_canio_can_send(canio_can_obj_t *self, canio_message_obj_t *message); +void common_hal_canio_can_send(canio_can_obj_t *self, mp_obj_t message); void common_hal_canio_reset(void); diff --git a/shared-bindings/canio/Listener.c b/shared-bindings/canio/Listener.c index 7b0e59063c..e5a9a2f83f 100644 --- a/shared-bindings/canio/Listener.c +++ b/shared-bindings/canio/Listener.c @@ -49,17 +49,11 @@ STATIC mp_obj_t canio_listener_receive(mp_obj_t self_in) { canio_listener_obj_t *self = MP_OBJ_TO_PTR(self_in); common_hal_canio_listener_check_for_deinit(self); - canio_message_obj_t *message = m_new_obj(canio_message_obj_t); + mp_obj_t message = common_hal_canio_listener_receive(self); + // note: receive fills out the type field of the message - if (common_hal_canio_listener_receiveinto(self, message)) { - if (message->rtr) { - message->base.type = &canio_remote_transmission_request_type; - } else { - message->base.type = &canio_message_type; - } + if (message) { return message; - } else { - m_free(message); // message did not escape into vm } return mp_const_none; } diff --git a/shared-bindings/canio/Listener.h b/shared-bindings/canio/Listener.h index d925fa2652..527ffe4cbb 100644 --- a/shared-bindings/canio/Listener.h +++ b/shared-bindings/canio/Listener.h @@ -37,7 +37,7 @@ typedef struct canio_listener_obj canio_listener_obj_t; void common_hal_canio_listener_construct(canio_listener_obj_t *self, canio_can_obj_t *can, size_t nmatch, canio_match_obj_t **matches, float timeout); void common_hal_canio_listener_check_for_deinit(canio_listener_obj_t *self); void common_hal_canio_listener_deinit(canio_listener_obj_t *self); -bool common_hal_canio_listener_receiveinto(canio_listener_obj_t *self, canio_message_obj_t *message); +mp_obj_t common_hal_canio_listener_receive(canio_listener_obj_t *self); int common_hal_canio_listener_in_waiting(canio_listener_obj_t *self); float common_hal_canio_listener_get_timeout(canio_listener_obj_t *self); void common_hal_canio_listener_set_timeout(canio_listener_obj_t *self, float timeout); diff --git a/shared-module/canio/Message.c b/shared-module/canio/Message.c index 34c2e61f76..a1df4f693d 100644 --- a/shared-module/canio/Message.c +++ b/shared-module/canio/Message.c @@ -32,7 +32,6 @@ void common_hal_canio_message_construct(canio_message_obj_t *self, int id, void { self->id = id; self->size = size; - self->rtr = !data; self->extended = extended; if (data) { memcpy(self->data, data, size); @@ -57,7 +56,6 @@ const void *common_hal_canio_message_get_data(const canio_message_obj_t *self) const void common_hal_canio_message_set_data(canio_message_obj_t *self, const void *data, size_t size) { - self->rtr = false; self->size = size; memcpy(self->data, data, size); } diff --git a/shared-module/canio/Message.h b/shared-module/canio/Message.h index b99c5c48e3..1ca0d42e2c 100644 --- a/shared-module/canio/Message.h +++ b/shared-module/canio/Message.h @@ -33,6 +33,5 @@ typedef struct { int id; uint8_t data[8]; size_t size:4; - bool rtr:1; bool extended:1; } canio_message_obj_t; diff --git a/shared-module/canio/RemoteTransmissionRequest.c b/shared-module/canio/RemoteTransmissionRequest.c index 00c9334b0a..9b4d5632f8 100644 --- a/shared-module/canio/RemoteTransmissionRequest.c +++ b/shared-module/canio/RemoteTransmissionRequest.c @@ -33,7 +33,6 @@ void common_hal_canio_remote_transmission_request_construct(canio_remote_transmi { self->id = id; self->size = size; - self->rtr = true; self->extended = extended; } From 04e434a7ce89eb756b5e2db8a2ef2f49ef3748ee Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Mon, 28 Sep 2020 17:19:28 -0500 Subject: [PATCH 12/19] canio: remove stray remote_transmission_request method declaration --- shared-bindings/canio/Message.h | 1 - 1 file changed, 1 deletion(-) diff --git a/shared-bindings/canio/Message.h b/shared-bindings/canio/Message.h index adcdb095ce..b76535939b 100644 --- a/shared-bindings/canio/Message.h +++ b/shared-bindings/canio/Message.h @@ -40,4 +40,3 @@ void common_hal_canio_message_set_extended(canio_message_obj_t *self, bool exten int common_hal_canio_message_get_id(const canio_message_obj_t *self); void common_hal_canio_message_set_id(canio_message_obj_t *self, int id); size_t common_hal_canio_message_get_length(const canio_message_obj_t *self); -void common_hal_canio_remote_transmission_request_set_length(canio_message_obj_t *self, size_t length); From 809225fd1e4bb4370d035e20c65405c02324fea8 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Mon, 28 Sep 2020 17:22:15 -0500 Subject: [PATCH 13/19] make translate --- locale/circuitpython.pot | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index 2b395356ca..17a3243272 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2020-09-24 16:55-0500\n" +"POT-Creation-Date: 2020-09-28 17:22-0500\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -1458,6 +1458,10 @@ msgstr "" msgid "Refresh too soon" msgstr "" +#: shared-bindings/canio/RemoteTransmissionRequest.c +msgid "RemoteTransmissionRequests limited to 8 bytes" +msgstr "" + #: shared-bindings/aesio/aes.c msgid "Requested AES mode is unsupported" msgstr "" @@ -2326,10 +2330,14 @@ msgstr "" msgid "exceptions must derive from BaseException" msgstr "" -#: shared-bindings/canio/CAN.c shared-bindings/canio/Listener.c +#: shared-bindings/canio/CAN.c msgid "expected '%q' but got '%q'" msgstr "" +#: shared-bindings/canio/CAN.c +msgid "expected '%q' or '%q' but got '%q'" +msgstr "" + #: py/objstr.c msgid "expected ':' after format specifier" msgstr "" @@ -3167,10 +3175,6 @@ msgstr "" msgid "source palette too large" msgstr "" -#: shared-bindings/canio/Message.c -msgid "specify size or data, but not both" -msgstr "" - #: py/objstr.c msgid "start/end indices" msgstr "" From 979ec3a458a463d45dfd062e86073ffc2198f82e Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Mon, 28 Sep 2020 19:47:06 -0500 Subject: [PATCH 14/19] can: RemoteTransmissionRequest: remove duplicate docstrings --- shared-bindings/canio/RemoteTransmissionRequest.c | 8 -------- 1 file changed, 8 deletions(-) diff --git a/shared-bindings/canio/RemoteTransmissionRequest.c b/shared-bindings/canio/RemoteTransmissionRequest.c index 4e3f674174..d762787b18 100644 --- a/shared-bindings/canio/RemoteTransmissionRequest.c +++ b/shared-bindings/canio/RemoteTransmissionRequest.c @@ -113,14 +113,6 @@ STATIC const mp_obj_property_t canio_remote_transmission_request_extended_obj = (mp_obj_t)&mp_const_none_obj}, }; -//| extended: bool -//| """True if the message's id is an extended id""" -//| - -//| id: int -//| """The numeric ID of the message""" -//| - //| length: int //| """The length of the requested message.""" //| From 03bd968450fe167db7b7bfcffc98461be850dd5e Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Mon, 28 Sep 2020 19:56:19 -0500 Subject: [PATCH 15/19] more doc improvements --- shared-bindings/canio/Listener.c | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/shared-bindings/canio/Listener.c b/shared-bindings/canio/Listener.c index e5a9a2f83f..5a4df3c46b 100644 --- a/shared-bindings/canio/Listener.c +++ b/shared-bindings/canio/Listener.c @@ -34,15 +34,20 @@ //| class Listener: //| """Listens for CAN message //| -//| canio.Listener is not constructed directly, but instead by calling -//| `~canio.CAN.listen`.""" +//| `canio.Listener` is not constructed directly, but instead by calling +//| `canio.CAN.listen`. +//| +//| In addition to using the `receive` method to retrieve a message or +//| the `in_waiting` method to check for an available message, a +//| listener can be used as an iterable, yielding messages until no +//| message arrives within ``self.timeout`` seconds.""" //| //| def receive(self) -> Optional[Union[RemoteTransmissionRequest,Message]]: -//| """Reads a message, after waiting up to self.timeout seconds +//| """Reads a message, after waiting up to ``self.timeout`` seconds //| -//| If no message is received in time, None is returned. Otherwise, -//| a Message is returned.""" +//| If no message is received in time, `None` is returned. Otherwise, +//| a `Message` or `RemoteTransmissionRequest` is returned.""" //| ... //| STATIC mp_obj_t canio_listener_receive(mp_obj_t self_in) { @@ -60,7 +65,8 @@ STATIC mp_obj_t canio_listener_receive(mp_obj_t self_in) { STATIC MP_DEFINE_CONST_FUN_OBJ_1(canio_listener_receive_obj, canio_listener_receive); //| def in_waiting(self) -> int: -//| """Returns the number of messages waiting""" +//| """Returns the number of messages (including remote +//| transmission requests) waiting""" //| ... //| STATIC mp_obj_t canio_listener_in_waiting(mp_obj_t self_in) { @@ -70,15 +76,21 @@ STATIC mp_obj_t canio_listener_in_waiting(mp_obj_t self_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(canio_listener_in_waiting_obj, canio_listener_in_waiting); -//| def __iter__(self): -//| """Returns self, unless the object is deinitialized""" +//| def __iter__(self) -> Listener: +//| """Returns self, unless the object is deinitialized. +//| +//| This method exists so that `Listener` can be used as an +//| iterable""" //| ... //| -//| def __next__(self): +//| def __next__(self) -> Union[RemoteTransmissionRequest,Message]: //| """Reads a message, after waiting up to self.timeout seconds //| //| If no message is received in time, raises StopIteration. Otherwise, -//| a Message is returned.""" +//| a Message or is returned. +//| +//| This method enables the `Listener` to be used as an +//| iterable, for instance in a for-loop.""" //| ... //| STATIC mp_obj_t canio_iternext(mp_obj_t self_in) { From 1bea099eb201526a777f3d30b8a9e017724aaa9d Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Tue, 29 Sep 2020 14:05:50 -0500 Subject: [PATCH 16/19] Allow the _state_count properties to be unimplemented This is the case on the SPI implementations MCP2515 and MCP2518. --- shared-bindings/canio/CAN.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/shared-bindings/canio/CAN.c b/shared-bindings/canio/CAN.c index 925b248fa6..15ce3dc08c 100644 --- a/shared-bindings/canio/CAN.c +++ b/shared-bindings/canio/CAN.c @@ -172,7 +172,12 @@ STATIC const mp_obj_property_t canio_can_receive_error_count_obj = { }; //| error_warning_state_count: int -//| """The number of times the controller enterted the Error Warning state (read-only). This number wraps around to 0 after an implementation-defined number of errors.""" +//| """The number of times the controller enterted the Error Warning +//| state (read-only). This number wraps around to 0 after an +//| implementation-defined number of errors. +//| +//| Not all implementations support this property. If the property +//| is unsupported, AttributeError will be raised.""" //| STATIC mp_obj_t canio_can_error_warning_state_count_get(mp_obj_t self_in) { canio_can_obj_t *self = MP_OBJ_TO_PTR(self_in); @@ -189,7 +194,12 @@ STATIC const mp_obj_property_t canio_can_error_warning_state_count_obj = { }; //| error_passive_state_count: int -//| """The number of times the controller enterted the Error Passive state (read-only). This number wraps around to 0 after an implementation-defined number of errors.""" +//| """The number of times the controller enterted the Error Passive +//| state (read-only). This number wraps around to 0 after an +//| implementation-defined number of errors. +//| +//| Not all implementations support this property. If the property +//| is unsupported, AttributeError will be raised.""" //| STATIC mp_obj_t canio_can_error_passive_state_count_get(mp_obj_t self_in) { canio_can_obj_t *self = MP_OBJ_TO_PTR(self_in); @@ -206,7 +216,12 @@ STATIC const mp_obj_property_t canio_can_error_passive_state_count_obj = { }; //| bus_off_state_count: int -//| """The number of times the controller enterted the Bus Off state (read-only). This number wraps around to 0 after an implementation-defined number of errors.""" +//| """The number of times the controller enterted the Bus Off state +//| (read-only). This number wraps around to 0 after an +//| implementation-defined number of errors. +//| +//| Not all implementations support this property. If the property +//| is unsupported, AttributeError will be raised.""" //| STATIC mp_obj_t canio_can_bus_off_state_count_get(mp_obj_t self_in) { canio_can_obj_t *self = MP_OBJ_TO_PTR(self_in); From 611f81ac1afce82b74acbe8b282fef5300dac4d9 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Tue, 29 Sep 2020 14:25:53 -0500 Subject: [PATCH 17/19] canio: actually drop the _error_count properties thanks @tannewt --- ports/atmel-samd/common-hal/canio/CAN.c | 26 ---------- shared-bindings/canio/CAN.c | 69 ------------------------- shared-bindings/canio/CAN.h | 3 -- 3 files changed, 98 deletions(-) diff --git a/ports/atmel-samd/common-hal/canio/CAN.c b/ports/atmel-samd/common-hal/canio/CAN.c index ec6b759510..76599a9a7b 100644 --- a/ports/atmel-samd/common-hal/canio/CAN.c +++ b/ports/atmel-samd/common-hal/canio/CAN.c @@ -275,21 +275,6 @@ int common_hal_canio_can_receive_error_count_get(canio_can_obj_t *self) return self->hw->ECR.bit.REC; } -int common_hal_canio_can_error_warning_state_count_get(canio_can_obj_t *self) -{ - return self->error_warning_state_count; -} - -int common_hal_canio_can_error_passive_state_count_get(canio_can_obj_t *self) -{ - return self->error_passive_state_count; -} - -int common_hal_canio_can_bus_off_state_count_get(canio_can_obj_t *self) -{ - return self->bus_off_state_count; -} - canio_bus_state_t common_hal_canio_can_state_get(canio_can_obj_t *self) { CAN_PSR_Type psr = self->hw->PSR; if (psr.bit.BO) { @@ -419,17 +404,6 @@ STATIC void can_handler(int i) { Can *hw = can_insts[i]; uint32_t ir = hri_can_read_IR_reg(hw); - /* Count up errors*/ - if (ir & CAN_IE_EWE) { - self->error_warning_state_count += 1; - } - if (ir & CAN_IE_EPE) { - self->error_passive_state_count += 1; - } - if (ir & CAN_IE_BOE) { - self->bus_off_state_count += 1; - } - /* Acknowledge interrupt */ hri_can_write_IR_reg(hw, ir); } diff --git a/shared-bindings/canio/CAN.c b/shared-bindings/canio/CAN.c index 15ce3dc08c..e8ab15a3c9 100644 --- a/shared-bindings/canio/CAN.c +++ b/shared-bindings/canio/CAN.c @@ -171,72 +171,6 @@ STATIC const mp_obj_property_t canio_can_receive_error_count_obj = { (mp_obj_t)mp_const_none}, }; -//| error_warning_state_count: int -//| """The number of times the controller enterted the Error Warning -//| state (read-only). This number wraps around to 0 after an -//| implementation-defined number of errors. -//| -//| Not all implementations support this property. If the property -//| is unsupported, AttributeError will be raised.""" -//| -STATIC mp_obj_t canio_can_error_warning_state_count_get(mp_obj_t self_in) { - canio_can_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_canio_can_check_for_deinit(self); - return MP_OBJ_NEW_SMALL_INT(common_hal_canio_can_error_warning_state_count_get(self)); -} -MP_DEFINE_CONST_FUN_OBJ_1(canio_can_error_warning_state_count_get_obj, canio_can_error_warning_state_count_get); - -STATIC const mp_obj_property_t canio_can_error_warning_state_count_obj = { - .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&canio_can_error_warning_state_count_get_obj, - (mp_obj_t)mp_const_none, - (mp_obj_t)mp_const_none}, -}; - -//| error_passive_state_count: int -//| """The number of times the controller enterted the Error Passive -//| state (read-only). This number wraps around to 0 after an -//| implementation-defined number of errors. -//| -//| Not all implementations support this property. If the property -//| is unsupported, AttributeError will be raised.""" -//| -STATIC mp_obj_t canio_can_error_passive_state_count_get(mp_obj_t self_in) { - canio_can_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_canio_can_check_for_deinit(self); - return MP_OBJ_NEW_SMALL_INT(common_hal_canio_can_error_passive_state_count_get(self)); -} -MP_DEFINE_CONST_FUN_OBJ_1(canio_can_error_passive_state_count_get_obj, canio_can_error_passive_state_count_get); - -STATIC const mp_obj_property_t canio_can_error_passive_state_count_obj = { - .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&canio_can_error_passive_state_count_get_obj, - (mp_obj_t)mp_const_none, - (mp_obj_t)mp_const_none}, -}; - -//| bus_off_state_count: int -//| """The number of times the controller enterted the Bus Off state -//| (read-only). This number wraps around to 0 after an -//| implementation-defined number of errors. -//| -//| Not all implementations support this property. If the property -//| is unsupported, AttributeError will be raised.""" -//| -STATIC mp_obj_t canio_can_bus_off_state_count_get(mp_obj_t self_in) { - canio_can_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_canio_can_check_for_deinit(self); - return MP_OBJ_NEW_SMALL_INT(common_hal_canio_can_bus_off_state_count_get(self)); -} -MP_DEFINE_CONST_FUN_OBJ_1(canio_can_bus_off_state_count_get_obj, canio_can_bus_off_state_count_get); - -STATIC const mp_obj_property_t canio_can_bus_off_state_count_obj = { - .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&canio_can_bus_off_state_count_get_obj, - (mp_obj_t)mp_const_none, - (mp_obj_t)mp_const_none}, -}; - //| state: State //| """The current state of the bus.""" STATIC mp_obj_t canio_can_state_get(mp_obj_t self_in) { @@ -414,10 +348,7 @@ STATIC const mp_rom_map_elem_t canio_can_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&canio_can_exit_obj) }, { MP_ROM_QSTR(MP_QSTR_auto_restart), MP_ROM_PTR(&canio_can_auto_restart_obj) }, { MP_ROM_QSTR(MP_QSTR_baudrate), MP_ROM_PTR(&canio_can_baudrate_obj) }, - { MP_ROM_QSTR(MP_QSTR_bus_off_state_count), MP_ROM_PTR(&canio_can_bus_off_state_count_obj) }, { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&canio_can_deinit_obj) }, - { MP_ROM_QSTR(MP_QSTR_error_passive_state_count), MP_ROM_PTR(&canio_can_error_passive_state_count_obj) }, - { MP_ROM_QSTR(MP_QSTR_error_warning_state_count), MP_ROM_PTR(&canio_can_error_warning_state_count_obj) }, { MP_ROM_QSTR(MP_QSTR_listen), MP_ROM_PTR(&canio_can_listen_obj) }, { MP_ROM_QSTR(MP_QSTR_loopback), MP_ROM_PTR(&canio_can_loopback_obj) }, { MP_ROM_QSTR(MP_QSTR_receive_error_count), MP_ROM_PTR(&canio_can_receive_error_count_obj) }, diff --git a/shared-bindings/canio/CAN.h b/shared-bindings/canio/CAN.h index 8d61bc8ed5..968b71f14c 100644 --- a/shared-bindings/canio/CAN.h +++ b/shared-bindings/canio/CAN.h @@ -39,9 +39,6 @@ void common_hal_canio_can_construct(canio_can_obj_t *self, mcu_pin_obj_t *tx, mc bool common_hal_canio_can_auto_restart_get(canio_can_obj_t *self); bool common_hal_canio_can_deinited(canio_can_obj_t *self); int common_hal_canio_can_baudrate_get(canio_can_obj_t *self); -int common_hal_canio_can_bus_off_state_count_get(canio_can_obj_t *self); -int common_hal_canio_can_error_passive_state_count_get(canio_can_obj_t *self); -int common_hal_canio_can_error_warning_state_count_get(canio_can_obj_t *self); bool common_hal_canio_can_loopback_get(canio_can_obj_t *self); int common_hal_canio_can_receive_error_count_get(canio_can_obj_t *self); canio_bus_state_t common_hal_canio_can_state_get(canio_can_obj_t *self); From d79f4e26621681d80e72a614ca8f2210ac4ca02f Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Tue, 29 Sep 2020 14:32:27 -0500 Subject: [PATCH 18/19] canio: doc improvement -- don't needlessly call out deinit behavior --- shared-bindings/canio/Listener.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared-bindings/canio/Listener.c b/shared-bindings/canio/Listener.c index 5a4df3c46b..93552af814 100644 --- a/shared-bindings/canio/Listener.c +++ b/shared-bindings/canio/Listener.c @@ -77,7 +77,7 @@ STATIC mp_obj_t canio_listener_in_waiting(mp_obj_t self_in) { STATIC MP_DEFINE_CONST_FUN_OBJ_1(canio_listener_in_waiting_obj, canio_listener_in_waiting); //| def __iter__(self) -> Listener: -//| """Returns self, unless the object is deinitialized. +//| """Returns self //| //| This method exists so that `Listener` can be used as an //| iterable""" From 88cbf772dfb5bcfd020c42c2470d0287c4b780a8 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Tue, 29 Sep 2020 20:13:52 -0500 Subject: [PATCH 19/19] canio.CAN: clarify read-only status of several properties --- shared-bindings/canio/CAN.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/shared-bindings/canio/CAN.c b/shared-bindings/canio/CAN.c index e8ab15a3c9..6dae36943a 100644 --- a/shared-bindings/canio/CAN.c +++ b/shared-bindings/canio/CAN.c @@ -172,7 +172,7 @@ STATIC const mp_obj_property_t canio_can_receive_error_count_obj = { }; //| state: State -//| """The current state of the bus.""" +//| """The current state of the bus. (read-only)""" STATIC mp_obj_t canio_can_state_get(mp_obj_t self_in) { canio_can_obj_t *self = MP_OBJ_TO_PTR(self_in); common_hal_canio_can_check_for_deinit(self); @@ -256,7 +256,8 @@ STATIC mp_obj_t canio_can_listen(size_t n_args, const mp_obj_t *pos_args, mp_map MP_DEFINE_CONST_FUN_OBJ_KW(canio_can_listen_obj, 1, canio_can_listen); //| loopback: bool -//| """True if the device was created in loopback mode, False otherwise""" +//| """True if the device was created in loopback mode, False +//| otherwise (read-only)""" //| STATIC mp_obj_t canio_can_loopback_get(mp_obj_t self_in) { canio_can_obj_t *self = MP_OBJ_TO_PTR(self_in); @@ -294,7 +295,8 @@ STATIC mp_obj_t canio_can_send(mp_obj_t self_in, mp_obj_t message_in) { MP_DEFINE_CONST_FUN_OBJ_2(canio_can_send_obj, canio_can_send); //| silent: bool -//| """True if the device was created in silent mode, False otherwise""" +//| """True if the device was created in silent mode, False +//| otherwise (read-only)""" //| STATIC mp_obj_t canio_can_silent_get(mp_obj_t self_in) { canio_can_obj_t *self = MP_OBJ_TO_PTR(self_in);