Skip to content

Commit 014e4d9

Browse files
committed
PulseIn: add frequency selection
.. implemented only on raspberrypi, where it's easiest. If this is considered useful enough, we can potentially implement it on at least SOME other ports. However, each port is a bit different so for now I just made them ignore the frequency= constructor parameter and return 1_000_000 for the frequency property. This is useful to me because using a frequency of 1000 lets me track the length of WWWB symbols, which range in length from 200ms to 800ms long.
1 parent e084a92 commit 014e4d9

File tree

9 files changed

+66
-12
lines changed

9 files changed

+66
-12
lines changed

ports/atmel-samd/common-hal/pulseio/PulseIn.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ void pulsein_reset() {
145145
}
146146

147147
void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self,
148-
const mcu_pin_obj_t* pin, uint16_t maxlen, bool idle_state) {
148+
const mcu_pin_obj_t* pin, uint16_t maxlen, uint32_t frequency, bool idle_state) {
149149
if (!pin->has_extint) {
150150
mp_raise_RuntimeError(translate("No hardware support on pin"));
151151
}
@@ -324,6 +324,10 @@ uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t* self) {
324324
return self->maxlen;
325325
}
326326

327+
uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t* self) {
328+
return 1000000;
329+
}
330+
327331
uint16_t common_hal_pulseio_pulsein_get_len(pulseio_pulsein_obj_t* self) {
328332
return self->len;
329333
}

ports/cxd56/common-hal/pulseio/PulseIn.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ static int pulsein_interrupt_handler(int irq, FAR void *context, FAR void *arg)
8484
}
8585

8686
void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self,
87-
const mcu_pin_obj_t *pin, uint16_t maxlen, bool idle_state) {
87+
const mcu_pin_obj_t *pin, uint16_t maxlen, uint32_t frequency, bool idle_state) {
8888
self->buffer = (uint16_t *)m_malloc(maxlen * sizeof(uint16_t), false);
8989
if (self->buffer == NULL) {
9090
mp_raise_msg_varg(&mp_type_MemoryError, translate("Failed to allocate RX buffer of %d bytes"), maxlen * sizeof(uint16_t));
@@ -175,6 +175,10 @@ uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t *self) {
175175
return self->maxlen;
176176
}
177177

178+
uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t* self) {
179+
return 1000000;
180+
}
181+
178182
bool common_hal_pulseio_pulsein_get_paused(pulseio_pulsein_obj_t *self) {
179183
return self->paused;
180184
}

ports/esp32s2/common-hal/pulseio/PulseIn.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void pulsein_reset(void) {
8484
}
8585

8686
void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self, const mcu_pin_obj_t *pin,
87-
uint16_t maxlen, bool idle_state) {
87+
uint16_t maxlen, uint32_t frequency, bool idle_state) {
8888
self->buffer = (uint16_t *)m_malloc(maxlen * sizeof(uint16_t), false);
8989
if (self->buffer == NULL) {
9090
mp_raise_msg_varg(&mp_type_MemoryError, translate("Failed to allocate RX buffer of %d bytes"), maxlen * sizeof(uint16_t));
@@ -203,6 +203,10 @@ uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t *self) {
203203
return self->maxlen;
204204
}
205205

206+
uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t* self) {
207+
return 1000000;
208+
}
209+
206210
bool common_hal_pulseio_pulsein_get_paused(pulseio_pulsein_obj_t *self) {
207211
return self->paused;
208212
}

ports/mimxrt10xx/common-hal/pulseio/PulseIn.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102
// }
103103

104104
void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self,
105-
const mcu_pin_obj_t *pin, uint16_t maxlen, bool idle_state) {
105+
const mcu_pin_obj_t *pin, uint16_t maxlen, uint32_t frequency, bool idle_state) {
106106
// if (!pin->has_extint) {
107107
// mp_raise_RuntimeError(translate("No hardware support on pin"));
108108
// }
@@ -218,6 +218,10 @@ uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t *self) {
218218
return 0;
219219
}
220220

