pimoroni-pico
pimoroni-pico copied to clipboard
unicorn pack micropython does not expose .clear()
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?
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!
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.
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);
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!
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
Version 1.19.6 from the official downloads page for pico:
MicroPython now executes the .clear() command when called.
Thanks Gadgetoid!
Sounds like this is now sorted, so closing for now!