Listerner: read -> receive, drop readinto

This prepares for creating a separate RemoteTransmissionRequest class
This commit is contained in:
Jeff Epler 2020-09-24 14:05:16 -05:00
parent 79ca430ddf
commit 48bda589b8
5 changed files with 22 additions and 26 deletions

View File

@ -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 {

View File

@ -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);

View File

@ -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;

View File

@ -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);

View File

@ -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);