Browse Source

Prepare for a text LCD to print the current IP addresses

Douglas Thrift 1 year ago
parent
commit
ef552ba7cc
2 changed files with 47 additions and 0 deletions
  1. 33 0
      rover/interfaces.go
  2. 14 0
      rover/main.go

+ 33 - 0
rover/interfaces.go

@@ -0,0 +1,33 @@
+package main
+
+import (
+	"log"
+	"net"
+)
+
+func printInterfaces() error {
+	ifs, err := net.Interfaces()
+	if err != nil {
+		return err
+	}
+
+	for _, i := range ifs {
+		if i.Flags&net.FlagLoopback != 0 {
+			continue
+		}
+
+		addrs, err := i.Addrs()
+		if err != nil {
+			return err
+		}
+		for _, addr := range addrs {
+			in, ok := addr.(*net.IPNet)
+			if ok && in.IP.To4() != nil {
+				log.Printf("interface %v: %v", i.Name, in.IP)
+				break
+			}
+		}
+	}
+
+	return nil
+}

+ 14 - 0
rover/main.go

@@ -4,6 +4,7 @@ import (
 	"flag"
 	"log"
 	"net/http"
+	"time"
 
 	"douglasthrift.net/dtrobots/gamepad"
 
@@ -53,6 +54,10 @@ func main() {
 	}
 	log.Printf("detected right motor: %v", rightMotor)
 
+	if err := printInterfaces(); err != nil {
+		log.Fatalf("error printing interfaces: %v", err)
+	}
+
 	go func() {
 		log.Fatalf("error controlling rover: %v", control(mode, g, leftMotor, rightMotor))
 	}()
@@ -60,6 +65,15 @@ func main() {
 		log.Fatalf("error reading from gamepad: %v", g.Read())
 	}()
 
+	ifTicker := time.NewTicker(1 * time.Minute)
+	go func() {
+		for range ifTicker.C {
+			if err := printInterfaces(); err != nil {
+				log.Fatalf("error printing interfaces: %v", err)
+			}
+		}
+	}()
+
 	log.Print("HTTP server started")
 	log.Fatalf("error listening and serving: %v", http.ListenAndServe(listen, nil))
 }