31
31
#include "py/runtime.h"
32
32
33
33
//| class Message:
34
- //| def __init__(self, id: int=0 , data: Optional[ bytes] = None , *, size: Optional[int] = None, rtr: bool = False , extended: bool = False):
35
- //| """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 .
34
+ //| def __init__(self, id: int, data: bytes, *, extended: bool = False):
35
+ //| """Construct a Message to send on a CAN bus.
36
36
//|
37
37
//| :param int id: The numeric ID of the message
38
38
//| :param bytes data: The content of the message
39
- //| :param int size: The amount of data requested, for an rtr
40
- //| :param bool rtr: True if the message represents an rtr (Remote Transmission Request)
41
39
//| :param bool extended: True if the message has an extended identifier, False if it has a standard identifier
42
40
//|
43
- //| In CAN, messages can have a size from 0 to 8 bytes.
44
- //|
45
- //| For a non-rtr message, specify ``data``. For an rtr-message, specify either ``data`` (a dummy buffer of the requested size) or ``size``.
41
+ //| In CAN, messages can have a length from 0 to 8 bytes.
46
42
//| """
47
43
//| ...
48
44
//|
49
45
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 ) {
50
- enum { ARG_id , ARG_data , ARG_size , ARG_rtr , ARG_extended , NUM_ARGS };
46
+ enum { ARG_id , ARG_data , ARG_extended , NUM_ARGS };
51
47
static const mp_arg_t allowed_args [] = {
52
- { MP_QSTR_id , MP_ARG_INT , {.u_obj = 0 } },
53
- { MP_QSTR_data , MP_ARG_OBJ , {.u_obj = 0 } },
54
- { MP_QSTR_size , MP_ARG_INT , {.u_int = -1 } },
55
- { MP_QSTR_rtr , MP_ARG_BOOL , {.u_bool = false} },
48
+ { MP_QSTR_id , MP_ARG_INT | MP_ARG_REQUIRED },
49
+ { MP_QSTR_data , MP_ARG_OBJ | MP_ARG_REQUIRED },
56
50
{ MP_QSTR_extended , MP_ARG_BOOL , {.u_bool = false} },
57
51
};
58
52
mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
59
53
MP_STATIC_ASSERT ( MP_ARRAY_SIZE (allowed_args ) == NUM_ARGS );
60
54
61
55
mp_arg_parse_all (n_args , pos_args , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
62
56
63
- bool rtr = args [ARG_rtr ].u_bool ;
64
- bool extended = args [ARG_extended ].u_bool ;
65
- size_t size = (size_t )args [ARG_size ].u_int ;
66
- bool specified_size = (size != (size_t )-1 );
67
- bool specified_data = (args [ARG_data ].u_obj != NULL );
68
-
69
- if (specified_size && specified_data ) {
70
- mp_raise_TypeError (translate ("specify size or data, but not both" ));
71
- }
72
-
73
57
mp_buffer_info_t data ;
74
- if (specified_data ) {
75
- mp_get_buffer_raise (args [ARG_data ].u_obj , & data , MP_BUFFER_READ );
76
- } else if (specified_size ) {
77
- data .buf = 0 ;
78
- data .len = size ;
79
- } else {
80
- data .buf = 0 ;
81
- data .len = 0 ;
82
- }
58
+ mp_get_buffer_raise (args [ARG_data ].u_obj , & data , MP_BUFFER_READ );
83
59
84
60
if (data .len > 8 ) {
85
61
mp_raise_ValueError (translate ("Messages limited to 8 bytes" ));
86
62
}
87
63
88
64
canio_message_obj_t * self = m_new_obj (canio_message_obj_t );
89
65
self -> base .type = & canio_message_type ;
90
- common_hal_canio_message_construct (self , args [ARG_id ].u_int , data .buf , data .len , rtr , extended );
66
+ common_hal_canio_message_construct (self , args [ARG_id ].u_int , data .buf , data .len , args [ ARG_extended ]. u_bool );
91
67
return self ;
92
68
}
93
69
@@ -115,13 +91,11 @@ STATIC const mp_obj_property_t canio_message_id_obj = {
115
91
};
116
92
117
93
//| data: bytes
118
- //| """The content of the message, or dummy content in the case of an rtr.
119
- //|
120
- //| Assigning to data also clears the rtr flag, if it was set."""
94
+ //| """The content of the message"""
121
95
//|
122
96
STATIC mp_obj_t canio_message_data_get (const mp_obj_t self_in ) {
123
97
canio_message_obj_t * self = self_in ;
124
- return mp_obj_new_bytes ((const byte * )common_hal_canio_message_get_data (self ), common_hal_canio_message_get_size (self ));
98
+ return mp_obj_new_bytes ((const byte * )common_hal_canio_message_get_data (self ), common_hal_canio_message_get_length (self ));
125
99
}
126
100
MP_DEFINE_CONST_FUN_OBJ_1 (canio_message_data_get_obj , canio_message_data_get );
127
101
@@ -147,7 +121,7 @@ STATIC const mp_obj_property_t canio_message_data_obj = {
147
121
148
122
149
123
//| extended: bool
150
- //| """True if the message represents a remote transmission request (RTR) """
124
+ //| """True if the message's id is an extended id """
151
125
//|
152
126
STATIC mp_obj_t canio_message_extended_get (const mp_obj_t self_in ) {
153
127
canio_message_obj_t * self = self_in ;
@@ -170,36 +144,80 @@ STATIC const mp_obj_property_t canio_message_extended_obj = {
170
144
(mp_obj_t )& mp_const_none_obj },
171
145
};
172
146
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
+ }
173
175
174
- //| rtr: bool
175
- //| """True if the message represents a remote transmission request (RTR). Setting rtr to true zeros out data"""
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."""
176
192
//|
177
- STATIC mp_obj_t canio_message_rtr_get (const mp_obj_t self_in ) {
193
+ STATIC mp_obj_t canio_remote_transmission_request_length_get (const mp_obj_t self_in ) {
178
194
canio_message_obj_t * self = self_in ;
179
- return mp_obj_new_bool ( common_hal_canio_message_get_rtr (self ));
195
+ return MP_OBJ_NEW_SMALL_INT ( common_hal_canio_message_get_length (self ));
180
196
}
181
- MP_DEFINE_CONST_FUN_OBJ_1 (canio_message_rtr_get_obj , canio_message_rtr_get );
197
+ MP_DEFINE_CONST_FUN_OBJ_1 (canio_remote_transmission_request_length_get_obj , canio_remote_transmission_request_length_get );
182
198
183
- STATIC mp_obj_t canio_message_rtr_set (const mp_obj_t self_in , const mp_obj_t rtr ) {
199
+ STATIC mp_obj_t canio_remote_transmission_request_length_set (const mp_obj_t self_in , const mp_obj_t length_in ) {
184
200
canio_message_obj_t * self = self_in ;
185
- common_hal_canio_message_set_rtr (self , mp_obj_is_true (rtr ));
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 );
186
206
return mp_const_none ;
187
207
}
188
- MP_DEFINE_CONST_FUN_OBJ_2 (canio_message_rtr_set_obj , canio_message_rtr_set );
208
+ MP_DEFINE_CONST_FUN_OBJ_2 (canio_remote_transmission_request_length_set_obj , canio_remote_transmission_request_length_set );
189
209
190
210
191
- STATIC const mp_obj_property_t canio_message_rtr_obj = {
211
+ STATIC const mp_obj_property_t canio_remote_transmission_request_length_obj = {
192
212
.base .type = & mp_type_property ,
193
- .proxy = {(mp_obj_t )& canio_message_rtr_get_obj ,
194
- (mp_obj_t )& canio_message_rtr_set_obj ,
213
+ .proxy = {(mp_obj_t )& canio_remote_transmission_request_length_get_obj ,
214
+ (mp_obj_t )& canio_remote_transmission_request_length_set_obj ,
195
215
(mp_obj_t )& mp_const_none_obj },
196
216
};
197
217
198
-
199
218
STATIC const mp_rom_map_elem_t canio_message_locals_dict_table [] = {
200
219
{ MP_ROM_QSTR (MP_QSTR_id ), MP_ROM_PTR (& canio_message_id_obj ) },
201
220
{ MP_ROM_QSTR (MP_QSTR_data ), MP_ROM_PTR (& canio_message_data_obj ) },
202
- { MP_ROM_QSTR (MP_QSTR_rtr ), MP_ROM_PTR (& canio_message_rtr_obj ) },
203
221
{ MP_ROM_QSTR (MP_QSTR_extended ), MP_ROM_PTR (& canio_message_extended_obj ) },
204
222
};
205
223
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 = {
210
228
.make_new = canio_message_make_new ,
211
229
.locals_dict = (mp_obj_t )& canio_message_locals_dict ,
212
230
};
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
+ };
0 commit comments