Addressing pixels of Unicorn Phat wierdness

I might be going crazy but it seems like the Python library for addressing pixels for the Unicorn Phat is messed up… either that or there is something odd about my phat.

If I use this code to light up the corner pixels:

import unicornhat as uh

uh.set_pixel(0,7,0,255,0) # green
uh.set_pixel(3,7,255,255,0) # yellow
uh.set_pixel(0,0,255,0,0) #red
uh.set_pixel(3,0,0,0,255) # blue
uh.show()

I get this - the opposite corners are the wrong way round. If red is 0,0 then there’s no way blue (3,0) should be opposite:

If I try the same code with an old school UnicornHAT it works correctly:

or am I missing something?

The pixel order between Unicorn HAT and Unicorn pHAT is physically different, so mappings needed to change in code to account for this.

First update to the latest library: sudo pip install unicornhat --upgrade

Then use the magic sekret sauce:

uh.set_layout(uh.PHAT)

And things should start to make sense.

I had a similar experience. I think one of the issues is that if you go more than the “4” or “8” wide, it just continues onto the next row/column. There really isn’t any definitive help for this module yet, it seems.

I recreated this as.

import unicornhat as uh
uh.set_layout(uh.PHAT)
uh.brightness(0.5)

uh.set_pixel(0,0,255,0,0) # red - top left
uh.set_pixel(7,0,0,255,0) # green - top right
uh.set_pixel(0,3,0,0,255) # blue - bottom left
uh.set_pixel(7,3,255,255,0) # yellow - bottom right

uh.show()

(the bottom right is yellower in real life).

ha ha… or have I got it upside down?

From recollection, that shouldn’t be the case, but I’d have to look into it. To make things as compatible as possible with Unicorn HAT I chose to simply ignore any set_pixel calls that fall outside of the usable pHAT area, so a Unicorn HAT example can be run with just the set_layout call and not spew countless errors or abort immediately.

Ok - I’m still having trouble and I’m back to basics. I think one of the problems around this pHAT is that the documentation and examples are all based around the 8x8 HAT. I’m even questioning which was is up, which is x and which is y. Is it 0 to 3 or 0 to 4, etc.
Can you provide any more documentation or insight please?

The PHAT origin, (0,0) is the LED nearer the top of the Pi header, i.e right to the ‘.com’ . The x axis runs alongside the header, from 0 to 7, and the y axis down, from 0 to 3.

I can confirm the y value will wrap around once, therefore from 0 to 7, which I think is the best way to handle compatibility with routines made for the HAT. Values above 7 will be ignored on both axis.

… I’ll check with @gadgetoid tomorrow if that’s the intended behaviour, but at least it made sense to me. That’s not to say documenting all of this in the repo shouldn’t be done - I’ll sketch a pull request and get the ball rolling!