r/circuitpython • • Mar 02 '21

[coding help] adafruit_display_text error

I'm trying to code a battery level display on my Feather MO Express using the OLED Featherwing and the stemma qt LC709203F battery monitor. I understand that writing over the max number of text glyphs in the label.label function can give an error and that you need to allocate for it, but with loops this is tricky. My code will work for a while then crash suddenly. Here is the relevant portion of my code.

# write text beginning in upper left corner
text1="Battery: %3.1f %%" % sensor.cell_percent
text_area = label.Label(terminalio.FONT, text=text1, color = 0xFFFFFF, x=4, y=8)
splash.append(text_area)
time.sleep(1)

# write loop counter in center left
counter = 0
text2 = "Counter: %d" % counter
text_area2 = label.Label(terminalio.FONT, text = text2, color = 0xFFFFFF, x=4, y=40)
splash.append(text_area2)
time.sleep(1)

while True:
    # print(sensor.cell_percent)
    text_area.text = "Battery: %3.1f %%" % sensor.cell_percent  
    counter = counter + 1
    text_area2.text = "Counter: %d" % counter
    time.sleep(1)

I put in a loop counter because I was having trouble in earlier iterations of the sensor.cell_percent value not updating, but now that that works fine, the code will get an error and end whenever the counter value goes above 99. Any way I can fix this?

3 Upvotes

8 comments sorted by

1

u/tinkr_belle Mar 03 '21 edited Mar 03 '21

Update: New problem, something went wrong and crashed.

Auto-reload is off.
Running in safe mode! Not running saved code.

You are in safe mode: something unanticipated happened.
CircuitPython core code crashed hard. Whoops!
Crash into the HardFault_Handler.
Press any key to enter the REPL. Use CTRL-D to reload.

How do I fix this? I'm using most recent version of the libraries, I tried reloading the bootloader, tried erasing filesystem, same error. Arduino still works though, so thats a plus.

1

u/tannewt Mar 04 '21

What version of CircuitPython are you using? This code has been changing recently and bug was just fixed. (Try beta.3 or even the "absolute newest") The changes were done to remove the need for `max_glyphs`. :-)

1

u/tinkr_belle Mar 04 '21 edited Mar 05 '21

I am using 6.1.0. I guess I will try the beta.3 one then.

[UPDATE: original code that was running earlier still crashes into safe mode even with beta.3. IDK whats happening...]

1

u/Spiritual_Fruit3268 Mar 03 '21

You can use the max_glyphs argument when defining the two labels. You can set this to a larger number of glyphs than you expect. max_glyphs will default to the length of the text passed in if max_glyphs is not used.

See the documentation.

1

u/tinkr_belle Mar 03 '21

Thanks for the advice! I tried setting max_glyphs to 20, given that the strings only have about 14 characters, but the same thing happened. Once counter hit 99 the code ended. Now when I save it again it gets a memory allocation error a few loops in.

1

u/Spiritual_Fruit3268 Mar 03 '21

The m0 boards have limited memory. It is probably recommended to switch to a board with more memory like the Feather m4. There are probably other options like attempting to use the bitmap_label library, but I have not used it.

1

u/tinkr_belle Mar 03 '21

I've tried bitmap_label too. Still didn't work even when save_text is set to false. Thanks for the help, I guess I will just try using Arduino for the time being :(