pimoroni-pico icon indicating copy to clipboard operation
pimoroni-pico copied to clipboard

unicorn pack micropython does not expose .clear()

Open byronczimmer opened this issue 3 years ago • 6 comments
trafficstars

When reviewing the demos for micropython for the pico's unicorn pack, the clear function is hand rolled:

Clear the display

for x in range(w): for y in range(h): picounicorn.set_pixel(x, y, 0, 0, 0)

The C library looks like it has a _clear() function, but it isn't exposed to micropython.

Can it be?

byronczimmer avatar Jul 23 '22 00:07 byronczimmer

Well, that's odd!

picounicorn does have a clear() function - https://github.com/pimoroni/pimoroni-pico/blob/0f090d8999a8c07bf00336c4602d21decab476ac/micropython/modules/pico_unicorn/pico_unicorn.c#L37

I'd venture that the examples were written before it was added... but that doesn't seem to be the case!

Gadgetoid avatar Jul 25 '22 15:07 Gadgetoid

This may be more than just documentation.

If I replace the hand rolled 'clear' in micropython with .clear() -- the display flashes when the "A" button is pressed ( like it does when you first start the program) but does not clear the display.

# Clear the display
#for x in range(w):
#    for y in range(h):
#        picounicorn.set_pixel(x, y, 0, 0, 0)

picounicorn.clear()

I then checked the picounicorn module in a very basic myropython script:

import picounicorn
print(dir(picounicorn))
>>> %Run -c $EDITOR_CONTENT
['__class__', '__name__', 'clear', 'BUTTON_A', 'BUTTON_B', 'BUTTON_X', 'BUTTON_Y', 'get_height', 'get_width', 'init', 'is_pressed', 'set_pixel', 'set_pixel_value']

MP has 'clear' in there... but it isn't working. Or am I calling it incorrectly?

I have tried: .clear() .clear(0) .clear(1)

None of them actually blank out the display.

byronczimmer avatar Jul 26 '22 06:07 byronczimmer

It's been a while since I've been in C code, but should the module declaration or the picounicorn_clear_obj be 'picounicorn_clear' and not 'picounicorn_init'. All the other module setups look syntactically similar to the object they're preparing.

https://github.com/pimoroni/pimoroni-pico/blob/0f090d8999a8c07bf00336c4602d21decab476ac/micropython/modules/pico_unicorn/pico_unicorn.c#L25

/***** Module Functions *****/
STATIC MP_DEFINE_CONST_FUN_OBJ_0(picounicorn_init_obj, picounicorn_init);
STATIC MP_DEFINE_CONST_FUN_OBJ_0(picounicorn_get_width_obj, picounicorn_get_width);
STATIC MP_DEFINE_CONST_FUN_OBJ_0(picounicorn_get_height_obj, picounicorn_get_height);
//STATIC MP_DEFINE_CONST_FUN_OBJ_0(picounicorn_update_obj, picounicorn_update);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(picounicorn_set_pixel_obj, 5, 5, picounicorn_set_pixel);
STATIC MP_DEFINE_CONST_FUN_OBJ_3(picounicorn_set_pixel_value_obj, picounicorn_set_pixel_value);
STATIC MP_DEFINE_CONST_FUN_OBJ_0(picounicorn_clear_obj, picounicorn_init);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(picounicorn_is_pressed_obj, picounicorn_is_pressed);

byronczimmer avatar Jul 26 '22 06:07 byronczimmer

Oh wow, I glanced over the code for a bug like this and didn't see it. Good sleuthing. Thank you. You're right. I'll get this fixed!

Gadgetoid avatar Jul 26 '22 10:07 Gadgetoid

Test builds:

  • Pico W (.uf2 not .elf) - https://github.com/pimoroni/pimoroni-pico/actions/runs/2738936946
  • Non-W - https://github.com/pimoroni/pimoroni-pico/actions/runs/2738936945

Gadgetoid avatar Jul 26 '22 11:07 Gadgetoid

Version 1.19.6 from the official downloads page for pico:

MicroPython now executes the .clear() command when called.

Thanks Gadgetoid!

byronczimmer avatar Jul 27 '22 22:07 byronczimmer

Sounds like this is now sorted, so closing for now!

helgibbons avatar Sep 06 '22 13:09 helgibbons