-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiny_lcd.go
80 lines (68 loc) · 1.53 KB
/
tiny_lcd.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
package lcd
import "strings"
type LCD struct {
rows int
columns int
grid []bool
changes map[int]bool
}
func NewLCD(columns, rows int) *LCD {
this := &LCD{
columns: columns,
rows: rows,
grid: []bool{},
changes: make(map[int]bool),
}
for cell := 0; cell < columns*rows; cell++ {
this.grid = append(this.grid, false)
}
return this
}
func (this *LCD) RectangleOn(columns, rows int) {
defer this.apply()
for r := 0; r < rows; r++ {
for c := 0; c < columns; c++ {
this.changes[r*this.columns+c] = true
}
}
}
func (this *LCD) RotateColumn(column, rotation int) {
defer this.apply()
for r := 0; r < this.rows; r++ {
currentIndex := r*this.columns + column
nextIndex := (currentIndex + this.columns*rotation) % len(this.grid)
this.changes[nextIndex] = this.grid[currentIndex]
}
}
func (this *LCD) RotateRow(row, rotation int) {
defer this.apply()
start := row * this.columns
end := start + this.columns
for c := 0; c < this.columns; c++ {
currentIndex := row*this.columns + c
nextIndex := currentIndex + rotation
if nextIndex >= end {
nextIndex -= this.columns
}
this.changes[nextIndex] = this.grid[currentIndex]
}
}
func (this *LCD) apply() {
for i, value := range this.changes {
this.grid[i] = value
}
this.changes = make(map[int]bool)
}
func (this *LCD) String() (result string) {
for i := 0; i < len(this.grid); i++ {
if this.grid[i] {
result += "#"
} else {
result += " "
}
if (i+1)%this.columns == 0 {
result += "\n"
}
}
return strings.TrimRight(result, "\n")
}