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

     1  package main
     2  
     3  import (
     4  	"machine"
     5  
     6  	"image/color"
     7  	"time"
     8  
     9  	"tinygo.org/x/drivers/ssd1306"
    10  )
    11  
    12  func main() {
    13  	machine.I2C0.Configure(machine.I2CConfig{
    14  		Frequency: machine.TWI_FREQ_400KHZ,
    15  	})
    16  
    17  	display := ssd1306.NewI2C(machine.I2C0)
    18  	display.Configure(ssd1306.Config{
    19  		Address: ssd1306.Address_128_32,
    20  		Width:   128,
    21  		Height:  32,
    22  	})
    23  
    24  	display.ClearDisplay()
    25  
    26  	x := int16(0)
    27  	y := int16(0)
    28  	deltaX := int16(1)
    29  	deltaY := int16(1)
    30  	for {
    31  		pixel := display.GetPixel(x, y)
    32  		c := color.RGBA{255, 255, 255, 255}
    33  		if pixel {
    34  			c = color.RGBA{0, 0, 0, 255}
    35  		}
    36  		display.SetPixel(x, y, c)
    37  		display.Display()
    38  
    39  		x += deltaX
    40  		y += deltaY
    41  
    42  		if x == 0 || x == 127 {
    43  			deltaX = -deltaX
    44  		}
    45  
    46  		if y == 0 || y == 31 {
    47  			deltaY = -deltaY
    48  		}
    49  		time.Sleep(1 * time.Millisecond)
    50  	}
    51  }