Skip to content

Commit 54f02a5

Browse files
committed
ili9340: emulate SC, EC, SP, EP registers correctly.
This reflects actual ILI9340 operation: * CASET sets SC and EC registers (start col, end col). * PASET sets SP and EP registers (start page, end page). * RAMWR resets col = SC, page = SP. * RAMWR enforces wrapping at EC and EP. Go's SubImage, on the other hand, doesn't actually re-map coordinates as the old code assumed, so it didn't work at all.
1 parent a2013d6 commit 54f02a5

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

ili9340/ili9340.go

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,23 @@ type Display struct {
4545
paramIndex uint8
4646
paramData uint32 // accumulator for current parameter
4747
img *image.RGBA
48-
window *image.RGBA
4948
nextX uint16
5049
nextY uint16
50+
startCol uint16
51+
endCol uint16
52+
startRow uint16
53+
endRow uint16
5154
}
5255

5356
func NewDisplay(pm spi.PinMap) (display *Display, err error) {
5457
img := createImage()
5558
display = &Display{
56-
spi: spi.NewSlave(pm),
57-
img: img,
58-
window: img.SubImage(img.Bounds()).(*image.RGBA),
59+
spi: spi.NewSlave(pm),
60+
img: img,
61+
startCol: 0,
62+
endCol: width - 1,
63+
startRow: 0,
64+
endRow: height - 1,
5965
}
6066
return
6167
}
@@ -117,6 +123,8 @@ func (d *Display) acceptCommand(b byte) {
117123
d.paramIndex = 0
118124
switch b {
119125
case cmdRamWrite:
126+
d.nextX = d.startCol
127+
d.nextY = d.startRow
120128
d.state = stateRamWrite
121129
case cmdColumnAddressSet:
122130
d.state = stateColumnAddressSet
@@ -157,34 +165,34 @@ func (d *Display) pixelWrite(p16 uint16) {
157165
r := uint8((p16 & 0xF800) >> 8) // map high 5-bit to 8-bit color
158166
g := uint8((p16 & 0x07E0) >> 3) // map mid 6-bit to 8-bit color
159167
b := uint8((p16 & 0x001F) << 3) // map low 5-bit to 8-bit color
168+
c := color.RGBA{r, g, b, 0xFF}
160169

161-
d.window.SetRGBA(int(d.nextX), int(d.nextY), color.RGBA{r, g, b, 0xFF})
170+
d.img.SetRGBA(int(d.nextX), int(d.nextY), c)
162171

163-
// move to next pixel
164-
d.nextX = (d.nextX + 1) % width
165-
if d.nextX == 0 {
166-
d.nextY = (d.nextY + 1) % height
172+
if d.nextX == d.endCol {
173+
d.nextX = d.startCol
174+
if d.nextY == d.endRow {
175+
d.nextY = d.startRow
176+
} else {
177+
d.nextY++
178+
}
179+
} else {
180+
d.nextX++
167181
}
168182
}
169183

170184
func (d *Display) acceptColumnAddressByte(b byte) {
171185
if d.paramIndex == 4 {
172-
x0 := int(d.paramData >> 16)
173-
x1 := int(d.paramData & 0xFFFF)
174-
d.window = d.img.SubImage(image.Rect(x0, d.window.Bounds().Min.Y, x1, d.window.Bounds().Max.Y)).(*image.RGBA)
175-
reportAddressWindow(d)
186+
d.startCol = uint16(d.paramData >> 16)
187+
d.endCol = uint16(d.paramData & 0xFFFF)
188+
fmt.Printf("ILI9340 column address range %d:%d\n", d.startCol, d.endCol)
176189
}
177190
}
178191

179192
func (d *Display) acceptPageAddressByte(b byte) {
180193
if d.paramIndex == 4 {
181-
y0 := int(d.paramData >> 16)
182-
y1 := int(d.paramData & 0xFFFF)
183-
d.window = d.img.SubImage(image.Rect(d.window.Bounds().Min.X, y0, d.window.Bounds().Max.X, y1)).(*image.RGBA)
184-
reportAddressWindow(d)
194+
d.startRow = uint16(d.paramData >> 16)
195+
d.endRow = uint16(d.paramData & 0xFFFF)
196+
fmt.Printf("ILI9340 row address range %d:%d\n", d.startRow, d.endRow)
185197
}
186198
}
187-
188-
func reportAddressWindow(d *Display) {
189-
fmt.Printf("ILI9340: address window: %v\n", d.window.Bounds())
190-
}

0 commit comments

Comments
 (0)