Skip to content

Commit d688fa3

Browse files
authored
ssd1306: Add function SetFlip and GetFlip (#702)
* ssd1306: Add SetRotation function
1 parent dd44f92 commit d688fa3

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

ssd1306/registers.go

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

3+
import "tinygo.org/x/drivers"
4+
35
// Registers
46
const (
57
Address = 0x3D
@@ -38,4 +40,7 @@ const (
3840

3941
EXTERNALVCC VccMode = 0x1
4042
SWITCHCAPVCC VccMode = 0x2
43+
44+
NO_ROTATION = drivers.Rotation0
45+
ROTATION_180 = drivers.Rotation180
4146
)

ssd1306/ssd1306.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ import (
1515
)
1616

1717
var (
18-
errBufferSize = errors.New("invalid size buffer")
19-
errOutOfRange = errors.New("out of screen range")
20-
errNotImplemented = errors.New("not implemented")
18+
errBufferSize = errors.New("invalid size buffer")
19+
errOutOfRange = errors.New("out of screen range")
2120
)
2221

2322
type ResetValue [2]byte
@@ -33,6 +32,7 @@ type Device struct {
3332
canReset bool
3433
resetCol ResetValue
3534
resetPage ResetValue
35+
rotation drivers.Rotation
3636
}
3737

3838
// Config is the configuration for the display
@@ -48,6 +48,7 @@ type Config struct {
4848
// If you're using a different size, you might need to set these values manually.
4949
ResetCol ResetValue
5050
ResetPage ResetValue
51+
Rotation drivers.Rotation
5152
}
5253

5354
type I2CBus struct {
@@ -149,8 +150,8 @@ func (d *Device) Configure(cfg Config) {
149150
}
150151
d.Command(MEMORYMODE)
151152
d.Command(0x00)
152-
d.Command(SEGREMAP | 0x1)
153-
d.Command(COMSCANDEC)
153+
154+
d.SetRotation(cfg.Rotation)
154155

155156
if (d.width == 128 && d.height == 64) || (d.width == 64 && d.height == 48) { // 128x64 or 64x48
156157
d.Command(SETCOMPINS)
@@ -363,13 +364,25 @@ func (d *Device) DrawBitmap(x, y int16, bitmap pixel.Image[pixel.Monochrome]) er
363364

364365
// Rotation returns the currently configured rotation.
365366
func (d *Device) Rotation() drivers.Rotation {
366-
return drivers.Rotation0
367+
return d.rotation
367368
}
368369

369370
// SetRotation changes the rotation of the device (clock-wise).
370-
// Would have to be implemented in software for this device.
371371
func (d *Device) SetRotation(rotation drivers.Rotation) error {
372-
return errNotImplemented
372+
d.rotation = rotation
373+
switch d.rotation {
374+
case drivers.Rotation0:
375+
d.Command(SEGREMAP | 0x1) // Reverse horizontal mapping
376+
d.Command(COMSCANDEC) // Reverse vertical mapping
377+
case drivers.Rotation180:
378+
d.Command(SEGREMAP) // Normal horizontal mapping
379+
d.Command(COMSCANINC) // Normal vertical mapping
380+
// nothing to do
381+
default:
382+
d.Command(SEGREMAP | 0x1) // Reverse horizontal mapping
383+
d.Command(COMSCANDEC) // Reverse vertical mapping
384+
}
385+
return nil
373386
}
374387

375388
// Set the sleep mode for this display. When sleeping, the panel uses a lot

0 commit comments

Comments
 (0)