kelderkold
Posts: 20
Joined: Sun Aug 19, 2018 7:52 am
Location: Danmark
Contact: Website

reading GPIO and Keyboard

Fri Aug 24, 2018 9:11 am

Hi I am new to programming, or I did some in the school in 1980 (Basis langue), but has not done anything since then.

I recently found the Rpi bought one, and started a project making a lap counter to my slotrace track.
And that works really fine, I using phototransistors and inputs from GPIO.

But I cannot find out how to get the mouse/touchscreen or tha keyboard to work at the same time, on my pc where i disabled the GPIO the keybord/mouse work fine.

I think the for event in pygame.event.get() and the GPIO.add_event_detect is the problem.
if I remove the for event in pygame.event.get() the program works fine just counting.

explaining the code: I make detections on the GPIO and that detection show the laptime, the fastest lap time and the number of laps.
I want to be able to touch the screen (480x320) within one of the 6 fields to reset the field, this word only on my PC without GPIO enabled, but when the for event in pygame.event.get(): is added the program crash.

here a piece of my code:

Code: Select all

#Main loop 
font = pygame.font.SysFont('none', 100)        
run = True
while run:

    GPIO.add_event_detect(38, GPIO.RISING, callback=Lane1, bouncetime=30)

    GPIO.add_event_detect(40, GPIO.RISING, callback=Lane2, bouncetime=30)
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

        elif event.type == pygame.KEYDOWN:
             if event.key == pygame.K_1:
                Lane1()
             if event.key == pygame.K_2:
                Lane2()
             if event.key == pygame.K_ESCAPE:
                pygame.quit()
                
        elif event.type == pygame.MOUSEBUTTONDOWN:
            mx, my = pygame.mouse.get_pos()
            print (mx, my)
            if mx >10 and mx <233 and my >33 and my <100:
                L1_OT = 66
            if mx >245 and mx <463 and my >33 and my <100:
                L2_OT = 77
            if mx >10 and mx <233 and my >133 and my <200:
                L1_HOT = 88
            if mx >245 and mx <463 and my >133 and my <200:
                L2_HOT = 99
            if mx >10 and mx <233 and my >233 and my <300:
                L1_KO = 0
            if mx >245 and mx <463 and my >233 and my <300:
                L2_KO = 0

            

    redrawGameWindow()
        

GPIO.cleanup()
print("Done")
pygame.quit()
racefuncounter.jpg
racefuncounter.jpg (136.27 KiB) Viewed 357 times

DirkS
Posts: 10371
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: reading GPIO and Keyboard

Fri Aug 24, 2018 9:32 am

You could catch the GPIO event and create a custom pygame event. This can then be handled in the normal pygame event loop.
I can't find a good example ATM, but maybe you have more success. Search for e.g. 'pygame.gpio'

Return to “Python”