Skip to content

Commit 7dbca2a

Browse files
committed
uc8151: correct DrawBitmap() also refactor SendCommand() and SendData() for clarity
Signed-off-by: deadprogram <[email protected]>
1 parent 50b6f18 commit 7dbca2a

File tree

1 file changed

+21
-27
lines changed

1 file changed

+21
-27
lines changed

uc8151/uc8151.go

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ func (d *Device) Configure(cfg Config) {
122122

123123
d.SendCommand(POF)
124124
d.WaitUntilIdle()
125-
126125
}
127126

128127
// Reset resets the device
@@ -146,29 +145,23 @@ func (d *Device) PowerOn() {
146145

147146
// SendCommand sends a command to the display
148147
func (d *Device) SendCommand(command uint8) {
149-
d.sendDataCommand(true, command)
148+
d.dc.Low()
149+
d.cs.Low()
150+
d.bus.Transfer(command)
151+
d.cs.High()
150152
}
151153

152154
// SendData sends a data byte to the display
153-
func (d *Device) SendData(data uint8) {
154-
d.sendDataCommand(false, data)
155-
}
156-
157-
// sendDataCommand sends image data or a command to the screen
158-
func (d *Device) sendDataCommand(isCommand bool, data uint8) {
159-
if isCommand {
160-
d.dc.Low()
161-
} else {
162-
d.dc.High()
163-
}
155+
func (d *Device) SendData(data ...uint8) {
156+
d.dc.High()
164157
d.cs.Low()
165-
d.bus.Transfer(data)
158+
d.bus.Tx(data, nil)
166159
d.cs.High()
167160
}
168161

169162
// SetPixel modifies the internal buffer in a single pixel.
170163
// The display have 2 colors: black and white
171-
// We use RGBA(0,0,0, 255) as white (transparent)
164+
// We use RGBA(0, 0, 0) as white (transparent)
172165
// Anything else as black
173166
func (d *Device) SetPixel(x int16, y int16, c color.RGBA) {
174167
x, y = d.xy(x, y)
@@ -177,22 +170,23 @@ func (d *Device) SetPixel(x int16, y int16, c color.RGBA) {
177170
return
178171
}
179172
byteIndex := x/8 + y*(d.width/8)
180-
if c.R == 0 && c.G == 0 && c.B == 0 { // TRANSPARENT / WHITE
181-
d.buffer[byteIndex] &^= 0x80 >> uint8(x%8)
182-
} else { // WHITE / EMPTY
173+
if c.R != 0 || c.G != 0 || c.B != 0 {
183174
d.buffer[byteIndex] |= 0x80 >> uint8(x%8)
175+
} else {
176+
d.buffer[byteIndex] &^= 0x80 >> uint8(x%8)
184177
}
185178
}
186179

187180
// DrawBitmap copies the bitmap to the screen at the given coordinates.
188181
func (d *Device) DrawBitmap(x, y int16, bitmap pixel.Image[pixel.Monochrome]) error {
189-
width, height := bitmap.Size()
190-
if x < 0 || x+int16(width) > d.width || y < 0 || y+int16(height) > d.height {
182+
dw, dh := d.Size()
183+
bw, bh := bitmap.Size()
184+
if x < 0 || x+int16(bw) > dw || y < 0 || y+int16(bh) > dh {
191185
return errOutOfRange
192186
}
193187

194-
for i := 0; i < width; i++ {
195-
for j := 0; j < height; j++ {
188+
for i := 0; i < bw; i++ {
189+
for j := 0; j < bh; j++ {
196190
d.SetPixel(x+int16(i), y+int16(j), bitmap.Get(i, j).RGBA())
197191
}
198192
}
@@ -205,19 +199,19 @@ func (d *Device) Display() error {
205199
if d.blocking {
206200
d.WaitUntilIdle()
207201
}
208-
d.SendCommand(PON)
202+
d.PowerOn()
203+
209204
d.SendCommand(PTOU)
210205
d.SendCommand(DTM2)
211-
for i := uint32(0); i < d.bufferLength; i++ {
212-
d.SendData(d.buffer[i])
213-
}
206+
d.SendData(d.buffer...)
214207

215208
d.SendCommand(DSP)
216209
d.SendCommand(DRF)
217210
if d.blocking {
218211
d.WaitUntilIdle()
219212
d.PowerOff()
220213
}
214+
221215
return nil
222216
}
223217

@@ -295,7 +289,7 @@ func (d *Device) ClearDisplay() {
295289
// WaitUntilIdle waits until the display is ready
296290
func (d *Device) WaitUntilIdle() {
297291
for !d.busy.Get() {
298-
time.Sleep(100 * time.Millisecond)
292+
time.Sleep(10 * time.Millisecond)
299293
}
300294
}
301295

0 commit comments

Comments
 (0)