LS027B7DSH01Display
- class quanser.devices.interfaces.LS027B7DSH01Display
A Python wrapper for the Quanser Devices API interface to Sharp LS027B7DSH01 displays.
Example
>>> from quanser.devices import LS027B7DSH01Display >>> display = LS027B7DSH01Display()
- beginDraw()
Put the LCD display into drawing mode so that the display is not updated until endDraw is called. This allows both graphics and text to be mixed on the display as an atomic update. If this function is not called then the LCD display updates immediately.
Example
Draw an image and then write text on top of it.
>>> from quanser.devices import LS027B7DSH01Display >>> display = LS027B7DSH01Display() >>> display.open("spi://localhost:1?word=8,baud=45000000,polarity=on,phase=on,memsize=8192,frame=0") >>> display.beginDraw() >>> display.drawImage(0, 0, (240, 400), image) >>> message = "Hello, world!" >>> display.printText(5, 0, message) >>> display.endDraw() >>> ... ... >>> display.close()
Redirect a QCar 2 camera to the LCD display and superimpose the frame rate on the image.
>>> from quanser.devices import LS027B7DSH01Display >>> from quanser.multimedia import VideoCapture, ImageFormat, ImageDataType >>> import numpy as np >>> import time >>> >>> fps = 20.0 >>> image_width = 400 >>> image_height = 240 >>> image_data = np.zeros((image_width, image_height), dtype = np.uint8) >>> >>> period = 1.0 / fps >>> >>> capture = VideoCapture("video://localhost:0", fps, image_width, image_height, ImageFormat.COL_MAJOR_GREYSCALE, ImageDataType.UINT8, None, 0) >>> >>> display = LS027B7DSH01Display() >>> display.open("spi://localhost:1?word=8,baud=45000000,polarity=on,phase=on,memsize=8192,frame=0") >>> display.setDarkMode(True) >>> display.setRotation(True) >>> >>> capture.start() >>> >>> try : >>> while True : >>> capture.read(image_data) >>> >>> display.beginDraw() >>> >>> display.drawImage(0, 0, (image_height, image_width), image_data) >>> >>> message = "FPS: %.0f" % fps >>> display.printText(11, 0, message) >>> >>> display.endDraw() >>> time.sleep(period) >>> finally: >>> display.close() >>> capture.stop() >>> capture.close()
- close()
Close the display.
Example
>>> from quanser.devices import LS027B7DSH01Display >>> display = LS027B7DSH01Display() >>> display.open("spi://localhost:1?word=8,baud=45000000,polarity=on,phase=on,memsize=8192,frame=0") >>> ... ... >>> display.close()
- drawImage(pixel_row, pixel_column, image_size, image)
Draw an image on the display at the given pixel coordinates. The display is not cleared before drawing the image, so multiple images may be drawn at different locations on the display.
- Parameters
pixel_row (int) – The pixel row at which to place the top edge of the image. A value of 0 corresponds to the top of the display.
pixel_column (int) – The pixel column at which to place the left edge of the image. A value of 0 corresponds to the left edge of the display.
image_size ((int, int)) – A tuple containing the height and width of the image as (height, width).
image (image) – The image to display as a 2D uint8 array that is image_height x image_width pixels and is stored in column-major order in memory. Each byte represents a single pixel in which a value less than or equal to 127 is either white or black, depending on whether dark mode is enabled, and a value greater than 127 is the opposite colour.
Examples
Draw a bitmap on the LCD display.
>>> from quanser.devices import LS027B7DSH01Display >>> import cv2 >>> display = LS027B7DSH01Display() >>> display.open("spi://localhost:1?word=8,baud=45000000,polarity=on,phase=on,memsize=8192,frame=0") >>> img = cv2.imread('Sample.png', cv2.IMREAD_GRAYSCALE) >>> # Transpose to convert row major to column major. Copy to make C-continuous. >>> img_col_major = img.transpose().copy() >>> display.drawImage(0, 0, img.shape, img_col_major) >>> ... ... >>> display.close()
- endDraw()
Take the LCD display out of drawing mode. The display will be updated at this point.
Example
Draw an image and then write text on top of it.
>>> from quanser.devices import LS027B7DSH01Display >>> display = LS027B7DSH01Display() >>> display.open("spi://localhost:1?word=8,baud=45000000,polarity=on,phase=on,memsize=8192,frame=0") >>> display.beginDraw() >>> display.drawImage(0, 0, (240,400), image) >>> message = "Hello, world!" >>> display.printText(5, 0, message) >>> display.endDraw() >>> ... ... >>> display.close()
- open(uri='spi://localhost:1?word=8,baud=45000000,polarity=on,phase=on,memsize=8192,frame=0', access=2)
Opens the Sharp LS027B7DSH01 display.
- Parameters
uri (string) – The URI indicating the communication channel with which to communicate with the display. For QCar 2’s display, the URI should be “spi://localhost:1?word=8,baud=45000000,polarity=on,phase=on,memsize=8192,frame=0”. If this parameter is not specified, the default URI is set for the QCar 2’s LCD display.
access (LCDAccess) – The access mode for the display, which determines whether the LCD can be shared or not. The default access is LCDAccess.EXCLUSIVE, which means the caller gets exclusive access to the display.
Example
>>> from quanser.devices import LS027B7DSH01Display >>> display = LS027B7DSH01Display() >>> display.open("spi://localhost:1?word=8,baud=45000000,polarity=on,phase=on,memsize=8192,frame=0") >>> ... ... >>> display.close()
- printText(line, column, message, length=2147483647)
Print to the display.
- Parameters
line (int) – The line where the message will be displayed. Line 0 is the first line.
column (int) – The column where the message will be displayed. Column 0 is the first column.
message (string) –
The message to display. Unrecognized characters are skipped. If more than 16 characters would be printed on a line then the line is truncated. A newline will cause the characters to be displayed on the next line. As there are only two lines on the display there should only be one newline in the format string. Printing starts at the given line and column. Line 0 is the first line and column 0 is the first column.
The format string is UTF-8 and does support Unicode (particularly, Katakana characters).
length (int) – The maximum length of the message in bytes, not characters.
Example
Print ‘Hello, World!’ to the top, left corner of the display.
>>> from quanser.devices import LS027B7DSH01Display >>> display = LS027B7DSH01Display() >>> display.open("spi://localhost:1?word=8,baud=45000000,polarity=on,phase=on,memsize=8192,frame=0") >>> message = "Hello, world!" >>> display.printText(0, 0, message) >>> ... ... >>> display.close()
- save(filename)
Save the current contents of the display to a PBM image file. The filename should have a .pbm extension.
- Parameters
filename (string) – The filename to which to write the display contents.
Examples
Save the current contents of the LCD display.
>>> from quanser.devices import LS027B7DSH01Display >>> import cv2 >>> display = LS027B7DSH01Display() >>> display.open("spi://localhost:1?word=8,baud=45000000,polarity=on,phase=on,memsize=8192,frame=0") >>> img = cv2.imread('Sample.png', cv2.IMREAD_GRAYSCALE) >>> # Transpose to convert row major to column major. Copy to make C-continuous. >>> img_col_major = img.transpose().copy() >>> display.drawImage(0, 0, img.shape, img_col_major) >>> display.save("lcd_display.pbm") ... >>> display.close()
- setCharacter(code, pattern)
Defines the character associated with the given character code.
- Parameters
code (int) – Defines the character associated with the given character code. Valid character codes are 0x10 to 0x17 i.e., ‘’ to ‘’. Using these characters will produce the bitmap defined in the pattern for that character.
pattern (bytes) –
The pattern defines each line of the character being defined, with the sixteen bits in each word defining the pixels of that line. For example to define the letter T, the pattern would be:
- pattern = np.array([0x0000, 0x0000, 0x0000, 0X1FF0, 0x0100, 0x0100, 0x0100, 0x0100, 0x0100, 0x0100, 0x0100, 0x0100,
0x0100, 0x0100, 0x0100, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000], dtype=np.uint16)
0x0000, # 0b0000000000000000 # # # 0x0000, # 0b0000000000000000 # # # 0x0000, # 0b0000000000000000 # # # 0x1ff0, # 0b0001111111110000 # # ……… # 0x0100, # 0b0000000100000000 # # . # 0x0100, # 0b0000000100000000 # # . # 0x0100, # 0b0000000100000000 # # . # 0x0100, # 0b0000000100000000 # # . # 0x0100, # 0b0000000100000000 # # . # 0x0100, # 0b0000000100000000 # # . # 0x0100, # 0b0000000100000000 # # . # 0x0100, # 0b0000000100000000 # # . # 0x0100, # 0b0000000100000000 # # . # 0x0100, # 0b0000000100000000 # # . # 0x0100, # 0b0000000100000000 # # . # 0x0000, # 0b0000000000000000 # # # 0x0000, # 0b0000000000000000 # # # 0x0000, # 0b0000000000000000 # # # 0x0000, # 0b0000000000000000 # # # 0x0000 # 0b0000000000000000 # # #
Bit 0 is the rightmost pixel and bit 15 is the leftmost pixel. The baseline is in pattern[14]. The top of a typical character is in pattern[3], although some characters go higher.
Examples
Print a custom character.
Using array:
>>> from quanser.devices import LS027B7DSH01Display >>> display = LS027B7DSH01Display() >>> display.open("spi://localhost:1?word=8,baud=45000000,polarity=on,phase=on,memsize=8192,frame=0") >>> character_code = 0o20 >>> pattern = array('H', [0x0000, 0x0000, 0x0000, 0X1FF0, 0x0100, 0x0100, 0x0100, 0x0100, 0X1FF0, 0x0100, 0x0100, 0x0100, 0x0100, 0x0100, 0X1FF0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000]) >>> display.setCharacter(character_code, pattern) >>> message = str(chr(character_code)) >>> display.printText(0, 0, message) >>> ... ... >>> display.close()
Using numpy:
>>> from quanser.devices import LS027B7DSH01Display >>> import numpy as np >>> display = LS027B7DSH01Display() >>> display.open("spi://localhost:1?word=8,baud=45000000,polarity=on,phase=on,memsize=8192,frame=0") >>> character_code = 0o20 >>> pattern = np.array([0x0000, 0x0000, 0x0000, 0X1FF0, 0x0100, 0x0100, 0x0100, 0x0100, 0X1FF0, 0x0100, 0x0100, 0x0100, 0x0100, 0x0100, 0X1FF0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000], dtype = np.uint16) >>> display.setCharacter(character_code, pattern) >>> message = str(chr(character_code)) >>> display.printText(0, 0, message) >>> ... ... >>> display.close()
- setDarkMode(dark)
Determine whether to use dark mode for the display.
- Parameters
dark (bool) –
True
to enable dark mode i.e., white text on a black background;False
to use normal mode i.e., black text on a white background.
Examples
Print “Hello, world!” in dark mode.
>>> from quanser.devices import LS027B7DSH01Display >>> display = LS027B7DSH01Display() >>> display.open("spi://localhost:1?word=8,baud=45000000,polarity=on,phase=on,memsize=8192,frame=0") >>> display.setDarkMode(True) >>> message = "Hello, world!" >>> display.printText(0, 0, message) >>> ... ... >>> display.close()
- setRotation(rotate)
Determine whether to rotate the display 180 degrees.
- Parameters
rotate (bool) –
True
to rotate the displayFalse
to use the normal orientation
Examples
Print “Hello, world!” in the opposite orientation.
>>> from quanser.devices import LS027B7DSH01Display >>> display = LS027B7DSH01Display() >>> display.open("spi://localhost:1?word=8,baud=45000000,polarity=on,phase=on,memsize=8192,frame=0") >>> display.setRotation(True) >>> message = "Hello, world!" >>> display.printText(0, 0, message) >>> ... ... >>> display.close()