Variables on OLED overwriting previous value

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
Stumpy_L
 
Posts: 86
Joined: Sun Jan 14, 2018 12:31 pm

Variables on OLED overwriting previous value

Post by Stumpy_L »

I am using a feathers2 with the Featherwing OLED display and a SHTC3 Temp & humidity sensor. As this is my first program with Circuitpython I hacked two of the examples together to read the temp and humidity and display the values on the display, that part works.

The problem is as values update the previous values are not cleared, or overwritten, so they are one on top of the other. After a few minutes some of the characters are just a blur.

Am I not formatting the value correctly, can I set the background of the character to black, is there a different way to send both the text and value in the same line?

Thanks for your help the code is below
John

Code: Select all


"""
Read Temp and Humidity
Dislay on OLED

"""


import board
import displayio
import terminalio
import adafruit_shtc3
import time

# can try import bitmap_label below for alternative
from adafruit_display_text import label
import adafruit_displayio_sh1107

displayio.release_displays()
# oled_reset = board.D9

# Use for I2C
i2c = board.I2C()
display_bus = displayio.I2CDisplay(i2c, device_address=0x3C)
# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
sht = adafruit_shtc3.SHTC3(i2c)

# SH1107 is vertically oriented 64x128
WIDTH = 128
HEIGHT = 64
BORDER = 2

display = adafruit_displayio_sh1107.SH1107(
    display_bus, width=WIDTH, height=HEIGHT, rotation=0
)

# Make the display context
splash = displayio.Group()
display.show(splash)

color_bitmap = displayio.Bitmap(WIDTH, HEIGHT, 1)
color_palette = displayio.Palette(1)
color_palette[0] = 0xFFFFFF  # White

#bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
#splash.append(bg_sprite)

# Draw a smaller inner rectangle in black
#inner_bitmap = displayio.Bitmap(WIDTH - BORDER * 2, HEIGHT - BORDER * 2, 1)
#inner_palette = displayio.Palette(1)
#inner_palette[0] = 0x000000  # Black
#inner_sprite = displayio.TileGrid(
#    inner_bitmap, pixel_shader=inner_palette, x=BORDER, y=BORDER
#)
#splash.append(inner_sprite)

# Draw some white squares
#sm_bitmap = displayio.Bitmap(8, 8, 1)
#sm_square = displayio.TileGrid(sm_bitmap, pixel_shader=color_palette, x=58, y=17)
#splash.append(sm_square)

#med_bitmap = displayio.Bitmap(16, 16, 1)
#med_square = displayio.TileGrid(med_bitmap, pixel_shader=color_palette, x=71, y=15)
#splash.append(med_square)

#lrg_bitmap = displayio.Bitmap(32, 32, 1)
#lrg_square = displayio.TileGrid(lrg_bitmap, pixel_shader=color_palette, x=91, y=28)
#splash.append(lrg_square)

# Draw some label text
#text1 = "0123456789ABCDEF123456789AB"  # overly long to see where it clips
#text_area = label.Label(terminalio.FONT, text=text1, color=0xFFFFFF, x=8, y=8)
#splash.append(text_area)
#text2 = "SH1107"
#text_area2 = label.Label(
#    terminalio.FONT, text=text2, scale=2, color=0xFFFFFF, x=9, y=44
#)
#splash.append(text_area2)

while True:
    temperature, relative_humidity = sht.measurements
    print("Temperature: %0.1f C" % temperature)
    print("Humidity: %0.1f %%" % relative_humidity)
    print("")

    text1 = "Temperature: %0.1f C" % temperature
    text_area = label.Label(terminalio.FONT, text=text1, color=0xFFFFFF, x=8, y=8)
    splash.append(text_area)

    text2 = "Humidity: %0.1f %%" % relative_humidity
    text_area2 = label.Label(
    terminalio.FONT, text=text2, scale=1, color=0xFFFFFF, x=9, y=44
    )
    splash.append(text_area2)
    time.sleep(1)


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

Re: Variables on OLED overwriting previous value

Post by dastels »

You are creating a new Label and adding it to splash each time through the loop.
Labels have a transparent background so all the labels (and their text) that you previously added show through. You'll also eventually run out of memory.
Instead, create/add the Label before the loop and update its text value inside the loop. Something like this:

Code: Select all

text_area = label.Label(terminalio.FONT, text="", color=0xFFFFFF, x=8, y=8)
splash.append(text_area)

count = 0.0
while True:
    count = count + 0.1
    text1 = "Count: %0.1f C" % count
    print(text1)
    text_area.text = text1
    time.sleep(1)
Dave

User avatar
Stumpy_L
 
Posts: 86
Joined: Sun Jan 14, 2018 12:31 pm

Re: Variables on OLED overwriting previous value

Post by Stumpy_L »

Terrific, I knew there had to be a better way. I'll give that a try tonight when I get home.

Thanks

User avatar
Stumpy_L
 
Posts: 86
Joined: Sun Jan 14, 2018 12:31 pm

Re: Variables on OLED overwriting previous value

Post by Stumpy_L »

Perfect, Once again you've helped me an educated me

Thanks

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

Re: Variables on OLED overwriting previous value

Post by dastels »

It's what I'm here for :)

Dave

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

Return to “Adafruit CircuitPython”