Skip to content

Commit 268717e

Browse files
committed
ParallelImageCapture: Switch to taking a list of pins
.. adopting validate_pins from RGBMatrix into shared-bindings .. updating other platforms for API change
1 parent 7782bc2 commit 268717e

File tree

8 files changed

+61
-29
lines changed

8 files changed

+61
-29
lines changed

locale/circuitpython.pot

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,11 @@ msgstr ""
12801280
msgid "Invalid data_count %d"
12811281
msgstr ""
12821282

1283+
#: ports/atmel-samd/common-hal/imagecapture/ParallelImageCapture.c
1284+
#, c-format
1285+
msgid "Invalid data_pins[%d]"
1286+
msgstr ""
1287+
12831288
#: shared-bindings/digitalio/DigitalInOut.c
12841289
msgid "Invalid direction."
12851290
msgstr ""
@@ -1813,6 +1818,7 @@ msgid ""
18131818
"constructor"
18141819
msgstr ""
18151820

1821+
#: ports/raspberrypi/common-hal/imagecapture/ParallelImageCapture.c
18161822
#: ports/raspberrypi/common-hal/rotaryio/IncrementalEncoder.c
18171823
msgid "Pins must be sequential"
18181824
msgstr ""

ports/atmel-samd/common-hal/imagecapture/ParallelImageCapture.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,16 @@
5050
#define PIN_PCC_CLK (PIN_PA14)
5151

