/
writer.go
173 lines (157 loc) · 3.14 KB
/
writer.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright 2020 Michal Derkacz. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package eve
// Writer extends DLW to allow write arbitrary data.
type Writer struct {
driver
}
// Width returns screen width.
func (w *Writer) Width() int {
return int(w.width)
}
// Height returns screen height.
func (w *Writer) Height() int {
return int(w.height)
}
// Flush writes any buffered data to the underlying DCI.
func (w *Writer) Flush() {
w.flush()
}
// Addr returns the address just after the last write operation. The address is
// calculated and may be ahead of actual writes due to internal buffering.
func (w *Writer) Addr() int {
if w.state == stateWriteCmd {
return mmap[w.typ].RAM_CMD.Start + w.addr&4095
}
return w.addr
}
// Close closes the wrtie transaction and returns the address just after the
// last write operation.
func (w *Writer) Close() int {
addr := w.Addr()
w.closeWriter(stateWrite)
return addr
}
func (w *Writer) wr8(u uint8) {
w.addr += 1
if len(w.buf) == cap(w.buf) {
w.flush()
}
n := len(w.buf)
w.buf = w.buf[:n+1]
w.buf[n] = u
}
// Write8 writes bytes.
func (w *Writer) Write8(v ...uint8) {
if len(v) == 0 {
return
}
w.addr += len(v)
if len(v) >= cap(w.buf) {
// write long data directly
if len(w.buf) > 0 {
w.flush()
}
if w.state == stateWriteCmd {
w.writeCmds(v, false)
} else {
w.dci.Write(v)
}
return
}
n := len(w.buf)
m := copy(w.buf[n:cap(w.buf)], v)
w.buf = w.buf[:n+m]
if m < len(v) {
v = v[m:]
w.flush()
w.buf = w.buf[:len(v)]
copy(w.buf, v)
}
}
func (w *Writer) Write(p []byte) (int, error) {
w.Write8(p...)
n := len(p)
err := w.dci.Err(false)
if err != nil {
n = 0
}
return n, err
}
func (w *Writer) ws(s string) {
w.addr += len(s)
for len(s) != 0 {
if len(w.buf) == cap(w.buf) {
w.flush()
}
n := len(w.buf)
c := copy(w.buf[n:cap(w.buf)], s)
w.buf = w.buf[:n+c]
s = s[c:]
}
}
func (w *Writer) WriteString(s string) (int, error) {
w.ws(s)
n := len(s)
err := w.dci.Err(false)
if err != nil {
n = 0
}
return n, err
}
// Write16 writes 16-bit words.
func (w *Writer) Write16(v ...uint16) {
w.addr += len(v) * 2
for _, u := range v {
if len(w.buf)+2 > cap(w.buf) {
w.flush()
}
n := len(w.buf)
w.buf = w.buf[:n+2]
w.buf[n] = byte(u)
w.buf[n+1] = byte(u >> 8)
}
}
func (w *Writer) wr32(u uint32) {
w.addr += 4
if len(w.buf)+4 > cap(w.buf) {
w.flush()
}
n := len(w.buf)
w.buf = w.buf[:n+4]
w.buf[n] = byte(u)
w.buf[n+1] = byte(u >> 8)
w.buf[n+2] = byte(u >> 16)
w.buf[n+3] = byte(u >> 24)
}
// Write32 writes 32-bit words.
func (w *Writer) Write32(v ...uint32) {
w.addr += len(v) * 4
for _, u := range v {
if len(w.buf)+4 > cap(w.buf) {
w.flush()
}
n := len(w.buf)
w.buf = w.buf[:n+4]
w.buf[n] = byte(u)
w.buf[n+1] = byte(u >> 8)
w.buf[n+2] = byte(u >> 16)
w.buf[n+3] = byte(u >> 24)
}
}
// Align writes random data to align the write address to n.
func (w *Writer) Align(n int) {
m := w.addr & (n - 1)
if m == 0 {
return
}
m = n - m
w.addr += m
m += len(w.buf)
if m > cap(w.buf) {
m -= len(w.buf)
w.flush()
}
w.buf = w.buf[:m]
}