Skip to content

Commit 813b581

Browse files
author
Daniel Campora
committed
cc3200: Add Pin.name() method.
1 parent 7784131 commit 813b581

4 files changed

Lines changed: 39 additions & 7 deletions

File tree

cc3200/misc/pin_named_pins.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,26 @@
3030
#include <string.h>
3131

3232
#include "py/mpconfig.h"
33+
#include MICROPY_HAL_H
3334
#include "py/obj.h"
3435
#include "inc/hw_types.h"
3536
#include "inc/hw_ints.h"
3637
#include "inc/hw_memmap.h"
3738
#include "pybpin.h"
38-
#include MICROPY_HAL_H
3939

4040

41+
STATIC void pin_named_pins_obj_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
42+
pin_named_pins_obj_t *self = self_in;
43+
mp_printf(print, "<Pin.%q>", self->name);
44+
}
45+
46+
const mp_obj_type_t pin_cpu_pins_obj_type = {
47+
{ &mp_type_type },
48+
.name = MP_QSTR_cpu,
49+
.print = pin_named_pins_obj_print,
50+
.locals_dict = (mp_obj_t)&pin_cpu_pins_locals_dict,
51+
};
52+
4153
pin_obj_t *pin_find_named_pin(const mp_obj_dict_t *named_pins, mp_obj_t name) {
4254
mp_map_t *named_map = mp_obj_dict_get_map((mp_obj_t)named_pins);
4355
mp_map_elem_t *named_elem = mp_map_lookup(named_map, name, MP_MAP_LOOKUP);

cc3200/mods/pybpin.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
/// Example callback:
7070
///
7171
/// def pincb(pin):
72-
/// print(pin.get_config().name)
72+
/// print(pin.name())
7373
///
7474
/// extint = pyb.Pin('GPIO10', 0, pyb.Pin.INT_RISING, pyb.GPIO.STD_PD, pyb.S2MA)
7575
/// extint.callback (mode=pyb.Pin.INT_RISING, handler=pincb)
@@ -525,6 +525,14 @@ STATIC mp_obj_t pin_toggle(mp_obj_t self_in) {
525525
}
526526
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_toggle_obj, pin_toggle);
527527

528+
/// \method name()
529+
/// Returns the qstr name of the pin
530+
STATIC mp_obj_t pin_name(mp_obj_t self_in) {
531+
pin_obj_t *self = self_in;
532+
return MP_OBJ_NEW_QSTR(self->name);
533+
}
534+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_name_obj, pin_name);
535+
528536
/// \method info()
529537
/// Returns a named tupple with the current configuration of the gpio pin
530538
STATIC mp_obj_t pin_info(mp_obj_t self_in) {
@@ -681,9 +689,13 @@ STATIC const mp_map_elem_t pin_locals_dict_table[] = {
681689
{ MP_OBJ_NEW_QSTR(MP_QSTR_low), (mp_obj_t)&pin_low_obj },
682690
{ MP_OBJ_NEW_QSTR(MP_QSTR_high), (mp_obj_t)&pin_high_obj },
683691
{ MP_OBJ_NEW_QSTR(MP_QSTR_toggle), (mp_obj_t)&pin_toggle_obj },
692+
{ MP_OBJ_NEW_QSTR(MP_QSTR_name), (mp_obj_t)&pin_name_obj },
684693
{ MP_OBJ_NEW_QSTR(MP_QSTR_info), (mp_obj_t)&pin_info_obj },
685694
{ MP_OBJ_NEW_QSTR(MP_QSTR_callback), (mp_obj_t)&pin_callback_obj },
686695

696+
// class attributes
697+
{ MP_OBJ_NEW_QSTR(MP_QSTR_cpu), (mp_obj_t)&pin_cpu_pins_obj_type },
698+
687699
// class constants
688700
/// \constant IN - set the pin to input mode
689701
/// \constant OUT - set the pin to output mode

cc3200/qstrdefsport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ Q(tell)
109109

110110
// for Pin class
111111
Q(Pin)
112+
Q(cpu)
112113
Q(init)
113114
Q(value)
114115
Q(low)

docs/library/pyb.Pin.rst

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Usage Model:
7272
You can also configure the Pin to generate interrupts. For instance::
7373

7474
def pincb(pin):
75-
print(pin.info().name)
75+
print(pin.name())
7676

7777
pin_int = pyb.Pin('GPIO10', af=0, mode=Pin.IN, type=pyb.Pin.STD_PD, strength=pyb.Pin.S2MA)
7878
pin_int.callback (mode=pyb.Pin.INT_RISING, handler=pincb)
@@ -229,10 +229,12 @@ Methods
229229
will match one of the allowed constants for the mode argument to the init
230230
function.
231231

232-
.. method:: pin.name()
233-
234-
Get the pin name.
235-
232+
.. method:: pin.name()
233+
234+
Get the pin name.
235+
236+
.. only:: port_pyboard
237+
236238
.. method:: pin.names()
237239

238240
Returns the cpu and board names for this pin.
@@ -262,6 +264,11 @@ Methods
262264
Return a 5-tuple with the configuration of the pin:
263265
``(name, alternate-function, mode, type, strength)``
264266

267+
.. warning::
268+
This method cannot be called within a callback (interrupt-context)
269+
because it needs to allocate memory to return the tuple and memory
270+
allocations are disabled while interrupts are being serviced.
271+
265272
.. method:: pin.callback(\*, mode, priority=1, handler=None, wakes=pyb.Sleep.ACTIVE)
266273

267274
Create a callback to be triggered when the input level at the pin changes.

0 commit comments

Comments
 (0)