Solve FutureWarning: I2CDisplay moved from displayio to i2cdisplaybus and I2CDisplay renamed I2CDisplayBus

In CircuitPython 8 and before, I2CDisplay is in displayio. In CircuitPython 9+, it's moved to i2cdisplaybus.I2CDisplayBus.

This exercise define display_bus in try:...except ImportError:...block, using displayio.I2CDisplay on CircuitPython 8, and i2cdisplaybus.I2CDisplayBus on CircuitPython 9.


rp2040_ssd1306_displayio_circuitpython89.py
"""
Solve:
FutureWarning: I2CDisplay moved from displayio to i2cdisplaybus
FutureWarning: I2CDisplay renamed I2CDisplayBus

In CircuitPython 8 and before, I2CDisplay is in displayio (https://docs.circuitpython.org/en/8.2.x/shared-bindings/displayio/index.html#displayio.I2CDisplay).
In CircuitPython 9+, it's moved to i2cdisplaybus.I2CDisplayBus (https://docs.circuitpython.org/en/latest/shared-bindings/i2cdisplaybus/index.html#i2cdisplaybus.I2CDisplayBus).

This exercise define display_bus in try:...except ImportError:...block,
using displayio.I2CDisplay on CircuitPython 8,
and i2cdisplaybus.I2CDisplayBus on CircuitPython 9.

lib needed (have to match with the CircuitPython running):
- adafruit_displayio_ssd1306.mpy
- adafruit_display_text folder
"""

import os
import sys
import board
import busio
import time

import displayio
import terminalio

# import adafruit_displayio_ssd1306 will raise the warning:
# FutureWarning: Display moved from displayio to busdisplay
# FutureWarning: Display renamed BusDisplay
# For now, I don't know how to handle it! May be the library will be changed later.
import adafruit_displayio_ssd1306

from adafruit_display_text import label

displayio.release_displays()

# Create the I2C interface and display object of SSD1306_I2C.
SCL = board.GP15
SDA = board.GP14
i2c = busio.I2C(SCL, SDA)

ssd1306_i2c_addr = 0x3C
display_width =128
display_height = 64

#--- Old approach for CircuitPython before 9 ---
#If run on CircuitPython 9, will raise warning:
#FutureWarning: I2CDisplay moved from displayio to i2cdisplaybus
#FutureWarning: I2CDisplay renamed I2CDisplayBus
#from displayio import I2CDisplay
#display_bus = I2CDisplay(i2c, device_address=ssd1306_i2c_addr)

#--- New approach for CircuitPython 9+ ---
#cannot run on CircuitPython 8
#ImportError: no module named 'i2cdisplaybus'
#from i2cdisplaybus import I2CDisplayBus
#display_bus = I2CDisplayBus(i2c, device_address=ssd1306_i2c_addr)

#--- Use try:...except ImportError:...block to load display_bus,
#Work on both CircuitPython 8 & 9
try:
    # for CircuitPython 9+
    from i2cdisplaybus import I2CDisplayBus
    display_bus = I2CDisplayBus(i2c, device_address=ssd1306_i2c_addr)
except ImportError:
    # for CircuitPython before 9
    from displayio import I2CDisplay
    display_bus = I2CDisplay(i2c, device_address=ssd1306_i2c_addr)
#--- End of try/except block ---

display = adafruit_displayio_ssd1306.SSD1306(
    display_bus, width=display_width, height=display_height)

print("=================================================")
info = sys.implementation[0] + ' ' + os.uname()[3] + '\n' + \
       'run on ' + os.uname()[4]
print(info)
print("=================================================")
print(adafruit_displayio_ssd1306.__name__,
      adafruit_displayio_ssd1306.__version__)
print("===> Actually, display_bus is:", display_bus)
print("SCL: ", SCL)
print("SDA: ", SDA)
print("display.width x height: ",
      display.width, " x ", display.height)
#================================================
# Make the display context
root_group = displayio.Group()

"""
show() is deprecated and will be removed in CircuitPython 9.0.0. Use .root_group = group instead.
https://docs.circuitpython.org/en/8.2.x/shared-bindings/displayio/index.html#displayio.Display.show
"""
#display.show(group)
display.root_group = root_group

bg_bitmap = displayio.Bitmap(display.width, display.height, 2)
bg_palette = displayio.Palette(2)
bg_palette[0] = 0x000000  # Black
bg_palette[1] = 0xFFFFFF  # White
bg_bitmap.fill(1)
background = displayio.TileGrid(bg_bitmap, pixel_shader=bg_palette, x=0, y=0)
root_group.append(background)

inner_bitmap = displayio.Bitmap(display.width-2, display.height-2, 2)
inner_bitmap.fill(0)
inner = displayio.TileGrid(inner_bitmap, pixel_shader=bg_palette, x=1, y=1)
root_group.append(inner)

# draw a label say hello
text_hello = "coxxect.blogspot.com"
label_hello = label.Label(terminalio.FONT, text=text_hello, scale=1, color=0xFFFFFF, x=5, y=10)
root_group.append(label_hello)

# label for CircuitPython info
sys_info = sys.implementation[0] + " " + os.uname()[2] + " run on " + os.uname()[4]
# labels for driver
label_sysinfo = label.Label(terminalio.FONT,
                            text=sys_info,
                            scale=1,
                            color=0xFFFFFF,
                            x=5, y=24)
root_group.append(label_sysinfo)

# labels for driver
label_drv = label.Label(terminalio.FONT,
                        text=adafruit_displayio_ssd1306.__name__
                        + " " + adafruit_displayio_ssd1306.__version__,
                        scale=1,
                        color=0xFFFFFF,
                        x=5, y=36)
root_group.append(label_drv)

# labels for driver
label_bus = label.Label(terminalio.FONT,
                        text=str(display_bus),
                        scale=1,
                        color=0xFFFFFF,
                        x=5, y=48)
root_group.append(label_bus)

# scroll()/reverse_scroll()
# ref:
# https://learn.adafruit.com/rgb-led-matrices-matrix-panels-with-circuitpython/example-simple-two-line-text-scroller

def scroll(line):
    line.x = line.x - 1
    line_width = line.bounding_box[2]
    if line.x < -line_width:
        line.x = display.width
        
def reverse_scroll(line):
    line.x = line.x + 1
    line_width = line.bounding_box[2]
    if line.x >= display.width:
        line.x = -line_width

time.sleep(1)

while True:
    reverse_scroll(label_hello)
    scroll(label_sysinfo)
    scroll(label_drv)
    display.refresh(minimum_frames_per_second=0)




Comments

Popular posts from this blog

480x320 TFT/ILI9488 SPI wih EP32C3 (arduino-esp32) using Arduino_GFX Library

my dev.tools - FNIRSI 2C23T 3-in-1 Dual Channel Oscilloscope/Multimeter/Signal Generator