LCDInfo.com
http://forum.lcdinfo.com/

USBD480 with Python. Basic examples.
http://forum.lcdinfo.com/viewtopic.php?f=19&t=2563
Page 1 of 1

Author:  Henri [ Fri Jan 02, 2009 16:25 ]
Post subject:  USBD480 with Python. Basic examples.

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)

Author:  Henri [ Fri Jan 02, 2009 16:29 ]
Post subject: 

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)

Author:  daparix [ Tue Feb 05, 2013 10:58 ]
Post subject:  Re: USBD480 with Python. Basic examples.

can we get some explicit example with Linux OS?

thanks

Page 1 of 1 All times are UTC + 2 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/