Refactor display options in ssd1306.go (#77)

pull/80/head
czechbol 2 years ago committed by GitHub
parent f9d46888f5
commit 853aec60c1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -84,11 +84,13 @@ const (
// DefaultOpts is the recommended default options. // DefaultOpts is the recommended default options.
var DefaultOpts = Opts{ var DefaultOpts = Opts{
W: 128, W: 128,
H: 64, H: 64,
Rotated: false, Rotated: false,
Sequential: false, MirrorVertical: false,
SwapTopBottom: false, MirrorHorizontal: false,
Sequential: false,
SwapTopBottom: false,
} }
// Opts defines the options for the device. // Opts defines the options for the device.
@ -96,11 +98,21 @@ type Opts struct {
W int W int
H int H int
// Rotated determines if the display is rotated by 180°. // Rotated determines if the display is rotated by 180°.
//
// Deprecated: Use MirrorVertical and MirrorHorizontal instead.
Rotated bool Rotated bool
// Sequential corresponds to the Sequential/Alternative COM pin configuration // Sequential corresponds to the Sequential/Alternative COM pin configuration
// in the OLED panel hardware. Try toggling this if half the rows appear to be // in the OLED panel hardware. Try toggling this if half the rows appear to be
// missing on your display. // missing on your display.
Sequential bool 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 // 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 // the OLED panel hardware. Try toggling this if the top and bottom halves of
// your display are swapped. // your display are swapped.
@ -354,6 +366,12 @@ func getInitCmd(opts *Opts) []byte {
comScan = 0xC0 comScan = 0xC0
columnAddr = byte(0xA0) columnAddr = byte(0xA0)
} }
if opts.MirrorVertical {
comScan = byte(0xC0)
}
if opts.MirrorHorizontal {
columnAddr = byte(0xA0)
}
// See page 40. // See page 40.
hwLayout := byte(0x02) hwLayout := byte(0x02)
if !opts.Sequential { if !opts.Sequential {

@ -29,7 +29,7 @@ func TestNewI2C_fail(t *testing.T) {
if d, err := NewI2C(&bus, &Opts{W: 64}); d != nil || err == nil { if d, err := NewI2C(&bus, &Opts{W: 64}); d != nil || err == nil {
t.Fatal(d, err) 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) t.Fatal(d, err)
} }
if err := bus.Close(); err != nil { if err := bus.Close(); err != nil {
@ -491,7 +491,7 @@ func TestSPI_3wire(t *testing.T) {
func TestSPI_4wire_String(t *testing.T) { func TestSPI_4wire_String(t *testing.T) {
port := spitest.Playback{ port := spitest.Playback{
Playback: conntest.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) 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{ port := spitest.Playback{
Playback: conntest.Playback{ Playback: conntest.Playback{
Ops: []conntest.IO{ 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 // Page 1
{W: []byte{0xB0, 0x00, 0x10}}, {W: []byte{0xB0, 0x00, 0x10}},
@ -573,7 +573,7 @@ func TestSPI_4wire_Write_differential_fail(t *testing.T) {
port := spitest.Playback{ port := spitest.Playback{
Playback: conntest.Playback{ Playback: conntest.Playback{
Ops: []conntest.IO{ 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 // Page 1
{W: []byte{0xB0, 0x00, 0x10}}, {W: []byte{0xB0, 0x00, 0x10}},
{W: buf1}, {W: buf1},
@ -623,7 +623,7 @@ func TestSPI_4wire_Write_differential_fail(t *testing.T) {
func TestSPI_4wire_gpio_fail(t *testing.T) { func TestSPI_4wire_gpio_fail(t *testing.T) {
port := spitest.Playback{ port := spitest.Playback{
Playback: conntest.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} pin := &failPin{fail: false}
@ -666,7 +666,7 @@ func TestInitCmd(t *testing.T) {
// //
func initCmdI2C() []byte { 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 { func getI2CPlayback() *i2ctest.Playback {

@ -94,7 +94,11 @@ func (s *SmokeTest) Run(f *flag.FlagSet, args []string) (err error) {
if len(*dcName) != 0 { if len(*dcName) != 0 {
dc = gpioreg.ByName(*dcName) 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 { if !*record {
return s.run(i2cBus, spiPort, dc, &opts) return s.run(i2cBus, spiPort, dc, &opts)
} }

Loading…
Cancel
Save