Skip to content

Commit 231ec57

Browse files
deadprogramconejoninja
authored andcommitted
microbitmatrix: matrix now working on microbit v2
Signed-off-by: deadprogram <[email protected]>
1 parent 5d3ad4b commit 231ec57

File tree

2 files changed

+159
-0
lines changed

2 files changed

+159
-0
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
// +build microbit_v2
2+
3+
// Package microbitmatrix implements a driver for the BBC micro:bit version 2 LED matrix.
4+
//
5+
// Schematic:
6+
//
7+
package microbitmatrix // import "tinygo.org/x/drivers/microbitmatrix"
8+
9+
import (
10+
"image/color"
11+
"machine"
12+
"time"
13+
)
14+
15+
var matrixRotations = [4][5][5][2]uint8{
16+
{ // 0
17+
{{0, 0}, {1, 0}, {2, 0}, {3, 0}, {4, 0}},
18+
{{0, 1}, {1, 1}, {2, 1}, {3, 1}, {4, 1}},
19+
{{0, 2}, {1, 2}, {2, 2}, {3, 2}, {4, 2}},
20+
{{0, 3}, {1, 3}, {2, 3}, {3, 3}, {4, 3}},
21+
{{0, 4}, {1, 4}, {2, 4}, {3, 4}, {4, 4}},
22+
},
23+
{ // 90 CCW
24+
{{4, 0}, {4, 1}, {4, 2}, {4, 3}, {4, 4}},
25+
{{3, 0}, {3, 1}, {3, 2}, {3, 3}, {3, 4}},
26+
{{2, 0}, {2, 1}, {2, 2}, {2, 3}, {2, 4}},
27+
{{1, 0}, {1, 1}, {1, 2}, {1, 3}, {1, 4}},
28+
{{0, 0}, {0, 1}, {0, 2}, {0, 3}, {0, 4}},
29+
},
30+
{ // 180
31+
{{4, 4}, {3, 4}, {2, 4}, {1, 4}, {0, 4}},
32+
{{4, 3}, {3, 3}, {2, 3}, {1, 3}, {0, 3}},
33+
{{4, 2}, {3, 2}, {2, 2}, {1, 2}, {0, 2}},
34+
{{4, 1}, {3, 1}, {2, 1}, {1, 1}, {0, 1}},
35+
{{4, 0}, {3, 0}, {2, 0}, {1, 0}, {0, 0}},
36+
},
37+
{ // 270
38+
{{0, 4}, {0, 3}, {0, 2}, {0, 1}, {0, 0}},
39+
{{1, 4}, {1, 3}, {1, 2}, {1, 1}, {1, 0}},
40+
{{2, 4}, {2, 3}, {2, 2}, {2, 1}, {2, 0}},
41+
{{3, 4}, {3, 3}, {3, 2}, {3, 1}, {3, 0}},
42+
{{4, 4}, {4, 3}, {4, 2}, {4, 1}, {4, 0}},
43+
},
44+
}
45+
46+
type Config struct {
47+
Rotation uint8
48+
}
49+
50+
type Device struct {
51+
pin [10]machine.Pin
52+
buffer [5][5]bool
53+
rotation uint8
54+
}
55+
56+
// New returns a new microbitmatrix driver.
57+
func New() Device {
58+
return Device{}
59+
}
60+
61+
// Configure sets up the device.
62+
func (d *Device) Configure(cfg Config) {
63+
d.SetRotation(cfg.Rotation)
64+
65+
d.pin[0] = machine.LED_COL_1
66+
d.pin[1] = machine.LED_COL_2
67+
d.pin[2] = machine.LED_COL_3
68+
d.pin[3] = machine.LED_COL_4
69+
d.pin[4] = machine.LED_COL_5
70+
71+
d.pin[5] = machine.LED_ROW_1
72+
d.pin[6] = machine.LED_ROW_2
73+
d.pin[7] = machine.LED_ROW_3
74+
d.pin[8] = machine.LED_ROW_4
75+
d.pin[9] = machine.LED_ROW_5
76+
77+
for i := 0; i < 10; i++ {
78+
d.pin[i].Configure(machine.PinConfig{Mode: machine.PinOutput})
79+
}
80+
81+
d.ClearDisplay()
82+
d.DisableAll()
83+
}
84+
85+
// SetRotation changes the rotation of the LED matrix
86+
func (d *Device) SetRotation(rotation uint8) {
87+
d.rotation = rotation % 4
88+
}
89+
90+
// SetPixel modifies the internal buffer in a single pixel.
91+
func (d *Device) SetPixel(x int16, y int16, c color.RGBA) {
92+
if x < 0 || x >= 5 || y < 0 || y >= 5 {
93+
return
94+
}
95+
if c.R != 0 || c.G != 0 || c.B != 0 {
96+
d.buffer[matrixRotations[d.rotation][x][y][0]][matrixRotations[d.rotation][x][y][1]] = true
97+
} else {
98+
d.buffer[matrixRotations[d.rotation][x][y][0]][matrixRotations[d.rotation][x][y][1]] = false
99+
}
100+
}
101+
102+
// GetPixel returns if the specific pixels is enabled
103+
func (d *Device) GetPixel(x int16, y int16) bool {
104+
if x < 0 || x >= 5 || y < 0 || y >= 5 {
105+
return false
106+
}
107+
return d.buffer[matrixRotations[d.rotation][x][y][0]][matrixRotations[d.rotation][x][y][1]]
108+
}
109+
110+
// Display sends the buffer (if any) to the screen.
111+
func (d *Device) Display() error {
112+
for x := 0; x < 5; x++ {
113+
d.DisableAll()
114+
d.pin[x].Low()
115+
116+
for y := 0; y < 5; y++ {
117+
if d.buffer[x][y] {
118+
d.pin[5+y].High()
119+
} else {
120+
d.pin[5+y].Low()
121+
}
122+
123+
}
124+
time.Sleep(time.Millisecond * 4)
125+
}
126+
return nil
127+
}
128+
129+
// ClearDisplay erases the internal buffer
130+
func (d *Device) ClearDisplay() {
131+
for row := 0; row < 5; row++ {
132+
for col := 0; col < 5; col++ {
133+
d.buffer[row][col] = false
134+
}
135+
}
136+
}
137+
138+
// DisableAll disables all the LEDs without modifying the buffer
139+
func (d *Device) DisableAll() {
140+
for i := 0; i < 5; i++ {
141+
d.pin[i].High()
142+
d.pin[5+i].Low()
143+
}
144+
}
145+
146+
// EnableAll enables all the LEDs without modifying the buffer
147+
func (d *Device) EnableAll() {
148+
for i := 0; i < 5; i++ {
149+
d.pin[i].Low()
150+
d.pin[5+i].High()
151+
}
152+
}
153+
154+
// Size returns the current size of the display.
155+
func (d *Device) Size() (w, h int16) {
156+
return 5, 5
157+
}

microbitmatrix/microbitmatrix.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build microbit
2+
13
// Package microbitmatrix implements a driver for the BBC micro:bit's LED matrix.
24
//
35
// Schematic: https://github.com/bbcmicrobit/hardware/blob/master/SCH_BBC-Microbit_V1.3B.pdf

0 commit comments

Comments
 (0)