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

     1  package main
     2  
     3  import (
     4  	"machine"
     5  
     6  	"image/color"
     7  	"time"
     8  
     9  	"tinygo.org/x/drivers/examples/hub75/gopherimg"
    10  	"tinygo.org/x/drivers/hub75"
    11  )
    12  
    13  var display hub75.Device
    14  
    15  func main() {
    16  	machine.SPI0.Configure(machine.SPIConfig{
    17  		Frequency: 8000000,
    18  		Mode:      0},
    19  	)
    20  
    21  	display = hub75.New(machine.SPI0, 11, 12, 6, 10, 18, 20)
    22  	display.Configure(hub75.Config{
    23  		Width:      64,
    24  		Height:     32,
    25  		RowPattern: 16,
    26  		ColorDepth: 6,
    27  	})
    28  
    29  	colors := []color.RGBA{
    30  		{255, 0, 0, 255},
    31  		{255, 255, 0, 255},
    32  		{0, 255, 0, 255},
    33  		{0, 255, 255, 255},
    34  		{0, 0, 255, 255},
    35  		{255, 0, 255, 255},
    36  		{255, 255, 255, 255},
    37  	}
    38  
    39  	display.ClearDisplay()
    40  	display.SetBrightness(100)
    41  
    42  	step := 0
    43  	then := time.Now()
    44  	size := int16(8)
    45  	x := int16(0)
    46  	y := int16(0)
    47  	dx := int16(1)
    48  	dy := int16(1)
    49  	c := 0
    50  	for {
    51  		if time.Since(then).Nanoseconds() > 800000000 {
    52  			then = time.Now()
    53  			step++
    54  
    55  			if step < 23 {
    56  				showRect(size, x*size, y*size, colors[c])
    57  				c = (c + 1) % 7
    58  				x += dx
    59  				y += dy
    60  				if x >= (64 / size) {
    61  					dx = -1
    62  					x += 2 * dx
    63  				}
    64  				if y >= (32 / size) {
    65  					dy = -1
    66  					y += 2 * dy
    67  				}
    68  				if x < 0 {
    69  					dx = 1
    70  					x += 2 * dx
    71  				}
    72  				if y < 0 {
    73  					dy = 1
    74  					y += 2 * dy
    75  				}
    76  			} else if step == 23 {
    77  				showGopher()
    78  			} else if step == 30 {
    79  				display.ClearDisplay()
    80  				step = 0
    81  				x = 0
    82  				y = 0
    83  			}
    84  		}
    85  		display.Display()
    86  	}
    87  }
    88  
    89  func showGopher() {
    90  	for i := int16(0); i < 64; i++ {
    91  		for j := int16(0); j < 32; j++ {
    92  			display.SetPixel(i, j, gopherimg.Int2Color(gopherimg.ImageArray[32*i+j]))
    93  		}
    94  	}
    95  }
    96  
    97  func showRect(size int16, x int16, y int16, c color.RGBA) {
    98  	for i := x; i < x+size; i++ {
    99  		for j := y; j < y+size; j++ {
   100  			display.SetPixel(i, j, c)
   101  		}
   102  	}
   103  }