Hello.
I'm new to the Raspberry Pi Pico, I had one before but never did much with it besides Hello World, but I got one this year for Christmas and after soldering the pins I have spent the past few days trying things out on it and getting to understand how it works. I've been using the Waveshare LCD 3.5 inch Touch Display.
A couple days ago, I made a basic interface displaying how much percentage the battery has left and the current time. However, I wanted an icon for the battery containing "milestones" if you like at 0%, 25%, 50%, 75%, and 100%, that would display depending on the milestone the percentage was closest to (e.g. 80% would display the battery with 3/4 of the charge left).
Yesterday, I made a bitmap image renderer for the Pico as a means to resolve this, and designed some low res battery icons. I've setup the bitmap renderer to store the pixels of the given image in a dictionary under a name, which can then be called to render at any time without having to read the file again and again. The idea behind this was to minimise how often the script would have to interact with the SD card and to stop it wasting time on decoding the image over and over again when really it only needed to be done once.
Here's the basic structure of my bitmap renderer. If you need more, you can find the full code on GitHub.
Now back to my interface. It continually wipes clean and redraws the time and battery percentage text and the corresponding battery bitmap. All five battery bitmaps have been decoded. It also has an FPS limit which I have set to 20 (generally I won't get more than 22 FPS with no limits, which is fine and expected).
The bitmap files for the battery are stored on a microSD card for which there is a slot built into my display, rather convenient. For the SD card I use this module. Before the loop begins, I mount the microSD using the following:
This causes extreme lag through the script. My FPS drops from a solid 18-20 to 1-2 FPS. It is noticeable too, sometimes the time misses a second or the Pico freezes altogether. This is even if I don't do any bitmap rendering or decoding. If I unmount the SD card using uos.umount the lag persists. If I set sd to None after initialising it continues. The line "sd = sdcard.SDCard(spi, Pin(22))" causes severe lag. If I remove the code connecting to the SD entirely the lag goes away.
I put the problem down to memory at first as the Pico only has half a megabyte and I assumed maybe the SD object was clogging the memory. However, adding print(gc.mem_free()) to the end of the loop gives me around 200-300KB, which is over half of available memory, so that can't be the cause. The memory outputs are around the same both with and without the SD code.
Could anyone here help me understand why simply connecting to the SD card is causing my Pico to lag so much, and even better suggest potential fixes? I know the Pico is low spec but I'd expect it to be able to render a 16x16 black and white image and display the time without lagging this badly, given I've seen a handheld games console powered by it and by some of the images these games are way more advanced graphics wise than what I'm trying to do.
I'm new to the Raspberry Pi Pico, I had one before but never did much with it besides Hello World, but I got one this year for Christmas and after soldering the pins I have spent the past few days trying things out on it and getting to understand how it works. I've been using the Waveshare LCD 3.5 inch Touch Display.
A couple days ago, I made a basic interface displaying how much percentage the battery has left and the current time. However, I wanted an icon for the battery containing "milestones" if you like at 0%, 25%, 50%, 75%, and 100%, that would display depending on the milestone the percentage was closest to (e.g. 80% would display the battery with 3/4 of the charge left).
Yesterday, I made a bitmap image renderer for the Pico as a means to resolve this, and designed some low res battery icons. I've setup the bitmap renderer to store the pixels of the given image in a dictionary under a name, which can then be called to render at any time without having to read the file again and again. The idea behind this was to minimise how often the script would have to interact with the SD card and to stop it wasting time on decoding the image over and over again when really it only needed to be done once.
Here's the basic structure of my bitmap renderer. If you need more, you can find the full code on GitHub.
Code:
class BitmapManager():def __init__(self):self.bitmaps = {}def decode(self, path, name):# decode the imageself.bitmaps[name] = decoded_imagedef render(self, LCD, name, start_x = 0, start_y = 0, scale = 1):image = self.bitmaps[name]# render image on LCD
The bitmap files for the battery are stored on a microSD card for which there is a slot built into my display, rather convenient. For the SD card I use this module. Before the loop begins, I mount the microSD using the following:
Code:
cs = Pin('LED', Pin.OUT)spi = SPI(1, baudrate=50000000, sck=Pin(10), mosi=Pin(11), miso=Pin(12))sd = sdcard.SDCard(spi, Pin(22))vfs = uos.VfsFat(sd)uos.mount(vfs, "/sd")
I put the problem down to memory at first as the Pico only has half a megabyte and I assumed maybe the SD object was clogging the memory. However, adding print(gc.mem_free()) to the end of the loop gives me around 200-300KB, which is over half of available memory, so that can't be the cause. The memory outputs are around the same both with and without the SD code.
Could anyone here help me understand why simply connecting to the SD card is causing my Pico to lag so much, and even better suggest potential fixes? I know the Pico is low spec but I'd expect it to be able to render a 16x16 black and white image and display the time without lagging this badly, given I've seen a handheld games console powered by it and by some of the images these games are way more advanced graphics wise than what I'm trying to do.
Statistics: Posted by ChasTech — Sat Dec 28, 2024 4:47 pm