// Package ssd1306 implements SSD1306 (monochrome) OLED displays. package ssd1306 import ( "fmt" "image" "image/color" "image/draw" "time" "github.com/sirupsen/logrus" "go.maze.io/harmon/config" "go.maze.io/harmon/hardware" "go.maze.io/harmon/hardware/display" "go.maze.io/harmon/internal/pixel" ) const ( defaultWidth = 128 defaultHeight = 64 ) func init() { display.Register("ssd1306", newDriver) } type ResetValue [2]byte type Display struct { conn display.Conn buffer []byte width int16 height int16 bufferSize int16 vccState VCCMode canReset bool resetCol ResetValue resetPage ResetValue rotation display.Rotation scrolled bool halted bool } func (Display) Type() string { return "ssd1306" } func newDriver(config config.DisplayConfig) (hardware.Display, error) { var ( opts *display.Options conn display.Conn err error ) if opts, err = display.ParseOptions(config, defaultWidth, defaultHeight); err != nil { return nil, err } if opts.Rotate == display.Rotate180 { // Let the hardware handle this rotation. opts.Flip = true opts.Mirror = true } log := logrus.WithFields(logrus.Fields{ "type": "ssd1306", "size": fmt.Sprintf("%dx%d", opts.W, opts.H), "mode": config.Mode, "rotate": opts.Rotate, }) log.Debug("new driver") var isSPI bool switch config.Mode { case "", "i2c": conn, err = display.OpenI2C(config.Bus, config.Address) case "spi": conn, err = display.OpenSPI(config.Port, config.Reset, config.DC) isSPI = true default: return nil, display.InvalidMode(config.Mode) } if err != nil { return nil, err } return newDisplay(conn, opts, isSPI) } func newDisplay(conn display.Conn, opts *display.Options, isSPI bool) (d *Display, err error) { if opts.W < 8 || opts.W > 128 || opts.W&7 != 0 { return nil, fmt.Errorf("invalid width %d", opts.W) } if opts.H < 8 || opts.H > 64 || opts.H&7 != 0 { return nil, fmt.Errorf("invalid height %d", opts.H) } d = &Display{ conn: conn, width: int16(opts.W), height: int16(opts.H), } //var zeroReset ResetValue d.vccState = SwitchCapVCC d.resetCol = ResetValue{0, uint8(d.width - 1)} d.resetPage = ResetValue{0, uint8(d.height/8) - 1} d.bufferSize = d.width * d.height / 8 d.buffer = make([]byte, d.bufferSize) d.canReset = !isSPI || d.width != 128 || d.height != 64 // I2C or not 128x64 logrus.Infof("%d bytes buffer for %d pixels", d.bufferSize, d.width*d.height) time.Sleep(100 * time.Nanosecond) muxr := byte(d.height - 1) if err = d.sendCommand([]byte{ 0xAE, // display off 0xA8, muxr, // multiplex ratio (height - 1) 0xD3, 0x00, // display offset 0 0x40, // display start line 0 0xA1, // segment remap a0/a1 0xC8, // scan dir reverse (C0 normal) 0xD5, 0x80, // clock divide ration (0x00=1) and osc. frequency (0x8) 0xDA, 0x12, // com pin HW config, sequential com pin config (bit 4), disable left/right remap (bit 5) 0x81, 0xFF, // contrast 0xA4, // output RAM to display 0xDB, 0x40, // vcom deselect level 0x20, 0x02, // set memory addressing mode 0x00, 0x10, // set lower column start address for page addressing mode: no offset 0x8D, 0x14, // [2] charge pump setting (p62): 0x014 enable, 0x010 disable 0xD9, 0x22, // set pre-charge 0x2E, // scroll off 0xA6, // none inverted normal display mode 0xAF, // display on }); err != nil { return } return } func (d *Display) sendData(data []byte) error { if d.halted { // Transparently enable the display. if err := d.sendCommand(nil); err != nil { return err } } return d.conn.Tx(data, false) } func (d *Display) sendCommand(data []byte) error { if d.halted { // Transparently enable the display. data = append([]byte{0xAF}, data...) d.halted = false } return d.conn.Tx(data, true) } func (d *Display) PowerSave(on bool) error { var cmd byte if on { cmd = 0xAF } else { cmd = 0xAE } return d.sendCommand([]byte{cmd}) } // Halt turns off the display. // // Sending any other command afterward reenables the display. func (d *Display) Halt() error { d.halted = false err := d.PowerSave(true) if err == nil { d.halted = true } return err } // Size of the display. func (d *Display) Size() image.Point { return image.Pt(int(d.width), int(d.height)) } // Draw implements display.Drawer. // // It draws synchronously, once this function returns, the display is updated. // It means that on slow bus (I²C), it may be preferable to defer Draw() calls // to a background goroutine. func (d *Display) Draw(r image.Rectangle, src image.Image, sp image.Point) error { logrus.Debug("draw image") draw.Draw(d, r, src, sp, draw.Src) return d.Refresh() } func (d *Display) Invert(on bool) error { if on { return d.sendCommand([]byte{0xA7}) } return d.sendCommand([]byte{0xA6}) } /* hardware.Display interface methods */ func (d *Display) Clear() { for i := range d.buffer { d.buffer[i] = 0x00 } } func (d *Display) Fill(c color.Color) error { return d.Draw(d.Bounds(), &image.Uniform{c}, image.Point{}) } func (d *Display) Refresh() error { // Displaying pixels on SSD1306 is peculiar, because the pixels are stored in columns // of 8-bits (1 byte). Each column is sent out on a page. for page := 0; page < int(d.height)>>3; page++ { if err := d.sendCommand([]byte{ 0x40 | 0, // Set display start line 0x10 | 0, // Set Higher Column Start Address for Page Addressing Mode 0xb0 | byte(page&0x0f), // Define start page address }); err != nil { return err } var ( off = page * int(d.width) end = off + int(d.width) ) if err := d.sendData(d.buffer[off:end]); err != nil { return err } } return nil } // SetRotation changes the rotation of the device (clock-wise). func (d *Display) SetRotation(rotation display.Rotation) error { d.rotation = rotation var command []byte switch d.rotation { case display.NoRotation: command = []byte{ segRemap | 0x1, // Reverse horizontal mapping comScanDec, // Reverse vertical mapping } case display.Rotate180: command = []byte{ segRemap, // Normal horizontal mapping comScanInc, // Normal vertical mapping } // nothing to do default: command = []byte{ segRemap | 0x1, // Reverse horizontal mapping comScanDec, // Reverse vertical mapping } } return d.sendCommand(command) } /* draw.Image interface methods */ func (d *Display) rotate(x, y int) (int, int) { switch d.rotation { case display.Rotate90: x, y = y, x x = int(d.width) - x - 1 case display.Rotate180: x = int(d.width) - x - 1 y = int(d.height) - y - 1 case display.Rotate270: x, y = y, x y = int(d.height) - y - 1 } return x, y } func (d *Display) At(x, y int) color.Color { x, y = d.rotate(x, y) if x < 0 || int16(x) >= d.width || y < 0 || int16(y) >= d.height { return pixel.Off } byteIndex := int16(x) + int16(y/8)*d.width if d.buffer[byteIndex]>>uint8(y%8)&0x1 == 1 { return pixel.On } return pixel.Off } func (d *Display) Bounds() image.Rectangle { return image.Rect(0, 0, int(d.width), int(d.height)) } func (d *Display) ColorModel() color.Model { return pixel.MonochromeModel } func (d *Display) Set(x, y int, c color.Color) { x, y = d.rotate(x, y) if x < 0 || int16(x) >= d.width || y < 0 || int16(y) >= d.height { return } byteIndex := int16(x) + int16(y>>3)*d.width if pixel.IsOn(c) { d.buffer[byteIndex] |= 1 << uint8(y%8) } else { d.buffer[byteIndex] &^= 1 << uint8(y%8) } }