From 853aec60c1aed19a4f585a51e33169277a4aecc0 Mon Sep 17 00:00:00 2001 From: czechbol Date: Mon, 21 Oct 2024 22:37:53 +0200 Subject: [PATCH] Refactor display options in ssd1306.go (#77) --- ssd1306/ssd1306.go | 28 ++++++++++++++++---- ssd1306/ssd1306_test.go | 12 ++++----- ssd1306/ssd1306smoketest/ssd1306smoketest.go | 6 ++++- 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/ssd1306/ssd1306.go b/ssd1306/ssd1306.go index 6c17079..39fd2d2 100644 --- a/ssd1306/ssd1306.go +++ b/ssd1306/ssd1306.go @@ -84,11 +84,13 @@ const ( // DefaultOpts is the recommended default options. var DefaultOpts = Opts{ - W: 128, - H: 64, - Rotated: false, - Sequential: false, - SwapTopBottom: false, + W: 128, + H: 64, + Rotated: false, + MirrorVertical: false, + MirrorHorizontal: false, + Sequential: false, + SwapTopBottom: false, } // Opts defines the options for the device. @@ -96,11 +98,21 @@ type Opts struct { W int H int // Rotated determines if the display is rotated by 180°. + // + // Deprecated: Use MirrorVertical and MirrorHorizontal instead. Rotated bool // Sequential corresponds to the Sequential/Alternative COM pin configuration // in the OLED panel hardware. Try toggling this if half the rows appear to be // missing on your display. Sequential bool + // MirrorVertical corresponds to the COM remap configuration in the OLED panel + // hardware. Try toggling this if the display is flipped vertically. + // Overwrites Rotated. + MirrorVertical bool + // MirrorHorizontal corresponds to the SEG remap configuration in the OLED panel + // hardware. Try toggling this if the display is flipped horizontally. + // Overwrites Rotated. + MirrorHorizontal bool // SwapTopBottom corresponds to the Left/Right remap COM pin configuration in // the OLED panel hardware. Try toggling this if the top and bottom halves of // your display are swapped. @@ -354,6 +366,12 @@ func getInitCmd(opts *Opts) []byte { comScan = 0xC0 columnAddr = byte(0xA0) } + if opts.MirrorVertical { + comScan = byte(0xC0) + } + if opts.MirrorHorizontal { + columnAddr = byte(0xA0) + } // See page 40. hwLayout := byte(0x02) if !opts.Sequential { diff --git a/ssd1306/ssd1306_test.go b/ssd1306/ssd1306_test.go index af4b671..0db41c3 100644 --- a/ssd1306/ssd1306_test.go +++ b/ssd1306/ssd1306_test.go @@ -29,7 +29,7 @@ func TestNewI2C_fail(t *testing.T) { if d, err := NewI2C(&bus, &Opts{W: 64}); d != nil || err == nil { t.Fatal(d, err) } - if d, err := NewI2C(&bus, &Opts{W: 64, H: 64, Rotated: true}); d != nil || err == nil { + if d, err := NewI2C(&bus, &Opts{W: 64, H: 64, MirrorVertical: true, MirrorHorizontal: true}); d != nil || err == nil { t.Fatal(d, err) } if err := bus.Close(); err != nil { @@ -491,7 +491,7 @@ func TestSPI_3wire(t *testing.T) { func TestSPI_4wire_String(t *testing.T) { port := spitest.Playback{ Playback: conntest.Playback{ - Ops: []conntest.IO{{W: getInitCmd(&Opts{W: 128, H: 64, Rotated: false})}}, + Ops: []conntest.IO{{W: getInitCmd(&Opts{W: 128, H: 64, MirrorVertical: false, MirrorHorizontal: false})}}, }, } dev, err := NewSPI(&port, &gpiotest.Pin{N: "pin1", Num: 42}, &DefaultOpts) @@ -516,7 +516,7 @@ func TestSPI_4wire_Write_differential(t *testing.T) { port := spitest.Playback{ Playback: conntest.Playback{ Ops: []conntest.IO{ - {W: getInitCmd(&Opts{W: 128, H: 64, Rotated: false})}, + {W: getInitCmd(&Opts{W: 128, H: 64, MirrorVertical: false, MirrorHorizontal: false})}, // Page 1 {W: []byte{0xB0, 0x00, 0x10}}, @@ -573,7 +573,7 @@ func TestSPI_4wire_Write_differential_fail(t *testing.T) { port := spitest.Playback{ Playback: conntest.Playback{ Ops: []conntest.IO{ - {W: getInitCmd(&Opts{W: 128, H: 64, Rotated: false})}, + {W: getInitCmd(&Opts{W: 128, H: 64, MirrorVertical: false, MirrorHorizontal: false})}, // Page 1 {W: []byte{0xB0, 0x00, 0x10}}, {W: buf1}, @@ -623,7 +623,7 @@ func TestSPI_4wire_Write_differential_fail(t *testing.T) { func TestSPI_4wire_gpio_fail(t *testing.T) { port := spitest.Playback{ Playback: conntest.Playback{ - Ops: []conntest.IO{{W: getInitCmd(&Opts{W: 128, H: 64, Rotated: false})}}, + Ops: []conntest.IO{{W: getInitCmd(&Opts{W: 128, H: 64, MirrorVertical: false, MirrorHorizontal: false})}}, }, } pin := &failPin{fail: false} @@ -666,7 +666,7 @@ func TestInitCmd(t *testing.T) { // func initCmdI2C() []byte { - return append([]byte{0}, getInitCmd(&Opts{W: 128, H: 64, Rotated: false})...) + return append([]byte{0}, getInitCmd(&Opts{W: 128, H: 64, MirrorVertical: false, MirrorHorizontal: false})...) } func getI2CPlayback() *i2ctest.Playback { diff --git a/ssd1306/ssd1306smoketest/ssd1306smoketest.go b/ssd1306/ssd1306smoketest/ssd1306smoketest.go index a101579..b10586d 100644 --- a/ssd1306/ssd1306smoketest/ssd1306smoketest.go +++ b/ssd1306/ssd1306smoketest/ssd1306smoketest.go @@ -94,7 +94,11 @@ func (s *SmokeTest) Run(f *flag.FlagSet, args []string) (err error) { if len(*dcName) != 0 { dc = gpioreg.ByName(*dcName) } - opts := ssd1306.Opts{W: *w, H: *h, Rotated: *rotated} + opts := ssd1306.Opts{W: *w, H: *h} + if *rotated { + opts.MirrorHorizontal = true + opts.MirrorVertical = true + } if !*record { return s.run(i2cBus, spiPort, dc, &opts) }