Pages

Sunday, June 16, 2013

Pygame event handler problem solved!!!

I studied carefully how the demo program chim.py handles event but could not understand why my version does not work.  I thought hard and guessed perhaps the Escape key from the PuTTY keyboard does not get through to the RPi.  So I hot plugged the Logitech wireless keyboard/mouse dongle, used command lsusb to make sure the wirelss keyboard is recognized, and then typed Escape at the wireless keyboard, not the PuTTY keyboard.  

This time it works!!!

The lesson learned is that though Ctrl-C from PuTTY keyboard works, I should not take for granted that every other key also works.

There was an error message about ALSA lib pcm-dmix.c.  I guess this is the sound mixer thing which is not available in RPi.  I did not notice this error in the Win 7 test.  I thought RPi does not have this sound utility.

pi@raspberrypi ~/fongtoy $ lsusb
Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.
Bus 001 Device 004: ID 0586:341f ZyXEL Communications Corp. NWD2205 802.11n Wireless N Adapter [Realtek RTL8192CU]
Bus 001 Device 005: ID 1a40:0101 Terminus Technology Inc. 4-Port HUB
Bus 001 Device 006: ID 046d:0825 Logitech, Inc. Webcam C270
Bus 001 Device 007: ID 046d:082d Logitech, Inc. [Logitech Webcam C920]
Bus 001 Device 008: ID 046d:c52b Logitech, Inc. Unifying Receiver [Logitech wireless kb/mse]

pi@raspberrypi ~/fongtoy $ sudo python fongtoy.py

*** Start Program - FongToy v1.11 tlfong01 2013jun15 ***

*** Sample run begin - TestWebCamCamera06() 2013jun16hkt21:18 ***

ALSA lib pcm_dmix.c:1018:(snd_pcm_dmix_open) unable to open slave
Number of web cameras found =  2
Enter camera number now: 1

*** Stop Program *** [exit cleanly if escape key pressed at the RPi wireless keyboard]

pi@raspberrypi ~/fongtoy $ date
Sun Jun 16 13:34:18 UTC 2013

def TestWebcamCamera06():

    ftprint.PrintDoubleSpaceLine("*** Sample run begin - TestWebCamCamera06() 2013jun16hkt21:18 ***")    
       
    Size640x480 = (640, 480)

    pygame.init()
    pygame.camera.init()

    webCamList = pygame.camera.list_cameras()
    webCamCount = len(webCamList)
    print "Number of web cameras found = ", webCamCount

    webCamNumber = raw_input("Enter camera number now: ")
    webCamCamera = pygame.camera.Camera(webCamList[int(webCamNumber)], Size640x480)
    webCamCamera.start()

    pygame.display.init()
    webCamDisplay = pygame.display.set_mode(Size640x480, 0)

    webCamSurface = pygame.surface.Surface(Size640x480, 0, webCamDisplay)   

    while True:
        for event in pygame.event.get(): # Escape key from local keyboard, NOT 
                                           PuTTY!!!
   if (event.type == QUIT or (event.type == KEYDOWN and event.key == 
                                       K_ESCAPE)):                                  
                webCamCamera.stop()
                pygame.quit()
return
        
if webCamCamera.query_image(): 
            webCamSurface = webCamCamera.get_image(webCamSurface)
            webCamDisplay.blit(webCamSurface, (0,0))
            pygame.display.flip()

    ftprint.PrintDoubleSpaceLine("*** Sample run end ***")

.END

No comments:

Post a Comment