The relative is working like this, just make the other UI items also work

This commit is contained in:
2025-02-21 23:08:15 +00:00
parent 977e5e4b38
commit dc4eab1503
4 changed files with 92 additions and 75 deletions
+9 -9
View File
@@ -4,8 +4,8 @@ import (
"os"
"esdi/logger"
// "esdi/sources/iracing"
"github.com/ESilva15/goirsdk"
"esdi/sources/iracing"
// "github.com/ESilva15/goirsdk"
"github.com/spf13/cobra"
)
@@ -30,16 +30,16 @@ func offlineTelemetryCmdAction(cmd *cobra.Command, args []string) {
log.Fatalf("Failed to open IBT file: %v", err)
}
// irsdk, err := iracing.Init(file, outFile, sessionFile)
// if err != nil {
// log.Fatalf("Failed to create iRacing interface: %v", err)
// }
irsdk, err := goirsdk.Init(file, outFile, sessionFile)
irsdk, err := iracing.Init(file, outFile, sessionFile)
if err != nil {
log.Fatalf("Failed to create irsdk instance: %v\n", err)
log.Fatalf("Failed to create iRacing interface: %v", err)
}
// irsdk, err := goirsdk.Init(file, outFile, sessionFile)
// if err != nil {
// log.Fatalf("Failed to create irsdk instance: %v\n", err)
// }
esdi.irsdk = irsdk
esdi.irsdk = irsdk.SDK
esdi.telemetry()
}
+4 -2
View File
@@ -11,7 +11,8 @@ type ESDI struct {
SerialConfig *serial.Config
SerialConn *serial.Port
irsdk *goirsdk.IBT
data DataPacket
data SimulationData
dataPacket DataPacket
// Source GameSource
}
@@ -35,5 +36,6 @@ func ESDIInit(port string, baud int) (ESDI, error) {
}
// return ESDI{sConfig, sPort, nil}, nil
return ESDI{sConfig, sPort, nil, DataPacket{}}, nil
return ESDI{sConfig, sPort, nil, SimulationData{}, DataPacket{}}, nil
// return ESDI{nil, nil, nil, SimulationData{}, DataPacket{}}, nil
}
+74 -60
View File
@@ -11,7 +11,6 @@ import (
"sync"
"syscall"
"time"
// "github.com/tarm/serial"
)
@@ -21,7 +20,7 @@ type GameSource interface {
GetSessionInfo() (interface{}, error)
}
type DataPacket struct {
type SimulationData struct {
Speed int32
Gear int32
RPM int32
@@ -168,28 +167,28 @@ func printData(e *ESDI, done <-chan string) {
buffer.WriteString("\033[?25l\033[2J\033[H")
mu.Lock()
buffer.WriteString("Car data:\n")
// buffer.WriteString("Car data:\n")
buffer.WriteString(fmt.Sprintf("Gear: %d, RPM: %d, Speed: %d\n\n",
e.data.Gear, e.data.RPM, e.data.Speed))
e.dataPacket.Gear, e.dataPacket.RPM, e.dataPacket.Speed))
//
// buffer.WriteString("Fuel data:\n")
// buffer.WriteString(fmt.Sprintf("Fuel: %sL/%sL %s/lap [%s%%]\n\n", e.dataPacket.FuelLiters,
// e.dataPacket.FuelTotal, e.dataPacket.FuelPerLap, e.dataPacket.FuelPct))
//
// buffer.WriteString("Lap data:\n")
// buffer.WriteString(fmt.Sprintf("LapTime: %s [%s]\n", e.dataPacket.LapTime,
// e.dataPacket.LapDelta))
// buffer.WriteString(fmt.Sprintf("Best Lap Time: %s\n", e.dataPacket.BestLapTime))
// buffer.WriteString(fmt.Sprintf("Last Lap Time: %s\n", e.dataPacket.LastLapTime))
// buffer.WriteString(fmt.Sprintf("Lap: %d [%.2f%%]\n\n", e.dataPacket.LapCount,
// e.data.LapDistPct))
//
// buffer.WriteString("Position data:\n")
// buffer.WriteString(fmt.Sprintf("Pos: %d\n", e.dataPacket.Position))
buffer.WriteString("Fuel data:\n")
buffer.WriteString(fmt.Sprintf("Fuel: %.2fL/%.2fL %.2f/lap [%.2f%%]\n\n", e.data.FuelLiters,
e.data.FuelTotal, e.data.FuelPerLap, e.data.FuelPct))
buffer.WriteString("Lap data:\n")
buffer.WriteString(fmt.Sprintf("LapTime: %s [%s]\n", e.data.LapTime,
e.data.LapDelta))
buffer.WriteString(fmt.Sprintf("Best Lap Time: %s\n", e.data.BestLapTime))
buffer.WriteString(fmt.Sprintf("Last Lap Time: %s\n", e.data.LastLapTime))
buffer.WriteString(fmt.Sprintf("Lap: %d [%.2f%%]\n\n", e.data.LapCount,
e.data.LapDistPct))
buffer.WriteString("Position data:\n")
buffer.WriteString(fmt.Sprintf("Pos: %d\n", e.data.Position))
for p, v := range e.data.Standings {
s := fmt.Sprintf("[%2d] [%2d] %-16s %3d %10f %s\n",
p+1, v.CarIdx, string(bytes.Trim(v.DriverName[:], "\x00")), v.Lap, v.LapPct, v.TimeBehindString)
for p, v := range e.dataPacket.Standings {
s := fmt.Sprintf("[%2d] %s %-16s %-16s\n",
p+1, v.Lap, string(bytes.Trim(v.DriverName[:], "\x00")), v.TimeBehindString)
buffer.WriteString(s)
}
mu.Unlock()
@@ -287,23 +286,41 @@ func (e *ESDI) positionData() {
mu.Unlock()
}
type DataToSend struct {
Speed int32
Gear int32
RPM int32
LapCount int32
LapDistPct float32
LapTime [16]byte // Current lap time
LapDelta [16]byte // Delta to selected reference lap
BestLapTime [16]byte // Best lap in session
LastLapTime [16]byte // Last lap time
FuelUsageCurLap float32
FuelPerLap float32
// FuelPct float32
// FuelLiters float32
// FuelTotal float32 // This will be calculated and passed in Liters
// Position int32
// Standings [5]StandingsLine
type StandingsLineDataPacket struct {
Lap [4]byte
DriverName [16]byte
TimeBehindString [16]byte
}
type DataPacket struct {
Speed int32
Gear int32
RPM int32
// LapCount int32
// LapTime [10]byte // Current lap time
// LapDelta [10]byte // Delta to selected reference lap
// BestLapTime [10]byte // Best lap in session
// LastLapTime [10]byte // Last lap time
// FuelPerLap [8]byte
// FuelPct [8]byte
// FuelLiters [8]byte
// FuelTotal [8]byte // This will be calculated and passed in Liters
// Position int32
Standings [5]StandingsLineDataPacket
}
func packageStandingsLineDataPacket(data [5]StandingsLine) [5]StandingsLineDataPacket {
var sl [5]StandingsLineDataPacket
for k := range data {
copy(sl[k].Lap[:], []byte(fmt.Sprintf("%-3d", data[k].Lap)))
copy(sl[k].DriverName[:], []byte(fmt.Sprintf("%-16s", data[k].DriverName[0:16])))
copy(sl[k].TimeBehindString[:], []byte(fmt.Sprintf("%-16s", data[k].TimeBehindString[0:7])))
sl[k].TimeBehindString[7] = '\x00'
sl[k].DriverName[15] = '\x00'
}
return sl
}
func (e *ESDI) telemetry() {
@@ -315,7 +332,7 @@ func (e *ESDI) telemetry() {
// go sendData(e, e.SerialConn, done, dataError)
go printData(e, done)
mainLoopTicker := time.NewTicker(time.Second / 60)
mainLoopTicker := time.NewTicker(time.Second / 240)
defer mainLoopTicker.Stop()
fuelLevels = make(map[int]float32, 256)
@@ -329,8 +346,6 @@ func (e *ESDI) telemetry() {
case s := <-dataError:
done <- s
case <-mainLoopTicker.C:
time.Sleep(time.Second / 60)
var err error
_, err = e.irsdk.Update(time.Millisecond * 100)
@@ -346,27 +361,26 @@ func (e *ESDI) telemetry() {
mu.Lock()
data := DataToSend{
Speed: e.data.Speed,
Gear: e.data.Gear,
RPM: e.data.RPM,
LapCount: e.data.LapCount,
LapDistPct: e.data.LapDistPct,
// FuelPerLap: e.data.FuelPerLap,
// FuelUsageCurLap: e.data.FuelUsageCurLap,
}
// Test the actual dataPacket we are sending over the wire
e.dataPacket.Speed = e.data.Speed
e.dataPacket.Gear = e.data.Gear
e.dataPacket.RPM = e.data.RPM
// e.dataPacket.LapCount = e.data.LapCount
// e.dataPacket.Position = e.data.Position
e.dataPacket.Standings = packageStandingsLineDataPacket(e.data.Standings)
copy(data.LapTime[:], e.data.LapTime[:])
copy(data.LapDelta[:], e.data.LapDelta[:])
copy(data.BestLapTime[:], e.data.BestLapTime[:])
copy(data.LastLapTime[:], e.data.LastLapTime[:])
// copy(e.dataPacket.FuelPerLap[:], []byte(fmt.Sprintf("%.2f", e.data.FuelPerLap)))
// copy(e.dataPacket.FuelPct[:], []byte(fmt.Sprintf("%.2f", e.data.FuelPct)))
// copy(e.dataPacket.FuelTotal[:], []byte(fmt.Sprintf("%.2f", e.data.FuelTotal)))
// copy(e.dataPacket.FuelLiters[:], []byte(fmt.Sprintf("%.2f", e.data.FuelLiters)))
//
// copy(e.dataPacket.LapTime[:], e.data.LapTime[:])
// copy(e.dataPacket.LapDelta[:], e.data.LapDelta[:])
// copy(e.dataPacket.BestLapTime[:], e.data.BestLapTime[:])
// copy(e.dataPacket.LastLapTime[:], e.data.LastLapTime[:])
var buf bytes.Buffer
err = binary.Write(&buf, binary.LittleEndian, data)
if len(buf.Bytes()) != 92 {
panic(fmt.Sprintf("Size is: %d, instead of 92", len(buf.Bytes())))
}
err = binary.Write(&buf, binary.LittleEndian, e.dataPacket)
_, err = e.SerialConn.Write(buf.Bytes())
if err != nil {
+5 -4
View File
@@ -58,16 +58,17 @@ func generalStandings(i *goirsdk.IBT, s []StandingsLine, id int) {
}
func relativeStandings(i *goirsdk.IBT, s []StandingsLine, id int) {
// We sort the racists solely by their position on the track because all
// we want to know is where people are in relation to us
// But we should add a way to diferentatiate lap counts
sort.Slice(s, func(i int, j int) bool {
return s[i].LapPct > s[j].LapPct
if s[i].Lap == int32(s[j].Lap) {
return s[i].LapPct > s[j].LapPct
}
return s[i].Lap > s[j].Lap
})
estTime := i.Vars.Vars["CarIdxEstTime"].Value.([]float32)
// Get the delta to a given carId
// TODO: fix the delta when the car behind is still in the previous lap.
for p := range s {
curCarEstimate := estTime[s[p].CarIdx]