File tree Expand file tree Collapse file tree 2 files changed +10
-0
lines changed
ports/raspberrypi/common-hal/pulseio Expand file tree Collapse file tree 2 files changed +10
-0
lines changed Original file line number Diff line number Diff line change @@ -56,6 +56,7 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self,
56
56
self -> idle_state = idle_state ;
57
57
self -> start = 0 ;
58
58
self -> len = 0 ;
59
+ self -> len_target = 0 ;
59
60
60
61
common_hal_rp2pio_statemachine_construct (& self -> state_machine ,
61
62
pulsein_program , MP_ARRAY_SIZE (pulsein_program ),
@@ -133,6 +134,8 @@ void common_hal_pulseio_pulsein_interrupt(void *self_in) {
133
134
self -> buffer [buf_index ] = (uint16_t )result ;
134
135
if (self -> len < self -> maxlen ) {
135
136
self -> len ++ ;
137
+ self -> len_target ++ ; // The interrupt will only cause a problem in either len or len_target, not both.
138
+ // So we can just check for a match, and choose the higher.
136
139
} else {
137
140
self -> start = (self -> start + 1 ) % self -> maxlen ;
138
141
}
@@ -174,7 +177,13 @@ uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t *self) {
174
177
}
175
178
uint16_t value = self -> buffer [self -> start ];
176
179
self -> start = (self -> start + 1 ) % self -> maxlen ;
180
+ self -> len_target -- ;
177
181
self -> len -- ;
182
+ if (self -> len != self -> len_target ) {
183
+ uint16_t len_accurate = self -> len > self -> len_target ? self -> len : self -> len_target ;
184
+ self -> len_target = len_accurate ;
185
+ self -> len = len_accurate ;
186
+ }
178
187
return value ;
179
188
}
180
189
Original file line number Diff line number Diff line change @@ -43,6 +43,7 @@ typedef struct {
43
43
volatile bool last_level ;
44
44
volatile uint32_t level_count ;
45
45
volatile uint16_t len ;
46
+ volatile uint16_t len_target ;
46
47
volatile uint16_t start ;
47
48
rp2pio_statemachine_obj_t state_machine ;
48
49
} pulseio_pulsein_obj_t ;
You can’t perform that action at this time.
0 commit comments