samd/machine_pin: Allow specifying a pin or LED by its name as a string.

The names are defined in pins.csv.
This commit is contained in:
robert-hh 2022-06-04 20:56:18 +02:00 committed by Damien George
parent 33eaf739d2
commit e8615f5813
3 changed files with 30 additions and 9 deletions

View File

@ -15,6 +15,8 @@ typedef struct _machine_pin_obj_t {
char *name;
} machine_pin_obj_t;
int pin_find(mp_obj_t pin, const machine_pin_obj_t machine_pin_obj[], int table_size);
"""
led_header_prefix = """typedef struct _machine_led_obj_t {
@ -33,19 +35,19 @@ class Pins:
def parse_csv_file(self, filename):
with open(filename, "r") as csvfile:
rows = csv.reader(csvfile)
rows = csv.reader(csvfile, skipinitialspace=True)
for row in rows:
# Pin numbers must start with "PIN_"
# LED numbers must start with "LED_"
if len(row) > 0:
if row[0].strip().startswith("PIN_"):
if row[0].startswith("PIN_"):
if len(row) == 1:
self.board_pins.append([row[0], row[0][4:]])
else:
self.board_pins.append([row[0], row[1]])
elif row[0].strip().startswith("LED_"):
elif row[0].startswith("LED_"):
self.board_leds.append(["PIN_" + row[0][4:], row[1]])
elif row[0].strip().startswith("-"):
elif row[0].startswith("-"):
self.board_pins.append(["-1", ""])
def print_pins(self, pins_filename):

View File

@ -30,12 +30,10 @@
#include "py/mphal.h"
#include "extmod/virtpin.h"
#include "modmachine.h"
#include "pins.h" // boards/<BOARD>/
#include "pins.h"
// ASF4 (MCU package specific pin defs in 'boards')
#include "hal_gpio.h"
#include "hpl_gpio.h"
#include "hal_atomic.h"
STATIC void machine_led_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
machine_led_obj_t *self = self_in;
@ -70,7 +68,7 @@ mp_obj_t mp_led_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw,
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
// get the wanted LED object
int wanted_led = mp_obj_get_int(args[0]);
int wanted_led = pin_find(args[0], (const machine_pin_obj_t *)machine_led_obj, MP_ARRAY_SIZE(machine_led_obj));
const machine_led_obj_t *self = NULL;
if (0 <= wanted_led && wanted_led < MP_ARRAY_SIZE(machine_led_obj)) {
self = (machine_led_obj_t *)&machine_led_obj[wanted_led];

View File

@ -26,6 +26,7 @@
* Uses pins.h & pins.c to create board (MCU package) specific 'machine_pin_obj' array.
*/
#include "string.h"
#include "py/runtime.h"
#include "py/mphal.h"
#include "extmod/virtpin.h"
@ -58,6 +59,26 @@ STATIC void pin_validate_drive(bool strength) {
}
}
int pin_find(mp_obj_t pin, const machine_pin_obj_t machine_pin_obj[], int table_size) {
int wanted_pin = -1;
if (mp_obj_is_small_int(pin)) {
// Pin defined by the index of pin table
wanted_pin = mp_obj_get_int(pin);
} else if (mp_obj_is_str(pin)) {
// Search by name
size_t slen;
const char *s = mp_obj_str_get_data(pin, &slen);
for (wanted_pin = 0; wanted_pin < table_size; wanted_pin++) {
if (slen == strlen(machine_pin_obj[wanted_pin].name) &&
strncmp(s, machine_pin_obj[wanted_pin].name, slen) == 0) {
break;
}
}
}
return wanted_pin;
}
// Pin.init(mode, pull=None, *, value=None, drive=0). No 'alt' yet.
STATIC mp_obj_t machine_pin_obj_init_helper(const machine_pin_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_mode, ARG_pull, ARG_value, ARG_drive, ARG_alt };
@ -112,7 +133,7 @@ mp_obj_t mp_pin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw,
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
// get the wanted pin object
int wanted_pin = mp_obj_get_int(args[0]);
int wanted_pin = pin_find(args[0], machine_pin_obj, MP_ARRAY_SIZE(machine_pin_obj));
const machine_pin_obj_t *self = NULL;
if (0 <= wanted_pin && wanted_pin < MP_ARRAY_SIZE(machine_pin_obj)) {