waxdred / go-i2c-oled

Implementation of I2C-bus written in Golang for Oled Raspberry
MIT License
9 stars 2 forks source link

ssd309 slighly different. #2

Open TVforME opened 3 months ago

TVforME commented 3 months ago

Is there any intention to add the ssd309? The later 128x64 OLED'S come with the newer SSD1309. Apparently it has a few addition registers and a couple of changed values for functions.

Other than that the drawing and text modes are the same.

const (
    SSD1306_CMD                 = 0x80
    SSD1306_SETDISPLAYCLOCKDIV  = 0xD5
    SSD1306_DISPLAYOFF          = 0xAE
    SSD1306_SETMULTIPLEX        = 0xA8
    SSD1306_SETDISPLAYOFFSET    = 0xD3
    SSD1306_SETSTARTLINE        = 0x0
    SSD1306_CHARGEPUMP          = 0x8D
    SSD1306_MEMORYMODE          = 0x20
    SSD1306_SEGREMAP            = 0xA0
    SSD1306_COMSCANDEC          = 0xC8
    SSD1306_SETCOMPINS          = 0xDA
    SSD1306_SETCONTRAST         = 0x81
    SSD1306_SETPRECHARGE        = 0xD9
    SSD1306_SETVCOMDETECT       = 0xDB
    SSD1306_DISPLAYALLON_RESUME = 0xA4
    SSD1306_NORMALDISPLAY       = 0xA6
    SSD1306_EXTERNALVCC         = 0x1
    SSD1306_SWITCHCAPVCC        = 0x2
)
const (
    SSD1309_SETPRECHARGELEVEL = 0xBB
    SSD1309_SETVCOMH          = 0xBE
    SSD1309_NOP               = 0xE3 // No operation command
)

package main

import (
    "image"
    "log"

    "periph.io/x/periph/conn/display/ssd1306"
    "periph.io/x/periph/conn/i2c/i2creg"
    "periph.io/x/periph/host"
)

const (
    SSD1309_DISPLAYOFF          = 0xAE
    SSD1309_SETDISPLAYCLOCKDIV  = 0xD5
    SSD1309_SETMULTIPLEX        = 0xA8
    SSD1309_SETDISPLAYOFFSET    = 0xD3
    SSD1309_SETSTARTLINE        = 0x00
    SSD1309_CHARGEPUMP          = 0x8D
    SSD1309_MEMORYMODE          = 0x20
    SSD1309_SEGREMAP            = 0xA1 // Different from SSD1306
    SSD1309_COMSCANINC          = 0xC0 // Different from SSD1306
    SSD1309_SETCOMPINS          = 0xDA
    SSD1309_SETCONTRAST         = 0x81
    SSD1309_SETPRECHARGE        = 0xD9
    SSD1309_SETVCOMDETECT       = 0xDB
    SSD1309_DISPLAYALLON_RESUME = 0xA4
    SSD1309_NORMALDISPLAY       = 0xA6
    SSD1309_DISPLAYON           = 0xAF
)

func main() {
    // Initialize periph.io.
    if _, err := host.Init(); err != nil {
        log.Fatal(err)
    }

    // Open I2C bus.
    bus, err := i2creg.Open("")
    if err != nil {
        log.Fatal(err)
    }
    defer bus.Close()

    // Create a new display.
    dev, err := ssd1306.NewI2C(bus, &ssd1306.Opts{
        W:         128,
        H:         64,
        Sequential: true, // SSD1309 typically uses Sequential layout
    })
    if err != nil {
        log.Fatal(err)
    }

    // Initialize the display with SSD1309-specific commands.
    cmds := []byte{
        SSD1309_DISPLAYOFF,
        SSD1309_SETDISPLAYCLOCKDIV, 0x80,
        SSD1309_SETMULTIPLEX, 0x3F,
        SSD1309_SETDISPLAYOFFSET, 0x00,
        SSD1309_SETSTARTLINE | 0x00,
        SSD1309_CHARGEPUMP, 0x14,
        SSD1309_MEMORYMODE, 0x00,
        SSD1309_SEGREMAP | 0x01,
        SSD1309_COMSCANINC,
        SSD1309_SETCOMPINS, 0x12,
        SSD1309_SETCONTRAST, 0xCF,
        SSD1309_SETPRECHARGE, 0xF1,
        SSD1309_SETVCOMDETECT, 0x40,
        SSD1309_DISPLAYALLON_RESUME,
        SSD1309_NORMALDISPLAY,
        SSD1309_DISPLAYON,
    }

    for _, cmd := range cmds {
        if err := dev.Command(cmd); err != nil {
            log.Fatal(err)
        }
    }

    // Clear the display.
    img := image.NewGray(image.Rect(0, 0, 128, 64))
    dev.Draw(dev.Bounds(), img, image.Point{})
}
waxdred commented 4 days ago

Hello,

Unfortunately, I don't have this OLED screen in my possession, but I would be happy to create a pull request to add support for this model. The SSD1309 has some additional registers and modified values compared to the SSD1306, and I can implement the necessary changes in the code.

Let me know if there are any specific requirements or additional details you'd like to include!