0

How to get static string data into my Trinket M0
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

How to get static string data into my Trinket M0

by piemonkey on Sat Apr 10, 2021 10:42 am

I'm trying to program a Trinket M0 to run a project I've built using a small Adafruit thermal printer to print random messages. Every way I've tried to get the string data in has come up across some limitation of the board, since the memory onboard is more than enough to hold the ~200 tuples of line-wrapped strings (each tuple being a message and containing 1-4 lines of 16 chars), is there a good way to import this? Things I've tried so far:

1. Put the messages in a list in a .py file and import it from code.py: Out of memory error trying to import the module.
2. As 1) but compile .py to .mpy: Out of memory error
3. Cut down the number of possible messages using 1) or 2): Limits me to ~20 messages which isn't enough to be interesting for the project. Any more than this and I get a memory error
4. Write a tiny script to split the messages each into their own .py file and add another .py file which uses `__import__()` to randomly import one of them: Works except each file I add to the board seems to take up 512B on the board, despite being ~20-50B in size, so I can only add ~50 messages. Better than 3) but still limited.

Any suggestions for this that don't involve re-writing it for Arduino?

piemonkey
 
Posts: 2
Joined: Sat Apr 10, 2021 10:21 am

Re: How to get static string data into my Trinket M0

by dastels on Sat Apr 10, 2021 4:21 pm

The problem is that the board has 2 types of memory. The most limiting is RAM. And you python code has to get loaded into ram. Each string isn't just a sequence of characters as they'd be in C++; they get wrapped in a Python object which takes more space in RAM. If you put those into a list, that's even more memory used.

I would suggest putting the strings in a file on CIRCUITPY (i.e. in flash of which there is more) and loading strings from there. That could end up taking up more memory for the code to do that than you'd save. If you kept each entry the same length, it would simplify things greatly. You could just seek in the file to QUOTE_SIZE * quote_number and then read QUOTE_SIZE characters into a string to display.

Dave

dastels
 
Posts: 6519
Joined: Tue Oct 20, 2015 3:22 pm

Re: How to get static string data into my Trinket M0

by piemonkey on Sun Apr 11, 2021 9:52 am

Thanks, I thought about that but found another way to implement, though it involved playing a little with python's internals. This had the advantage that I could keep things like the line wrapping without having to come up with a way of encoding this in a file.

The solution I went with and that works is pre-splitting the messages into groups of 10 in a module each. Then, to pick a message I randomly pick a module (they're named e.g. `messages5.py`), importing it using `__import__()`, but based on this post in the Micropython forums, importing to a fresh dictionary (`{}`) instead of the global namespace, then picking a random one of the messages from the module and deleting the reference to the module from `sys.modules` allowing it to be garbage collected. Weirdly the `gc.threshold()` method doesn't seem to be implemented on the Trinket, so I had to manually check for free memory and run a gc.

Retrospectively, maybe figuring out how to add frozen modules would have been easier, but now I know a little more about how Python imports modules...

piemonkey
 
Posts: 2
Joined: Sat Apr 10, 2021 10:21 am

Re: How to get static string data into my Trinket M0

by dastels on Sun Apr 11, 2021 2:03 pm

Clever.

Dave

dastels
 
Posts: 6519
Joined: Tue Oct 20, 2015 3:22 pm

Please be positive and constructive with your questions and comments.