Skip to content

Commit

Permalink
fix the init sequence and drawing
Browse files Browse the repository at this point in the history
  • Loading branch information
rakyll committed Jun 23, 2016
1 parent 0a76d9a commit f1409db
Showing 1 changed file with 35 additions and 50 deletions.
85 changes: 35 additions & 50 deletions ssd1306/ssd1306.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}
Expand All @@ -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<<uint((y%8))
return o.drawBuf(x, o.buf)
return nil
}

// DrawImage draws an image on the OLED display starting from x, y.
func (o *OLED) DrawImage(x, y int, img image.Image) error {
func (o *OLED) DrawImage(x int, img image.Image) error {
panic("not implemented")
}

func (o *OLED) drawBuf(x int, buf []byte) error {
if err := o.dev.Write([]byte{0xa4}); err != nil { // the write mode
return err
}
if err := o.dev.Write([]byte{0x40 | byte(x)}); err != nil { //set the start line to x
func (o *OLED) Draw() error {
if err := o.dev.Write([]byte{
0x21, 0, ssd1306_LCDWIDTH - 1,
0x22, 0, 7,
}); err != nil { // the write mode
return err
}
return o.dev.Write(buf)
return o.dev.Write(o.buf)
}

// StartScroll starts scrolling in the horizontal direction starting from
Expand Down

0 comments on commit f1409db

Please sign in to comment.