Skip to content

Commit 01acd97

Browse files
deadprogramconejoninja
authored andcommitted
microbitmatrix: refactor to eliminate duplicate code with microbit v1/v2
Signed-off-by: deadprogram <[email protected]>
1 parent 231ec57 commit 01acd97

File tree

3 files changed

+112
-139
lines changed

3 files changed

+112
-139
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// +build microbit
2+
3+
// Package microbitmatrix implements a driver for the BBC micro:bit's LED matrix.
4+
//
5+
// Schematic: https://github.com/bbcmicrobit/hardware/blob/master/SCH_BBC-Microbit_V1.3B.pdf
6+
//
7+
package microbitmatrix // import "tinygo.org/x/drivers/microbitmatrix"
8+
9+
import (
10+
"machine"
11+
"time"
12+
)
13+
14+
var matrixRotations = [4][5][5][2]uint8{
15+
{ // 0
16+
{{0, 0}, {1, 3}, {0, 1}, {1, 4}, {0, 2}},
17+
{{2, 3}, {2, 4}, {2, 5}, {2, 6}, {2, 7}},
18+
{{1, 1}, {0, 8}, {1, 2}, {2, 8}, {1, 0}},
19+
{{0, 7}, {0, 6}, {0, 5}, {0, 4}, {0, 3}},
20+
{{2, 2}, {1, 6}, {2, 0}, {1, 5}, {2, 1}},
21+
},
22+
{ // 90 CCW
23+
{{0, 2}, {2, 7}, {1, 0}, {0, 3}, {2, 1}},
24+
{{1, 4}, {2, 6}, {2, 8}, {0, 4}, {1, 5}},
25+
{{0, 1}, {2, 5}, {1, 2}, {0, 5}, {2, 0}},
26+
{{1, 3}, {2, 4}, {0, 8}, {0, 6}, {1, 6}},
27+
{{0, 0}, {2, 3}, {1, 1}, {0, 7}, {2, 2}},
28+
},
29+
{ // 180
30+
{{2, 1}, {1, 5}, {2, 0}, {1, 6}, {2, 2}},
31+
{{0, 3}, {0, 4}, {0, 5}, {0, 6}, {0, 7}},
32+
{{1, 0}, {2, 8}, {1, 2}, {0, 8}, {1, 1}},
33+
{{2, 7}, {2, 6}, {2, 5}, {2, 4}, {2, 3}},
34+
{{0, 2}, {1, 4}, {0, 1}, {1, 3}, {0, 0}},
35+
},
36+
{ // 270
37+
{{2, 2}, {0, 7}, {1, 1}, {2, 3}, {0, 0}},
38+
{{1, 6}, {0, 6}, {0, 8}, {2, 4}, {1, 3}},
39+
{{2, 0}, {0, 5}, {1, 2}, {2, 5}, {0, 1}},
40+
{{1, 5}, {0, 4}, {2, 8}, {2, 6}, {1, 4}},
41+
{{2, 1}, {0, 3}, {1, 0}, {2, 7}, {0, 2}},
42+
},
43+
}
44+
45+
type Device struct {
46+
pin [12]machine.Pin
47+
buffer [3][9]bool
48+
rotation uint8
49+
}
50+
51+
// Configure sets up the device.
52+
func (d *Device) Configure(cfg Config) {
53+
d.SetRotation(cfg.Rotation)
54+
55+
for i := machine.LED_COL_1; i <= machine.LED_ROW_3; i++ {
56+
d.pin[i-machine.LED_COL_1] = i
57+
d.pin[i-machine.LED_COL_1].Configure(machine.PinConfig{Mode: machine.PinOutput})
58+
}
59+
d.ClearDisplay()
60+
d.DisableAll()
61+
}
62+
63+
// Display sends the buffer (if any) to the screen.
64+
func (d *Device) Display() error {
65+
for row := 0; row < 3; row++ {
66+
d.DisableAll()
67+
d.pin[9+row].High()
68+
69+
for col := 0; col < 9; col++ {
70+
if d.buffer[row][col] {
71+
d.pin[col].Low()
72+
}
73+
74+
}
75+
time.Sleep(time.Millisecond * 2)
76+
}
77+
return nil
78+
}
79+
80+
// ClearDisplay erases the internal buffer
81+
func (d *Device) ClearDisplay() {
82+
for row := 0; row < 3; row++ {
83+
for col := 0; col < 9; col++ {
84+
d.buffer[row][col] = false
85+
}
86+
}
87+
}
88+
89+
// DisableAll disables all the LEDs without modifying the buffer
90+
func (d *Device) DisableAll() {
91+
for i := machine.LED_COL_1; i <= machine.LED_COL_9; i++ {
92+
d.pin[i-machine.LED_COL_1].High()
93+
}
94+
for i := machine.LED_ROW_1; i <= machine.LED_ROW_3; i++ {
95+
d.pin[i-machine.LED_COL_1].Low()
96+
}
97+
}
98+
99+
// EnableAll enables all the LEDs without modifying the buffer
100+
func (d *Device) EnableAll() {
101+
for i := machine.LED_COL_1; i <= machine.LED_COL_9; i++ {
102+
d.pin[i-machine.LED_COL_1].Low()
103+
}
104+
for i := machine.LED_ROW_1; i <= machine.LED_ROW_3; i++ {
105+
d.pin[i-machine.LED_COL_1].High()
106+
}
107+
}
108+
109+
// Size returns the current size of the display.
110+
func (d *Device) Size() (w, h int16) {
111+
return 5, 5
112+
}

