how to write UART interrupt callback code?

General discussion about topics related to OpenMV.
laiyingjie
Posts: 10
Joined: Thu Nov 16, 2017 4:57 pm

how to write UART interrupt callback code?

Postby laiyingjie » Thu Nov 16, 2017 5:15 pm

class ExtInt – configure I/O pins to interrupt on external events
"There are a total of 22 interrupt lines. 16 of these can come from GPIO pins and the remaining 6 are from internal sources."

my code :

Code: Select all

# Hello World Example
#
# Welcome to the OpenMV IDE! Click on the green run arrow button below to run the script!

import sensor, image, time
#import time
from pyb import Pin, ExtInt
from pyb import UART
from pyb import LED
red_led   = LED(1)
green_led = LED(2)
blue_led  = LED(3)
ir_led    = LED(4)
sensor.reset()                      # Reset and initialize the sensor.
sensor.set_pixformat(sensor.GRAYSCALE) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.VGA)   # Set frame size to QVGA (320x240)
sensor.skip_frames(time = 2000)     # Wait for settings take effect.
clock = time.clock()                # Create a clock object to track the FPS.
uart = UART(3, 19200)
buf=66
data = bytearray(10)  # create a buffer
def print_X1(line):
    red_led.toggle()#print("X1 is pressed")
    print("X1 is pressed")
ext = ExtInt(Pin('P5'), ExtInt.IRQ_FALLING, Pin.PULL_NONE, print_X1)
ext.enable()
while(True):
    clock.tick()                    # Update the FPS clock.
    img = sensor.snapshot()         # Take a picture and return the image.
    img.draw_rectangle((100,100,100,100),128)#设置坐标(10,10)的像素点为红色(255,0,0)

    print(clock.fps())              # Note: OpenMV Cam runs about half as fast when connected
                                    # to the IDE. The FPS should increase once disconnected.
but when I send "a",I saw 3 lines print("X1 is pressed") on IDE Serial Terminal.
So I think my "ExtInt" codes do not work as UART interrupt.
User avatar
kwagyeman
Posts: 1246
Joined: Sun May 24, 2015 2:10 pm

Re: how to write UART interrupt callback code?

Postby kwagyeman » Thu Nov 16, 2017 11:58 pm

Hi, to print to the UART you need to do:

Code: Select all

def print_X1(line):
    red_led.toggle()#print("X1 is pressed")
    uart.write("X1 is pressed")
That said... the UART blocks when it's full of bytes and you're going to crash the system with that code.

Also, make sure to initialize the uart like so:

Code: Select all

uart = UART(3, 19200, timeout_char = 1000)
Anyway, if you want to send data on an interrupt do that in the main loop and only set a flag to go high in the interrupt. In particular, code like this works great for passing state:

In the interrupt handler:

Code: Select all

if not flag: flag = True
And in the main loop:

Code: Select all

if flag:
    flag = False
    # do something
Nyamekye,
laiyingjie
Posts: 10
Joined: Thu Nov 16, 2017 4:57 pm

Re: how to write UART interrupt callback code?

Postby laiyingjie » Fri Nov 17, 2017 7:24 am

Code: Select all

import sensor, image, time
#import time
from pyb import Pin, ExtInt
from pyb import UART
from pyb import LED
red_led   = LED(1)
uart = UART(3, 19200)
def flag_True(line):
    red_led.toggle()
ext = ExtInt(Pin('P5'), ExtInt.IRQ_FALLING, Pin.PULL_NONE,flag_True)
ext.enable()
while(True):
    pass
These codes works.but when my codes like this:

Code: Select all

import sensor, image, time
#import time
from pyb import Pin, ExtInt
from pyb import UART
from pyb import LED
red_led   = LED(1)
uart = UART(3, 19200, timeout_char = 1000)
flag = False
def flag_True(line):
    if not flag: flag = True
ext = ExtInt(Pin('P5'), ExtInt.IRQ_FALLING, Pin.PULL_NONE,flag_True)
ext.enable()
while(True):
    if flag:
        flag = False
        red_led.toggle()# do something
IDE Serial Terminal display:
Uncaught exception in ExtInt interrupt handler line 11
NameError:

I want to know why.
User avatar
kwagyeman
Posts: 1246
Joined: Sun May 24, 2015 2:10 pm

Re: how to write UART interrupt callback code?

Postby kwagyeman » Fri Nov 17, 2017 11:28 am

Hi, python requires you to make a variable as global if you are changing a variable outside of function.

So do:

Global flag

As the first line of the interrupt call back method.
Nyamekye,
laiyingjie
Posts: 10
Joined: Thu Nov 16, 2017 4:57 pm

Re: how to write UART interrupt callback code?

Postby laiyingjie » Fri Nov 17, 2017 8:06 pm

Thanks.
I add global_list.py as:

Code: Select all

flag = False
counter=1
and main.py as:

Code: Select all

import global_list
import sensor, image, time
from pyb import Pin, ExtInt
from pyb import UART
from pyb import LED
red_led   = LED(1)
uart = UART(3, 19200, timeout_char = 1000)
def flag_True(line):
	if not global_list.flag: global_list.flag = True
	global_list.counter=global_list.counter+1
ext = ExtInt(Pin('P5'), ExtInt.IRQ_FALLING, Pin.PULL_NONE,flag_True)
ext.enable()
while(True):
	if global_list.flag:
		global_list.flag = False
		red_led.toggle()
		print("counter=",global_list.counter)
User avatar
kwagyeman
Posts: 1246
Joined: Sun May 24, 2015 2:10 pm

Re: how to write UART interrupt callback code?

Postby kwagyeman » Fri Nov 17, 2017 9:52 pm

Not quite sure what you are doing.
Nyamekye,
laiyingjie
Posts: 10
Joined: Thu Nov 16, 2017 4:57 pm

Re: how to write UART interrupt callback code?

Postby laiyingjie » Sun Nov 19, 2017 10:05 am

And then I want to konw how to receive a frame like"AA 56 FF FC FF"(AA:frame head, FF FC FF:frame end).
……I need help again.
User avatar
iabdalkader
Posts: 380
Joined: Sun May 24, 2015 3:53 pm

Re: how to write UART interrupt callback code?

Postby iabdalkader » Sun Nov 19, 2017 10:51 am

User avatar
kwagyeman
Posts: 1246
Joined: Sun May 24, 2015 2:10 pm

Re: how to write UART interrupt callback code?

Postby kwagyeman » Sun Nov 19, 2017 12:01 pm

Nyamekye,

Return to “General Topics”

Who is online

Users browsing this forum: Google [Bot] and 1 guest