Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
88f50218a4 | ||
|
|
4251348c15 | ||
|
|
502bf03835 | ||
|
|
ee03eab4c1 | ||
|
|
bfc9882bb2 | ||
|
|
9a9d486f9f |
@@ -1,3 +1,5 @@
|
||||
// Package bngsdk defines an API to interact with the BeamNG outgauge data in
|
||||
// Go
|
||||
package bngsdk
|
||||
|
||||
import (
|
||||
@@ -8,16 +10,18 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
UDP_IP = "127.0.0.1"
|
||||
UDP_PORT = 4444
|
||||
// DefaultUDPIP is the IP of the UDP outgauge server
|
||||
DefaultUDPIP = "127.0.0.1"
|
||||
// DefaultUDPPort is the port of the UDP outgauge server
|
||||
DefaultUDPPort = 4444
|
||||
)
|
||||
|
||||
type BNGSDK struct {
|
||||
type BeamNGSDK struct {
|
||||
Addr *net.UDPAddr
|
||||
Conn *net.UDPConn
|
||||
Buffer []byte
|
||||
Data *Outgauge
|
||||
DataDict map[string]interface{}
|
||||
DataDict map[string]any
|
||||
}
|
||||
|
||||
func createUDPConnection(ip string, port int) (*net.UDPConn, *net.UDPAddr, error) {
|
||||
@@ -37,7 +41,7 @@ func createUDPConnection(ip string, port int) (*net.UDPConn, *net.UDPAddr, error
|
||||
}
|
||||
|
||||
// ReadData will read new data from the UDP server
|
||||
func (sdk *BNGSDK) ReadData() error {
|
||||
func (sdk *BeamNGSDK) ReadData() error {
|
||||
// Receive data from the socket
|
||||
n, _, err := sdk.Conn.ReadFromUDP(sdk.Buffer)
|
||||
if err != nil {
|
||||
@@ -52,6 +56,8 @@ func (sdk *BNGSDK) ReadData() error {
|
||||
}
|
||||
|
||||
// Read the binary data into the struct
|
||||
// NOTE: create a reader for this struct and then reset it with the data from here
|
||||
// instead of creating this one everytime
|
||||
reader := bytes.NewReader(sdk.Buffer[:n])
|
||||
if err := binary.Read(reader, binary.LittleEndian, sdk.Data); err != nil {
|
||||
fmt.Println("Error decoding UDP packet:", err)
|
||||
@@ -59,25 +65,28 @@ func (sdk *BNGSDK) ReadData() error {
|
||||
}
|
||||
|
||||
// Update the local map
|
||||
// NOTE: maybe stop doing this here and make the user request this when he
|
||||
// explicitly wants it
|
||||
sdk.DataDict = sdk.Data.ToMap()
|
||||
|
||||
// this means there's new data
|
||||
return nil
|
||||
}
|
||||
|
||||
func (sdk *BNGSDK) Close() {
|
||||
sdk.Conn.Close()
|
||||
func (sdk *BeamNGSDK) Close() error {
|
||||
return sdk.Conn.Close()
|
||||
}
|
||||
|
||||
// Init initializes a BeamNG SDK struct
|
||||
func Init(ip string, port int) (BNGSDK, error) {
|
||||
// NOTE: Change this to output a *BeamNGSDK
|
||||
func Init(ip string, port int) (BeamNGSDK, error) {
|
||||
var err error
|
||||
sdk := BNGSDK{}
|
||||
sdk := BeamNGSDK{}
|
||||
|
||||
// Create the connection to the OutGauge server
|
||||
sdk.Conn, sdk.Addr, err = createUDPConnection(ip, port)
|
||||
if err != nil {
|
||||
return BNGSDK{}, err
|
||||
return BeamNGSDK{}, err
|
||||
}
|
||||
|
||||
// Initiate the data variables
|
||||
@@ -87,3 +96,169 @@ func Init(ip string, port int) (BNGSDK, error) {
|
||||
|
||||
return sdk, nil
|
||||
}
|
||||
|
||||
// SDK utilities
|
||||
|
||||
// ShowLights - functions to check if a given dash light is on [START]
|
||||
|
||||
// ShiftLight returns:
|
||||
// true if the shift light is on
|
||||
// false if the shift light is off
|
||||
func (sdk *BeamNGSDK) ShiftLight() bool {
|
||||
return sdk.Data.ShowLights&DL_SHIFT != 0
|
||||
}
|
||||
|
||||
// HighBeam returns:
|
||||
// true if the high beams are on
|
||||
// false if the high beams are off
|
||||
func (sdk *BeamNGSDK) HighBeam() bool {
|
||||
return sdk.Data.ShowLights&DL_FULLBEAM != 0
|
||||
}
|
||||
|
||||
// Handbrake returns:
|
||||
// true if the handbrake is pulled
|
||||
// false if the handbrake is down
|
||||
func (sdk *BeamNGSDK) Handbrake() bool {
|
||||
return sdk.Data.ShowLights&DL_HANDBRAKE != 0
|
||||
}
|
||||
|
||||
// Pitspeed returns:
|
||||
// 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
|
||||
func (sdk *BeamNGSDK) Pitspeed() bool {
|
||||
return sdk.Data.ShowLights&DL_HANDBRAKE != 0
|
||||
}
|
||||
|
||||
// TractionControl returns:
|
||||
// true if traction control is on
|
||||
// false if traction control is off
|
||||
func (sdk *BeamNGSDK) TractionControl() bool {
|
||||
return sdk.Data.ShowLights&DL_TC != 0
|
||||
}
|
||||
|
||||
// LeftIndicator returns:
|
||||
// true if the left indicator is on
|
||||
// false if the left indicator is off
|
||||
func (sdk *BeamNGSDK) LeftIndicator() bool {
|
||||
return sdk.Data.ShowLights&DL_SIGNAL_L != 0
|
||||
}
|
||||
|
||||
// RightIndicator returns:
|
||||
// true if the right indicator is on
|
||||
// false if the right indicator is off
|
||||
func (sdk *BeamNGSDK) RightIndicator() bool {
|
||||
return sdk.Data.ShowLights&DL_SIGNAL_R != 0
|
||||
}
|
||||
|
||||
// AnyIndicator returns:
|
||||
// true if the any indicator is on
|
||||
// false if the all indicators are off
|
||||
func (sdk *BeamNGSDK) AnyIndicator() bool {
|
||||
return sdk.Data.ShowLights&DL_SIGNAL_ANY != 0
|
||||
}
|
||||
|
||||
// OilLight returns
|
||||
// true if the oil light is on
|
||||
// false if the oil light is off
|
||||
func (sdk *BeamNGSDK) OilLight() bool {
|
||||
return sdk.Data.ShowLights&DL_OILWARN != 0
|
||||
}
|
||||
|
||||
// BatteryLight returns
|
||||
// true if the battery light is on
|
||||
// false if the battery light is off
|
||||
func (sdk *BeamNGSDK) BatteryLight() bool {
|
||||
return sdk.Data.ShowLights&DL_BATTERY != 0
|
||||
}
|
||||
|
||||
// ABS returns
|
||||
// true if the ABS is engaged
|
||||
// false if the ABS isn't engaged
|
||||
func (sdk *BeamNGSDK) ABS() bool {
|
||||
return sdk.Data.ShowLights&DL_ABS != 0
|
||||
}
|
||||
|
||||
// ShowLights - functions to check if a given dash light is on [END]
|
||||
|
||||
// DashLights - functions to check if a given dash light is provided [START]
|
||||
|
||||
// HasShiftLight returns:
|
||||
// true if its available
|
||||
// false if its unavailable
|
||||
func (sdk *BeamNGSDK) HasShiftLight() bool {
|
||||
return sdk.Data.DashLights&DL_SHIFT != 0
|
||||
}
|
||||
|
||||
// HasHighBeamLight returns:
|
||||
// true if its available
|
||||
// false if its unavailable
|
||||
func (sdk *BeamNGSDK) HasHighBeamLight() bool {
|
||||
return sdk.Data.DashLights&DL_FULLBEAM != 0
|
||||
}
|
||||
|
||||
// HasHandbrakeLight returns:
|
||||
// true if its available
|
||||
// false if its unavailable
|
||||
func (sdk *BeamNGSDK) HasHandbrakeLight() bool {
|
||||
return sdk.Data.DashLights&DL_HANDBRAKE != 0
|
||||
}
|
||||
|
||||
// HasPitspeed returns:
|
||||
// true if its available
|
||||
// false if its unavailable
|
||||
// NOTE: this may not be used in BeamNG.drive, haven't checked yet
|
||||
func (sdk *BeamNGSDK) HasPitspeed() bool {
|
||||
return sdk.Data.DashLights&DL_HANDBRAKE != 0
|
||||
}
|
||||
|
||||
// HasTractionControlLight returns:
|
||||
// true if its available
|
||||
// false if its unavailable
|
||||
func (sdk *BeamNGSDK) HasTractionControlLight() bool {
|
||||
return sdk.Data.DashLights&DL_TC != 0
|
||||
}
|
||||
|
||||
// HasLeftIndicatorLight returns:
|
||||
// true if its available
|
||||
// false if its unavailable
|
||||
func (sdk *BeamNGSDK) HasLeftIndicatorLight() bool {
|
||||
return sdk.Data.DashLights&DL_SIGNAL_L != 0
|
||||
}
|
||||
|
||||
// HasRightIndicatorLight returns:
|
||||
// true if its available
|
||||
// false if its unavailable
|
||||
func (sdk *BeamNGSDK) HasRightIndicatorLight() bool {
|
||||
return sdk.Data.DashLights&DL_SIGNAL_R != 0
|
||||
}
|
||||
|
||||
// HasAnyIndicatorLight returns:
|
||||
// true if its available
|
||||
// false if its unavailable
|
||||
func (sdk *BeamNGSDK) HasAnyIndicatorLight() bool {
|
||||
return sdk.Data.DashLights&DL_SIGNAL_ANY != 0
|
||||
}
|
||||
|
||||
// HasOilLight returns:
|
||||
// true if its available
|
||||
// false if its unavailable
|
||||
func (sdk *BeamNGSDK) HasOilLight() bool {
|
||||
return sdk.Data.DashLights&DL_OILWARN != 0
|
||||
}
|
||||
|
||||
// HasBatteryLight returns:
|
||||
// true if its available
|
||||
// false if its unavailable
|
||||
func (sdk *BeamNGSDK) HasBatteryLight() bool {
|
||||
return sdk.Data.DashLights&DL_BATTERY != 0
|
||||
}
|
||||
|
||||
// HasABSLight returns:
|
||||
// true if its available
|
||||
// false if its unavailable
|
||||
func (sdk *BeamNGSDK) HasABSLight() bool {
|
||||
return sdk.Data.DashLights&DL_ABS != 0
|
||||
}
|
||||
|
||||
// DashLights - functions to check if a given dash light is provided [END]
|
||||
|
||||
+30
-7
@@ -1,11 +1,33 @@
|
||||
package bngsdk
|
||||
|
||||
/* TODO
|
||||
- OG_x Flags
|
||||
- DL_x Flags
|
||||
*/
|
||||
// More documentation at https://go.beamng.com/protocols.
|
||||
// Or at BeamNG/lua/vehicle/protocols/outgauge.lua
|
||||
// OG_x buts for flags
|
||||
const (
|
||||
OG_SHIFT = 1 // key // N/A
|
||||
OG_CTRL = 2 // key // N/A
|
||||
OG_TURBO = 8192 // show turbo gauge
|
||||
OG_KM = 16384 // if not set - user prefers MILES
|
||||
OG_BAR = 32768 // if not set - user prefers PSI
|
||||
)
|
||||
|
||||
// More documentation at https://go.beamng.com/protocols
|
||||
// DL_x Flags
|
||||
const (
|
||||
DL_SHIFT = 1 << 0 // shift light
|
||||
DL_FULLBEAM = 1 << 1 // full beam
|
||||
DL_HANDBRAKE = 1 << 2 // handbrake
|
||||
DL_PITSPEED = 1 << 3 // pit speed limiter // N/A
|
||||
DL_TC = 1 << 4 // tc active or switched off
|
||||
DL_SIGNAL_L = 1 << 5 // left turn signal
|
||||
DL_SIGNAL_R = 1 << 6 // right turn signal
|
||||
DL_SIGNAL_ANY = 1 << 7 // shared turn signal // N/A
|
||||
DL_OILWARN = 1 << 8 // oil pressure warning
|
||||
DL_BATTERY = 1 << 9 // battery warning
|
||||
DL_ABS = 1 << 10 // abs active or switched off
|
||||
DL_SPARE = 1 << 11 // N/A
|
||||
)
|
||||
|
||||
// Outgauge describes the data served by the BeamNG outgauge server
|
||||
type Outgauge struct {
|
||||
Time uint32 // time in milliseconds (to check order)
|
||||
Car [4]byte // Car name
|
||||
@@ -29,8 +51,9 @@ type Outgauge struct {
|
||||
ID int32 // optional - only if OutGauge ID is specified
|
||||
}
|
||||
|
||||
func (o *Outgauge) ToMap() map[string]interface{} {
|
||||
return map[string]interface{}{
|
||||
// ToMap creates a map with the data in the Outgauge struct
|
||||
func (o *Outgauge) ToMap() map[string]any {
|
||||
return map[string]any{
|
||||
"Time": o.Time, // time in milliseconds (to check order)
|
||||
"Car": o.Car, // Car name
|
||||
"Flags": o.Flags, // Info (see OG_x below)
|
||||
|
||||
Reference in New Issue
Block a user