microbitmatrix/microbitmatrix-v2.go

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
package microbitmatrix // import "tinygo.org/x/drivers/microbitmatrix"
88

99
import (
10-
"image/color"
1110
"machine"
1211
"time"
1312
)
@@ -43,21 +42,12 @@ var matrixRotations = [4][5][5][2]uint8{
4342
},
4443
}
4544

46-
type Config struct {
47-
Rotation uint8
48-
}
49-
5045
type Device struct {
5146
pin [10]machine.Pin
5247
buffer [5][5]bool
5348
rotation uint8
5449
}
5550

56-
// New returns a new microbitmatrix driver.
57-
func New() Device {
58-
return Device{}
59-
}
60-
6151
// Configure sets up the device.
6252
func (d *Device) Configure(cfg Config) {
6353
d.SetRotation(cfg.Rotation)
@@ -82,31 +72,6 @@ func (d *Device) Configure(cfg Config) {
8272
d.DisableAll()
8373
}
8474

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-
11075
// Display sends the buffer (if any) to the screen.
11176
func (d *Device) Display() error {
11277
for x := 0; x < 5; x++ {

microbitmatrix/microbitmatrix.go

Lines changed: 0 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// +build microbit
2-
31
// Package microbitmatrix implements a driver for the BBC micro:bit's LED matrix.
42
//
53
// Schematic: https://github.com/bbcmicrobit/hardware/blob/master/SCH_BBC-Microbit_V1.3B.pdf
@@ -8,68 +6,17 @@ package microbitmatrix // import "tinygo.org/x/drivers/microbitmatrix"
86

97
import (
108
"image/color"
11-
"machine"
12-
"time"
139
)
1410

15-
var matrixRotations = [4][5][5][2]uint8{
16-
{ // 0
17-
{{0, 0}, {1, 3}, {0, 1}, {1, 4}, {0, 2}},
18-
{{2, 3}, {2, 4}, {2, 5}, {2, 6}, {2, 7}},
19-
{{1, 1}, {0, 8}, {1, 2}, {2, 8}, {1, 0}},
20-
{{0, 7}, {0, 6}, {0, 5}, {0, 4}, {0, 3}},
21-
{{2, 2}, {1, 6}, {2, 0}, {1, 5}, {2, 1}},
22-
},
23-
{ // 90 CCW
24-
{{0, 2}, {2, 7}, {1, 0}, {0, 3}, {2, 1}},
25-
{{1, 4}, {2, 6}, {2, 8}, {0, 4}, {1, 5}},
26-
{{0, 1}, {2, 5}, {1, 2}, {0, 5}, {2, 0}},
27-
{{1, 3}, {2, 4}, {0, 8}, {0, 6}, {1, 6}},
28-
{{0, 0}, {2, 3}, {1, 1}, {0, 7}, {2, 2}},
29-
},
30-
{ // 180
31-
{{2, 1}, {1, 5}, {2, 0}, {1, 6}, {2, 2}},
32-
{{0, 3}, {0, 4}, {0, 5}, {0, 6}, {0, 7}},
33-
{{1, 0}, {2, 8}, {1, 2}, {0, 8}, {1, 1}},
34-
{{2, 7}, {2, 6}, {2, 5}, {2, 4}, {2, 3}},
35-
{{0, 2}, {1, 4}, {0, 1}, {1, 3}, {0, 0}},
36-
},
37-
{ // 270
38-
{{2, 2}, {0, 7}, {1, 1}, {2, 3}, {0, 0}},
39-
{{1, 6}, {0, 6}, {0, 8}, {2, 4}, {1, 3}},
40-
{{2, 0}, {0, 5}, {1, 2}, {2, 5}, {0, 1}},
41-
{{1, 5}, {0, 4}, {2, 8}, {2, 6}, {1, 4}},
42-
{{2, 1}, {0, 3}, {1, 0}, {2, 7}, {0, 2}},
43-
},
44-
}
45-
4611
type Config struct {
4712
Rotation uint8
4813
}
4914

50-
type Device struct {
51-
pin [12]machine.Pin
52-
buffer [3][9]bool
53-
rotation uint8
54-
}
55-
5615
// New returns a new microbitmatrix driver.
5716
func New() Device {
5817
return Device{}
5918
}
6019

61-
// Configure sets up the device.
62-
func (d *Device) Configure(cfg Config) {
63-
d.SetRotation(cfg.Rotation)
64-
65-
for i := machine.LED_COL_1; i <= machine.LED_ROW_3; i++ {
66-
d.pin[i-machine.LED_COL_1] = i
67-
d.pin[i-machine.LED_COL_1].Configure(machine.PinConfig{Mode: machine.PinOutput})
68-
}
69-
d.ClearDisplay()
70-
d.DisableAll()
71-
}
72-
7320
// SetRotation changes the rotation of the LED matrix
7421
func (d *Device) SetRotation(rotation uint8) {
7522
d.rotation = rotation % 4
@@ -94,54 +41,3 @@ func (d *Device) GetPixel(x int16, y int16) bool {
9441
}
9542
return d.buffer[matrixRotations[d.rotation][x][y][0]][matrixRotations[d.rotation][x][y][1]]
9643
}
97-
98-
// Display sends the buffer (if any) to the screen.
99-
func (d *Device) Display() error {
100-
for row := 0; row < 3; row++ {
101-
d.DisableAll()
102-
d.pin[9+row].High()
103-
104-
for col := 0; col < 9; col++ {
105-
if d.buffer[row][col] {
106-
d.pin[col].Low()
107-
}
108-
109-
}
110-
time.Sleep(time.Millisecond * 2)
111-
}
112-
return nil
113-
}
114-
115-
// ClearDisplay erases the internal buffer
116-
func (d *Device) ClearDisplay() {
117-
for row := 0; row < 3; row++ {
118-
for col := 0; col < 9; col++ {
119-
d.buffer[row][col] = false
120-
}
121-
}
122-
}
123-
124-
// DisableAll disables all the LEDs without modifying the buffer
125-
func (d *Device) DisableAll() {
126-
for i := machine.LED_COL_1; i <= machine.LED_COL_9; i++ {
127-
d.pin[i-machine.LED_COL_1].High()
128-
}
129-
for i := machine.LED_ROW_1; i <= machine.LED_ROW_3; i++ {
130-
d.pin[i-machine.LED_COL_1].Low()
131-
}
132-
}
133-
134-
// EnableAll enables all the LEDs without modifying the buffer
135-
func (d *Device) EnableAll() {
136-
for i := machine.LED_COL_1; i <= machine.LED_COL_9; i++ {
137-
d.pin[i-machine.LED_COL_1].Low()
138-
}
139-
for i := machine.LED_ROW_1; i <= machine.LED_ROW_3; i++ {
140-
d.pin[i-machine.LED_COL_1].High()
141-
}
142-
}
143-
144-
// Size returns the current size of the display.
145-
func (d *Device) Size() (w, h int16) {
146-
return 5, 5
147-
}

0 commit comments

Comments
 (0)