SSD1306 OLED referesh using displayio library

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
RainySun
 
Posts: 73
Joined: Thu Nov 18, 2021 2:55 pm

SSD1306 OLED referesh using displayio library

Post by RainySun »

Hey, I am currently using displayio library, and I wanted to know how to refresh SSD1306 OLED so that the value can be updated.
Currently, I am able to output a value on OLED but as each value gets updated, it stays on top of the previous value.
How can I refresh the OLED, so that I can get the latest value.
I have looked into the displayio library, and it does mention about OLED_reset, and it has to be assigned to a pin. Is there a way to do a periodic refresh without assigning oled_reset to any pin.
I hope to hear your response.

Below is the code,

Code: Select all

import time
import board
import displayio
import terminalio
import adafruit_displayio_ssd1306
from adafruit_display_text import label
from analogio import AnalogIn

analog_in = AnalogIn(board.A1)
analog_in1 = 0


# OLED Connection
displayio.release_displays()
# Using I2C connection
i2c = board.I2C()
display_bus = displayio.I2CDisplay(i2c, device_address=0x3C)

WIDTH = 128
HEIGHT = 32
BORDER = 5

display = adafruit_displayio_ssd1306.SSD1306(display_bus, width=128, height=32)

# Display Conect below
splash = displayio.Group()
display.show(splash)


while True:
    initial = time.monotonic()
    for i in range(100):
        analog_in2 = analog_in.value
        if analog_in2 > analog_in1:
            analog_in1 = analog_in2
        time.sleep(0.05)
    # print((analog_in1 * 3.3) / 65536)
    text = "V = " + str((analog_in1 * 3.3) / 65536)
    text_area = label.Label(
        terminalio.FONT, text=text, color=0xFFFFFF, x=28, y=HEIGHT // 2 - 1
    )
    splash.append(text_area)

User avatar
dastels
 
Posts: 15608
Joined: Tue Oct 20, 2015 3:22 pm

Re: SSD1306 OLED referesh using displayio library

Post by dastels »

You need to build your UI before the loop and inthe look change the text of the label. E.g.:

Code: Select all

text_area.text = "V = " + str((analog_in1 * 3.3) / 65536)
Dave

User avatar
RainySun
 
Posts: 73
Joined: Thu Nov 18, 2021 2:55 pm

Re: SSD1306 OLED referesh using displayio library

Post by RainySun »

Thank you Dave. I was able to build a UI, and display updates. I am not entirely sure how to call the UI and update labels (values) if I put them outside the loop.
This is what I currently have to display updates.

Code: Select all

while True:
    # initial = time.monotonic()
    for i in range(100):
        analog_in2 = analog_in.value
        if analog_in2 > analog_in1:
            analog_in1 = analog_in2
        time.sleep(0.05)
    # print((analog_in1 * 3.3) / 65536, ",", initial)
    color_bitmap = displayio.Bitmap(128, 32, 1)
    color_palette = displayio.Palette(1)
    color_palette[0] = 0x000000
    bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
    splash.append(bg_sprite)

    textf = "V = " + str((analog_in1 * 3.3) / 65536)
    text_area = label.Label(
        terminalio.FONT, text=textf, color=0xFFFFFF, x=28, y=HEIGHT // 2 - 1
    )
    splash.append(text_area)

User avatar
dastels
 
Posts: 15608
Joined: Tue Oct 20, 2015 3:22 pm

Re: SSD1306 OLED referesh using displayio library

Post by dastels »

Your problem is that you create a new label each time through the loop and each new one overlays the previous ones. It's also allocating a new object each time and you risk an eventual crash due to running out of memory. Well I'd say there's a 100% chance of that happening.

When you build the UI, simply put the label in a variable like you're doing now. They use it in the loop to update its text value.

Same with the TileGrid; create it once before the loop.

Dave

User avatar
RainySun
 
Posts: 73
Joined: Thu Nov 18, 2021 2:55 pm

Re: SSD1306 OLED referesh using displayio library

Post by RainySun »

Really thankful for your guidance. This is the latest code. I am trying my best to figure it out but at this point, I feel that I am working hard, not smart.
I really hope that you do not lose interest in helping me out.

Code: Select all

import time
import board
import displayio
import terminalio
import adafruit_displayio_ssd1306
from adafruit_display_text import label
from analogio import AnalogIn

analog_in = AnalogIn(board.A1)
analog_in1 = 0


# OLED Connection
displayio.release_displays()
# Using I2C connection
i2c = board.I2C()
display_bus = displayio.I2CDisplay(i2c, device_address=0x3C)

WIDTH = 128
HEIGHT = 32
BORDER = 5

display = adafruit_displayio_ssd1306.SSD1306(display_bus, width=128, height=32)

# Display Conect below
splash = displayio.Group()
display.show(splash)


def back(y):
    color_bitmap = displayio.Bitmap(128, 32, 1)
    color_palette = displayio.Palette(y)
    color_palette[0] = 0x000000
    bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
    splash.append(bg_sprite)


def foo(x):
    text_area = label.Label(
        terminalio.FONT, text=x, color=0xFFFFFF, x=28, y=HEIGHT // 2 - 1
    )
    splash.append(text_area)


while True:
    # initial = time.monotonic()
    for i in range(100):
        analog_in2 = analog_in.value
        if analog_in2 > analog_in1:
            analog_in1 = analog_in2
        time.sleep(0.05)
    # print((analog_in1 * 3.3) / 65536, ",", initial)
    back(1)
    textf = "V = " + str((analog_in1 * 3.3) / 65536)
    foo(textf)


User avatar
dastels
 
Posts: 15608
Joined: Tue Oct 20, 2015 3:22 pm

Re: SSD1306 OLED referesh using displayio library

Post by dastels »

You're still creating Labels and TileGrids in the loop. Yes, not physically in the loop, but in functions that are called from the loop. You have to do that in code that is executed before entering the loop. E.g. just after splash is created. In the loop you should only be updating the contents of the UI elements you previously created.

For an example (a rather involved on) see https://learn.adafruit.com/making-a-pyp ... -full-code. Here's a simpler example: https://learn.adafruit.com/pyportal-cal ... ython-code.

Dave

User avatar
RainySun
 
Posts: 73
Joined: Thu Nov 18, 2021 2:55 pm

Re: SSD1306 OLED referesh using displayio library

Post by RainySun »

Thank you, Dave! The example really helped. It was much simpler than I thought compared to using Arduino IDE. I thought that I needed to create a function to call but it seems that it automatically updates. I was able to leave the OLED on for long, and it did not crash. Attached is the latest code.

Code: Select all

text_area = label.Label(
    terminalio.FONT, text="Data View", color=0xFFFFFF, x=28, y=HEIGHT // 2 - 1
)
view.append(text_area)

while True:
    # initial = time.monotonic()
    for i in range(100):
        analog_in2 = analog_in.value
        if analog_in2 > analog_in1:
            analog_in1 = analog_in2
        time.sleep(0.05)
    # print((analog_in1 * 3.3) / 65536, ",", initial)
    text_area.text = "V = " + str((analog_in1 * 3.3) / 65536)


User avatar
dastels
 
Posts: 15608
Joined: Tue Oct 20, 2015 3:22 pm

Re: SSD1306 OLED referesh using displayio library

Post by dastels »

Looks good!

Dave

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”