Skip to content

Control screen from your own code

Matthieu Houdebine edited this page Sep 21, 2023 · 11 revisions

This repository can be used to easily control your display from external Python code.
It will act as an abstraction library to handle specific protocols and capabilities of each supported smart screen models in a transparent way for the user.

Code on this page is extracted from the full example in simple-program.py

Import required modules

In your Pyhon code, import LcdComm modules.

# Import only the modules for LCD communication
from library.lcd.lcd_comm import Orientation
from library.lcd.lcd_comm_rev_a import LcdCommRevA
from library.lcd.lcd_comm_rev_b import LcdCommRevB
from library.lcd.lcd_comm_rev_c import LcdCommRevC
from library.lcd.lcd_comm_rev_d import LcdCommRevD

These modules contains:

  • low-level communications management (serial port open/read/write/close)
  • display configuration functions: set backlight, set orientation, set backplate RGB LEDs for rev. flasghip...
  • wrapper functions to display basic graphical components: bitmap, text, progress bar...

Create LcdComm object

First step in your program is to create a LcdComm object.
You need to identify your hardware revision. Then you can create an LcdCommRevA / B / C / ... for your smart screen model

lcd_comm = LcdCommRevA(com_port="AUTO",
                       display_width=320,
                       display_height=480)

# OR

lcd_comm = LcdCommRevB(...) / LcdCommRevC(...) / ...

The given arguments are:

  • com_port: an absolute link to your serial port ("/dev/tty3", "COM3"...) or "AUTO" to enable COM port autodiscovery
  • display_width: the width in pixels when in portrait orientation
  • display_height: the height in pixels when in portrait orientation
  • update_queue optional: a Queue where the LCD serial operations can be stored to be processed by another thread, or Noneif you want the serial operations to be processed sequentially by the main thread

Do not use display_width / display_height to rotate screen! Use SetOrientation() function.

Initialize & configure your display

LcdComm objects open the COM port at creation. You then have to initialize (and configure if needed) your display before using it:

# Reset screen in case it was in an unstable state (screen is also cleared)
lcd_comm.Reset()  # Optional, always initialize comm. after that

# Send initialization commands
lcd_comm.InitializeComm()  # Mandatory!

# Set brightness in % (⚠️ Turing / rev. A displays can get hot at high brightness!)
lcd_comm.SetBrightness(level=25)  # Optional

# Set backplate RGB LED color (for supported HW only)
lcd_comm.SetBackplateLedColor(led_color=(255, 255, 255))  # Optional

# Set orientation (screen starts in Portrait)
lcd_comm.SetOrientation(orientation=Orientation.PORTRAIT)  # Optional

Note: you can configure your display at runtime, not only at startup. If you do a Reset(), be sure to run a InitializeComm() just after.

Display graphical content

After your display has been initialized, you can display graphical content on it.
The screen only accepts bitmaps, so every graphical content (pictures, text, progress bar...) is generated as a bitmap then sent to screen.

Note: the bigger the content is, the longer it will take to display it.
Try to have big content (e.g. background pictures) displayed at the beginning of your program, then refresh only small parts of the screen at runtime.

Pictures

# Define a background picture
background = "res/backgrounds/example.png"

# Display background picture
lcd_comm.DisplayBitmap(background)

# Display a custom picture
lcd_comm.DisplayBitmap("logo/icon.png", x=100, y=100)

If you do not specify x and y, picture is displayed at 0,0 (top left corner). If you do not specify width and height, they are detected from your picture.

Text

# Display custom text with solid background
lcd_comm.DisplayText("Custom italic text", x=5, y=150,
                     font="roboto/Roboto-Italic.ttf",
                     font_size=30,
                     font_color=(0, 0, 255),
                     background_color=(255, 255, 0))

# Display custom text with transparent background
lcd_comm.DisplayText("Transparent bold text", x=5, y=250,
                     font="geforce/GeForce-Bold.ttf",
                     font_size=30,
                     font_color=(255, 255, 255),
                     background_image=background)

You can specify a font .ttf filename (fetched from res/fonts/ folder) as well as a font size and font color. For text to have a transparent background, you must provide the path to the image you used as background picture.

Progress bars

# Progress bar with solid background and outline
lcd_comm.DisplayProgressBar(x=10, y=40,
                            width=140, height=30,
                            min_value=0, max_value=100, value=bar_value,
                            bar_color=(255, 255, 0), bar_outline=True,
                            background_image=background)

# Progress bar with transparent background and no outline
lcd_comm.DisplayProgressBar(x=160, y=40,
                            width=140, height=30,
                            min_value=0, max_value=19, value=bar_value,
                            bar_color=(0, 255, 0), bar_outline=False,
                            background_image=background)

Progress bars can have outline or not. The outline will have the same color as the bar.
The bar_value must be inside [min_value, max_value].
For progress bars to have a transparent background, you must provide the path to the image you used as background picture.