I have a 16x16 NeoPixel-style Matrix & when I print one pixel after another my pattern is serpentine from upper left down/up/down, ending at upper right.
The Matrix Mapper’s default doesn’t seem to work for this pattern.
I wanted to write a more flexible default that could be tweaked depending on the layout of lights in a given matrix. Did I do this right?
Improved Matrix Mapper Code · GitHub
Please advise if I have any of this wrong, I’m doing something “non-standard” or I’m over complicating things.
Full code here:
function (pixelCount) {
width = 16 // width of LED panel
angle = 90 // rotation in degrees: 0, 90, 180, 270
flip = true // does the rendering need to be flipped?
height = width/pixelCount
map = []
for (i = 0; i < pixelCount; i++) {
y = Math.floor(i / width)
x = i % width
x = y % 2 == 1 ? width - 1 - x : x //zigzag
map.push([x, y])
}
if (flip) {
for (i = 0; i < map.length; i++) {
map[i][0] = (width - 1) - map[i][0]; // mirror X
}
}
// helper: apply 90°-step rotation
function rotate90(x, y, angle) {
if (angle === 90) return [y, width - 1 - x]
if (angle === 180) return [width - 1 - x, height - 1 - y]
if (angle === 270) return [height - 1 - y, x]
return [x, y] // 0° or default
}
// apply rotation of angle
for (i = 0; i < map.length; i++) {
p = rotate90(map[i][0], map[i][1], angle)
map[i][0] = p[0]
map[i][1] = p[1]
}
return map
}