Adafruit 8x8 LED matrix. More compact code?

Questions and discussion about running MicroPython on a micro:bit board.
Target audience: MicroPython users with a micro:bit.
BruinBear
Posts: 27
Joined: Sat Jan 14, 2017 11:59 am

Adafruit 8x8 LED matrix. More compact code?

Post by BruinBear » Wed Feb 01, 2017 10:11 pm

Hello

I am using deshipu's driver for the HT16K33 on the Adafruit backpack for their 8x8 LED matrix. To make a smiley face, I am using the following code but I was getting a memory error when compiling. I chopped out the unused bits from deshipu's driver (which is why I added an x to the name) and the code works. My question is, is there a more memory-compact way of displaying a somewhat random matrix of individual pixels like a smiley face?

Code: Select all

from microbit import i2c, sleep
from ht16k33x import Matrix8x8
# address of HT16K33 is 0x70
display = Matrix8x8(i2c, address=0x70)

# Clear the display
display.fill(0x00)
display.show()
sleep(1000)

# display smiley face
display.pixel(2,0,1)
display.pixel(3,0,1)
display.pixel(4,0,1)
display.pixel(5,0,1)

display.pixel(1,1,1)
display.pixel(6,1,1)

display.pixel(0,2,1)
display.pixel(2,2,1)
display.pixel(5,2,1)
display.pixel(7,2,1)

display.pixel(0,3,1)
display.pixel(7,3,1)

display.pixel(0,4,1)
display.pixel(2,4,1)
display.pixel(5,4,1)
display.pixel(7,4,1)

display.pixel(0,5,1)
display.pixel(3,5,1)
display.pixel(4,5,1)
display.pixel(7,5,1)

display.pixel(1,6,1)
display.pixel(6,6,1)

display.pixel(2,7,1)
display.pixel(3,7,1)
display.pixel(4,7,1)
display.pixel(5,7,1)

display.show()
sleep (30000)

# Clear the display
display.fill(0x00)
display.show()
sleep(1000)

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Adafruit 8x8 LED matrix. More compact code?

Post by deshipu » Thu Feb 02, 2017 12:13 am

You can encode the picture in a more efficient way:

Code: Select all

image = (
    0b01111110,
    0b10000001,
    0b10100101,
    0b10000001,
    0b10100101,
    0b10011001,
    0b10000000,
    0b01111110,
)
for y, line in enumerate(image):
    for x in range(8):
        if line & (1 << x):
            display.pixel(x, y, 1)

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Adafruit 8x8 LED matrix. More compact code?

Post by deshipu » Thu Feb 02, 2017 1:02 am

Of course, you can print the "image" tuple once you design your image, to get a even more space-efficient representation of it:

Code: Select all

>>> image = (
...     0b01111110,
...     0b10000001,
...     0b10100101,
...     0b10000001,
...     0b10100101,
...     0b10011001,
...     0b10000001,
...     0b01111110,
... )
>>> print(image)
(126, 129, 165, 129, 165, 153, 129, 126)
And you can make it even more efficient:

Code: Select all

>>> print(repr(bytearray(image)))
bytearray(b'~\x81\xa5\x81\xa5\x99\x81~')
>>> image = b'~\x81\xa5\x81\xa5\x99\x81~'
>>> for y, line in enumerate(bytearray(image)):
...     for x in range(8):
...         if line & (1 << x):
...             display.pixel(x, y, 1)

BruinBear
Posts: 27
Joined: Sat Jan 14, 2017 11:59 am

Re: Adafruit 8x8 LED matrix. More compact code?

Post by BruinBear » Mon Feb 27, 2017 6:45 pm

Thanks again deshipu. I'm gradually getting the hang of Python "arrays". Sorry I was slow to say "thank you" - I didn't notice that you had replied.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Adafruit 8x8 LED matrix. More compact code?

Post by pythoncoder » Tue Feb 28, 2017 12:29 pm

Even more efficient, discard the bytearray ;)

Code: Select all

for y, line in enumerate(image):
    for x in range(8):
        if line & (1 << x):
            display.pixel(x, y, 1)
Peter Hinch
Index to my micropython libraries.

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Adafruit 8x8 LED matrix. More compact code?

Post by deshipu » Tue Feb 28, 2017 6:08 pm

That gives you characters, and not integers, though.

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: Adafruit 8x8 LED matrix. More compact code?

Post by SpotlightKid » Tue Feb 28, 2017 6:32 pm

If you access single bytes of a bytes instance (the Python 3 variant) by indexing or iteration, you get integers, not bytes.

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Adafruit 8x8 LED matrix. More compact code?

Post by deshipu » Tue Feb 28, 2017 7:44 pm

Code: Select all

>>> b'abc'[0]
'a'

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Adafruit 8x8 LED matrix. More compact code?

Post by deshipu » Tue Feb 28, 2017 7:45 pm

Ah, sorry, that was the wrong python version. You are right.

BruinBear
Posts: 27
Joined: Sat Jan 14, 2017 11:59 am

Re: Adafruit 8x8 LED matrix. More compact code?

Post by BruinBear » Thu Mar 02, 2017 11:07 am

deshipu

Your driver works great but, compared with other drivers/code examples, the rows and columns are transposed. Most other examples are oriented with the Adafruit logo at the bottom and the pins at the top. My smiley face example has the Adafruit logo on the right and the pins on the left. Here's a photo showing the same type of display connected to a micro:bit and also a Picaxe processor turning on what should be the same pixels.
IMG_0803.JPG
IMG_0803.JPG (187.94 KiB) Viewed 9009 times

Post Reply