I have been trying to get this code to work
import pyb
import utime
import micropython
micropython.alloc_emergency_exception_buf(500)
# Global constants
buttonPressedTime = 200 # milliseconds
# Ring buffer implementation
ringTail = 0
ringHead = 0
ringSize = 20
ringFull = False
msRingBuffer = None
trRingBuffer = None
# Global variables
buttonPin = None
extInt = None
# button pressed flag
buttonPressed = 0
greenLedOn = 0
# button transition ISR
def buttonTransitionISR():
global buttonPin
global trRingBuffer
global msRingBuffer
global ringHead
global ringFull
if ringFull == False:
trRingBuffer[ringHead] = buttonPin.value()
msRingBuffer[ringHead] = utime.ticks_ms()
localRingHead = (ringHead + 1) % ringSize
if localRingHead == ringTail:
ringFull = True
else:
ringHead = localRingHead
# else: do nothing
def initialiseInterrupts():
global buttonPin
global buttonClock
global extInt
global trRingBuffer
global msRingBuffer
global buttonPressed
# Initialise ring buffer
trRingBuffer = bytearray(ringSize)
msRingBuffer = bytearray()
# Have to do this as we do not know what utime.ticks_ms() returns
for index in range(0,ringSize):
msRingBuffer.append(utime.ticks_ms())
# experimental button pin
buttonPin = pyb.Pin('P0', pyb.Pin.IN)
# set up ISR for button press
extInt = pyb.ExtInt(buttonPin, pyb.ExtInt.IRQ_RISING_FALLING, pyb.Pin.PULL_UP, buttonTransitionISR)
# check for button pressed condition
def buttonPressedCheck():
pass
# Initialise interrupt handlers
initialiseInterrupts()
# use these LEDs for experiments
display_led = pyb.LED(1)
green_led = pyb.LED(2)
blue_led = pyb.LED(3)
red_led = pyb.LED(4)
# start with LED off
green_led.off()
while(True):
buttonPressedCheck()
if buttonPressed == 1:
if greenLedOn == 0:
print("green led ON")
greenLedOn = 1
green_led.on()
else:
greenLedOn = 0
print("green led OFF")
green_led.off()
But when I run it on an M7 with firmware V3.1.0 and ground P0 I get the error
Uncaught exception in ExtInt interrupt handler line 15
TypeError: function takes 0 positional arguments but 1 were given
I was expecting it to run with multiple P0 grounding’s and eventually get a ringFull = True.
What am I doing wrong?