Skip to content

Commit f99deed

Browse files
authored
Merge pull request adafruit#4668 from DavePutz/issue_4659
Increased possible pulsein length to ~65 ms.
2 parents 9e183bb + 91739de commit f99deed

File tree

1 file changed

+14
-7
lines changed
  • ports/raspberrypi/common-hal/pulseio

1 file changed

+14
-7
lines changed

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@
3939
pulseio_pulsein_obj_t *save_self;
4040

4141
#define NO_PIN 0xff
42+
#define MAX_PULSE 65535
43+
#define MIN_PULSE 10
4244
volatile bool last_level;
43-
volatile uint16_t level_count = 0;
44-
volatile uint16_t result = 0;
45+
volatile uint32_t level_count = 0;
46+
volatile uint32_t result = 0;
4547
volatile uint16_t buf_index = 0;
4648

4749
uint16_t pulsein_program[] = {
@@ -133,16 +135,21 @@ void common_hal_pulseio_pulsein_interrupt() {
133135
result = level_count;
134136
last_level = level;
135137
level_count = 1;
136-
// ignore pulses that are too long and too short
137-
if (result < 4000 && result > 10) {
138-
self->buffer[buf_index] = result;
138+
// Pulses that are londger than MAX_PULSE will return MAX_PULSE
139+
if (result > MAX_PULSE ) {
140+
result = MAX_PULSE;
141+
}
142+
// ignore pulses that are too short
143+
if (result <= MAX_PULSE && result > MIN_PULSE) {
144+
self->buffer[buf_index] = (uint16_t) result;
139145
buf_index++;
140146
self->len++;
141147
}
142148
}
143149
}
144-
// check for a pulse thats too long (4000 us) or maxlen reached, and reset
145-
if ((level_count > 4000) || (buf_index >= self->maxlen)) {
150+
151+
// check for a pulse thats too long (MAX_PULSE us) or maxlen reached, and reset
152+
if ((level_count > MAX_PULSE) || (buf_index >= self->maxlen)) {
146153
pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, false);
147154
pio_sm_init(self->state_machine.pio, self->state_machine.state_machine, self->state_machine.offset, &self->state_machine.sm_config);
148155
pio_sm_restart(self->state_machine.pio,self->state_machine.state_machine);

0 commit comments

Comments
 (0)