Skip to content

Commit 5bd1da2

Browse files
committed
Increased possible pulsein length to 32 ms.
1 parent d5768c5 commit 5bd1da2

File tree

1 file changed

+6
-3
lines changed
  • ports/raspberrypi/common-hal/pulseio

1 file changed

+6
-3
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
pulseio_pulsein_obj_t *save_self;
4040

4141
#define NO_PIN 0xff
42+
#define MAX_PULSE 32678
43+
#define MIN_PULSE 10
4244
volatile bool last_level;
4345
volatile uint16_t level_count = 0;
4446
volatile uint16_t result = 0;
@@ -139,15 +141,15 @@ void common_hal_pulseio_pulsein_interrupt() {
139141
last_level = level;
140142
level_count = 1;
141143
// ignore pulses that are too long and too short
142-
if (result < 4000 && result > 10) {
144+
if (result < MAX_PULSE && result > MIN_PULSE) {
143145
self->buffer[buf_index] = result;
144146
buf_index++;
145147
self->len++;
146148
}
147149
}
148150
}
149-
// check for a pulse thats too long (4000 us) or maxlen reached, and reset
150-
if ((level_count > 4000) || (buf_index >= self->maxlen)) {
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)) {
151153
pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, false);
152154
pio_sm_init(self->state_machine.pio, self->state_machine.state_machine, self->state_machine.offset, &self->state_machine.sm_config);
153155
pio_sm_restart(self->state_machine.pio,self->state_machine.state_machine);
@@ -180,6 +182,7 @@ void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t *self,
180182
void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t *self) {
181183
self->start = 0;
182184
self->len = 0;
185+
buf_index = 0;
183186
}
184187

185188
uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t *self) {

0 commit comments

Comments
 (0)