1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
/*
* 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/Listener.h"
#include "shared-bindings/canio/Message.h"
#include "common-hal/canio/Listener.h"
#include "py/runtime.h"
#include "py/objproperty.h"
//| class Listener:
//| """Listens for CAN message
//|
//| `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
//|
//| 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) {
canio_listener_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_canio_listener_check_for_deinit(self);
mp_obj_t message = common_hal_canio_listener_receive(self);
// note: receive fills out the type field of the message
if (message) {
return message;
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(canio_listener_receive_obj, canio_listener_receive);
//| def in_waiting(self) -> int:
//| """Returns the number of messages (including remote
//| transmission requests) waiting"""
//| ...
//|
STATIC mp_obj_t canio_listener_in_waiting(mp_obj_t self_in) {
canio_listener_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_canio_listener_check_for_deinit(self);
return MP_OBJ_NEW_SMALL_INT(common_hal_canio_listener_in_waiting(self));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(canio_listener_in_waiting_obj, canio_listener_in_waiting);
//| def __iter__(self) -> Listener:
//| """Returns self
//|
//| This method exists so that `Listener` can be used as an
//| iterable"""
//| ...
//|
//| 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 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) {
mp_obj_t result = canio_listener_receive(self_in);
if (result == mp_const_none) {
return MP_OBJ_STOP_ITERATION;
}
return result;
}
//| def deinit(self) -> None:
//| """Deinitialize this object, freeing its hardware resources"""
//| ...
//|
STATIC mp_obj_t canio_listener_deinit(mp_obj_t self_in) {
canio_listener_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_canio_listener_deinit(self);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(canio_listener_deinit_obj, canio_listener_deinit);
//| def __enter__(self) -> CAN:
//| """Returns self, to allow the object to be used in a `with` statement for resource control"""
//| ...
//|
STATIC mp_obj_t canio_listener_enter(mp_obj_t self_in) {
canio_listener_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_canio_listener_check_for_deinit(self);
return self_in;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(canio_listener_enter_obj, canio_listener_enter);
//| def __exit__(self, unused1: Optional[Type[BaseException]], unused2: Optional[BaseException], unused3: Optional[TracebackType]) -> None:
//| """Calls deinit()"""
//| ...
STATIC mp_obj_t canio_listener_exit(size_t num_args, const mp_obj_t args[]) {
canio_listener_obj_t *self = MP_OBJ_TO_PTR(args[0]);
common_hal_canio_listener_deinit(self);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(canio_listener_exit_obj, 4, 4, canio_listener_exit);
//| timeout : float
STATIC mp_obj_t canio_listener_timeout_get(mp_obj_t self_in) {
canio_listener_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_canio_listener_check_for_deinit(self);
return mp_obj_new_float(common_hal_canio_listener_get_timeout(self));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(canio_listener_timeout_get_obj, canio_listener_timeout_get);
STATIC mp_obj_t canio_listener_timeout_set(mp_obj_t self_in, mp_obj_t timeout_in) {
canio_listener_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_canio_listener_check_for_deinit(self);
common_hal_canio_listener_set_timeout(self, mp_obj_get_float(timeout_in));
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(canio_listener_timeout_set_obj, canio_listener_timeout_set);
MP_PROPERTY_GETSET(canio_listener_timeout_obj,
(mp_obj_t)&canio_listener_timeout_get_obj,
(mp_obj_t)&canio_listener_timeout_set_obj);
STATIC const mp_rom_map_elem_t canio_listener_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&canio_listener_enter_obj) },
{ 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_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);
const mp_obj_type_t canio_listener_type = {
{ &mp_type_type },
.name = MP_QSTR_Listener,
.flags = MP_TYPE_FLAG_EXTENDED,
.locals_dict = (mp_obj_dict_t *)&canio_listener_locals_dict,
MP_TYPE_EXTENDED_FIELDS(
.getiter = mp_identity_getiter,
.iternext = canio_iternext,
),
};
|