Light Matrix
The LEGO 3x3 Color Light Matrix (45608) is nine pixels in a square. Each one lights in one of LEGO's colors, at a brightness from 0 to 10.
BuildHat comes from a library, not from the language, so this code won't build without it. In
IntelliJ, choose Tools | Nylon | Add Nylon Library… and pick build-hat. It brings the Raspberry
Pi library with it, and adds one line to your project's build.markup.code:
(+dependencies : RepositoryDependency coordinate={"com.grandtheorem:build-hat:0.3.0"} /)
Light the matrix
Plug the matrix into port D. Any port works, as long as the program names the same one.
Pi is the board, BuildHat is the HAT on top of it, Port names the HAT's ports, and Color names LEGO's colors.
A Picture is all nine pixels, drawn at once.
# A color, a pixel, a picture and a bar meter.
namespace lights
import grandtheorem.rpi.Pi
import grandtheorem.buildhat.BuildHat
import grandtheorem.buildhat.Port
import grandtheorem.buildhat.Color
import grandtheorem.buildhat.Picture
import namespace nylon.time
BuildHat.connect starts the HAT, and lightMatrix gives the matrix on a port, switched on and clear.
# The Light Matrix in port D.
entrypoint.default main() {
let hat = BuildHat.connect(Pi)
let matrix = hat.lightMatrix(Port.D)
fill sets every pixel, and pixel sets one, at x and y, each 0 to 2. Each call shows at once.
# All blue, then the middle red.
matrix.fill(Color.Blue, 10)
sleep(2.seconds())
matrix.pixel(1, 1, Color.Red, 10)
sleep(2.seconds())
draw shows a whole Picture in one go, so the matrix changes without flicker. level turns it into a bar meter,
from 0 to 9.
# Dim white with a red middle, drawn
# at once, then a meter rising to full.
matrix.draw(Picture().fill(Color.White, 3)
.pixel(1, 1, Color.Red, 10))
sleep(2.seconds())
var height = 0
while (height <= 9) {
matrix.level(height)
sleep(300.milliseconds())
height = height + 1
}
matrix.clear()
}
The matrix lights blue, then its middle pixel turns red. Next it shows dim white with a red middle, then a bar meter rises to full, and it clears.
Everything the matrix can do
| Call | What it does |
|---|---|
hat.lightMatrix(port) | The Light Matrix on a port, switched on and clear |
matrix.fill(color, brightness) | Every pixel color, at brightness 0 to 10 |
matrix.pixel(x, y, color, brightness) | One pixel, at x and y, each 0 to 2; the others stay as they are |
matrix.draw(picture) | All nine pixels at once, from a Picture |
matrix.level(height) | A bar meter, 0 to 9, in the matrix's own green |
matrix.transition(how) | How the next changes appear: MatrixTransition.Immediate, Wipe or Fade. It clears the matrix |
matrix.clear() | Every pixel off |
Picture().fill(color, brightness) | A picture with every pixel color, to build on |
picture.pixel(x, y, color, brightness) | The picture with one pixel set |
Color is one of LEGO's colors: Color.Black, Pink, Lilac, Blue, Cyan, Turquoise, Green, Yellow,
Orange, Red and White. MatrixTransition comes from grandtheorem.buildhat, as Color does.
Good to know
A brightness past 0 to 10 is that end. 15 is full brightness, and -1 is off.
A pixel off the matrix ends the run. An x or y past 0 to 2 ends it, naming the pixel.
The middle pixel is (1, 1). Which corner is (0, 0) depends on which way the matrix faces, so light one corner and look before you draw a pattern that needs it.
The wrong part on the port ends the run. If port D has a motor instead, the run ends and says what's there.
Boards: every board on Boards.