221+
uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t* self) {
222+
return 1000000;
223+
}
224+
221225
uint16_t common_hal_pulseio_pulsein_get_len(pulseio_pulsein_obj_t *self) {
222226
// return self->len;
223227
return 0;

ports/nrf/common-hal/pulseio/PulseIn.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ void pulsein_reset(void) {
125125
memset(_objs, 0, sizeof(_objs));
126126
}
127127

128-
void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self, const mcu_pin_obj_t *pin, uint16_t maxlen, bool idle_state) {
128+
void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self, const mcu_pin_obj_t *pin, uint16_t maxlen, uint32_t frequency, bool idle_state) {
129129
int idx = _find_pulsein_obj(NULL);
130130
if (idx < 0) {
131131
mp_raise_NotImplementedError(NULL);
@@ -308,6 +308,10 @@ uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t *self) {
308308
return self->maxlen;
309309
}
310310

311+
uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t* self) {
312+
return 1000000;
313+
}
314+
311315
bool common_hal_pulseio_pulsein_get_paused(pulseio_pulsein_obj_t *self) {
312316
return self->paused;
313317
}

ports/raspberrypi/common-hal/pulseio/PulseIn.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ uint16_t pulsein_program[] = {
4949
};
5050

5151
void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self,
52-
const mcu_pin_obj_t *pin, uint16_t maxlen, bool idle_state) {
52+
const mcu_pin_obj_t *pin, uint32_t frequency, uint16_t maxlen, bool idle_state) {
5353

5454
self->buffer = (uint16_t *)m_malloc(maxlen * sizeof(uint16_t), false);
5555
if (self->buffer == NULL) {
@@ -67,7 +67,7 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self,
6767

6868
bool ok = rp2pio_statemachine_construct(&state_machine,
6969
pulsein_program, sizeof(pulsein_program) / sizeof(pulsein_program[0]),
70-
1000000,
70+
frequency,
7171
NULL, 0,
7272
NULL, 0,
7373
pin, 1,
@@ -206,6 +206,10 @@ uint16_t common_hal_pulseio_pulsein_get_len(pulseio_pulsein_obj_t *self) {
206206
return self->len;
207207
}
208208

209+
uint16_t common_hal_pulseio_pulsein_get_frequency(pulseio_pulsein_obj_t *self) {
210+
return common_hal_rp2pio_statemachine_get_frequency(&self->state_machine);
211+
}
212+
209213
bool common_hal_pulseio_pulsein_get_paused(pulseio_pulsein_obj_t *self) {
210214
return true;
211215
}

ports/stm/common-hal/pulseio/PulseIn.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ void pulsein_reset(void) {
109109
}
110110

111111
void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self, const mcu_pin_obj_t *pin,
112-
uint16_t maxlen, bool idle_state) {
112+
uint16_t maxlen, uint32_t frequency, bool idle_state) {
113113
// STM32 has one shared EXTI for each pin number, 0-15
114114
uint8_t p_num = pin->number;
115115
if (_objs[p_num]) {
@@ -272,6 +272,10 @@ uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t *self) {
272272
return self->maxlen;
273273
}
274274

275+
uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t* self) {
276+
return 1000000;
277+
}
278+
275279
bool common_hal_pulseio_pulsein_get_paused(pulseio_pulsein_obj_t *self) {
276280
return self->paused;
277281
}

shared-bindings/pulseio/PulseIn.c

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
//| and low cost temperature sensors (DHT). The pulsed signal consists of timed active and
4141
//| idle periods. Unlike PWM, there is no set duration for active and idle pairs."""
4242
//|
43-
//| def __init__(self, pin: microcontroller.Pin, maxlen: int = 2, *, idle_state: bool = False) -> None:
43+
//| def __init__(self, pin: microcontroller.Pin, maxlen: int = 2, *, idle_state: bool = False, frequency : int = 1_000_000) -> None:
4444
//| """Create a PulseIn object associated with the given pin. The object acts as
4545
//| a read-only sequence of pulse lengths with a given max length. When it is
4646
//| active, new pulse lengths are added to the end of the list. When there is
@@ -49,6 +49,10 @@
4949
//|
5050
//| :param ~microcontroller.Pin pin: Pin to read pulses from.
5151
//| :param int maxlen: Maximum number of pulse durations to store at once
52+
//| :param int frequency: Pulses are measured in number of
53+
//| cycles. The default makes 1 = 1µs. This value may be
54+
//| rounded, the actual value can be retrieved via the
55+
//| `PulseIn.frequency` property.
5256
//| :param bool idle_state: Idle state of the pin. At start and after `resume`
5357
//| the first recorded pulse will the opposite state from idle.
5458
//|
@@ -77,10 +81,11 @@
7781
//| ...
7882
//|
7983
STATIC mp_obj_t pulseio_pulsein_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
80-
enum { ARG_pin, ARG_maxlen, ARG_idle_state };
84+
enum { ARG_pin, ARG_maxlen, ARG_frequency, ARG_idle_state };
8185
static const mp_arg_t allowed_args[] = {
8286
{ MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_OBJ },
8387
{ MP_QSTR_maxlen, MP_ARG_INT, {.u_int = 2} },
88+
{ MP_QSTR_frequency, MP_ARG_INT, {.u_int = 1000000} },
8489
{ MP_QSTR_idle_state, MP_ARG_BOOL, {.u_bool = false} },
8590
};
8691
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
@@ -90,7 +95,7 @@ STATIC mp_obj_t pulseio_pulsein_make_new(const mp_obj_type_t *type, size_t n_arg
9095
pulseio_pulsein_obj_t *self = m_new_obj(pulseio_pulsein_obj_t);
9196
self->base.type = &pulseio_pulsein_type;
9297

93-
common_hal_pulseio_pulsein_construct(self, pin, args[ARG_maxlen].u_int,
98+
common_hal_pulseio_pulsein_construct(self, pin, args[ARG_maxlen].u_int, args[ARG_frequency].u_int,
9499
args[ARG_idle_state].u_bool);
95100

96101
return MP_OBJ_FROM_PTR(self);
@@ -196,6 +201,25 @@ STATIC mp_obj_t pulseio_pulsein_obj_popleft(mp_obj_t self_in) {
196201
}
197202
MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pulsein_popleft_obj, pulseio_pulsein_obj_popleft);
198203

204+
//| frequency: int
205+
//| """The frequency of the counter in Hz. 1_000_000 indicates that
206+
//| pulse lengths are in units of microseconds."""
207+
//|
208+
STATIC mp_obj_t pulseio_pulsein_obj_get_frequency(mp_obj_t self_in) {
209+
pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in);
210+
check_for_deinit(self);
211+
212+
return MP_OBJ_NEW_SMALL_INT(common_hal_pulseio_pulsein_get_frequency(self));
213+
}
214+
MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pulsein_get_frequency_obj, pulseio_pulsein_obj_get_frequency);
215+
216+
const mp_obj_property_t pulseio_pulsein_frequency_obj = {
217+
.base.type = &mp_type_property,
218+
.proxy = {(mp_obj_t)&pulseio_pulsein_get_frequency_obj,
219+
(mp_obj_t)&mp_const_none_obj,
220+
(mp_obj_t)&mp_const_none_obj},
221+
};
222+
199223
//| maxlen: int
200224
//| """The maximum length of the PulseIn. When len() is equal to maxlen,
201225
//| it is unclear which pulses are active and which are idle."""
@@ -302,6 +326,7 @@ STATIC const mp_rom_map_elem_t pulseio_pulsein_locals_dict_table[] = {
302326
{ MP_ROM_QSTR(MP_QSTR_popleft), MP_ROM_PTR(&pulseio_pulsein_popleft_obj) },
303327

304328
// Properties
329+
{ MP_ROM_QSTR(MP_QSTR_frequency), MP_ROM_PTR(&pulseio_pulsein_frequency_obj) },
305330
{ MP_ROM_QSTR(MP_QSTR_maxlen), MP_ROM_PTR(&pulseio_pulsein_maxlen_obj) },
306331
{ MP_ROM_QSTR(MP_QSTR_paused), MP_ROM_PTR(&pulseio_pulsein_paused_obj) },
307332
};

shared-bindings/pulseio/PulseIn.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@
3333
extern const mp_obj_type_t pulseio_pulsein_type;
3434

3535
extern void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self,
36-
const mcu_pin_obj_t *pin, uint16_t maxlen, bool idle_state);
36+
const mcu_pin_obj_t *pin, uint32_t frequency, uint16_t maxlen, bool idle_state);
3737
extern void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t *self);
3838
extern bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t *self);
3939
extern void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t *self);
4040
extern void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t *self, uint16_t trigger_duration);
4141
extern void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t *self);
4242
extern uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t *self);
43+
extern uint16_t common_hal_pulseio_pulsein_get_frequency(pulseio_pulsein_obj_t *self);
4344
extern uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t *self);
4445
extern bool common_hal_pulseio_pulsein_get_paused(pulseio_pulsein_obj_t *self);
4546
extern uint16_t common_hal_pulseio_pulsein_get_len(pulseio_pulsein_obj_t *self);

0 commit comments

Comments
 (0)