Matrix/main.go

65 lines
1.5 KiB
Go
Raw Normal View History

2024-04-12 07:54:27 +00:00
package main
import (
2024-04-14 19:47:13 +00:00
"fmt"
2024-04-12 08:00:53 +00:00
2024-04-14 23:25:33 +00:00
"github.com/buger/goterm"
2024-04-12 08:00:53 +00:00
"github.com/go-resty/resty/v2"
"github.com/joho/godotenv"
2024-04-12 07:54:27 +00:00
)
2024-04-14 20:46:27 +00:00
func output(client *resty.Client, url string, width, height int, arr [][]Cell) {
2024-04-15 15:35:02 +00:00
goterm.MoveCursor(1, 4)
2024-04-14 20:46:27 +00:00
// Prepare instructions for matrix
2024-04-17 06:09:37 +00:00
instructions := make([]interface{}, 1)
instructions[0] = Clear{Endpoint: CLEAR}
2024-04-14 20:46:27 +00:00
// Append all live cells as pixel instructions
2024-04-14 23:26:20 +00:00
for j := 0; j < height; j++ {
for i := 0; i < width; i++ {
2024-04-14 19:47:13 +00:00
if arr[i][j].live {
2024-04-14 20:46:27 +00:00
instructions = append(instructions, Pixel{X: i, Y: j, Endpoint: PIXEL})
2024-04-14 23:25:33 +00:00
goterm.Print("X")
continue
2024-04-14 19:47:13 +00:00
}
2024-04-14 23:25:33 +00:00
goterm.Print(" ")
2024-04-14 19:47:13 +00:00
}
2024-04-14 23:25:33 +00:00
goterm.Println()
2024-04-14 19:47:13 +00:00
}
2024-04-14 20:46:27 +00:00
// Append update instruction
instructions = append(instructions, Update{Endpoint: UPDATE})
// Send to matrix
sendRequest(client, url, instructions)
2024-04-14 23:25:33 +00:00
// Update terminal UI
goterm.Flush()
2024-04-14 19:47:13 +00:00
}
2024-04-12 07:54:27 +00:00
func main() {
2024-04-14 23:39:43 +00:00
const FPS = 10
2024-04-15 15:35:02 +00:00
// Try loading .env file if it exists
godotenv.Load(".env")
// Load env server data
2024-04-12 11:30:00 +00:00
url, width, height := loadMatrixData()
2024-04-14 23:39:59 +00:00
goterm.Printf("On '%s' at [%d x %d] with %d FPS\n", url, width, height, FPS)
// Initialize resty client
client := resty.New()
2024-04-14 20:46:27 +00:00
// Initialize pixel color
2024-04-15 15:35:02 +00:00
err := sendRequest(client, url, []interface{}{Color{R: 255, G: 255, B: 255, Endpoint: COLOR}})
if err != nil {
goterm.Println(err)
}
2024-04-14 19:48:03 +00:00
// Run Game of Life
done := setup(func(c [][]Cell) {
output(client, url, width, height, c)
2024-04-14 23:39:43 +00:00
}, width, height, FPS)
2024-04-12 12:13:21 +00:00
2024-04-14 19:48:03 +00:00
// Wait for user input to quit
2024-04-14 20:34:59 +00:00
fmt.Scanln()
2024-04-14 19:48:03 +00:00
// Stop Game of Life, wait for routine
done <- true
2024-04-12 07:54:27 +00:00
}