Skip to content

Commit b99bf47

Browse files
committed
cleanup, readme, example ...
1 parent 2df2f84 commit b99bf47

File tree

5 files changed

+73
-57
lines changed

5 files changed

+73
-57
lines changed

README.md

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,12 @@ ACC must be running. Otherwise, `telemetry.Connect()` will fail with an error.
99

1010
Methods `StaticPointer()`, `PhysicsPointer()` and `GraphicsPointer()` returns pointer to shared memory, so data will change over time. It's up to client code to create snapshot of the data if they wish so.
1111

12+
`RealtimeCarUpdate()` is read from UDP broadcast and it returns instance of latest `RealtimeCarUpdate` state.
13+
1214
### Example:
13-
```go
14-
func main() {
15-
telemetry := acctelemetry.New()
16-
err := telemetry.Connect()
17-
if err != nil {
18-
t.Error(fmt.Errorf("unable to connect to ACC: %w", err))
19-
}
20-
21-
// we can run this in loop
22-
ticker := time.NewTicker(1 * time.Second)
23-
for _ = range ticker.C {
24-
fmt.Printf("%+v\n\n", telemetry.StaticPointer())
25-
fmt.Printf("%+v\n\n", telemetry.PhysicsPointer())
26-
fmt.Printf("%+v\n\n", telemetry.GraphicsPointer())
27-
}
28-
}
29-
```
15+
16+
see [examples/main.go](examples/main.go)
17+
18+
### Configs:
19+
20+
see [telemetry_config.go](telemetry_config.go)

examples/main.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"time"
7+
8+
"github.com/sparkoo/acctelemetry-go"
9+
)
10+
11+
func main() {
12+
telemetry := acctelemetry.New(acctelemetry.DefaultUdpConfig())
13+
err := telemetry.Connect()
14+
if err != nil {
15+
log.Fatalf("unable to connect to ACC: %s", err)
16+
}
17+
18+
// we can run this in loop
19+
ticker := time.NewTicker(1 * time.Second)
20+
for _ = range ticker.C {
21+
fmt.Printf("%+v\n\n", telemetry.StaticPointer())
22+
fmt.Printf("%+v\n\n", telemetry.PhysicsPointer())
23+
fmt.Printf("%+v\n\n", telemetry.GraphicsPointer())
24+
fmt.Printf("%+v\n\n", telemetry.RealtimeCarUpdate())
25+
}
26+
}

telemetry.go

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,23 @@ package acctelemetry
33
import (
44
"fmt"
55
"net"
6-
"time"
76
"unsafe"
87
)
98

109
const STATIC_FILE_MMAP = "Local\\acpmf_static"
1110
const PHYSICS_FILE_MMAP = "Local\\acpmf_physics"
1211
const GRAPHIS_FILE_MMAP = "Local\\acpmf_graphics"
1312

14-
type AccTelemetryConfig struct {
15-
EnableUdp bool
16-
UdpIpPort string
17-
UdpConnectionPassword string
18-
UdpPollRate time.Duration
19-
udpCommandPassword string
20-
udpDisplayName string
21-
udpRealtimeUpdateIntervalMS int32
22-
}
23-
24-
func DefaultConfig() *AccTelemetryConfig {
25-
return &AccTelemetryConfig{
26-
EnableUdp: false,
27-
}
28-
}
29-
30-
func DefaultUdpConfig() *AccTelemetryConfig {
31-
return UdpConfig("127.0.0.1:9000", "asd")
32-
}
33-
34-
func UdpConfig(ipPort string, password string) *AccTelemetryConfig {
35-
return &AccTelemetryConfig{
36-
EnableUdp: true,
37-
UdpIpPort: ipPort,
38-
UdpConnectionPassword: password,
39-
udpCommandPassword: "",
40-
udpDisplayName: "RaceMate",
41-
udpRealtimeUpdateIntervalMS: 100,
42-
}
43-
}
44-
4513
type AccTelemetry struct {
46-
config *AccTelemetryConfig
14+
config *accTelemetryConfig
4715

4816
staticData *accDataHolder[AccStatic]
4917
physicsData *accDataHolder[AccPhysics]
5018
graphicsData *accDataHolder[AccGraphic]
5119

5220
udpConnection *net.UDPConn
5321

54-
RealtimeCarUpdate *RealtimeCarUpdate
22+
realtimeCarUpdate *RealtimeCarUpdate
5523
}
5624

5725
type accDataHolder[T AccGraphic | AccPhysics | AccStatic] struct {
@@ -127,7 +95,7 @@ func (telemetry *AccTelemetry) connectUdp() error {
12795
return nil
12896
}
12997

130-
func New(config *AccTelemetryConfig) *AccTelemetry {
98+
func New(config *accTelemetryConfig) *AccTelemetry {
13199
return &AccTelemetry{
132100
config: config,
133101
}
@@ -158,8 +126,8 @@ func (t *AccTelemetry) PhysicsPointer() *AccPhysics {
158126
}
159127

160128
// returns what is current RealtimeCarUpdate
161-
func (t *AccTelemetry) ReadUdpMessage() *RealtimeCarUpdate {
162-
return t.RealtimeCarUpdate
129+
func (t *AccTelemetry) RealtimeCarUpdate() *RealtimeCarUpdate {
130+
return t.realtimeCarUpdate
163131
}
164132

165133
func (t *AccTelemetry) Close() error {

telemetry_config.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package acctelemetry
2+
3+
type accTelemetryConfig struct {
4+
EnableUdp bool
5+
UdpIpPort string
6+
UdpConnectionPassword string
7+
udpCommandPassword string
8+
udpDisplayName string
9+
udpRealtimeUpdateIntervalMS int32
10+
}
11+
12+
func DefaultConfig() *accTelemetryConfig {
13+
return &accTelemetryConfig{
14+
EnableUdp: false,
15+
}
16+
}
17+
18+
func DefaultUdpConfig() *accTelemetryConfig {
19+
return UdpConfig("127.0.0.1:9000", "asd")
20+
}
21+
22+
func UdpConfig(ipPort string, password string) *accTelemetryConfig {
23+
return &accTelemetryConfig{
24+
EnableUdp: true,
25+
UdpIpPort: ipPort,
26+
UdpConnectionPassword: password,
27+
udpCommandPassword: "",
28+
udpDisplayName: "RaceMate",
29+
udpRealtimeUpdateIntervalMS: 100,
30+
}
31+
}

telemetry_udp.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ func (t *AccTelemetry) connect() error {
6161
if err == nil {
6262
realtimeCarUpdate, createMsgErr := t.createMessage(payload)
6363
if createMsgErr != nil {
64-
fmt.Printf("failed to create realtime car update message: %s", createMsgErr)
64+
fmt.Printf("failed to create realtime car update message: %s\n", createMsgErr)
6565
}
66-
t.RealtimeCarUpdate = realtimeCarUpdate
66+
t.realtimeCarUpdate = realtimeCarUpdate
6767
} else {
6868
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
6969
fmt.Printf("UDP read timeout, ACC may not be running: %s\n", netErr)
@@ -108,7 +108,7 @@ func (t *AccTelemetry) handshake() error {
108108
return fmt.Errorf("failed to read connection response: %w", err)
109109
}
110110

111-
fmt.Printf("Connected to ACC, listen for messages: '%+v'", connectionResult)
111+
fmt.Printf("Connected to ACC, listen for messages: '%+v'\n", connectionResult)
112112

113113
return nil
114114
}

0 commit comments

Comments
 (0)