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
|
|
|
"log"
|
|
|
|
|
|
|
|
"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) {
|
|
|
|
// Prepare instructions for matrix
|
|
|
|
instructions := make([]interface{}, 0)
|
|
|
|
// Append all live cells as pixel instructions
|
2024-04-14 19:47:13 +00:00
|
|
|
for i := 0; i < width; i++ {
|
|
|
|
for j := 0; j < height; j++ {
|
|
|
|
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 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 19:47:13 +00:00
|
|
|
}
|
|
|
|
|
2024-04-12 07:54:27 +00:00
|
|
|
func main() {
|
2024-04-12 08:02:12 +00:00
|
|
|
// Load .env file if it exists
|
|
|
|
err := godotenv.Load(".env")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error loading .env file")
|
|
|
|
}
|
2024-04-12 08:02:59 +00:00
|
|
|
|
|
|
|
// Load env server data
|
2024-04-12 11:30:00 +00:00
|
|
|
url, width, height := loadMatrixData()
|
2024-04-12 08:14:17 +00:00
|
|
|
log.Printf("At '%s': %d x %d\n", url, width, height)
|
2024-04-12 08:02:59 +00:00
|
|
|
|
|
|
|
// Initialize resty client
|
|
|
|
client := resty.New()
|
2024-04-14 20:46:27 +00:00
|
|
|
// Initialize pixel color
|
|
|
|
sendRequest(client, url, []interface{}{Color{R: 255, G: 255, B: 255, Endpoint: COLOR}})
|
2024-04-12 08:02:59 +00:00
|
|
|
|
2024-04-14 19:48:03 +00:00
|
|
|
// Run Game of Life
|
|
|
|
done := setup(func(c [][]Cell) {
|
|
|
|
output(client, url, width, height, c)
|
|
|
|
}, width, height, 5)
|
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
|
|
|
}
|