Skip to content

Commit bd01d31

Browse files
committed
rp2pio: Add pins_are_sequential
This can be used where the standard API calls for a list of pins, to check that they satisfy the requirements of the rp2pio state machine, e.g., ```python def __init__(self, pin_a, pin_b): if not rp2pio.pins_are_sequential([pin_a, pin_b]): raise ValueError("Pins must be sequential") ```
1 parent ff62b0d commit bd01d31

File tree

3 files changed

+87
-1
lines changed

3 files changed

+87
-1
lines changed

ports/raspberrypi/bindings/rp2pio/__init__.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,28 @@
2828
#include "py/runtime.h"
2929

3030
#include "bindings/rp2pio/StateMachine.h"
31+
#include "bindings/rp2pio/__init__.h"
3132

3233
//| """Hardware interface to RP2 series' programmable IO (PIO) peripheral."""
3334
//|
3435

36+
//| def pins_are_sequential(pins: List[microcontroller.Pin]) -> bool:
37+
//| """Return True if the pins have sequential GPIO numbers, False otherwise"""
38+
//| ...
39+
//|
40+
STATIC mp_obj_t rp2pio_pins_are_sequential(const mp_obj_t pins) {
41+
size_t len;
42+
mp_obj_t *items;
43+
mp_obj_get_array(pins, &len, &items);
44+
return mp_obj_new_bool(common_hal_rp2pio_pins_are_sequential(len, items));
45+
}
46+
47+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(rp2pio_pins_are_sequential_obj, rp2pio_pins_are_sequential);
48+
3549
STATIC const mp_rom_map_elem_t rp2pio_module_globals_table[] = {
3650
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_rp2pio) },
3751
{ MP_ROM_QSTR(MP_QSTR_StateMachine), MP_ROM_PTR(&rp2pio_statemachine_type) },
52+
{ MP_ROM_QSTR(MP_QSTR_pins_are_sequential), MP_ROM_PTR(&rp2pio_pins_are_sequential_obj) },
3853
};
3954

4055
STATIC MP_DEFINE_CONST_DICT(rp2pio_module_globals, rp2pio_module_globals_table);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 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+
bool common_hal_rp2pio_pins_are_sequential(size_t len, mp_obj_t *items);
Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,43 @@
1-
// Nothing yet.
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 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 "py/obj.h"
28+
#include "shared-bindings/microcontroller/Pin.h"
29+
#include "bindings/rp2pio/__init__.h"
30+
31+
bool common_hal_rp2pio_pins_are_sequential(size_t len, mp_obj_t *items) {
32+
if(len == 0) {
33+
return true;
34+
}
35+
mcu_pin_obj_t *last_pin = validate_obj_is_pin(items[0]);
36+
for(int i=1; i<len; i++) {
37+
mcu_pin_obj_t *pin = validate_obj_is_pin(items[i]);
38+
if (pin->number != last_pin->number + 1) {
39+
return false;
40+
}
41+
}
42+
return true;
43+
}

0 commit comments

Comments
 (0)