473,320 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Graphics too slow :-(

Hi all,
i've written a widget that emulate a LED Matrix. All works but drawing in
too slow. How i can speed-up ?

Here's the code:

################################################## ####################
# Name: MatrixLed #
# Purpose: A LED Matrix Display #
# #
# Author: Pinassi "O-Zone" Michele <o-****@siena.linux.it> #
# Licence: Same as wxPython's #
# #
# v1.0 - Initial Release #
# #
################################################## ####################

import wx

class LEDMatrix(wx.Window):
def __init__(self, parent, id=-1, pos=wx.DefaultPosition, size=wx.DefaultSize, style=0, name=""):

wx.Window.__init__(self, parent, id, pos, size, style, name)

self.parent = parent

self.bgColour = wx.NamedColour("GREY76")
self.fgColour = wx.NamedColour("LIGHTBLUE2")
self.onColour = wx.NamedColour("DEEPSKYBLUE4")
self.charSpace = 9

self.romAlpha = { "A" : "0001000000101000010001001000001011111110100000101 000001010000010",
"B" : "1111111001000001010000010111111001000001010000010 100000111111110",
"C" : "0011111001000000100000001000000010000000100000000 100000000111110",
"D" : "1111100001000100010001000100010001000100010001000 100010011111000",
"E" : "1111111001000000010000000100000001111000010000000 100000011111110",
"F" : "1111111001000000010000000111100001000000010000000 100000001000000",
"G" : "0011110001000010100000001000000010000000100001100 100001000111100",
"H" : "0100001001000010010000100100001001111110010000100 100001001000010",
"I" : "0011100000010000000100000001000000010000000100000 001000000111000",
"L" : "0100000001000000010000000100000001000000010000000 100000001111110",
"M" : "1000001011000110101010101001001010000010100000101 000001010000010",
"N" : "1100001001100010010100100100101001000110010000100 100001001000010",
"O" : "0011110001000010010000100100001001000010010000100 100001000111100",
"P" : "1111110001000010010000100111110001000000010000000 100000001000000",
"Q" : "0011110001000010010000100100001001000010010000100 100011000111111",
"R" : "1111110001000010010000100111110001010000010010000 100010001000010",
"S" : "0011110001000010010000000010000000011100000000100 100001000111100",
"T" : "1111111000010000000100000001000000010000000100000 001000000010000",
"U" : "0100001001000010010000100100001001000010010000100 100001000111100",
"V" : "1000001010000010100000100100010001000100001010000 010100000010000",
"Z" : "1111111010000010000001000000100000010000001000000 100001011111110",
"X" : "1000000101000010001001000001100000011000001001000 100001010000001",
"Y" : "0100001001000010001001000001100000010000000100000 001000000010000",
"W" : "",
"K" : "",
"J" : "",
"0" : "",
"1" : "",
"2" : "",
"3" : "",
"4" : "",
"5" : "",
"6" : "",
"7" : "",
"8" : "",
"9" : "",
":" : "",
"." : "",
" " : "0000000000000000000000000000000000000000000000000 000000000000000"
}

self.SetForegroundColour(wx.Colour(0, 204, 204))

# Event handling
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_SIZE, self.OnSize)

# Now for the starting value
self.DoArray()

def OnSize(self, event):
print "OnSize X:",self.matrix_x,"Y:",self.matrix_y
# Salvare l'array precedente
temp_x = self.matrix_x
temp_y = self.matrix_y
tempArray = [[0 for i in range(temp_x)] for i in range(temp_y)]
for x in range(temp_x):
for y in range(temp_y):
tempArray[y][x] = self.matrixArray[y][x]

# Ricreare l'array
self.DoArray()

# Copiare i dati dell'array precedente su quello nuovo
for x in range(temp_x):
for y in range(temp_y):
self.matrixArray[y][x] = tempArray[y][x]

def OnPaint(self, event):
dc = wx.PaintDC(self)
self._selColours(self.bgColour, dc)
w, h = self.GetSize()
print "OnPaint()"
self._doClear(dc)
#dc.DrawRectangle((0, 0), (w, h))
#self._doMatrix(dc)
self._doUpdate(dc)

def DoArray(self):
w, h = self.GetSize()
self.matrixArray = [[0 for i in range(w)] for i in range(h)]
self.matrix_x = w
self.matrix_y = h
print "DoArray X:",w,"Y:",h

def _doUpdate(self,dc):
self._selColours(self.onColour, dc)
print "doUpdate X:",self.matrix_x,"Y:",self.matrix_y
for x in range(self.matrix_x):
for y in range(self.matrix_y):
if int(self.matrixArray[y][x]):
self._drawXY(x,y,dc)

def _doClear(self,dc):
self._selColours(self.bgColour, dc)
print "doClear X:",self.matrix_x,"Y:",self.matrix_y
for x in range(self.matrix_x):
for y in range(self.matrix_y):
if (self.matrixArray[y][x] == 0):
self._drawXY(x,y,dc)

