[Microbit-Python] Image is hard to use

Larry Hastings larry at hastings.org
Wed Oct 7 07:59:07 CEST 2015


On 10/06/2015 03:05 PM, Damien George wrote:
> On Mon, Oct 5, 2015 at 8:46 AM, Larry Hastings <larry at hastings.org> wrote:
>> I can only create so many Image objects before I get an 020 / the thing
>> seizes up.
> Yes, this is because the microbit is very low on RAM, as many of us
> have established by now!
>
> In this case the problem is exacerbated by the fact that there are 2
> heaps: the DAL heap and the uPy heap.  When you create an image in
> Python it makes an image object on the uPy heap, but then also makes
> the image C++ object in the DAL heap.

I just tried this on a freshly reset micro:bit with stock firmware:

    import microbit as m
    initializer = '0,0,0,0,0\n'*5
    for i in range(100):
         print(i)
         image = m.Image(initializer)
         del image


Any guesses as to the last number printed?  45.  So we were able to 
create 45 bitmap objects before we ran out of memory.

What kills me about this is that I'm /cleaning them up./  Why can't I 
create an image and immediately destroy it in a loop /forever?/ This 
strongly implies to me that there's something leaking.


As for the two heaps: so, you have a stack and two heaps in 16k of memory.


>> Change the repr so it prints the internal array of pixels.
> How exactly do you want it to be printed, like a bytearray?

Right now I get this:

     >>> import microbit as m
     >>> m.Image('0,255,0,0,0\n'*5)
    0,255,0,0,0
    0,255,0,0,0
    0,255,0,0,0
    0,255,0,0,0
    0,255,0,0,0

How about, throw a "<class MicroBitImage" and ">" around that.


If you expose .clear(), .set(), and .paste() (with the alpha parameter) 
I'd be pretty happy.  I'd also like .fill(value) and an alternate Image 
constructor form that lets me specify width and height and creates a 
black (all zeroes) bitmap.  If I had all that I could do all sorts of 
things.  For example, I could create my 11x11 window, empty it, draw on 
the square, then copy it to a second 11x11, shift it left and up, and 
boom! virtual window is done.

.paste() is a bad name there though.  But .copy() has a well-established 
meaning in Python.  .stamp(), as in the verb?


//arry/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/mailman/private/microbit/attachments/20151006/95690c18/attachment-0001.html>


More information about the Microbit mailing list