diff options
Diffstat (limited to 'example/main.go')
| -rw-r--r-- | example/main.go | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/example/main.go b/example/main.go new file mode 100644 index 0000000..3986791 --- /dev/null +++ b/example/main.go @@ -0,0 +1,46 @@ +package main + +import ( + pb "buf.build/gen/go/meshtastic/protobufs/protocolbuffers/go/meshtastic" + "context" + "github.com/charmbracelet/log" + "github.com/crypto-smoke/meshtastic-go/transport" + "github.com/crypto-smoke/meshtastic-go/transport/serial" + "google.golang.org/protobuf/proto" + "os" + "os/signal" +) + +func main() { + ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) + defer cancel() + + log.SetLevel(log.DebugLevel) + + ports := serial.GetPorts() + serialConn, err := serial.Connect(ports[0]) + if err != nil { + panic(err) + } + streamConn, err := transport.NewClientStreamConn(serialConn) + if err != nil { + panic(err) + } + defer func() { + if err := streamConn.Close(); err != nil { + panic(err) + } + }() + + client := transport.NewClient(streamConn, false) + client.Handle(new(pb.MeshPacket), func(msg proto.Message) { + pkt := msg.(*pb.MeshPacket) + log.Info("Received message from radio", "msg", pkt) + }) + if client.Connect() != nil { + panic("Failed to connect to the radio") + } + + log.Info("Waiting for interrupt signal") + <-ctx.Done() +} |
