Page 1 of 2

Adafruit 8x8 LED matrix. More compact code?

Posted: Wed Feb 01, 2017 10:11 pm
by BruinBear
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)

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

Posted: Thu Feb 02, 2017 12:13 am
by deshipu
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)

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

Posted: Thu Feb 02, 2017 1:02 am
by deshipu
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)

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

Posted: Mon Feb 27, 2017 6:45 pm
by BruinBear
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.

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

Posted: Tue Feb 28, 2017 12:29 pm
by pythoncoder
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)

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

Posted: Tue Feb 28, 2017 6:08 pm
by deshipu
That gives you characters, and not integers, though.

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

Posted: Tue Feb 28, 2017 6:32 pm
by SpotlightKid
If you access single bytes of a bytes instance (the Python 3 variant) by indexing or iteration, you get integers, not bytes.

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

Posted: Tue Feb 28, 2017 7:44 pm
by deshipu

Code: Select all

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

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

Posted: Tue Feb 28, 2017 7:45 pm
by deshipu
Ah, sorry, that was the wrong python version. You are right.

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

Posted: Thu Mar 02, 2017 11:07 am
by BruinBear
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 9053 times