adafruit / Adafruit_CircuitPython_ProgressBar

Dynamic progress bar widget for CircuitPython displays
MIT License
6 stars 9 forks source link

Progress Bar slows down progressively #7

Closed gfbarros closed 4 years ago

gfbarros commented 4 years ago

With the example slightly modified to work with the Adafruit FeatherWing OLED screen, I see the progress bar progression across the screen slow down as the percentage increases. Any pointers on how to troubleshoot this?

Here is a video: https://youtu.be/B2AHjPbJh7c

And the code:

import time
import board
import displayio
import terminalio
from adafruit_bitmap_font import bitmap_font
from adafruit_display_text import label
import adafruit_displayio_ssd1306
from adafruit_progressbar import ProgressBar

displayio.release_displays()

i2c = board.I2C()
display_bus = displayio.I2CDisplay(i2c, device_address=0x3C)
display = adafruit_displayio_ssd1306.SSD1306(display_bus, width=128, height=32)

splash = displayio.Group(max_size=10)
display.show(splash)

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

progress_bar = ProgressBar(0, 0, 128, 32, 1.0)

splash.append(progress_bar)

textbox_bitmap = displayio.Bitmap(80,20,1)
textbox_palette = displayio.Palette(1)
textbox_palette[0] = 0x000000 #Black
textbox_sprite = displayio.TileGrid(textbox_bitmap, pixel_shader=textbox_palette,x=25, y=5)
splash.append(textbox_sprite)

text = "Fuel Pumping!"
text_area = label.Label(terminalio.FONT, text=text, color=0xFFFFFF, x=27, y=14)
splash.append(text_area)

current_progress = 0.0
while True:
    while current_progress <= 1.0:
        print("Progress: {}%".format(current_progress * 100))
        progress_bar.progress = current_progress
        current_progress += 0.01
        if current_progress >= 1.0:
            current_progress = 0.0
        time.sleep(0.01)
FoamyGuy commented 4 years ago

The youtube link is having a problem, it points to a github page instead of youtube. Copy pasting the youtube URL worked though. I think I see what you mean about it slowing down toward the end.

The fact that the decimals in your print outs change from .00 to .9999 seems odd to me I wonder if that is related to the slowed performance.

Another thing I've noticed is that printing takes a relatively long time compared to many other operations. You might try removing (or commenting) the print statement and see if it has an impact on the performance.

I can test this out later tonight on some other devices to see if I can replicate the issue and try to understand what could be causing it.

Edit: I took a quick peek at the code. At first glance I think this for loop might be responsible for the slowed performance: https://github.com/adafruit/Adafruit_CircuitPython_ProgressBar/blob/48bea3680e8a5a73758f8021803b10d75693fff8/adafruit_progressbar.py#L125

If I am understanding that section of the code correctly I think it means that it is filling in the entire progress bar each time you update, even the section to the left of the new value that was already filled in gets filled in again. Since larger progresses require more pixels to be filled during this progress it takes longer.

gfbarros commented 4 years ago

Quick update, tried it without the print with the same outcome.

tannewt commented 4 years ago

I think @FoamyGuy is right on the money. The code could only modify the new pixels. If it had refresh control it could also pause refreshes.

It could also not use a bitmap and save memory.

FoamyGuy commented 4 years ago

I have created PR #8 that attempts to resolve this issue. @gfbarros if you have a spare moment it would be great if you can try out the code from that PR and see if it is increasing the performance in your project.

gfbarros commented 4 years ago

Happy to. I haven't built mpy's before so I'll look into how to do that this weekend. Thanks!

gfbarros commented 4 years ago

I tested it and it no longer has the slowdown, thanks! I'm a bit confused by 100% now being 21, could you explain? This print output is a bit wonky as well, I added a *100 which fixed it.

gfbarros commented 4 years ago

This is resolved my merged PR #8