LCDInfo.com

http://www.lcdinfo.com
It is currently Sun Nov 24, 2024 22:48

All times are UTC + 2 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Fri Jan 02, 2009 16:25 
Offline

Joined: Sun May 05, 2002 22:05
Posts: 2063
Location: Lappeenranta, Finland
I thought I'd post a very basic example of how the USBD480 can be used with Python. This example uses the Windows API DLL but if there is interest in a similar example for some other OS let me know.

This example just defines a basic interface to the display and creates a framebuffer in memory and draws some very simple things to it. Then this framebuffer is sent to the display.

Code:
from ctypes import *
import numpy

class DisplayInfo(Structure):
     _fields_ = [("Width", c_uint),
                 ("Height", c_uint),
             ("PixelFormat", c_uint),
             ("Name", c_char * 32),
             ("UName", c_char * 16),
             ("Handle", c_uint),
             ("Version", c_uint)]

USBD480_GetNumberOfDisplays = windll.USBD480_lib.USBD480_GetNumberOfDisplays
USBD480_GetNumberOfDisplays.restype = c_uint         
USBD480_GetDisplayConfiguration = windll.USBD480_lib.USBD480_GetDisplayConfiguration
USBD480_GetDisplayConfiguration.restype = c_uint
USBD480_GetDisplayConfiguration.argtypes = [c_uint, POINTER(DisplayInfo)]
USBD480_Open = windll.USBD480_lib.USBD480_Open
USBD480_Open.restype = c_uint
USBD480_Open.argtypes = [POINTER(DisplayInfo), c_uint]
USBD480_Close = windll.USBD480_lib.USBD480_Close
USBD480_Close.restype = c_uint
USBD480_Close.argtypes = [POINTER(DisplayInfo)]
USBD480_DrawFullScreen = windll.USBD480_lib.USBD480_DrawFullScreen
USBD480_DrawFullScreen.restype = c_uint
USBD480_DrawFullScreen.argtypes = [POINTER(DisplayInfo), POINTER(c_char)]
USBD480_DrawFullScreenRGB32 = windll.USBD480_lib.USBD480_DrawFullScreenRGB32
USBD480_DrawFullScreenRGB32.restype = c_uint
USBD480_DrawFullScreenRGB32.argtypes = [POINTER(DisplayInfo), POINTER(c_uint)]
USBD480_SetBrightness = windll.USBD480_lib.USBD480_SetBrightness
USBD480_SetBrightness.restype = c_uint
USBD480_SetBrightness.argtypes = [POINTER(DisplayInfo), c_uint]

width = 480
height = 272

# define some basic colors, format is 0xAABBGGRR
RED      = 0xFF0000FF   
GREEN   = 0xFF00FF00
BLUE   = 0xFFFF0000

img = numpy.empty((width,height),numpy.uint32) # 480x272 image array   
img.shape=height,width

img[10, 10] = BLUE # plot one point to the image
img[12, 10] = BLUE # plot one point to the image
img[12, 11] = BLUE # plot one point to the image

img[10:21,112:224] = GREEN # draw a green rectangle
      
disp = DisplayInfo()

print USBD480_GetNumberOfDisplays()
print USBD480_GetDisplayConfiguration(0, disp)
print disp.Width
print disp.Height
print USBD480_Open(disp, 0)
print USBD480_DrawFullScreenRGB32(disp, img.ctypes.data_as(POINTER(c_uint)))
#print USBD480_SetBrightness(disp, 255)
print USBD480_Close(disp)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 02, 2009 16:29 
Offline

Joined: Sun May 05, 2002 22:05
Posts: 2063
Location: Lappeenranta, Finland
Another example that adds a little more to the previous one by showing how the Python Imaging Library (PIL) can be used.

Code:
from ctypes import *
import numpy
from PIL import Image

class DisplayInfo(Structure):
     _fields_ = [("Width", c_uint),
                 ("Height", c_uint),
             ("PixelFormat", c_uint),
             ("Name", c_char * 32),
             ("UName", c_char * 16),
             ("Handle", c_uint),
             ("Version", c_uint)]

USBD480_GetNumberOfDisplays = windll.USBD480_lib.USBD480_GetNumberOfDisplays
USBD480_GetNumberOfDisplays.restype = c_uint         
USBD480_GetDisplayConfiguration = windll.USBD480_lib.USBD480_GetDisplayConfiguration
USBD480_GetDisplayConfiguration.restype = c_uint
USBD480_GetDisplayConfiguration.argtypes = [c_uint, POINTER(DisplayInfo)]
USBD480_Open = windll.USBD480_lib.USBD480_Open
USBD480_Open.restype = c_uint
USBD480_Open.argtypes = [POINTER(DisplayInfo), c_uint]
USBD480_Close = windll.USBD480_lib.USBD480_Close
USBD480_Close.restype = c_uint
USBD480_Close.argtypes = [POINTER(DisplayInfo)]
USBD480_DrawFullScreen = windll.USBD480_lib.USBD480_DrawFullScreen
USBD480_DrawFullScreen.restype = c_uint
USBD480_DrawFullScreen.argtypes = [POINTER(DisplayInfo), POINTER(c_char)]
USBD480_DrawFullScreenRGB32 = windll.USBD480_lib.USBD480_DrawFullScreenRGB32
USBD480_DrawFullScreenRGB32.restype = c_uint
USBD480_DrawFullScreenRGB32.argtypes = [POINTER(DisplayInfo), POINTER(c_uint)]
USBD480_SetBrightness = windll.USBD480_lib.USBD480_SetBrightness
USBD480_SetBrightness.restype = c_uint
USBD480_SetBrightness.argtypes = [POINTER(DisplayInfo), c_uint]

width = 480
height = 272

# define some basic colors, format is 0xAABBGGRR
RED      = 0xFF0000FF   
GREEN   = 0xFF00FF00
BLUE   = 0xFFFF0000

img = numpy.empty((width,height),numpy.uint32) # 480x272 image array   
img.shape=height,width

img[10, 10] = BLUE # plot one point to the image
img[12, 10] = BLUE # plot one point to the image
img[12, 11] = BLUE # plot one point to the image

img[10:21,112:224] = GREEN # draw a green rectangle

#pilimg = Image.new('RGBA', (480,272))

pilImage = Image.fromarray(img,'RGBA') # load the numpy array as a PIL image
pilImage.save('my.png') # save to disk

pilImage.putpixel((100,100), (255,255,255,255)) # set a white pixel to 100, 100

image2 = Image.open('rgba.png') # load image from disk
pilImage.paste(image2, (190,110)) # paste this image to the buffer

arr = numpy.asarray(pilImage) # get a numpy array from the PIL image buffer
      
disp = DisplayInfo()

print USBD480_GetNumberOfDisplays()
print USBD480_GetDisplayConfiguration(0, disp)
print disp.Width
print disp.Height
print USBD480_Open(disp, 0)
#print USBD480_DrawFullScreenRGB32(disp, img.ctypes.data_as(POINTER(c_uint)))
print USBD480_DrawFullScreenRGB32(disp, arr.ctypes.data_as(POINTER(c_uint)))
#print USBD480_SetBrightness(disp, 255)
print USBD480_Close(disp)


Top
 Profile  
 
PostPosted: Tue Feb 05, 2013 10:58 
Offline

Joined: Fri Sep 10, 2010 18:45
Posts: 4
can we get some explicit example with Linux OS?

thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC + 2 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group