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

     1  //go:build macropad_rp2040
     2  
     3  package main
     4  
     5  import (
     6  	"image/color"
     7  	"machine"
     8  	"time"
     9  
    10  	"tinygo.org/x/drivers/sh1106"
    11  )
    12  
    13  var (
    14  	display = sh1106.NewSPI(machine.SPI1, machine.OLED_DC, machine.OLED_RST, machine.OLED_CS)
    15  )
    16  
    17  func init() {
    18  	machine.SPI1.Configure(machine.SPIConfig{
    19  		Frequency: 48000000,
    20  	})
    21  	display.Configure(sh1106.Config{
    22  		Width:  128,
    23  		Height: 64,
    24  	})
    25  }
    26  
    27  func main() {
    28  
    29  	display.ClearDisplay()
    30  
    31  	x := int16(0)
    32  	y := int16(0)
    33  	deltaX := int16(1)
    34  	deltaY := int16(1)
    35  	for {
    36  		pixel := display.GetPixel(x, y)
    37  		c := color.RGBA{255, 255, 255, 255}
    38  		if pixel {
    39  			c = color.RGBA{0, 0, 0, 255}
    40  		}
    41  		display.SetPixel(x, y, c)
    42  		display.Display()
    43  
    44  		x += deltaX
    45  		y += deltaY
    46  
    47  		if x == 0 || x == 127 {
    48  			deltaX = -deltaX
    49  		}
    50  
    51  		if y == 0 || y == 63 {
    52  			deltaY = -deltaY
    53  		}
    54  		time.Sleep(1 * time.Millisecond)
    55  	}
    56  }