micropython icon indicating copy to clipboard operation
micropython copied to clipboard

Can extra functions be exposed

Open mrpotes opened this issue 9 years ago • 11 comments

It's been a long time since I used C/C++, and I'm pretty new to Python, but I notice that inc/microbit/microbitimage.h has a function declaration, microbit_image_for_char, that sounds like just what I need - could it be exposed as a python function for turning a single character into the LED image string, or maybe a string into an array of LED image strings?

Thanks :)

mrpotes avatar Jan 15 '17 18:01 mrpotes

is SHOW 'a' not sufficient?

ZanderBrown avatar Jan 15 '17 18:01 ZanderBrown

You can do better:

from microbit import Image

letter_a = Image('A')

deshipu avatar Jan 15 '17 19:01 deshipu

No, we need to modify the LED arrangement so that it will read correctly in a mirror, but start from a normal letter representation to do so, so it would be great not to have to reproduce something the microbit already knows how to do.

mrpotes avatar Jan 15 '17 19:01 mrpotes

Why do you need a string for it? Can't you invert the image?

deshipu avatar Jan 15 '17 19:01 deshipu

@deshipu does Image('A') work? The docs say:

If string is used, it has to consist of digits 0-9 arranged into lines, describing the image

mrpotes avatar Jan 15 '17 19:01 mrpotes

If there was only some way of checking if it works...

deshipu avatar Jan 15 '17 19:01 deshipu

@deshipu we want the text to scroll just like it does if you use the scroll function, but mirrored. As I understand it, invert inverts the brightness of the image (0 -> 9, 1 -> 8, etc), it doesn't do flip horizontally (or vertically, for that matter)

mrpotes avatar Jan 15 '17 19:01 mrpotes

If there was only some way of checking if it works...

If only the microbits weren't locked away in a cupboard at school, you mean?

mrpotes avatar Jan 15 '17 20:01 mrpotes

Yes, you will have to write the function that mirrors the image yourself. But it should be easier and faster with an image than with a string.

An efficient way to do it is to only iterate over half of the image, and swap its pixels with the other half. This way you can do it in place, without having to create a new image and allocate memory. Something like:

def mirror(image):
    for y in range(image.height()):
        for x in range(image.width() // 2):
            other_x = image.width() - x -1
            pixel = image.get_pixel(x, y)
            image.set_pixel(x, y, image.get_pixel(other_x, y))
            image.set_pixel(other_x, y, pixel)

deshipu avatar Jan 15 '17 20:01 deshipu

If only the microbits weren't locked away in a cupboard at school, you mean?

I'm sorry, I didn't think.

deshipu avatar Jan 15 '17 20:01 deshipu

I'm sorry, I didn't think.

No problem :)

So if Image('A') works that's great, then I'm thinking something like (pseudo code as I haven't worked out all of this in python):

def scrollMirrored(str):
  show(
    chars(str)
      .map(c: repr(Image(c)).split(':'))
      .zip()
      .map(str_join)
      .map(s: s.reverse)
      .map(s: chars(str).slidingWindow(5))
      .zip()
      .map(arr: arr.join(':')))

mrpotes avatar Jan 15 '17 21:01 mrpotes