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
|
package bngsdk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -8,16 +10,18 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
UDP_IP = "127.0.0.1"
|
// DefaultUDPIP is the IP of the UDP outgauge server
|
||||||
UDP_PORT = 4444
|
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
|
Addr *net.UDPAddr
|
||||||
Conn *net.UDPConn
|
Conn *net.UDPConn
|
||||||
Buffer []byte
|
Buffer []byte
|
||||||
Data *Outgauge
|
Data *Outgauge
|
||||||
DataDict map[string]interface{}
|
DataDict map[string]any
|
||||||
}
|
}
|
||||||
|
|
||||||
func createUDPConnection(ip string, port int) (*net.UDPConn, *net.UDPAddr, error) {
|
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
|
// ReadData will read new data from the UDP server
|
||||||
func (sdk *BNGSDK) ReadData() error {
|
func (sdk *BeamNGSDK) ReadData() error {
|
||||||
// Receive data from the socket
|
// Receive data from the socket
|
||||||
n, _, err := sdk.Conn.ReadFromUDP(sdk.Buffer)
|
n, _, err := sdk.Conn.ReadFromUDP(sdk.Buffer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -52,6 +56,8 @@ func (sdk *BNGSDK) ReadData() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Read the binary data into the struct
|
// 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])
|
reader := bytes.NewReader(sdk.Buffer[:n])
|
||||||
if err := binary.Read(reader, binary.LittleEndian, sdk.Data); err != nil {
|
if err := binary.Read(reader, binary.LittleEndian, sdk.Data); err != nil {
|
||||||
fmt.Println("Error decoding UDP packet:", err)
|
fmt.Println("Error decoding UDP packet:", err)
|
||||||
@@ -59,25 +65,28 @@ func (sdk *BNGSDK) ReadData() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Update the local map
|
// 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()
|
sdk.DataDict = sdk.Data.ToMap()
|
||||||
|
|
||||||
// this means there's new data
|
// this means there's new data
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sdk *BNGSDK) Close() {
|
func (sdk *BeamNGSDK) Close() error {
|
||||||
sdk.Conn.Close()
|
return sdk.Conn.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init initializes a BeamNG SDK struct
|
// 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
|
var err error
|
||||||
sdk := BNGSDK{}
|
sdk := BeamNGSDK{}
|
||||||
|
|
||||||
// Create the connection to the OutGauge server
|
// Create the connection to the OutGauge server
|
||||||
sdk.Conn, sdk.Addr, err = createUDPConnection(ip, port)
|
sdk.Conn, sdk.Addr, err = createUDPConnection(ip, port)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return BNGSDK{}, err
|
return BeamNGSDK{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initiate the data variables
|
// Initiate the data variables
|
||||||
@@ -87,3 +96,169 @@ func Init(ip string, port int) (BNGSDK, error) {
|
|||||||
|
|
||||||
return sdk, nil
|
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
|
package bngsdk
|
||||||
|
|
||||||
/* TODO
|
// More documentation at https://go.beamng.com/protocols.
|
||||||
- OG_x Flags
|
// Or at BeamNG/lua/vehicle/protocols/outgauge.lua
|
||||||
- DL_x Flags
|
// 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 {
|
type Outgauge struct {
|
||||||
Time uint32 // time in milliseconds (to check order)
|
Time uint32 // time in milliseconds (to check order)
|
||||||
Car [4]byte // Car name
|
Car [4]byte // Car name
|
||||||
@@ -29,8 +51,9 @@ type Outgauge struct {
|
|||||||
ID int32 // optional - only if OutGauge ID is specified
|
ID int32 // optional - only if OutGauge ID is specified
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *Outgauge) ToMap() map[string]interface{} {
|
// ToMap creates a map with the data in the Outgauge struct
|
||||||
return map[string]interface{}{
|
func (o *Outgauge) ToMap() map[string]any {
|
||||||
|
return map[string]any{
|
||||||
"Time": o.Time, // time in milliseconds (to check order)
|
"Time": o.Time, // time in milliseconds (to check order)
|
||||||
"Car": o.Car, // Car name
|
"Car": o.Car, // Car name
|
||||||
"Flags": o.Flags, // Info (see OG_x below)
|
"Flags": o.Flags, // Info (see OG_x below)
|
||||||
|
|||||||
Reference in New Issue
Block a user