decoupled the winIDs from the telemetry data

hell yeah, brother!
This commit is contained in:
2026-09-17 23:55:54 +01:00
parent 1ac4ab1865
commit dda300ce6b
8 changed files with 164 additions and 91 deletions
+36 -73
View File
@@ -1,12 +1,18 @@
package telemetry
import (
"math"
"strconv"
"sync"
"time"
)
func init() {
fieldNameToID = make(map[string]FieldID, MaxFields)
for id, name := range FieldNames {
fieldNameToID[name] = FieldID(id)
}
}
// NOTE: allow the user to create custom data things. For example, iRacing provides
// multiple surface temps, but I guess the user doesn't want all of them at once.
// allow him to make something that allows some data transformation to occur.
@@ -64,7 +70,7 @@ const (
// NOTE: we can optimize this via a special command that says a given piece of data
// is for multiple targets
type TelemetryField struct {
IDs []int16 // Identification for the serial device
// IDs []int16 // Identification for the serial device
Type DataType
Raw uint64
Str string // Only to be used with DataTypeSTRING
@@ -75,49 +81,6 @@ func (tf *TelemetryField) Unused() {
tf.Raw = uint64('-')
}
// Pack will pack this current TelemetryField into bytes to send over the wire
// Format:
// 0x00 - Field ID
// 0x00 |
// 0x01 - DataType
// 0x02 - if its a (u)int8
// or
// 0x02 - if its a (u)int16 - first byte
// 0x02 - if its a (u)int16 - second byte
// or
// 0x02 - str len max is 255 chars
// [0x02] - str
func (tf *TelemetryField) Pack(dest []byte) []byte {
// NOTE: maybe we can have a pool of these so we don't have to create them here
// or whatever
for _, id := range tf.IDs {
dest = append(dest, uint8(id), uint8(id>>8))
dest = append(dest, uint8(tf.Type))
switch tf.Type {
case DataTypeINT8, DataTypeUINT8, DataTypeCHAR:
dest = append(dest, uint8(tf.Raw))
case DataTypeINT16, DataTypeUINT16:
dest = append(dest, uint8(tf.Raw), uint8(tf.Raw>>8))
case DataTypeINT32, DataTypeUINT32:
dest = append(dest, uint8(tf.Raw), uint8(tf.Raw>>8), uint8(tf.Raw>>16), uint8(tf.Raw>>24))
case DataTypeINT64, DataTypeUINT64:
dest = append(
dest, uint8(tf.Raw), uint8(tf.Raw>>8), uint8(tf.Raw>>16),
uint8(tf.Raw>>24), uint8(tf.Raw>>32), uint8(tf.Raw>>40), uint8(tf.Raw>>48),
uint8(tf.Raw>>56),
)
case DataTypeSTRING:
l := min(len(tf.Str), math.MaxUint8)
dest = append(dest, uint8(l))
dest = append(dest, tf.Str[:l]...)
}
}
return dest
}
func (tf *TelemetryField) String() string {
switch tf.Type {
case DataTypeSTRING:
@@ -259,12 +222,12 @@ func GetFieldName(id FieldID) string {
var fieldNameToID map[string]FieldID
func initFieldNamesMap() {
fieldNameToID = make(map[string]FieldID, MaxFields)
for id, name := range FieldNames {
fieldNameToID[name] = FieldID(id)
}
}
// func initFieldNamesMap() {
// fieldNameToID = make(map[string]FieldID, MaxFields)
// for id, name := range FieldNames {
// fieldNameToID[name] = FieldID(id)
// }
// }
func GetFieldID(name string) (FieldID, bool) {
id, ok := fieldNameToID[name]
@@ -286,25 +249,25 @@ func NewTelemetryData() *TelemetryData {
return &TelemetryData{}
}
func (td *TelemetryData) Pack() []byte {
bufPtr := bufferPool.Get().(*[]byte)
buf := (*bufPtr)[:0]
// for _, bind := range td.ActiveBinds {
// buf = td.Values[bind.ID].Pack(buf)
// }
for k := range td.Values {
if len(td.Values[k].IDs) > 0 {
buf = td.Values[k].Pack(buf)
}
}
// We have to copy here because we have to return the buffer
result := make([]byte, len(buf))
copy(result, buf)
bufferPool.Put(&buf)
return result
}
// func (td *TelemetryData) Pack() []byte {
// bufPtr := bufferPool.Get().(*[]byte)
// buf := (*bufPtr)[:0]
//
// // for _, bind := range td.ActiveBinds {
// // buf = td.Values[bind.ID].Pack(buf)
// // }
//
// for k := range td.Values {
// if len(td.Values[k].IDs) > 0 {
// buf = td.Values[k].Pack(buf)
// }
// }
//
// // We have to copy here because we have to return the buffer
// result := make([]byte, len(buf))
// copy(result, buf)
//
// bufferPool.Put(&buf)
//
// return result
// }
+3 -3
View File
@@ -1,5 +1,5 @@
package telemetry
func Init() {
initFieldNamesMap()
}
// func Init() {
// initFieldNamesMap()
// }