Skip to content

Commit

Permalink
Refactor display options in ssd1306.go (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
czechbol authored Oct 21, 2024
1 parent f9d4688 commit 853aec6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
28 changes: 23 additions & 5 deletions ssd1306/ssd1306.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,35 @@ 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.
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.
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions ssd1306/ssd1306_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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}},
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion ssd1306/ssd1306smoketest/ssd1306smoketest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 853aec6

Please sign in to comment.