5252
void common_hal_imagecapture_parallelimagecapture_construct(imagecapture_parallelimagecapture_obj_t *self,
53-
const mcu_pin_obj_t *data0,
53+
const uint8_t data_pins[],
54+
uint8_t data_count,
5455
const mcu_pin_obj_t *data_clock,
5556
const mcu_pin_obj_t *vertical_sync,
56-
const mcu_pin_obj_t *horizontal_reference,
57-
int data_count) {
58-
if (data0->number != PIN_PCC_D0) {
59-
mp_raise_ValueError_varg(translate("Invalid %q pin"), MP_QSTR_data0);
57+
const mcu_pin_obj_t *horizontal_reference) {
58+
59+
for (int i = 0; i < data_count; i++) {
60+
if (data_pins[i] != PIN_PCC_D0 + i) {
61+
mp_raise_ValueError_varg(translate("Invalid data_pins[%d]"), i);
62+
}
6063
}
6164
// The peripheral supports 8, 10, 12, or 14 data bits, but the code only supports 8 at present
6265
if (data_count != 8) {
@@ -73,7 +76,7 @@ void common_hal_imagecapture_parallelimagecapture_construct(imagecapture_paralle
7376
}
7477
// technically, 0 was validated as free already but check again
7578
for (int i = 0; i < data_count; i++) {
76-
if (!pin_number_is_free(data0->number + i)) {
79+
if (!pin_number_is_free(data_pins[i])) {
7780
mp_raise_ValueError_varg(translate("data pin #%d in use"), i);
7881
}
7982
}

ports/raspberrypi/common-hal/imagecapture/ParallelImageCapture.c

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,33 @@
6868
/* .wrap */ \
6969
}
7070

71+
mcu_pin_obj_t *pin_from_number(uint8_t number) {
72+
const mp_map_t *mcu_map = &mcu_pin_globals.map;
73+
for (uint8_t i = 0; i < mcu_map->alloc; i++) {
74+
mp_obj_t val = mcu_map->table[i].value;
75+
if (!mp_obj_is_type(val, &mcu_pin_type)) {
76+
continue;
77+
}
78+
mcu_pin_obj_t *pin = MP_OBJ_TO_PTR(val);
79+
if (pin->number == number) {
80+
return pin;
81+
}
82+
}
83+
return NULL;
84+
}
85+
7186
void common_hal_imagecapture_parallelimagecapture_construct(imagecapture_parallelimagecapture_obj_t *self,
72-
const mcu_pin_obj_t *data0,
87+
const uint8_t data_pins[],
88+
uint8_t data_count,
7389
const mcu_pin_obj_t *data_clock,
7490
const mcu_pin_obj_t *vertical_sync,
75-
const mcu_pin_obj_t *horizontal_reference,
76-
int data_count) {
91+
const mcu_pin_obj_t *horizontal_reference) {
92+
93+
for (int i = 1; i < data_count; i++) {
94+
if (data_pins[i] - data_pins[0] != i) {
95+
mp_raise_RuntimeError(translate("Pins must be sequential"));
96+
}
97+
}
7798

7899
uint16_t imagecapture_code[] = IMAGECAPTURE_CODE(data_count, data_clock->number, vertical_sync->number, horizontal_reference->number);
79100

@@ -82,7 +103,7 @@ void common_hal_imagecapture_parallelimagecapture_construct(imagecapture_paralle
82103
common_hal_mcu_processor_get_frequency(), // full speed (4 instructions per loop -> max pclk 30MHz @ 120MHz)
83104
0, 0, // init
84105
NULL, 0, 0, 0, // out pins
85-
data0, data_count, // in pins
106+
pin_from_number(data_pins[0]), data_count, // in pins
86107
0, 0, // in pulls
87108
NULL, 0, 0, 0, // set pins
88109
#if DEBUG_STATE_MACHINE

shared-bindings/imagecapture/ParallelImageCapture.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,24 @@
3939
//| def __init__(
4040
//| self,
4141
//| *,
42-
//| data0: microcontroller.Pin,
43-
//| data_count: int=8,
42+
//| data_pins: List[microcontroller.Pin],
4443
//| clock: microcontroller.Pin,
4544
//| vsync: Optional[microcontroller.Pin],
4645
//| href: Optional[microcontroller.Pin],
4746
//| ):
4847
//| """Create a parallel image capture object
4948
//| :param microcontroller.Pin data0: The first data pin. Additional data pins are assumed to follow this pin directly in the microcontroller's standard pin ordering.
50-
//| :param int data_count: The number of data pins.
5149
//| :param microcontroller.Pin clock: The pixel clock input.
5250
//| :param microcontroller.Pin vsync: The vertical sync input, which has a negative-going pulse at the beginning of each frame.
5351
//| :param microcontroller.Pin href: The horizontal reference input, which is high whenever the camera is transmitting valid pixel information.
5452
//| """
5553
//| ...
5654
//|
5755
STATIC mp_obj_t imagecapture_parallelimagecapture_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
58-
enum { ARG_data0, ARG_data_count, ARG_clock, ARG_vsync, ARG_href,
56+
enum { ARG_data_pins, ARG_clock, ARG_vsync, ARG_href,
5957
NUM_ARGS };
6058
static const mp_arg_t allowed_args[] = {
61-
{ MP_QSTR_data0, MP_ARG_OBJ | MP_ARG_REQUIRED | MP_ARG_KW_ONLY },
62-
{ MP_QSTR_data_count, MP_ARG_INT | MP_ARG_KW_ONLY, { .u_int = 8 } },
59+
{ MP_QSTR_data_pins, MP_ARG_OBJ | MP_ARG_KW_ONLY, { .u_obj = MP_ROM_NONE } },
6360
{ MP_QSTR_clock, MP_ARG_OBJ | MP_ARG_REQUIRED | MP_ARG_KW_ONLY },
6461
{ MP_QSTR_vsync, MP_ARG_OBJ | MP_ARG_REQUIRED | MP_ARG_KW_ONLY },
6562
{ MP_QSTR_href, MP_ARG_OBJ | MP_ARG_REQUIRED | MP_ARG_KW_ONLY },
@@ -68,15 +65,18 @@ STATIC mp_obj_t imagecapture_parallelimagecapture_make_new(const mp_obj_type_t *
6865
MP_STATIC_ASSERT(MP_ARRAY_SIZE(allowed_args) == NUM_ARGS);
6966
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
7067

71-
mcu_pin_obj_t *data0 = validate_obj_is_free_pin(args[ARG_data0].u_obj);
68+
uint8_t pins[32];
69+
uint8_t pin_count;
70+
validate_pins(MP_QSTR_data, pins, MP_ARRAY_SIZE(pins), args[ARG_data_pins].u_obj, &pin_count);
71+
7272
mcu_pin_obj_t *clock = validate_obj_is_free_pin(args[ARG_clock].u_obj);
7373
mcu_pin_obj_t *vsync = validate_obj_is_free_pin_or_none(args[ARG_vsync].u_obj);
7474
mcu_pin_obj_t *href = validate_obj_is_free_pin_or_none(args[ARG_href].u_obj);
7575

7676
imagecapture_parallelimagecapture_obj_t *self = m_new_obj(imagecapture_parallelimagecapture_obj_t);
7777
self->base.type = &imagecapture_parallelimagecapture_type;
7878

79-
common_hal_imagecapture_parallelimagecapture_construct(self, data0, clock, vsync, href, args[ARG_data_count].u_int);
79+
common_hal_imagecapture_parallelimagecapture_construct(self, pins, pin_count, clock, vsync, href);
8080

8181
return self;
8282
}

shared-bindings/imagecapture/ParallelImageCapture.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@
3131
typedef struct imagecapture_parallelimagecapture_obj imagecapture_parallelimagecapture_obj_t;
3232
extern const mp_obj_type_t imagecapture_parallelimagecapture_type;
3333

34+
// if only the first element of data_pins is non-NULL, the pins are sequential in microcontroller pin numbering.
3435
void common_hal_imagecapture_parallelimagecapture_construct(imagecapture_parallelimagecapture_obj_t *self,
35-
const mcu_pin_obj_t *data0,
36+
const uint8_t data_pins[],
37+
uint8_t data_count,
3638
const mcu_pin_obj_t *data_clock,
3739
const mcu_pin_obj_t *vertical_sync,
38-
const mcu_pin_obj_t *horizontal_sync,
39-
int data_count);
40+
const mcu_pin_obj_t *horizontal_sync);
4041
void common_hal_imagecapture_parallelimagecapture_deinit(imagecapture_parallelimagecapture_obj_t *self);
4142
bool common_hal_imagecapture_parallelimagecapture_deinited(imagecapture_parallelimagecapture_obj_t *self);
4243
void common_hal_imagecapture_parallelimagecapture_capture(imagecapture_parallelimagecapture_obj_t *self, void *buffer, size_t bufsize);

shared-bindings/microcontroller/Pin.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,11 @@ void assert_pin_free(const mcu_pin_obj_t *pin) {
137137
mp_raise_ValueError_varg(translate("%q in use"), name);
138138
}
139139
}
140+
141+
void validate_pins(qstr what, uint8_t *pin_nos, mp_int_t max_pins, mp_obj_t seq, uint8_t *count_out) {
142+
mcu_pin_obj_t *pins[max_pins];
143+
validate_list_is_free_pins(what, pins, max_pins, seq, count_out);
144+
for (mp_int_t i = 0; i < *count_out; i++) {
145+
pin_nos[i] = common_hal_mcu_pin_number(pins[i]);
146+
}
147+
}

shared-bindings/microcontroller/Pin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ mcu_pin_obj_t *validate_obj_is_pin_or_none(mp_obj_t obj);
3838
mcu_pin_obj_t *validate_obj_is_free_pin(mp_obj_t obj);
3939
mcu_pin_obj_t *validate_obj_is_free_pin_or_none(mp_obj_t obj);
4040
void validate_list_is_free_pins(qstr what, mcu_pin_obj_t **pins_out, mp_int_t max_pins, mp_obj_t seq, uint8_t *count_out);
41+
void validate_pins(qstr what, uint8_t *pin_nos, mp_int_t max_pins, mp_obj_t seq, uint8_t *count_out);
4142

4243
void assert_pin_free(const mcu_pin_obj_t *pin);
4344

shared-bindings/rgbmatrix/RGBMatrix.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,6 @@ STATIC uint8_t validate_pin(mp_obj_t obj) {
4949
return common_hal_mcu_pin_number(result);
5050
}
5151

52-
STATIC void validate_pins(qstr what, uint8_t *pin_nos, mp_int_t max_pins, mp_obj_t seq, uint8_t *count_out) {
53-
mcu_pin_obj_t *pins[max_pins];
54-
validate_list_is_free_pins(what, pins, max_pins, seq, count_out);
55-
for (mp_int_t i = 0; i < *count_out; i++) {
56-
pin_nos[i] = common_hal_mcu_pin_number(pins[i]);
57-
}
58-
}
59-
6052
STATIC void claim_and_never_reset_pin(mp_obj_t pin) {
6153
common_hal_mcu_pin_claim(pin);
6254
common_hal_never_reset_pin(pin);

0 commit comments

Comments
 (0)