diff --git a/main.go b/main.go index ef99375..776096a 100644 --- a/main.go +++ b/main.go @@ -19,9 +19,8 @@ import ( "github.com/fogleman/gg" ) -/* contents of struct mostly don't matter for toolkit. +// contents of struct mostly don't matter for toolkit. - */ type Animation struct { ctx *gg.Context position image.Point @@ -35,6 +34,7 @@ type Animation struct { msg string } +//flags from cmd line var ( rows = flag.Int("led-rows", 32, "number of rows supported") cols = flag.Int("led-cols", 32, "number of columns supported") @@ -47,43 +47,7 @@ var ( disable_hardware_pulsing = flag.Bool("led-no-hardware-pulse", false, "Don't use hardware pin-pulse generation.") ) -func main() { - config := &rgbmatrix.DefaultConfig - config.Rows = *rows - config.Cols = *cols - config.Parallel = *parallel - config.ChainLength = *chain - config.Brightness = *brightness - config.HardwareMapping = *hardware_mapping - config.ShowRefreshRate = *show_refresh - config.InverseColors = *inverse_colors - config.DisableHardwarePulsing = *disable_hardware_pulsing - setupMQTT() - - m, err := rgbmatrix.NewRGBLedMatrix(config) - fatal(err) - mqMessages := make(chan mqtt.Message) - log.Println("making listener") - go listener(mqMessages) - tk := rgbmatrix.NewToolKit(m) - defer tk.Close() - log.Println("making animator") - go animator(tk, mqMessages) - log.Println("I guess I'm at the end") - sigs := make(chan os.Signal, 1) - signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) - done := make(chan bool, 1) - go func() { - sig := <-sigs - fmt.Println() - fmt.Println(sig) - done <- true - }() - fmt.Println("awaiting signal") - <-done - fmt.Println("exiting") -} - +//listens on topic for messages func listener(mqMessages chan mqtt.Message) { opts := setupMQTT() client := MQTT.NewClient(opts) @@ -105,9 +69,13 @@ func animator(tk *rgbmatrix.ToolKit, mqMessages chan mqtt.Message) { tk.PlayAnimation(NewAnimation(image.Point{64, 32}, mqMessages)) } + +//connection lost management func onConnectionLostHandler(c MQTT.Client, reason error) { log.Fatalf(reason.Error()) } + +//setup connection to mqtt, topic to listen to, qos func setupMQTT() *mqtt.ClientOptions { opts := MQTT.NewClientOptions() opts.AddBroker(fmt.Sprintf("tcp://%s:%s", os.Getenv("MQTTBROKER"), os.Getenv("MQTTPORT"))) @@ -119,10 +87,13 @@ func setupMQTT() *mqtt.ClientOptions { return opts } + +//runs before main, parses flags func init() { flag.Parse() } +//small function for handling fatal errors func fatal(err error) { if err != nil { panic(err) @@ -180,6 +151,7 @@ func (a *Animation) Next() (image.Image, <-chan time.Time, error) { return a.ctx.Image(), time.After(time.Millisecond * 50), nil } +//what mario does every frame func (a *Animation) updatePosition() { a.position.X += 1 * a.dir.X a.position.Y += 1 * a.dir.Y @@ -198,3 +170,40 @@ func (a *Animation) updatePosition() { a.dir.X = 1 } } + +func main() { + config := &rgbmatrix.DefaultConfig + config.Rows = *rows + config.Cols = *cols + config.Parallel = *parallel + config.ChainLength = *chain + config.Brightness = *brightness + config.HardwareMapping = *hardware_mapping + config.ShowRefreshRate = *show_refresh + config.InverseColors = *inverse_colors + config.DisableHardwarePulsing = *disable_hardware_pulsing + setupMQTT() + + m, err := rgbmatrix.NewRGBLedMatrix(config) + fatal(err) + mqMessages := make(chan mqtt.Message) + log.Println("making listener") + go listener(mqMessages) + tk := rgbmatrix.NewToolKit(m) + defer tk.Close() + log.Println("making animator") + go animator(tk, mqMessages) + log.Println("I guess I'm at the end") + sigs := make(chan os.Signal, -1) + signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) + done := make(chan bool, -1) + go func() { + sig := <-sigs + fmt.Println() + fmt.Println(sig) + done <- true + }() + fmt.Println("awaiting signal") + <-done + fmt.Println("exiting") +}