package sh1106 import ( "fmt" "image" "image/color" "image/draw" "time" "git.maze.io/go/harmon/config" "git.maze.io/go/harmon/display" "git.maze.io/go/harmon/display/oled" "git.maze.io/go/harmon/internal/pixel" ) func init() { display.Register("sh1106", newDevice) } func newDevice(config *config.Display) (display.Display, error) { width, height, err := display.ParseSize(config.Size, 128, 0) if err != nil || height == 0 { return nil, fmt.Errorf("invalid size %q: %w", config.Size, err) } /* rotation, err := display.ParseRotation(config.Rotate) if err != nil { return nil, err } */ bus, err := oled.NewBus(config) if err != nil { return nil, err } dev := &Device{ bus: bus, } if err = dev.Configure(Config{ Width: int16(width), Height: int16(height), }); err != nil { return nil, err } return dev, nil } // Device wraps an SPI connection. type Device struct { bus oled.Bus buffer pixel.Image[pixel.Monochrome] width int16 height int16 bufferSize int16 vccState VccMode } // Config is the configuration for the display type Config struct { Width int16 Height int16 VccState VccMode } type VccMode uint8 // Configure initializes the display with default configuration func (d *Device) Configure(cfg Config) error { if cfg.Width != 0 { d.width = cfg.Width } else { d.width = 128 } if cfg.Height != 0 { d.height = cfg.Height } else { d.height = 64 } if cfg.VccState != 0 { d.vccState = cfg.VccState } else { d.vccState = SWITCHCAPVCC } d.bufferSize = d.width * d.height / 8 d.buffer = pixel.NewImage[pixel.Monochrome](int(d.width), int(d.height)) //make([]byte, d.bufferSize) time.Sleep(100 * time.Nanosecond) d.Command(DISPLAYOFF) d.Command(SETDISPLAYCLOCKDIV) d.Command(0x80) d.Command(SETMULTIPLEX) d.Command(uint8(d.height - 1)) d.Command(SETDISPLAYOFFSET) d.Command(0x0) d.Command(SETSTARTLINE | 0x0) d.Command(CHARGEPUMP) if d.vccState == EXTERNALVCC { d.Command(0x10) } else { d.Command(0x14) } d.Command(MEMORYMODE) d.Command(0x00) d.Command(SEGREMAP | 0x1) d.Command(COMSCANDEC) if (d.width == 128 && d.height == 64) || (d.width == 64 && d.height == 48) { // 128x64 or 64x48 d.Command(SETCOMPINS) d.Command(0x12) d.Command(SETCONTRAST) if d.vccState == EXTERNALVCC { d.Command(0x9F) } else { d.Command(0xCF) } } else if d.width == 128 && d.height == 32 { // 128x32 d.Command(SETCOMPINS) d.Command(0x02) d.Command(SETCONTRAST) d.Command(0x8F) } else if d.width == 96 && d.height == 16 { // 96x16 d.Command(SETCOMPINS) d.Command(0x2) d.Command(SETCONTRAST) if d.vccState == EXTERNALVCC { d.Command(0x10) } else { d.Command(0xAF) } } else { return fmt.Errorf("sh1106: no configuration for %dx%d", d.width, d.height) } d.Command(SETPRECHARGE) if d.vccState == EXTERNALVCC { d.Command(0x22) } else { d.Command(0xF1) } d.Command(SETVCOMDETECT) d.Command(0x40) d.Command(DISPLAYALLON_RESUME) d.Command(NORMALDISPLAY) d.Command(DEACTIVATE_SCROLL) d.Command(DISPLAYON) return nil } // ClearBuffer clears the image buffer func (d *Device) ClearBuffer() { d.buffer.Clear() } // ClearDisplay clears the image buffer and clear the display func (d *Device) ClearDisplay() { d.buffer.Clear() d.Refresh() } // Refresh sends the whole buffer to the screen func (d *Device) Refresh() error { // In the 128x64 (SPI) screen resetting to 0x0 after 128 times corrupt the buffer // Since we're printing the whole buffer, avoid resetting it if d.width != 128 || d.height != 64 { d.Command(COLUMNADDR) d.Command(0) d.Command(uint8(d.width - 1)) d.Command(PAGEADDR) d.Command(0) d.Command(uint8(d.height/8) - 1) } raw := d.buffer.RawBuffer() for pg := uint8(0); pg < uint8(d.height/8); pg++ { d.Command(0xB0 | (pg & 0x07)) // SET_PAGE_ADDR d.Command(SETLOWCOLUMN | 2) d.Command(SETHIGHCOLUMN | 0) d.Tx(raw[uint16(pg)*0x80:uint16(pg+1)*0x80], false) } return nil } // SetPixel enables or disables a pixel in the buffer // color.RGBA{0, 0, 0, 255} is consider transparent, anything else // with enable a pixel on the screen func (d *Device) SetPixel(x int16, y int16, c color.Color) { /* if x < 0 || x >= d.width || y < 0 || y >= d.height { return } byteIndex := x + (y/8)*d.width if c.R != 0 || c.G != 0 || c.B != 0 { d.buffer[byteIndex] |= 1 << uint8(y%8) } else { d.buffer[byteIndex] &^= 1 << uint8(y%8) } */ c = pixel.MonochromeModel.Convert(c) d.buffer.SetPixel(int(x), int(y), c.(pixel.Monochrome)) } // GetPixel returns if the specified pixel is on (true) or off (false) func (d *Device) GetPixel(x int16, y int16) color.Color { /* if x < 0 || x >= d.width || y < 0 || y >= d.height { return pixel.Off } byteIndex := x + (y/8)*d.width return (d.buffer[byteIndex] >> uint8(y%8) & 0x1) == 1 */ return d.buffer.GetPixel(int(x), int(y)) } /* // SetBuffer changes the whole buffer at once func (d *Device) SetBuffer(buffer []byte) error { if int16(len(buffer)) != d.bufferSize { //return ErrBuffer return errors.New("wrong size buffer") } for i := int16(0); i < d.bufferSize; i++ { d.buffer[i] = buffer[i] } return nil } */ // Command sends a command to the display func (d *Device) Command(command uint8) error { return d.bus.Tx([]byte{command}, true) } // Tx sends data to the display func (d *Device) Tx(data []byte, isCommand bool) error { return d.bus.Tx(data, isCommand) } /* display.Image interface methods */ func (d *Device) At(x, y int) color.Color { return d.buffer.At(x, y) } func (d *Device) Bounds() image.Rectangle { return d.buffer.Bounds() } func (Device) ColorModel() color.Model { return pixel.MonochromeModel } func (d *Device) Halt() error { return d.bus.Halt() } func (d *Device) Set(x, y int, c color.Color) { d.buffer.Set(x, y, c) } // Size returns the current size of the display. func (d *Device) Size() image.Point { return image.Pt(int(d.width), int(d.height)) } func (d *Device) String() string { return "sh1106@" + d.bus.String() } /* display.imageDrawer interface methods */ func (d *Device) DrawImage(img image.Image) error { draw.Draw(d.buffer, d.buffer.Bounds(), img, image.Point{}, draw.Src) return d.Refresh() }