4 Commits
Author SHA1 Message Date
esilva 502bf03835 Small update but it changes the API 2025-10-02 14:33:59 +01:00
esilva ee03eab4c1 Simple rename 2025-10-01 23:09:49 +01:00
esilva bfc9882bb2 Added the OG_x and DL_x fields 2025-10-01 20:08:13 +01:00
esilva 9a9d486f9f Some Go updates to the code and docstrings
Like replacing interface{} with any and adding docstrings
2025-10-01 19:24:50 +01:00
2 changed files with 44 additions and 17 deletions
+14 -10
View File
@@ -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
// UDPIP is the IP of the UDP outgauge server
UDPIP = "127.0.0.1"
// UDPPort is the port of the UDP outgauge server
UDPPort = 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 {
@@ -65,19 +69,19 @@ func (sdk *BNGSDK) ReadData() error {
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) {
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
+30 -7
View File
@@ -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 = 2 ^ 0 // shift light
DL_FULLBEAM = 2 ^ 1 // full beam
DL_HANDBRAKE = 2 ^ 2 // handbrake
DL_PITSPEED = 2 ^ 3 // pit speed limiter // N/A
DL_TC = 2 ^ 4 // tc active or switched off
DL_SIGNAL_L = 2 ^ 5 // left turn signal
DL_SIGNAL_R = 2 ^ 6 // right turn signal
DL_SIGNAL_ANY = 2 ^ 7 // shared turn signal // N/A
DL_OILWARN = 2 ^ 8 // oil pressure warning
DL_BATTERY = 2 ^ 9 // battery warning
DL_ABS = 2 ^ 10 // abs active or switched off
DL_SPARE = 2 ^ 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)