i2c: SSD1306.WithDisplayHeight() and SSD1306.WithDisplayWidth() for SSD1306 that use different display ratios

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2017-10-22 10:54:31 +02:00
parent 9f1e5fe34b
commit 9a6c270521
2 changed files with 44 additions and 9 deletions

View File

@ -117,7 +117,9 @@ type SSD1306Driver struct {
Config
gobot.Commander
Buffer *DisplayBuffer
DisplayWidth int
DisplayHeight int
Buffer *DisplayBuffer
}
// NewSSD1306Driver creates a new SSD1306Driver.
@ -126,22 +128,27 @@ type SSD1306Driver struct {
// conn Connector - the Adaptor to use with this Driver
//
// Optional params:
// WithBus(int): bus to use with this driver
// WithAddress(int): address to use with this driver
// WithBus(int): bus to use with this driver
// WithAddress(int): address to use with this driver
// WithDisplayWidth(int): width of display (defaults to 128)
// WithDisplayHeight(int): height of display (defaults to 64)
//
func NewSSD1306Driver(a Connector, options ...func(Config)) *SSD1306Driver {
s := &SSD1306Driver{
name: gobot.DefaultName("SSD1306"),
Commander: gobot.NewCommander(),
connector: a,
Config: NewConfig(),
Buffer: NewDisplayBuffer(ssd1306Width, ssd1306Height),
name: gobot.DefaultName("SSD1306"),
Commander: gobot.NewCommander(),
connector: a,
Config: NewConfig(),
DisplayHeight: ssd1306Height,
DisplayWidth: ssd1306Width,
}
for _, option := range options {
option(s)
}
s.Buffer = NewDisplayBuffer(s.DisplayWidth, s.DisplayHeight)
s.AddCommand("Display", func(params map[string]interface{}) interface{} {
err := s.Display()
return map[string]interface{}{"err": err}
@ -208,6 +215,32 @@ func (s *SSD1306Driver) Start() (err error) {
// Halt returns true if device is halted successfully
func (s *SSD1306Driver) Halt() (err error) { return nil }
// WithDisplayWidth option sets the SSD1306Driver DisplayWidth option.
func WithDisplayWidth(val int) func(Config) {
return func(c Config) {
d, ok := c.(*SSD1306Driver)
if ok {
d.DisplayWidth = val
} else {
// TODO: return error for trying to set DisplayWidth for non-SSD1306Driver
return
}
}
}
// WithDisplayHeight option sets the SSD1306Driver DisplayHeight option.
func WithDisplayHeight(val int) func(Config) {
return func(c Config) {
d, ok := c.(*SSD1306Driver)
if ok {
d.DisplayHeight = val
} else {
// TODO: return error for trying to set DisplayHeight for non-SSD1306Driver
return
}
}
}
// Init turns display on
func (s *SSD1306Driver) Init() (err error) {
s.Off()

View File

@ -100,8 +100,10 @@ func TestSSD1306DriverName(t *testing.T) {
}
func TestSSD1306DriverOptions(t *testing.T) {
s := NewSSD1306Driver(newI2cTestAdaptor(), WithBus(2))
s := NewSSD1306Driver(newI2cTestAdaptor(), WithBus(2), WithDisplayHeight(32), WithDisplayWidth(128))
gobottest.Assert(t, s.GetBusOrDefault(1), 2)
gobottest.Assert(t, s.DisplayHeight, 32)
gobottest.Assert(t, s.DisplayWidth, 128)
}
func TestSSD1306DriverDisplay(t *testing.T) {