It consistently happens after a fixed period of time (about 6 minutes) which makes me think it is a memory problem.
I see no exceptions and my two threads keep running normally.
I have one thread that calculates speed etc and one thread that updates the display.
Both work as expected even after the interrupts stop coming in.
I don't know how to debug this. I have been looking at gc.mem_free() and that looks fine.
I have no experience with interrupts so I might be doing something obviously wrong so any advise is welcome
Code: Select all
#mySensor
import machine
import utime
import uarray
import micropython
micropython.alloc_emergency_exception_buf(100)
class MySensor:
sensorGPIOPin = 6
bounce = 2000
myRB = uarray.array('i',[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])
rb_len = 0
w_cursor = 0
r_cursor = 0
sensor = None
oldTime = 0
tus = utime.ticks_us
tdif = utime.ticks_diff
def init_GPIO(self): # initialize GPIO
self.sensor = machine.Pin(self.sensorGPIOPin, machine.Pin.IN, machine.Pin.PULL_UP)
def init_interrupt(self):
self.oldTime = self.tus()
self.sensor.irq(trigger=machine.Pin.IRQ_FALLING, handler=self.mark_time)
def mark_time(self,channel): # callback function
newTime = self.tus()
dT = self.tdif(newTime,self.oldTime)
if dT > self.bounce:
self.write(dT)
self.oldTime = newTime
def write(self,time):
self.myRB[self.w_cursor] = time
self.w_cursor+=1
if self.w_cursor >= self.rb_len:
self.w_cursor = 0
def read(self):
if self.r_cursor != self.w_cursor:
val = self.myRB[self.r_cursor]
self.r_cursor += 1
if self.r_cursor >= self.rb_len:
self.r_cursor = 0
else:
val = False
return val
def __init__(self, pinNr):
self.rb_len = len(self.myRB)
self.sensorGPIOPin = pinNr
self.init_GPIO()
self.init_interrupt()