Skip to content

Commit c689c11

Browse files
aykevldeadprogram
authored andcommitted
ili9341: st7735: st7789: unify rotation support
* Unify the Rotation type, so that SetRotation can be used in interfaces. * Add Rotation() method to these displays, so that the current rotation can be read. * Change SetRotation() so that it can return an error (if the given rotation isn't supported).
1 parent 59d66d1 commit c689c11

File tree

7 files changed

+97
-54
lines changed

7 files changed

+97
-54
lines changed

displayer.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,19 @@ type Displayer interface {
1212
// Display sends the buffer (if any) to the screen.
1313
Display() error
1414
}
15+
16+
// Rotation is how much a display has been rotated. Displays can be rotated, and
17+
// sometimes also mirrored.
18+
type Rotation uint8
19+
20+
// Clockwise rotation of the screen.
21+
const (
22+
Rotation0 = iota
23+
Rotation90
24+
Rotation180
25+
Rotation270
26+
Rotation0Mirror
27+
Rotation90Mirror
28+
Rotation180Mirror
29+
Rotation270Mirror
30+
)

ili9341/ili9341.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,21 @@ import (
55
"image/color"
66
"machine"
77
"time"
8+
9+
"tinygo.org/x/drivers"
810
)
911

1012
type Config struct {
1113
Width int16
1214
Height int16
13-
Rotation Rotation
15+
Rotation drivers.Rotation
1416
DisplayInversion bool
1517
}
1618

1719
type Device struct {
1820
width int16
1921
height int16
20-
rotation Rotation
22+
rotation drivers.Rotation
2123
driver driver
2224

2325
x0, x1 int16 // cached address window; prevents useless/expensive
@@ -262,13 +264,20 @@ func (d *Device) Sleep(sleepEnabled bool) error {
262264
return nil
263265
}
264266

265-
// GetRotation returns the current rotation of the device
266-
func (d *Device) GetRotation() Rotation {
267+
// Rotation returns the current rotation of the device.
268+
func (d *Device) Rotation() drivers.Rotation {
267269
return d.rotation
268270
}
269271

270-
// SetRotation changes the rotation of the device (clock-wise)
271-
func (d *Device) SetRotation(rotation Rotation) {
272+
// GetRotation returns the current rotation of the device.
273+
//
274+
// Deprecated: use Rotation instead.
275+
func (d *Device) GetRotation() drivers.Rotation {
276+
return d.rotation
277+
}
278+
279+
// SetRotation changes the rotation of the device (clock-wise).
280+
func (d *Device) SetRotation(rotation drivers.Rotation) error {
272281
madctl := uint8(0)
273282
switch rotation % 8 {
274283
case Rotation0:
@@ -291,6 +300,7 @@ func (d *Device) SetRotation(rotation Rotation) {
291300
cmdBuf[0] = madctl
292301
d.sendCommand(MADCTL, cmdBuf[:1])
293302
d.rotation = rotation
303+
return nil
294304
}
295305

296306
// SetScrollArea sets an area to scroll with fixed top/bottom or left/right parts of the display

ili9341/registers.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package ili9341
22

3+
import "tinygo.org/x/drivers"
4+
35
type Rotation uint8
46

57
const (
@@ -77,13 +79,13 @@ const (
7779
)
7880

7981
const (
80-
Rotation0 Rotation = 0
81-
Rotation90 Rotation = 1 // 90 degrees clock-wise rotation
82-
Rotation180 Rotation = 2
83-
Rotation270 Rotation = 3
84-
85-
Rotation0Mirror Rotation = 4
86-
Rotation90Mirror Rotation = 5
87-
Rotation180Mirror Rotation = 6
88-
Rotation270Mirror Rotation = 7
82+
Rotation0 = drivers.Rotation0
83+
Rotation90 = drivers.Rotation90 // 90 degrees clock-wise rotation
84+
Rotation180 = drivers.Rotation180
85+
Rotation270 = drivers.Rotation270
86+
87+
Rotation0Mirror = drivers.Rotation0Mirror
88+
Rotation90Mirror = drivers.Rotation90Mirror
89+
Rotation180Mirror = drivers.Rotation180Mirror
90+
Rotation270Mirror = drivers.Rotation270Mirror
8991
)

st7735/registers.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package st7735
22

3+
import "tinygo.org/x/drivers"
4+
35
// Registers
46
const (
57
NOP = 0x00
@@ -52,8 +54,8 @@ const (
5254
GREENTAB Model = 0
5355
MINI80x160 Model = 1
5456

55-
NO_ROTATION Rotation = 0
56-
ROTATION_90 Rotation = 1 // 90 degrees clock-wise rotation
57-
ROTATION_180 Rotation = 2
58-
ROTATION_270 Rotation = 3
57+
NO_ROTATION = drivers.Rotation0
58+
ROTATION_90 = drivers.Rotation90 // 90 degrees clock-wise rotation
59+
ROTATION_180 = drivers.Rotation180
60+
ROTATION_270 = drivers.Rotation270
5961
)

st7735/st7735.go

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ import (
1414
)
1515

1616
type Model uint8
17-
type Rotation uint8
17+
18+
// Rotation controls the rotation used by the display.
19+
//
20+
// Deprecated: use drivers.Rotation instead.
21+
type Rotation = drivers.Rotation
1822

1923
var (
2024
errOutOfBounds = errors.New("rectangle coordinates outside display area")
@@ -31,7 +35,7 @@ type Device struct {
3135
height int16
3236
columnOffset int16
3337
rowOffset int16
34-
rotation Rotation
38+
rotation drivers.Rotation
3539
batchLength int16
3640
model Model
3741
isBGR bool
@@ -42,7 +46,7 @@ type Device struct {
4246
type Config struct {
4347
Width int16
4448
Height int16
45-
Rotation Rotation
49+
Rotation drivers.Rotation
4650
Model Model
4751
RowOffset int16
4852
ColumnOffset int16
@@ -215,7 +219,7 @@ func (d *Device) SetPixel(x int16, y int16, c color.RGBA) {
215219

216220
// setWindow prepares the screen to be modified at a given rectangle
217221
func (d *Device) setWindow(x, y, w, h int16) {
218-
if d.rotation == NO_ROTATION || d.rotation == ROTATION_180 {
222+
if d.rotation == drivers.Rotation0 || d.rotation == drivers.Rotation180 {
219223
x += d.columnOffset
220224
y += d.rowOffset
221225
} else {
@@ -345,35 +349,38 @@ func (d *Device) DrawFastHLine(x0, x1, y int16, c color.RGBA) {
345349

346350
// FillScreen fills the screen with a given color
347351
func (d *Device) FillScreen(c color.RGBA) {
348-
if d.rotation == NO_ROTATION || d.rotation == ROTATION_180 {
352+
if d.rotation == drivers.Rotation0 || d.rotation == drivers.Rotation180 {
349353
d.FillRectangle(0, 0, d.width, d.height, c)
350354
} else {
351355
d.FillRectangle(0, 0, d.height, d.width, c)
352356
}
353357
}
354358

359+
// Rotation returns the currently configured rotation.
360+
func (d *Device) Rotation() drivers.Rotation {
361+
return d.rotation
362+
}
363+
355364
// SetRotation changes the rotation of the device (clock-wise)
356-
func (d *Device) SetRotation(rotation Rotation) {
365+
func (d *Device) SetRotation(rotation drivers.Rotation) error {
366+
d.rotation = rotation
357367
madctl := uint8(0)
358368
switch rotation % 4 {
359-
case 0:
369+
case drivers.Rotation0:
360370
madctl = MADCTL_MX | MADCTL_MY
361-
break
362-
case 1:
371+
case drivers.Rotation90:
363372
madctl = MADCTL_MY | MADCTL_MV
364-
break
365-
case 2:
366-
break
367-
case 3:
373+
case drivers.Rotation180:
374+
// nothing to do
375+
case drivers.Rotation270:
368376
madctl = MADCTL_MX | MADCTL_MV
369-
break
370377
}
371378
if d.isBGR {
372379
madctl |= MADCTL_BGR
373380
}
374381
d.Command(MADCTL)
375382
d.Data(madctl)
376-
383+
return nil
377384
}
378385

379386
// Command sends a command to the display
@@ -394,7 +401,7 @@ func (d *Device) Tx(data []byte, isCommand bool) {
394401

395402
// Size returns the current size of the display.
396403
func (d *Device) Size() (w, h int16) {
397-
if d.rotation == NO_ROTATION || d.rotation == ROTATION_180 {
404+
if d.rotation == drivers.Rotation0 || d.rotation == drivers.Rotation180 {
398405
return d.width, d.height
399406
}
400407
return d.height, d.width

st7789/registers.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package st7789
22

3+
import "tinygo.org/x/drivers"
4+
35
// Registers
46
const (
57
NOP = 0x00
@@ -53,10 +55,10 @@ const (
5355
VSCRDEF = 0x33
5456
VSCRSADD = 0x37
5557

56-
NO_ROTATION Rotation = 0
57-
ROTATION_90 Rotation = 1 // 90 degrees clock-wise rotation
58-
ROTATION_180 Rotation = 2
59-
ROTATION_270 Rotation = 3
58+
NO_ROTATION = drivers.Rotation0
59+
ROTATION_90 = drivers.Rotation90 // 90 degrees clock-wise rotation
60+
ROTATION_180 = drivers.Rotation180
61+
ROTATION_270 = drivers.Rotation270
6062

6163
// Allowable frame rate codes for FRCTRL2 (Identifier is in Hz)
6264
FRAMERATE_111 FrameRate = 0x01

st7789/st7789.go

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ import (
1717
)
1818

1919
// Rotation controls the rotation used by the display.
20-
type Rotation uint8
20+
//
21+
// Deprecated: use drivers.Rotation instead.
22+
type Rotation = drivers.Rotation
2123

2224
// FrameRate controls the frame rate used by the display.
2325
type FrameRate uint8
@@ -39,7 +41,7 @@ type Device struct {
3941
rowOffsetCfg int16
4042
columnOffset int16
4143
rowOffset int16
42-
rotation Rotation
44+
rotation drivers.Rotation
4345
frameRate FrameRate
4446
batchLength int32
4547
isBGR bool
@@ -51,7 +53,7 @@ type Device struct {
5153
type Config struct {
5254
Width int16
5355
Height int16
54-
Rotation Rotation
56+
Rotation drivers.Rotation
5557
RowOffset int16
5658
ColumnOffset int16
5759
FrameRate FrameRate
@@ -251,8 +253,8 @@ func (d *Device) Display() error {
251253
// SetPixel sets a pixel in the screen
252254
func (d *Device) SetPixel(x int16, y int16, c color.RGBA) {
253255
if x < 0 || y < 0 ||
254-
(((d.rotation == NO_ROTATION || d.rotation == ROTATION_180) && (x >= d.width || y >= d.height)) ||
255-
((d.rotation == ROTATION_90 || d.rotation == ROTATION_270) && (x >= d.height || y >= d.width))) {
256+
(((d.rotation == drivers.Rotation0 || d.rotation == drivers.Rotation180) && (x >= d.width || y >= d.height)) ||
257+
((d.rotation == drivers.Rotation90 || d.rotation == drivers.Rotation270) && (x >= d.height || y >= d.width))) {
256258
return
257259
}
258260
d.FillRectangle(x, y, 1, 1, c)
@@ -373,35 +375,37 @@ func (d *Device) FillScreen(c color.RGBA) {
373375
}
374376
}
375377

378+
// Rotation returns the current rotation of the device.
379+
func (d *Device) Rotation() drivers.Rotation {
380+
return d.rotation
381+
}
382+
376383
// SetRotation changes the rotation of the device (clock-wise)
377-
func (d *Device) SetRotation(rotation Rotation) {
384+
func (d *Device) SetRotation(rotation Rotation) error {
378385
madctl := uint8(0)
379386
switch rotation % 4 {
380-
case 0:
387+
case drivers.Rotation0:
381388
madctl = MADCTL_MX | MADCTL_MY
382389
d.rowOffset = d.rowOffsetCfg
383390
d.columnOffset = d.columnOffsetCfg
384-
break
385-
case 1:
391+
case drivers.Rotation90:
386392
madctl = MADCTL_MY | MADCTL_MV
387393
d.rowOffset = d.columnOffsetCfg
388394
d.columnOffset = d.rowOffsetCfg
389-
break
390-
case 2:
395+
case drivers.Rotation180:
391396
d.rowOffset = 0
392397
d.columnOffset = 0
393-
break
394-
case 3:
398+
case drivers.Rotation270:
395399
madctl = MADCTL_MX | MADCTL_MV
396400
d.rowOffset = 0
397401
d.columnOffset = 0
398-
break
399402
}
400403
if d.isBGR {
401404
madctl |= MADCTL_BGR
402405
}
403406
d.Command(MADCTL)
404407
d.Data(madctl)
408+
return nil
405409
}
406410

407411
// Command sends a command to the display.
@@ -442,7 +446,7 @@ func (d *Device) Rx(command uint8, data []byte) {
442446

443447
// Size returns the current size of the display.
444448
func (d *Device) Size() (w, h int16) {
445-
if d.rotation == NO_ROTATION || d.rotation == ROTATION_180 {
449+
if d.rotation == drivers.Rotation0 || d.rotation == drivers.Rotation180 {
446450
return d.width, d.height
447451
}
448452
return d.height, d.width

0 commit comments

Comments
 (0)