Skip to content

Commit

Permalink
uc8151: update to support all functions needed by tinygl and board pa…
Browse files Browse the repository at this point in the history
…ckage Displayer interface

Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed May 7, 2024
1 parent bb0e6b8 commit b270b0a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 20 deletions.
3 changes: 2 additions & 1 deletion examples/uc8151/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"image/color"
"machine"

"tinygo.org/x/drivers"
"tinygo.org/x/drivers/uc8151"
)

Expand All @@ -21,7 +22,7 @@ func main() {

display = uc8151.New(machine.SPI0, machine.EPD_CS_PIN, machine.EPD_DC_PIN, machine.EPD_RESET_PIN, machine.EPD_BUSY_PIN)
display.Configure(uc8151.Config{
Rotation: uc8151.ROTATION_270,
Rotation: drivers.Rotation270,
Speed: uc8151.MEDIUM,
Blocking: true,
})
Expand Down
5 changes: 0 additions & 5 deletions uc8151/registers.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,6 @@ const (
HZ_100 = 0b00111010
HZ_200 = 0b00111001

NO_ROTATION Rotation = 0
ROTATION_90 Rotation = 1 // 90 degrees clock-wise rotation
ROTATION_180 Rotation = 2
ROTATION_270 Rotation = 3

DEFAULT Speed = 0
MEDIUM Speed = 1
FAST Speed = 2
Expand Down
60 changes: 46 additions & 14 deletions uc8151/uc8151.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@ import (
"time"

"tinygo.org/x/drivers"
"tinygo.org/x/drivers/pixel"
)

var (
errOutOfRange = errors.New("out of screen range")
)

type Config struct {
Width int16
Height int16
Rotation Rotation // Rotation is clock-wise
Speed Speed // Value from DEFAULT, MEDIUM, FAST, TURBO
Rotation drivers.Rotation // Rotation is clock-wise
Speed Speed // Value from DEFAULT, MEDIUM, FAST, TURBO
Blocking bool
}

Expand All @@ -31,12 +36,11 @@ type Device struct {
height int16
buffer []uint8
bufferLength uint32
rotation Rotation
rotation drivers.Rotation
speed Speed
blocking bool
}

type Rotation uint8
type Speed uint8

// New returns a new epd2in13x driver. Pass in a fully configured SPI bus.
Expand Down Expand Up @@ -175,6 +179,22 @@ func (d *Device) SetPixel(x int16, y int16, c color.RGBA) {
}
}

// DrawBitmap copies the bitmap to the screen at the given coordinates.
func (d *Device) DrawBitmap(x, y int16, bitmap pixel.Image[pixel.Monochrome]) error {
width, height := bitmap.Size()
if x < 0 || x+int16(width) > d.width || y < 0 || y+int16(height) > d.height {
return errOutOfRange
}

for i := 0; i < width; i++ {
for j := 0; j < height; j++ {
d.SetPixel(x+int16(i), y+int16(j), bitmap.Get(i, j).RGBA())
}
}

return nil
}

// Display sends the buffer to the screen.
func (d *Device) Display() error {
if d.blocking {
Expand Down Expand Up @@ -208,13 +228,14 @@ func (d *Device) DisplayRect(x int16, y int16, width int16, height int16) error
if x < 0 || y < 0 || x >= d.width || y >= d.height || width < 0 || height < 0 {
return errors.New("wrong rectangle")
}
if d.rotation == ROTATION_90 {
switch d.rotation {
case drivers.Rotation0:
width, height = height, width
x -= width
} else if d.rotation == ROTATION_180 {
case drivers.Rotation90:
x -= width - 1
y -= height - 1
} else if d.rotation == ROTATION_270 {
case drivers.Rotation180:
width, height = height, width
y -= height
}
Expand Down Expand Up @@ -287,32 +308,43 @@ 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.width
}
return d.width, d.height
}

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

// SetRotation changes the rotation (clock-wise) of the device
func (d *Device) SetRotation(rotation Rotation) {
func (d *Device) SetRotation(rotation drivers.Rotation) error {
d.rotation = rotation
return nil
}

// Set the sleep mode for this display. SInce it is e-ink, it is not really sleep mode. :)
func (d *Device) Sleep(sleepEnabled bool) error {
return nil
}

// SetBlocking changes the blocking flag of the device
func (d *Device) SetBlocking(blocking bool) {
d.blocking = blocking
}

// xy chages the coordinates according to the rotation
// xy changes 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
Expand Down

0 comments on commit b270b0a

Please sign in to comment.