def _doMatrix(self, dc):
w, h = self.GetSize()
self._selColours(self.fgColour, dc)
x = y = 0
for x in range(w/4):
for y in range(h/4):
self._drawXY(x,y,dc)

def _drawXY(self,x,y,dc):
dc.DrawRectangle(((x*4)+2, (y*4)+2), (3, 3))

def _selColours(self, colour, dc):
dc.SetPen(wx.Pen(colour, 1, wx.SOLID))
dc.SetBrush(wx.Brush(colour, wx.SOLID))

def _drawAlpha(self,dc,char,ox,oy):
alpha = self.romAlpha[char]
for y in range(0, 8):
for x in range(0, 8):
if int(alpha[(y*8)+x]):
self.matrixArray[oy+y][ox+x] = 1;

def SetValue(self, value):
vo = value.upper()
dc = wx.PaintDC(self)
self._selColours(self.onColour, dc)
cx = 5
for x in range(0, len(vo)):
self._drawAlpha(dc,vo[x],cx,5)
cx = cx + self.charSpace

def EVTUpdate(self,evt): # A method to Bind UPDATE to an EVENT
dc = wx.PaintDC(self)
self._doClear(dc)
self._doUpdate(dc)

def ShiftLeft(self): # Alpha - Don't use !
print "ShiftLeft"
for x in range(0,self.matrix_x-1):
for y in range(0,self.matrix_y-1):
if int(self.matrixArray[y][x]):
self.matrixArray[y][x-1] = self.matrixArray[y][x];
self.matrixArray[y][x] = 0
--
------
O-Zone ! (C) 2004
http://myphotos.zerozone.it << MyPHOTOS GPL Photo Blog ! Join now !

Jul 18 '05 #1
2 1543
O-Zone wrote:
Hi all,
i've written a widget that emulate a LED Matrix. All works but drawing in
too slow. How i can speed-up ?


Have you isolated the cause of the slowness yet e.g. using timeit or
hotshot?

Cheers,
Brian
Jul 18 '05 #2
O-Zone wrote:
i've written a widget that emulate a LED Matrix. All works but drawing in
too slow. How i can speed-up ?


It looks like you are using 2D lists and a lot of w * h iterations.
You repeat these at least 6 times per update event. Those add up
real fast.

You could probably speed things up considerably by using a single
list of size w*h and indexed accordingly. Then modifying and
copying these list could be done a lot faster, without explicit
iterations.

But I think the real solution is to do away with iterating w*h times.
Define bigger (and static) display elements, and iterate over
those instead of the window resolution.

Istvan.
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Matthew | last post by:
I am not the most talented programmer to grace the earth by a long shot. But I've got a gripe I need to air about the .NET implementaion of Visual Basic. I can live with alot of the major changes...
0
by: Martin | last post by:
I am using graphics as backgrounds for forms,buttons,labels etc. The question is: is it faster to load all graphics from files on app start or to use it embeded (places in editor during design)....
4
by: Martin | last post by:
I am using graphics as backgrounds for forms,buttons,labels etc. The question is: is it faster to load all graphics from files on app start or to use it embeded (places in editor during design)....
3
by: Dr. Zharkov | last post by:
Hello. Inform, please, how to find the programs on building the 3D-graphics of function z=f (x, y) and only for Visual Studio .NET: Visual C# .NET, Visual Basic .NET or Visual C++ .NET and GDI+...
5
by: Vin | last post by:
Hi, I am using the following code to draw whatever the user draws using x,y. // draws lines directly on a winform. CreateGraphics().DrawLine(APen, x, y, OldX, OldY); Now how do I save the...
5
by: Tim | last post by:
hi I used to do this Dim gfx As System.Drawing.Graphics = pic1.CreateGraphics gfx.FillEllipse blah blah blah to draw straight onto a form. but this is frowned up (slow).
3
by: | last post by:
I've got a winForm that has a good number of custom controls with custom graphics that are stored as resources in the project. Recently, I updated all of the graphics with a new look. Added all the...
1
by: =?Utf-8?B?bXIgcGVhbnV0?= | last post by:
My application requires user interaction with a graphics interface that is changing (data is being collected and plotted). Sometimes the graphics updates are slow and the user is locked out until...
8
by: Galen Somerville | last post by:
My graphics involves a lot of line drawings in a short period of time. I have all the graphics in a separate module. I repeatedly get the pixel data from a USB device, draw the requisite traces,...
0
by: raylopez99 | last post by:
Hi, I'm getting into GDI+ Forms 2.0 graphics for C#3 using Visual Studio 2008. One thing I notice: the graphics are really slow and flicker on a Pentium IV, with 2 GB RAM, even with...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.