Skip to content

Commit 6df9247

Browse files
sago35deadprogram
authored andcommitted
ili9341: avoid heap allocations
1 parent 56fb346 commit 6df9247

File tree

1 file changed

+52
-44
lines changed

1 file changed

+52
-44
lines changed

ili9341/ili9341.go

Lines changed: 52 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,33 @@ type Device struct {
2929
rd machine.Pin
3030
}
3131

32+
var cmdBuf [6]byte
33+
34+
var initCmd = []byte{
35+
0xEF, 3, 0x03, 0x80, 0x02,
36+
0xCF, 3, 0x00, 0xC1, 0x30,
37+
0xED, 4, 0x64, 0x03, 0x12, 0x81,
38+
0xE8, 3, 0x85, 0x00, 0x78,
39+
0xCB, 5, 0x39, 0x2C, 0x00, 0x34, 0x02,
40+
0xF7, 1, 0x20,
41+
0xEA, 2, 0x00, 0x00,
42+
PWCTR1, 1, 0x23, // Power control VRH[5:0]
43+
PWCTR2, 1, 0x10, // Power control SAP[2:0];BT[3:0]
44+
VMCTR1, 2, 0x3e, 0x28, // VCM control
45+
VMCTR2, 1, 0x86, // VCM control2
46+
MADCTL, 1, 0x48, // Memory Access Control
47+
VSCRSADD, 1, 0x00, // Vertical scroll zero
48+
PIXFMT, 1, 0x55,
49+
FRMCTR1, 2, 0x00, 0x18,
50+
DFUNCTR, 3, 0x08, 0x82, 0x27, // Display Function Control
51+
0xF2, 1, 0x00, // 3Gamma Function Disable
52+
GAMMASET, 1, 0x01, // Gamma curve selected
53+
GMCTRP1, 15, 0x0F, 0x31, 0x2B, 0x0C, 0x0E, 0x08, // Set Gamma
54+
0x4E, 0xF1, 0x37, 0x07, 0x10, 0x03, 0x0E, 0x09, 0x00,
55+
GMCTRN1, 15, 0x00, 0x0E, 0x14, 0x03, 0x11, 0x07, // Set Gamma
56+
0x31, 0xC1, 0x48, 0x08, 0x0F, 0x0C, 0x31, 0x36, 0x0F,
57+
}
58+
3259
// Configure prepares display for use
3360
func (d *Device) Configure(config Config) {
3461

@@ -81,42 +108,15 @@ func (d *Device) Configure(config Config) {
81108
delay(150)
82109
}
83110

84-
initCmd := []byte{
85-
0xEF, 3, 0x03, 0x80, 0x02,
86-
0xCF, 3, 0x00, 0xC1, 0x30,
87-
0xED, 4, 0x64, 0x03, 0x12, 0x81,
88-
0xE8, 3, 0x85, 0x00, 0x78,
89-
0xCB, 5, 0x39, 0x2C, 0x00, 0x34, 0x02,
90-
0xF7, 1, 0x20,
91-
0xEA, 2, 0x00, 0x00,
92-
PWCTR1, 1, 0x23, // Power control VRH[5:0]
93-
PWCTR2, 1, 0x10, // Power control SAP[2:0];BT[3:0]
94-
VMCTR1, 2, 0x3e, 0x28, // VCM control
95-
VMCTR2, 1, 0x86, // VCM control2
96-
MADCTL, 1, 0x48, // Memory Access Control
97-
VSCRSADD, 1, 0x00, // Vertical scroll zero
98-
PIXFMT, 1, 0x55,
99-
FRMCTR1, 2, 0x00, 0x18,
100-
DFUNCTR, 3, 0x08, 0x82, 0x27, // Display Function Control
101-
0xF2, 1, 0x00, // 3Gamma Function Disable
102-
GAMMASET, 1, 0x01, // Gamma curve selected
103-
GMCTRP1, 15, 0x0F, 0x31, 0x2B, 0x0C, 0x0E, 0x08, // Set Gamma
104-
0x4E, 0xF1, 0x37, 0x07, 0x10, 0x03, 0x0E, 0x09, 0x00,
105-
GMCTRN1, 15, 0x00, 0x0E, 0x14, 0x03, 0x11, 0x07, // Set Gamma
106-
0x31, 0xC1, 0x48, 0x08, 0x0F, 0x0C, 0x31, 0x36, 0x0F,
107-
}
108-
109111
if config.DisplayInversion {
110-
initCmd = append(initCmd, []byte{
111-
INVON, 0x80,
112-
}...)
112+
initCmd = append(initCmd, INVON, 0x80)
113113
}
114114

115-
initCmd = append(initCmd, []byte{
115+
initCmd = append(initCmd,
116116
SLPOUT, 0x80, // Exit Sleep
117117
DISPON, 0x80, // Display on
118118
0x00, // End of list
119-
}...)
119+
)
120120
for i, c := 0, len(initCmd); i < c; {
121121
cmd := initCmd[i]
122122
if cmd == 0x00 {
@@ -267,24 +267,28 @@ func (d *Device) SetRotation(rotation Rotation) {
267267
case Rotation270Mirror:
268268
madctl = MADCTL_MX | MADCTL_MY | MADCTL_MV | MADCTL_BGR
269269
}
270-
d.sendCommand(MADCTL, []uint8{madctl})
270+
cmdBuf[0] = madctl
271+
d.sendCommand(MADCTL, cmdBuf[:1])
271272
d.rotation = rotation
272273
}
273274

274275
// SetScrollArea sets an area to scroll with fixed top/bottom or left/right parts of the display
275276
// Rotation affects scroll direction
276277
func (d *Device) SetScrollArea(topFixedArea, bottomFixedArea int16) {
277-
d.sendCommand(VSCRDEF, []uint8{
278-
uint8(topFixedArea >> 8), uint8(topFixedArea),
279-
uint8(d.height - topFixedArea - bottomFixedArea>>8),
280-
uint8(d.height - topFixedArea - bottomFixedArea),
281-
uint8(bottomFixedArea >> 8), uint8(bottomFixedArea),
282-
})
278+
cmdBuf[0] = uint8(topFixedArea >> 8)
279+
cmdBuf[1] = uint8(topFixedArea)
280+
cmdBuf[2] = uint8(d.height - topFixedArea - bottomFixedArea>>8)
281+
cmdBuf[3] = uint8(d.height - topFixedArea - bottomFixedArea)
282+
cmdBuf[4] = uint8(bottomFixedArea >> 8)
283+
cmdBuf[5] = uint8(bottomFixedArea)
284+
d.sendCommand(VSCRDEF, cmdBuf[:6])
283285
}
284286

285287
// SetScroll sets the vertical scroll address of the display.
286288
func (d *Device) SetScroll(line int16) {
287-
d.sendCommand(VSCRSADD, []uint8{uint8(line >> 8), uint8(line)})
289+
cmdBuf[0] = uint8(line >> 8)
290+
cmdBuf[1] = uint8(line)
291+
d.sendCommand(VSCRSADD, cmdBuf[:2])
288292
}
289293

290294
// StopScroll returns the display to its normal state
@@ -298,16 +302,20 @@ func (d *Device) setWindow(x, y, w, h int16) {
298302
//y += d.rowOffset
299303
x1 := x + w - 1
300304
if x != d.x0 || x1 != d.x1 {
301-
d.sendCommand(CASET, []uint8{
302-
uint8(x >> 8), uint8(x), uint8(x1 >> 8), uint8(x1),
303-
})
305+
cmdBuf[0] = uint8(x >> 8)
306+
cmdBuf[1] = uint8(x)
307+
cmdBuf[2] = uint8(x1 >> 8)
308+
cmdBuf[3] = uint8(x1)
309+
d.sendCommand(CASET, cmdBuf[:4])
304310
d.x0, d.x1 = x, x1
305311
}
306312
y1 := y + h - 1
307313
if y != d.y0 || y1 != d.y1 {
308-
d.sendCommand(PASET, []uint8{
309-
uint8(y >> 8), uint8(y), uint8(y1 >> 8), uint8(y1),
310-
})
314+
cmdBuf[0] = uint8(y >> 8)
315+
cmdBuf[1] = uint8(y)
316+
cmdBuf[2] = uint8(y1 >> 8)
317+
cmdBuf[3] = uint8(y1)
318+
d.sendCommand(PASET, cmdBuf[:4])
311319
d.y0, d.y1 = y, y1
312320
}
313321
d.sendCommand(RAMWR, nil)

0 commit comments

Comments
 (0)