Matrix/main.go
2024-04-17 08:18:22 +02:00

65 lines
1.6 KiB
Go

package main
import (
"fmt"
"github.com/buger/goterm"
"github.com/go-resty/resty/v2"
"github.com/joho/godotenv"
)
func output(client *resty.Client, url string, scale, width, height uint, arr [][]Cell) {
goterm.MoveCursor(1, 4)
// Prepare instructions for matrix
instructions := make([]interface{}, 0)
// Append all live cells as pixel instructions
for j := 0; j < int(height/scale); j++ {
for i := 0; i < int(width/scale); i++ {
if arr[i][j].live {
instructions = append(instructions, Rectangle{X: i * int(scale), Y: j * int(scale), W: scale, H: scale, Endpoint: RECTANGLE})
goterm.Print("X")
continue
}
goterm.Print(" ")
}
goterm.Println()
}
// Append update instruction
instructions = append(instructions, Update{Endpoint: UPDATE})
// Send to matrix
sendRequest(client, url, instructions)
// Update terminal UI
goterm.Flush()
}
func main() {
const FPS = 2
const SCALE = 2
// Try loading .env file if it exists
godotenv.Load(".env")
// Load env server data
url, width, height := loadMatrixData()
goterm.Printf("On '%s' at [%d x %d] with %d FPS\n", url, width, height, FPS)
// Initialize resty client
client := resty.New()
// Initialize pixel color
err := sendRequest(client, url, []interface{}{Color{R: 255, G: 255, B: 255, Endpoint: COLOR}})
if err != nil {
goterm.Println(err)
}
// Run Game of Life
done := setup(func(c [][]Cell) {
output(client, url, SCALE, width, height, c)
}, SCALE, width, height, FPS)
// Wait for user input to quit
fmt.Scanln()
// Stop Game of Life, wait for routine
done <- true
}