Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions displayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,19 @@ type Displayer interface {
// Display sends the buffer (if any) to the screen.
Display() error
}

// Rotation is how much a display has been rotated. Displays can be rotated, and
// sometimes also mirrored.
type Rotation uint8

// Clockwise rotation of the screen.
const (
Rotation0 = iota
Rotation90
Rotation180
Rotation270
Rotation0Mirror
Rotation90Mirror
Rotation180Mirror
Rotation270Mirror
)
22 changes: 16 additions & 6 deletions ili9341/ili9341.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ import (
"image/color"
"machine"
"time"

"tinygo.org/x/drivers"
)

type Config struct {
Width int16
Height int16
Rotation Rotation
Rotation drivers.Rotation
DisplayInversion bool
}

type Device struct {
width int16
height int16
rotation Rotation
rotation drivers.Rotation
driver driver

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

// GetRotation returns the current rotation of the device
func (d *Device) GetRotation() Rotation {
// Rotation returns the current rotation of the device.
func (d *Device) Rotation() drivers.Rotation {
return d.rotation
}

// SetRotation changes the rotation of the device (clock-wise)
func (d *Device) SetRotation(rotation Rotation) {
// GetRotation returns the current rotation of the device.
//
// Deprecated: use Rotation instead.
func (d *Device) GetRotation() drivers.Rotation {
return d.rotation
}

// SetRotation changes the rotation of the device (clock-wise).
func (d *Device) SetRotation(rotation drivers.Rotation) error {
madctl := uint8(0)
switch rotation % 8 {
case Rotation0:
Expand All @@ -291,6 +300,7 @@ func (d *Device) SetRotation(rotation Rotation) {
cmdBuf[0] = madctl
d.sendCommand(MADCTL, cmdBuf[:1])
d.rotation = rotation
return nil
}

// SetScrollArea sets an area to scroll with fixed top/bottom or left/right parts of the display
Expand Down
20 changes: 11 additions & 9 deletions ili9341/registers.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ili9341

import "tinygo.org/x/drivers"

type Rotation uint8

const (
Expand Down Expand Up @@ -77,13 +79,13 @@ const (
)

const (
Rotation0 Rotation = 0
Rotation90 Rotation = 1 // 90 degrees clock-wise rotation
Rotation180 Rotation = 2
Rotation270 Rotation = 3

Rotation0Mirror Rotation = 4
Rotation90Mirror Rotation = 5
Rotation180Mirror Rotation = 6
Rotation270Mirror Rotation = 7
Rotation0 = drivers.Rotation0
Rotation90 = drivers.Rotation90 // 90 degrees clock-wise rotation
Rotation180 = drivers.Rotation180
Rotation270 = drivers.Rotation270

Rotation0Mirror = drivers.Rotation0Mirror
Rotation90Mirror = drivers.Rotation90Mirror
Rotation180Mirror = drivers.Rotation180Mirror
Rotation270Mirror = drivers.Rotation270Mirror
)
10 changes: 6 additions & 4 deletions st7735/registers.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package st7735

import "tinygo.org/x/drivers"

// Registers
const (
NOP = 0x00
Expand Down Expand Up @@ -52,8 +54,8 @@ const (
GREENTAB Model = 0
MINI80x160 Model = 1

NO_ROTATION Rotation = 0
ROTATION_90 Rotation = 1 // 90 degrees clock-wise rotation
ROTATION_180 Rotation = 2
ROTATION_270 Rotation = 3
NO_ROTATION = drivers.Rotation0
ROTATION_90 = drivers.Rotation90 // 90 degrees clock-wise rotation
ROTATION_180 = drivers.Rotation180
ROTATION_270 = drivers.Rotation270
)
39 changes: 23 additions & 16 deletions st7735/st7735.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import (
)

type Model uint8
type Rotation uint8

// Rotation controls the rotation used by the display.
//
// Deprecated: use drivers.Rotation instead.
type Rotation = drivers.Rotation

var (
errOutOfBounds = errors.New("rectangle coordinates outside display area")
Expand All @@ -31,7 +35,7 @@ type Device struct {
height int16
columnOffset int16
rowOffset int16
rotation Rotation
rotation drivers.Rotation
batchLength int16
model Model
isBGR bool
Expand All @@ -42,7 +46,7 @@ type Device struct {
type Config struct {
Width int16
Height int16
Rotation Rotation
Rotation drivers.Rotation
Model Model
RowOffset int16
ColumnOffset int16
Expand Down Expand Up @@ -215,7 +219,7 @@ func (d *Device) SetPixel(x int16, y int16, c color.RGBA) {

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

// FillScreen fills the screen with a given color
func (d *Device) FillScreen(c color.RGBA) {
if d.rotation == NO_ROTATION || d.rotation == ROTATION_180 {
if d.rotation == drivers.Rotation0 || d.rotation == drivers.Rotation180 {
d.FillRectangle(0, 0, d.width, d.height, c)
} else {
d.FillRectangle(0, 0, d.height, d.width, c)
}
}

// Rotation returns the currently configured rotation.
func (d *Device) Rotation() drivers.Rotation {
return d.rotation
}

// SetRotation changes the rotation of the device (clock-wise)
func (d *Device) SetRotation(rotation Rotation) {
func (d *Device) SetRotation(rotation drivers.Rotation) error {
d.rotation = rotation
madctl := uint8(0)
switch rotation % 4 {
case 0:
case drivers.Rotation0:
madctl = MADCTL_MX | MADCTL_MY
break
case 1:
case drivers.Rotation90:
madctl = MADCTL_MY | MADCTL_MV
break
case 2:
break
case 3:
case drivers.Rotation180:
// nothing to do
case drivers.Rotation270:
madctl = MADCTL_MX | MADCTL_MV
break
}
if d.isBGR {
madctl |= MADCTL_BGR
}
d.Command(MADCTL)
d.Data(madctl)

return nil
}

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

// Size returns the current size of the display.
func (d *Device) Size() (w, h int16) {
if d.rotation == NO_ROTATION || d.rotation == ROTATION_180 {
if d.rotation == drivers.Rotation0 || d.rotation == drivers.Rotation180 {
return d.width, d.height
}
return d.height, d.width
Expand Down
10 changes: 6 additions & 4 deletions st7789/registers.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package st7789

import "tinygo.org/x/drivers"

// Registers
const (
NOP = 0x00
Expand Down Expand Up @@ -53,10 +55,10 @@ const (
VSCRDEF = 0x33
VSCRSADD = 0x37

NO_ROTATION Rotation = 0
ROTATION_90 Rotation = 1 // 90 degrees clock-wise rotation
ROTATION_180 Rotation = 2
ROTATION_270 Rotation = 3
NO_ROTATION = drivers.Rotation0
ROTATION_90 = drivers.Rotation90 // 90 degrees clock-wise rotation
ROTATION_180 = drivers.Rotation180
ROTATION_270 = drivers.Rotation270

// Allowable frame rate codes for FRCTRL2 (Identifier is in Hz)
FRAMERATE_111 FrameRate = 0x01
Expand Down
34 changes: 19 additions & 15 deletions st7789/st7789.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import (
)

// Rotation controls the rotation used by the display.
type Rotation uint8
//
// Deprecated: use drivers.Rotation instead.
type Rotation = drivers.Rotation

// FrameRate controls the frame rate used by the display.
type FrameRate uint8
Expand All @@ -39,7 +41,7 @@ type Device struct {
rowOffsetCfg int16
columnOffset int16
rowOffset int16
rotation Rotation
rotation drivers.Rotation
frameRate FrameRate
batchLength int32
isBGR bool
Expand All @@ -51,7 +53,7 @@ type Device struct {
type Config struct {
Width int16
Height int16
Rotation Rotation
Rotation drivers.Rotation
RowOffset int16
ColumnOffset int16
FrameRate FrameRate
Expand Down Expand Up @@ -251,8 +253,8 @@ func (d *Device) Display() error {
// SetPixel sets a pixel in the screen
func (d *Device) SetPixel(x int16, y int16, c color.RGBA) {
if x < 0 || y < 0 ||
(((d.rotation == NO_ROTATION || d.rotation == ROTATION_180) && (x >= d.width || y >= d.height)) ||
((d.rotation == ROTATION_90 || d.rotation == ROTATION_270) && (x >= d.height || y >= d.width))) {
(((d.rotation == drivers.Rotation0 || d.rotation == drivers.Rotation180) && (x >= d.width || y >= d.height)) ||
((d.rotation == drivers.Rotation90 || d.rotation == drivers.Rotation270) && (x >= d.height || y >= d.width))) {
return
}
d.FillRectangle(x, y, 1, 1, c)
Expand Down Expand Up @@ -373,35 +375,37 @@ func (d *Device) FillScreen(c color.RGBA) {
}
}

// Rotation returns the current rotation of the device.
func (d *Device) Rotation() drivers.Rotation {
return d.rotation
}

// SetRotation changes the rotation of the device (clock-wise)
func (d *Device) SetRotation(rotation Rotation) {
func (d *Device) SetRotation(rotation Rotation) error {
madctl := uint8(0)
switch rotation % 4 {
case 0:
case drivers.Rotation0:
madctl = MADCTL_MX | MADCTL_MY
d.rowOffset = d.rowOffsetCfg
d.columnOffset = d.columnOffsetCfg
break
case 1:
case drivers.Rotation90:
madctl = MADCTL_MY | MADCTL_MV
d.rowOffset = d.columnOffsetCfg
d.columnOffset = d.rowOffsetCfg
break
case 2:
case drivers.Rotation180:
d.rowOffset = 0
d.columnOffset = 0
break
case 3:
case drivers.Rotation270:
madctl = MADCTL_MX | MADCTL_MV
d.rowOffset = 0
d.columnOffset = 0
break

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These break instructions aren't necessary in Go (it's the default behavior, unlike C/C++).

}
if d.isBGR {
madctl |= MADCTL_BGR
}
d.Command(MADCTL)
d.Data(madctl)
return nil
}

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

// Size returns the current size of the display.
func (d *Device) Size() (w, h int16) {
if d.rotation == NO_ROTATION || d.rotation == ROTATION_180 {
if d.rotation == drivers.Rotation0 || d.rotation == drivers.Rotation180 {
return d.width, d.height
}
return d.height, d.width
Expand Down