-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdisplay_test.go
153 lines (135 loc) · 3.19 KB
/
display_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package scrollphathd_test
import (
"testing"
"github.com/tomnz/scroll-phat-hd-go"
)
func TestDisplay_Basic(t *testing.T) {
// Set one pixel - it shouldn't tile, so that should be the only pixel displayed
dev, disp := getDisplay()
disp.SetPixel(0, 0, 1)
disp.Show()
dev.checkPixels(t, [][]byte{
{1, 0, 0},
{0, 0, 0},
{0, 0, 0},
})
disp.Clear()
dev.checkPixels(t, [][]byte{
{0, 0, 0},
{0, 0, 0},
{0, 0, 0},
})
}
func TestDisplay_Fill(t *testing.T) {
dev, disp := getDisplay()
disp.Fill(1, 1, 3, 3, 1)
disp.Show()
dev.checkPixels(t, [][]byte{
{0, 0, 0},
{0, 1, 1},
{0, 1, 1},
})
}
func TestDisplay_Scroll(t *testing.T) {
// Set a pixel outside the frame - buffer should dynamically resize, and nothing
// should be displayed
dev, disp := getDisplay()
disp.SetPixel(3, 3, 1)
disp.Show()
dev.checkPixels(t, [][]byte{
{0, 0, 0},
{0, 0, 0},
{0, 0, 0},
})
// Pixel should move left two and up one, from the bottom right corner
disp.ScrollTo(2, 1)
disp.Show()
dev.checkPixels(t, [][]byte{
{0, 0, 0},
{0, 0, 0},
{0, 1, 0},
})
}
func TestDisplay_Tile(t *testing.T) {
dev, disp := getDisplay()
disp.SetPixel(0, 0, 1)
disp.SetPixel(1, 1, 2)
disp.SetPixel(2, 2, 3)
disp.SetPixel(3, 1, 4)
// Scroll several tiles forward
disp.ScrollTo(7, 0)
disp.Show()
dev.checkPixels(t, [][]byte{
{0, 1, 0},
{4, 0, 2},
{0, 0, 0},
})
// Now try with tiling disabled
dev, disp = getDisplay(scrollphathd.WithTiling(false))
disp.SetPixel(0, 0, 1)
disp.SetPixel(1, 1, 2)
disp.SetPixel(2, 2, 3)
disp.SetPixel(3, 1, 4)
disp.ScrollTo(7, 0)
disp.Show()
dev.checkPixels(t, [][]byte{
{0, 0, 0},
{0, 0, 0},
{0, 0, 0},
})
}
func TestDisplay_Flip(t *testing.T) {
dev, disp := getDisplay()
disp.SetPixel(0, 0, 1)
disp.SetPixel(1, 1, 2)
disp.SetPixel(2, 2, 3)
disp.SetPixel(3, 1, 4)
// Flip horizontal
disp.SetFlip(true, false)
disp.Show()
dev.checkPixels(t, [][]byte{
{0, 0, 0},
{4, 0, 2},
{0, 3, 0},
})
// Flip horizontal AND vertical
disp.SetFlip(true, true)
disp.Show()
dev.checkPixels(t, [][]byte{
{0, 3, 0},
{4, 0, 2},
{0, 0, 0},
})
// Try it with scrolling
disp.ScrollTo(7, 0)
disp.Show()
dev.checkPixels(t, [][]byte{
{0, 0, 3},
{0, 4, 0},
{1, 0, 0},
})
}
// testDevice is a fake device that implements the Device interface to validate output.
// It's designed to display a 3x3 area.
type testDevice struct {
buffer [][]byte
}
func (d *testDevice) Width() int { return 3 }
func (d *testDevice) Height() int { return 3 }
func (d *testDevice) SetBuffer(buffer [][]byte) { d.buffer = buffer }
func (d *testDevice) SetBrightness(brightness byte) {}
func (d *testDevice) Clear() error { return nil }
func (d *testDevice) Show() error { return nil }
func getDisplay(opts ...scrollphathd.DisplayOption) (*testDevice, *scrollphathd.Display) {
dev := &testDevice{}
return dev, scrollphathd.NewWithDevice(dev, opts...)
}
func (d *testDevice) checkPixels(t *testing.T, expected [][]byte) {
for y, row := range d.buffer {
for x, val := range row {
if val != expected[y][x] {
t.Fatalf("value at (%d, %d) was different (%d) than expected (%d)", x, y, val, expected[y][x])
}
}
}
}