tonygo2
Posts: 86
Joined: Sun Aug 28, 2011 2:50 pm
Location: Leicester, UK

Pygame KEYDOWN event crashes

Fri Mar 30, 2018 11:57 am

My pygame script keeps crashing without an error message after about 20 key presses. I cannot work out what is wrong and some ideas would be useful.

This project uses UP, DOWN, LEFT and RIGHT to set LED RGB vales for a Blinkt strip and displays graphically the values on a tiny SSD1306 I2C display. RETURN is used to halt the execution and shut down the displays.

The display routines work perfectly, when tested on their own, so I feel that the problem is in the main loop with the pygame keyboard events. I have restricted the events to just KEYDOWN but this appears to have made no difference.

Please help if you are better at pygame than I am.

OUTPUT

>>>
=== RESTART: /home/pi/Adafruit_Python_SSD1306/examples/GraphKeysBlinkt4.py ===
Started
1 50 40 50
1 50 30 50
1 50 20 50
1 50 10 50
1 50 0 50
0 50 0 50
0 40 0 50
0 30 0 50
0 20 0 50
0 10 0 50
0 0 0 50
1 0 0 50
2 0 0 50
2 0 0 40
2 0 0 30
2 0 0 20
2 0 0 10
2 0 0 0
1 0 0 0
0 0 0 0
0 10 0 0
0 20 0 0
0 30 0 0
0 40 0 0
0 50 0 0
0 60 0 0
0 70 0 0
0 80 0 0
0 90 0 0
0 100 0 0
1 100 0 0
1 100 10 0
1 100 20 0
1 100 30 0

=============================== RESTART: Shell ===============================
>>>
=== RESTART: /home/pi/Adafruit_Python_SSD1306/examples/GraphKeysBlinkt4.py ===
Started
1 50 40 50
0 50 40 50
0 40 40 50
0 40 40 50
** BYE from RETURN **
Finished
>>>
=============================== RESTART: Shell ===============================
>>>

CODE

Code: Select all

#!python3
# Uses events = Falls over???????????????????
# --------------Set up BLINKT --------------------
from blinkt import set_pixel, set_brightness, show, clear
# --------------Set up SSD1306 -------------------
import Adafruit_SSD1306
from time import sleep
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
RST = 24
# 128x64 display with hardware I2C:
disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST)
# Initialize library.
disp.begin()
# Clear display.
disp.clear()
disp.display()
# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
width = disp.width
height = disp.height
image = Image.new('1', (width, height))
# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)
# Draw a black filled box to clear the image.
draw.rectangle((0,0,width,height), outline=0, fill=0)
# Load default font.
font = ImageFont.load_default()
# --------- Set up pygame ----------------
import pygame
from pygame.locals import *
pygame.init()
pygame.display.set_mode((90,5)) #Big enough

pygame.event.set_allowed(None)            #Testing
pygame.event.set_allowed(pygame.KEYDOWN)  #Testing

global vals
global old_vals
global p
global cols

def showgraph():   #Display on SSD 1306 (Works fine)
    draw.rectangle((0,0,width,height), outline=0, fill=0)
    disp.image(image)
    disp.display()
    draw.text((0, 10), "R", font=font, fill=255)
    draw.text((0, 25), "G", font=font, fill=255)
    draw.text((0, 40), "B", font=font, fill=255)
    old_no = "   "
    draw.rectangle((10, 10, vals[0]+10, 20), outline=0, fill=255)
    draw.rectangle((10, 25, vals[1]+10, 35), outline=0, fill=255)
    draw.rectangle((10, 40, vals[2]+10, 50), outline=0, fill=255)
    draw.line((10, 63, 10, 1), fill=255)
    draw.text((111, 10), str(vals[0]), font=font, fill=255)
    draw.text((111, 25), str(vals[1]), font=font, fill=255)
    draw.text((111, 40), str(vals[2]), font=font, fill=255)
    draw.text((30, 52), cols[p], font=font, fill=255)
    disp.image(image)
    disp.display()
    
def showBlinkt(): #Display colours on Blankt
    set_pixel(7,cvals[0],cvals[1],cvals[2])
    set_pixel(1,cvals[0],0,0)
    set_pixel(2,0,cvals[1],0)
    set_pixel(3,0,0,cvals[2])
    show()

vals =[50,50,50]      #RGB values as %
cvals = [128,128,128] #RGB values 0 - 250
old_vals = [50,50,50]
cols = [" RED","GREEN"," BLUE"]
p = 1 #list pointer
showBlinkt()

show()      # Update Blinkt
showgraph() # Update SSD1306
print("Started")

running = True
while running:
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            old_p = p
            old_vals[p] = vals[p]
            if event.key == pygame.K_UP:
                vals[p] = vals[p] + 10
                if vals[p] > 100:
                    vals[p] = 100
            elif event.key == pygame.K_DOWN:
                vals[p]= vals[p]- 10
                if vals[p] < 0:
                    vals[p] = 0
            elif event.key == pygame.K_RIGHT and p < 2:
                p = p + 1
            elif event.key == pygame.K_LEFT and p > 0:
                p = p - 1
            elif event.key == pygame.K_RETURN:
                running = False
                print("** BYE from RETURN **")

            if running:
                print(p, vals[0], vals[1], vals[2])
                showgraph()  # Update SSD1306
                for i in range(3):
                    cvals[i] = int(vals[i]*2.5)
                showBlinkt() # Update Blinkt
        else:
            print("Event:",event) # Never prints - Good!

# Clear display.
clear() #Blinkt LEDs off
show()
disp.clear() # SSD1306 off
disp.display()
print("Finished") 

Any help with a solution would be great.

Return to “Python”

Who is online

Users browsing this forum: Baidu [Spider], buja, Galbi3000, MrYsLab, przemof and 16 guests