3 Commits
2 changed files with 33 additions and 86 deletions
+32 -84
View File
@@ -1,9 +1,7 @@
// Package bngsdk defines an API to interact with the BeamNG outgauge data in // Package bngsdk defines an API to interact with the BeamNG outgauge data in Go
// Go
package bngsdk package bngsdk
import ( import (
"bytes"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"math" "math"
@@ -22,8 +20,6 @@ type BeamNGSDK struct {
Conn *net.UDPConn Conn *net.UDPConn
Buffer []byte Buffer []byte
Data Outgauge Data Outgauge
// Internal data
reader bytes.Reader
} }
func createUDPConnection(ip string, port int) (*net.UDPConn, *net.UDPAddr, error) { func createUDPConnection(ip string, port int) (*net.UDPConn, *net.UDPAddr, error) {
@@ -84,7 +80,6 @@ func (sdk *BeamNGSDK) ReadData() error {
} }
// Read the binary data into the struct // Read the binary data into the struct
sdk.reader.Reset(sdk.Buffer[:n])
err = sdk.parseData() err = sdk.parseData()
if err != nil { if err != nil {
fmt.Println("Error decoding UDP packet:", err) fmt.Println("Error decoding UDP packet:", err)
@@ -121,80 +116,59 @@ func Init(ip string, port int) (BeamNGSDK, error) {
// ShowLights - functions to check if a given dash light is on [START] // ShowLights - functions to check if a given dash light is on [START]
// ShiftLight returns: // ShiftLight reports whether the shift light is on
// true if the shift light is on
// false if the shift light is off
func (sdk *BeamNGSDK) ShiftLight() bool { func (sdk *BeamNGSDK) ShiftLight() bool {
return sdk.Data.ShowLights&DL_SHIFT != 0 return sdk.Data.ShowLights&DL_SHIFT != 0
} }
// HighBeam returns: // HighBeam reports whether high beams are on
// true if the high beams are on
// false if the high beams are off
func (sdk *BeamNGSDK) HighBeam() bool { func (sdk *BeamNGSDK) HighBeam() bool {
return sdk.Data.ShowLights&DL_FULLBEAM != 0 return sdk.Data.ShowLights&DL_FULLBEAM != 0
} }
// Handbrake returns: // Handbrake reports whether the handbrake is pulled
// true if the handbrake is pulled
// false if the handbrake is down
func (sdk *BeamNGSDK) Handbrake() bool { func (sdk *BeamNGSDK) Handbrake() bool {
return sdk.Data.ShowLights&DL_HANDBRAKE != 0 return sdk.Data.ShowLights&DL_HANDBRAKE != 0
} }
// Pitspeed returns: // Pitspeed reports whether the pit speed limiter is engaged
// true if the pit speed limiter is on //
// false if the pit speed limiter is off
// NOTE: this may not be used in BeamNG.drive, haven't checked yet // NOTE: this may not be used in BeamNG.drive, haven't checked yet
func (sdk *BeamNGSDK) Pitspeed() bool { func (sdk *BeamNGSDK) Pitspeed() bool {
return sdk.Data.ShowLights&DL_HANDBRAKE != 0 return sdk.Data.ShowLights&DL_PITSPEED != 0
} }
// TractionControl returns: // TractionControl reports wheter TC is engaged
// true if traction control is on
// false if traction control is off
func (sdk *BeamNGSDK) TractionControl() bool { func (sdk *BeamNGSDK) TractionControl() bool {
return sdk.Data.ShowLights&DL_TC != 0 return sdk.Data.ShowLights&DL_TC != 0
} }
// LeftIndicator returns: // LeftIndicator reports whether the left indicator is on
// true if the left indicator is on
// false if the left indicator is off
func (sdk *BeamNGSDK) LeftIndicator() bool { func (sdk *BeamNGSDK) LeftIndicator() bool {
return sdk.Data.ShowLights&DL_SIGNAL_L != 0 return sdk.Data.ShowLights&DL_SIGNAL_L != 0
} }
// RightIndicator returns: // RightIndicator reports wheter the right indicator is on
// true if the right indicator is on
// false if the right indicator is off
func (sdk *BeamNGSDK) RightIndicator() bool { func (sdk *BeamNGSDK) RightIndicator() bool {
return sdk.Data.ShowLights&DL_SIGNAL_R != 0 return sdk.Data.ShowLights&DL_SIGNAL_R != 0
} }
// AnyIndicator returns: // AnyIndicator reports whether any indicator is on
// true if the any indicator is on
// false if the all indicators are off
func (sdk *BeamNGSDK) AnyIndicator() bool { func (sdk *BeamNGSDK) AnyIndicator() bool {
return sdk.Data.ShowLights&DL_SIGNAL_ANY != 0 return sdk.Data.ShowLights&DL_SIGNAL_ANY != 0
} }
// OilLight returns // OilLight reports whether the oil warning light is on
// true if the oil light is on
// false if the oil light is off
func (sdk *BeamNGSDK) OilLight() bool { func (sdk *BeamNGSDK) OilLight() bool {
return sdk.Data.ShowLights&DL_OILWARN != 0 return sdk.Data.ShowLights&DL_OILWARN != 0
} }
// BatteryLight returns // BatteryLight reports whether the battery light is on
// true if the battery light is on
// false if the battery light is off
func (sdk *BeamNGSDK) BatteryLight() bool { func (sdk *BeamNGSDK) BatteryLight() bool {
return sdk.Data.ShowLights&DL_BATTERY != 0 return sdk.Data.ShowLights&DL_BATTERY != 0
} }
// ABS returns // ABS reports whether the ABS light is on
// true if the ABS is engaged
// false if the ABS isn't engaged
func (sdk *BeamNGSDK) ABS() bool { func (sdk *BeamNGSDK) ABS() bool {
return sdk.Data.ShowLights&DL_ABS != 0 return sdk.Data.ShowLights&DL_ABS != 0
} }
@@ -203,80 +177,58 @@ func (sdk *BeamNGSDK) ABS() bool {
// DashLights - functions to check if a given dash light is provided [START] // DashLights - functions to check if a given dash light is provided [START]
// HasShiftLight returns: // HasShiftLight reports whether a shift light is available
// true if its available
// false if its unavailable
func (sdk *BeamNGSDK) HasShiftLight() bool { func (sdk *BeamNGSDK) HasShiftLight() bool {
return sdk.Data.DashLights&DL_SHIFT != 0 return sdk.Data.DashLights&DL_SHIFT != 0
} }
// HasHighBeamLight returns: // HasHighBeamLight reports whether a high beam light is available
// true if its available
// false if its unavailable
func (sdk *BeamNGSDK) HasHighBeamLight() bool { func (sdk *BeamNGSDK) HasHighBeamLight() bool {
return sdk.Data.DashLights&DL_FULLBEAM != 0 return sdk.Data.DashLights&DL_FULLBEAM != 0
} }
// HasHandbrakeLight returns: // HasHandbrakeLight reports wheter a handbrake light is available
// true if its available
// false if its unavailable
func (sdk *BeamNGSDK) HasHandbrakeLight() bool { func (sdk *BeamNGSDK) HasHandbrakeLight() bool {
return sdk.Data.DashLights&DL_HANDBRAKE != 0 return sdk.Data.DashLights&DL_HANDBRAKE != 0
} }
// HasPitspeed returns: // HasPitspeed reports whether a pit speed limitr is available
// true if its available
// false if its unavailable
// NOTE: this may not be used in BeamNG.drive, haven't checked yet // NOTE: this may not be used in BeamNG.drive, haven't checked yet
func (sdk *BeamNGSDK) HasPitspeed() bool { func (sdk *BeamNGSDK) HasPitspeed() bool {
return sdk.Data.DashLights&DL_HANDBRAKE != 0 return sdk.Data.DashLights&DL_HANDBRAKE != 0
} }
// HasTractionControlLight returns: // HasTractionControlLight reports whether a traction control light is available
// true if its available
// false if its unavailable
func (sdk *BeamNGSDK) HasTractionControlLight() bool { func (sdk *BeamNGSDK) HasTractionControlLight() bool {
return sdk.Data.DashLights&DL_TC != 0 return sdk.Data.DashLights&DL_TC != 0
} }
// HasLeftIndicatorLight returns: // HasLeftIndicatorLight reports whether a left indicator is available
// true if its available
// false if its unavailable
func (sdk *BeamNGSDK) HasLeftIndicatorLight() bool { func (sdk *BeamNGSDK) HasLeftIndicatorLight() bool {
return sdk.Data.DashLights&DL_SIGNAL_L != 0 return sdk.Data.DashLights&DL_SIGNAL_L != 0
} }
// HasRightIndicatorLight returns: // HasRightIndicatorLight reports whether a right indicator is available
// true if its available
// false if its unavailable
func (sdk *BeamNGSDK) HasRightIndicatorLight() bool { func (sdk *BeamNGSDK) HasRightIndicatorLight() bool {
return sdk.Data.DashLights&DL_SIGNAL_R != 0 return sdk.Data.DashLights&DL_SIGNAL_R != 0
} }
// HasAnyIndicatorLight returns: // HasAnyIndicatorLight reports whether an any indicator light is available
// true if its available
// false if its unavailable
func (sdk *BeamNGSDK) HasAnyIndicatorLight() bool { func (sdk *BeamNGSDK) HasAnyIndicatorLight() bool {
return sdk.Data.DashLights&DL_SIGNAL_ANY != 0 return sdk.Data.DashLights&DL_SIGNAL_ANY != 0
} }
// HasOilLight returns: // HasOilLight reports whether a oil light is available
// true if its available
// false if its unavailable
func (sdk *BeamNGSDK) HasOilLight() bool { func (sdk *BeamNGSDK) HasOilLight() bool {
return sdk.Data.DashLights&DL_OILWARN != 0 return sdk.Data.DashLights&DL_OILWARN != 0
} }
// HasBatteryLight returns: // HasBatteryLight reports whether a battery light is available
// true if its available
// false if its unavailable
func (sdk *BeamNGSDK) HasBatteryLight() bool { func (sdk *BeamNGSDK) HasBatteryLight() bool {
return sdk.Data.DashLights&DL_BATTERY != 0 return sdk.Data.DashLights&DL_BATTERY != 0
} }
// HasABSLight returns: // HasABSLight reports whether an ABS light is available
// true if its available
// false if its unavailable
func (sdk *BeamNGSDK) HasABSLight() bool { func (sdk *BeamNGSDK) HasABSLight() bool {
return sdk.Data.DashLights&DL_ABS != 0 return sdk.Data.DashLights&DL_ABS != 0
} }
@@ -285,25 +237,21 @@ func (sdk *BeamNGSDK) HasABSLight() bool {
// Flags - functions to check if a given flag is ON [START] // Flags - functions to check if a given flag is ON [START]
// HasTurbo returns: // HasTurbo reports whether there's a turbo
// true if the car has a turbo
// false if it doesn't
func (sdk *BeamNGSDK) HasTurbo() bool { func (sdk *BeamNGSDK) HasTurbo() bool {
return sdk.Data.Flags&OG_TURBO != 0 return sdk.Data.Flags&OG_TURBO != 0
} }
// PrefersKm returns: // PrefersKm reports whether the user prefers kilometers:
// true if true // - true is prefers Km
// false if false // - false is prefers Mi
// wow
func (sdk *BeamNGSDK) PrefersKm() bool { func (sdk *BeamNGSDK) PrefersKm() bool {
return sdk.Data.Flags&OG_KM != 0 return sdk.Data.Flags&OG_KM != 0
} }
// PrefersBAR returns: // PrefersBAR reports whether the user prefers BAR:
// true if true // - true is prefers BAR
// false if false // - false is prefers PSI
// wow
func (sdk *BeamNGSDK) PrefersBAR() bool { func (sdk *BeamNGSDK) PrefersBAR() bool {
return sdk.Data.Flags&OG_BAR != 0 return sdk.Data.Flags&OG_BAR != 0
} }
+1 -2
View File
@@ -38,9 +38,8 @@ func BenchmarkReadData(b *testing.B) {
} }
packetBytes := buf.Bytes() packetBytes := buf.Bytes()
// 4. Reset the timer to isolate the setup allocations from the loop metrics
b.ResetTimer() b.ResetTimer()
b.ReportAllocs() // Tracks memory allocations per operation (B/op and allocs/op) b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
// Feed a packet into the network buffer right before reading it // Feed a packet into the network buffer right before reading it