From 14994a3f31aa0dcd1475967442236befe618fece Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Mon, 6 Nov 2023 18:17:51 +0100 Subject: [PATCH] epd2in13: unify rotation configuration with other displays This commit updates rotation behavior to match other displays. For more information, see: https://github.com/tinygo-org/drivers/pull/550 This changes the signature of `SetRotation` but I don't think any existing code will be affected by this change. --- waveshare-epd/epd2in13/epd2in13.go | 35 +++++++++++++++++------------ waveshare-epd/epd2in13/registers.go | 10 +++++---- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/waveshare-epd/epd2in13/epd2in13.go b/waveshare-epd/epd2in13/epd2in13.go index c0d190f..bc289ad 100644 --- a/waveshare-epd/epd2in13/epd2in13.go +++ b/waveshare-epd/epd2in13/epd2in13.go @@ -15,8 +15,8 @@ import ( type Config struct { Width int16 // Width is the display resolution Height int16 - LogicalWidth int16 // LogicalWidth must be a multiple of 8 and same size or bigger than Width - Rotation Rotation // Rotation is clock-wise + LogicalWidth int16 // LogicalWidth must be a multiple of 8 and same size or bigger than Width + Rotation drivers.Rotation } type Device struct { @@ -30,10 +30,11 @@ type Device struct { height int16 buffer []uint8 bufferLength uint32 - rotation Rotation + rotation drivers.Rotation } -type Rotation uint8 +// Deprecated: use drivers.Rotation instead. +type Rotation = drivers.Rotation // Look up table for full updates var lutFullUpdate = [30]uint8{ @@ -209,13 +210,13 @@ func (d *Device) DisplayRect(x int16, y int16, width int16, height int16) error if x < 0 || y < 0 || x >= d.logicalWidth || y >= d.height || width < 0 || height < 0 { return errors.New("wrong rectangle") } - if d.rotation == ROTATION_90 { + if d.rotation == drivers.Rotation90 { width, height = height, width x -= width - } else if d.rotation == ROTATION_180 { + } else if d.rotation == drivers.Rotation180 { x -= width - 1 y -= height - 1 - } else if d.rotation == ROTATION_270 { + } else if d.rotation == drivers.Rotation270 { width, height = height, width y -= height } @@ -301,27 +302,33 @@ func (d *Device) ClearBuffer() { // Size returns the current size of the display. func (d *Device) Size() (w, h int16) { - if d.rotation == ROTATION_90 || d.rotation == ROTATION_270 { + if d.rotation == drivers.Rotation90 || d.rotation == drivers.Rotation270 { return d.height, d.logicalWidth } return d.logicalWidth, d.height } -// SetRotation changes the rotation (clock-wise) of the device -func (d *Device) SetRotation(rotation 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. +func (d *Device) SetRotation(rotation drivers.Rotation) error { d.rotation = rotation + return nil } // xy chages the coordinates according to the rotation func (d *Device) xy(x, y int16) (int16, int16) { switch d.rotation { - case NO_ROTATION: + case drivers.Rotation0: return x, y - case ROTATION_90: + case drivers.Rotation90: return d.width - y - 1, x - case ROTATION_180: + case drivers.Rotation180: return d.width - x - 1, d.height - y - 1 - case ROTATION_270: + case drivers.Rotation270: return y, d.height - x - 1 } return x, y diff --git a/waveshare-epd/epd2in13/registers.go b/waveshare-epd/epd2in13/registers.go index b5ee2e7..4ebd0da 100644 --- a/waveshare-epd/epd2in13/registers.go +++ b/waveshare-epd/epd2in13/registers.go @@ -1,5 +1,7 @@ package epd2in13 +import "tinygo.org/x/drivers" + // Registers const ( DRIVER_OUTPUT_CONTROL = 0x01 @@ -24,8 +26,8 @@ const ( SET_RAM_Y_ADDRESS_COUNTER = 0x4F TERMINATE_FRAME_READ_WRITE = 0xFF - 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 )