diff --git a/ssd1306/ssd1306.go b/ssd1306/ssd1306.go index 6241bd0..f98e616 100644 --- a/ssd1306/ssd1306.go +++ b/ssd1306/ssd1306.go @@ -40,56 +40,43 @@ type OLED struct { var initSeq = []byte{ 0xae, - 0x00, - 0x10, - 0x40, - 0x81, - 0xcf, - 0xa1, - 0xc8, - 0xa6, - 0xa4, - 0xa8, - 0x3f, - 0xd3, - 0x00, - 0xd5, - 0x80, - 0xd9, - 0xf1, - 0xda, - 0x12, - 0xdb, - 0x40, - 0x20, - 0x00, - 0x8d, - 0x14, - 0xa5, + 0x00 | 0x00, // row offset + 0x10 | 0x00, // column offset + 0xd5, 0x40, + 0xa8, ssd1306_LCDHEIGHT - 1, + 0xd3, 0x00, // set display offset to no offset + 0x40 | 0, + 0x8d, 0x14, + 0x20, 0x0, + + 0xA0 | 0x1, + 0xC8, + 0xda, 0x12, + 0x81, 0x9f, // set contast + 0x9d, 0xf1, + 0xdb, 0x40, + 0xa4, 0xa6, + + 0x2E, 0xaf, } // Open opens an SSD1306 OLED display. Once not in use, it needs to // be close by calling Close. // The default width is 128, height is 64 if zero values are given. -func Open(o driver.Opener, width, height int) (*OLED, error) { - if width == 0 { - width = ssd1306_LCDWIDTH - } - if height == 0 { - height = ssd1306_LCDHEIGHT - } +func Open(o driver.Opener) (*OLED, error) { + w := ssd1306_LCDWIDTH + h := ssd1306_LCDHEIGHT dev, err := i2c.Open(o) if err != nil { return nil, err } if err := dev.Write(initSeq); err != nil { - dev.Close() return nil, err } - buf := make([]byte, width*(height/8)+1) + buf := make([]byte, w*(h/8)+1) buf[0] = 0x40 // start frame of pixel data - return &OLED{dev: dev, w: width, h: height, buf: buf}, nil + return &OLED{dev: dev, w: w, h: h, buf: buf}, nil } // On turns on the display if it is off. @@ -104,15 +91,13 @@ func (o *OLED) Off() error { // Clear clears the entire display. func (o *OLED) Clear() error { - for i := 0; i < len(o.buf); i++ { - o.buf[0] = 0 + for i := 1; i < len(o.buf); i++ { + o.buf[i] = 0 } - return o.drawBuf(0, o.buf) + return o.Draw() } -// DrawPixel draws a pixel on the OLED display. -// v can either be 0 or 1. -func (o *OLED) DrawPixel(x, y int, v byte) error { +func (o *OLED) SetPixel(x, y int, v byte) error { if x >= o.w || y >= o.h { return fmt.Errorf("(x=%v, y=%v) is out of bounds on this %vx%v display", x, y, o.w, o.h) } @@ -121,22 +106,22 @@ func (o *OLED) DrawPixel(x, y int, v byte) error { } i := x + (y/8)*o.w + 1 o.buf[i] = o.buf[i] | v<