Skip to content

Commit a4cc3ad

Browse files
committed
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
1 parent 3e97e9c commit a4cc3ad

File tree

8 files changed

+308
-107
lines changed

8 files changed

+308
-107
lines changed

py/circuitpy_defns.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ SRC_SHARED_MODULE_ALL = \
411411
_bleio/ScanResults.c \
412412
canio/Match.c \
413413
canio/Message.c \
414+
canio/RemoteTransmissionRequest.c \
414415
_eve/__init__.c \
415416
_pixelbuf/PixelBuf.c \
416417
_pixelbuf/__init__.c \

shared-bindings/canio/Message.c

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -144,77 +144,6 @@ STATIC const mp_obj_property_t canio_message_extended_obj = {
144144
(mp_obj_t)&mp_const_none_obj},
145145
};
146146

147-
//| class RemoteTransmissionRequest:
148-
//| def __init__(self, id: int, length: int, *, extended: bool = False):
149-
//| """Construct a Message to send on a CAN bus.
150-
//|
151-
//| :param int id: The numeric ID of the requested message
152-
//| :param int length: The length of the requested message
153-
//| :param bool extended: True if the message has an extended identifier, False if it has a standard identifier
154-
//|
155-
//| In CAN, messages can have a length from 0 to 8 bytes.
156-
//| """
157-
//| ...
158-
//|
159-
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) {
160-
enum { ARG_id, ARG_length, ARG_extended, NUM_ARGS };
161-
static const mp_arg_t allowed_args[] = {
162-
{ MP_QSTR_id, MP_ARG_INT | MP_ARG_REQUIRED },
163-
{ MP_QSTR_length, MP_ARG_INT | MP_ARG_REQUIRED },
164-
{ MP_QSTR_extended, MP_ARG_BOOL, {.u_bool = false} },
165-
};
166-
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
167-
MP_STATIC_ASSERT( MP_ARRAY_SIZE(allowed_args) == NUM_ARGS );
168-
169-
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
170-
171-
int length = args[ARG_length].u_int;
172-
if (length < 0 || length > 8) {
173-
mp_raise_ValueError(translate("Messages limited to 8 bytes"));
174-
}
175-
176-
canio_message_obj_t *self = m_new_obj(canio_message_obj_t);
177-
self->base.type = &canio_remote_transmission_request_type;
178-
common_hal_canio_message_construct(self, args[ARG_id].u_int, NULL, length, args[ARG_extended].u_bool);
179-
return self;
180-
}
181-
182-
//| extended: bool
183-
//| """True if the message's id is an extended id"""
184-
//|
185-
186-
//| id: int
187-
//| """The numeric ID of the message"""
188-
//|
189-
190-
//| length: int
191-
//| """The length of the requested message."""
192-
//|
193-
STATIC mp_obj_t canio_remote_transmission_request_length_get(const mp_obj_t self_in) {
194-
canio_message_obj_t *self = self_in;
195-
return MP_OBJ_NEW_SMALL_INT(common_hal_canio_message_get_length(self));
196-
}
197-
MP_DEFINE_CONST_FUN_OBJ_1(canio_remote_transmission_request_length_get_obj, canio_remote_transmission_request_length_get);
198-
199-
STATIC mp_obj_t canio_remote_transmission_request_length_set(const mp_obj_t self_in, const mp_obj_t length_in) {
200-
canio_message_obj_t *self = self_in;
201-
int length = mp_obj_get_int(length_in);
202-
if (length < 0 || length > 8) {
203-
mp_raise_ValueError(translate("Messages limited to 8 bytes"));
204-
}
205-
common_hal_canio_remote_transmission_request_set_length(self, length);
206-
return mp_const_none;
207-
}
208-
MP_DEFINE_CONST_FUN_OBJ_2(canio_remote_transmission_request_length_set_obj, canio_remote_transmission_request_length_set);
209-
210-
211-
STATIC const mp_obj_property_t canio_remote_transmission_request_length_obj = {
212-
.base.type = &mp_type_property,
213-
.proxy = {(mp_obj_t)&canio_remote_transmission_request_length_get_obj,
214-
(mp_obj_t)&canio_remote_transmission_request_length_set_obj,
215-
(mp_obj_t)&mp_const_none_obj},
216-
};
217-
218147
STATIC const mp_rom_map_elem_t canio_message_locals_dict_table[] = {
219148
{ MP_ROM_QSTR(MP_QSTR_id), MP_ROM_PTR(&canio_message_id_obj) },
220149
{ 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 = {
228157
.make_new = canio_message_make_new,
229158
.locals_dict = (mp_obj_t)&canio_message_locals_dict,
230159
};
231-
232-
STATIC const mp_rom_map_elem_t canio_remote_transmission_request_locals_dict_table[] = {
233-
{ MP_ROM_QSTR(MP_QSTR_id), MP_ROM_PTR(&canio_message_id_obj) },
234-
{ MP_ROM_QSTR(MP_QSTR_length), MP_ROM_PTR(&canio_remote_transmission_request_length_obj) },
235-
{ MP_ROM_QSTR(MP_QSTR_extended), MP_ROM_PTR(&canio_message_extended_obj) },
236-
};
237-
STATIC MP_DEFINE_CONST_DICT(canio_remote_transmission_request_locals_dict, canio_remote_transmission_request_locals_dict_table);
238-
239-
const mp_obj_type_t canio_remote_transmission_request_type = {
240-
{ &mp_type_type },
241-
.name = MP_QSTR_RemoteTransmissionRequest,
242-
.make_new = canio_remote_transmission_request_make_new,
243-
.locals_dict = (mp_obj_t)&canio_remote_transmission_request_locals_dict,
244-
};

shared-bindings/canio/Message.h

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

shared-module/canio/Message.c

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,26 +68,6 @@ size_t common_hal_canio_message_get_length(const canio_message_obj_t *self)
6868
return self->size;
6969
}
7070

71-
void common_hal_canio_remote_transmission_request_set_length(canio_message_obj_t *self, size_t size)
72-
{
73-
memset(self->data, 0, size);
74-
self->size = size;
75-
}
76-
77-
78-
bool common_hal_canio_message_get_rtr(const canio_message_obj_t *self)
79-
{
80-
return self->rtr;
81-
}
82-
83-
void common_hal_canio_message_set_rtr(canio_message_obj_t *self, bool rtr)
84-
{
85-
self->rtr = rtr;
86-
if (rtr) {
87-
memset(self->data, 0, self->size);
88-
}
89-
}
90-
9171
bool common_hal_canio_message_get_extended(const canio_message_obj_t *self)
9272
{
9373
return self->extended;

0 commit comments

Comments
 (0)