tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/examples/ssd1306/i2c_128x64/main.go (about)

     1  // This example shows how to use 128x64 display over I2C
     2  // Tested on Seeeduino XIAO Expansion Board https://wiki.seeedstudio.com/Seeeduino-XIAO-Expansion-Board/
     3  //
     4  // According to manual, I2C address of the display is 0x78, but that's 8-bit address.
     5  // TinyGo operates on 7-bit addresses and respective 7-bit address would be 0x3C, which we use below.
     6  //
     7  // To learn more about different types of I2C addresses, please see following page
     8  // https://www.totalphase.com/support/articles/200349176-7-bit-8-bit-and-10-bit-I2C-Slave-Addressing
     9  
    10  package main
    11  
    12  import (
    13  	"machine"
    14  
    15  	"image/color"
    16  	"time"
    17  
    18  	"tinygo.org/x/drivers/ssd1306"
    19  )
    20  
    21  func main() {
    22  	machine.I2C0.Configure(machine.I2CConfig{
    23  		Frequency: machine.TWI_FREQ_400KHZ,
    24  	})
    25  
    26  	display := ssd1306.NewI2C(machine.I2C0)
    27  	display.Configure(ssd1306.Config{
    28  		Address: 0x3C,
    29  		Width:   128,
    30  		Height:  64,
    31  	})
    32  
    33  	display.ClearDisplay()
    34  
    35  	x := int16(0)
    36  	y := int16(0)
    37  	deltaX := int16(1)
    38  	deltaY := int16(1)
    39  	for {
    40  		pixel := display.GetPixel(x, y)
    41  		c := color.RGBA{255, 255, 255, 255}
    42  		if pixel {
    43  			c = color.RGBA{0, 0, 0, 255}
    44  		}
    45  		display.SetPixel(x, y, c)
    46  		display.Display()
    47  
    48  		x += deltaX
    49  		y += deltaY
    50  
    51  		if x == 0 || x == 127 {
    52  			deltaX = -deltaX
    53  		}
    54  
    55  		if y == 0 || y == 63 {
    56  			deltaY = -deltaY
    57  		}
    58  		time.Sleep(1 * time.Millisecond)
    59  	}
    60  }