this is a doozy - device auto discovery and handling

The main goal was to decouple more things
We got devices being looked for in the background and the stream view
is like a device now too
This commit is contained in:
2026-09-17 22:55:17 +01:00
parent 20025dae1b
commit 65f03fd4c9
17 changed files with 327 additions and 111 deletions
+20 -2
View File
@@ -1,9 +1,27 @@
package cdashdisplay
import "esdi/peripheral/devices"
import (
"esdi/peripheral/devices"
"esdi/telemetry"
)
// TODO: I believe we don't need the #esdi/peripheral/devices thing anymore
const (
ID = devices.CDashDisplayDevID
Name = devices.CDashDisplayDevName
NAME = devices.CDashDisplayDevName
)
func (cds *CDashDisplay) Name() string {
return NAME
}
func (cds *CDashDisplay) RequiredFields() map[int16]telemetry.FieldID {
fields := make(map[int16]telemetry.FieldID, len(cds.State.Layout.Windows))
for _, w := range cds.State.Layout.Windows {
fieldID, _ := telemetry.GetFieldID(w.UIData.TelemetryField)
fields[w.UIData.IDX] = fieldID
}
return fields
}
+56 -5
View File
@@ -1,9 +1,60 @@
// Package devices simply defines what a device should have
// and some utils if necessary
// Package devices is a peripheral factory
package devices
import "esdi/telemetry"
import (
"errors"
type Device interface {
SendData(*telemetry.TelemetryData)
"esdi/devices/cdashdisplay"
"esdi/devices/uidevice"
"esdi/peripheral"
)
type Device struct {
Name string
Discover func() (peripheral.Peripheral, error)
}
var List map[string]Device = map[string]Device{
uidevice.NAME: {
Name: uidevice.NAME,
Discover: DiscoverUIDevice,
},
cdashdisplay.NAME: {
Name: cdashdisplay.NAME,
Discover: DiscoverCDashDisplay,
},
}
func DiscoverUIDevice() (peripheral.Peripheral, error) {
uidev, err := uidevice.NewUIDevice()
if err != nil {
return nil, err
}
return uidev, nil
}
func DiscoverCDashDisplay() (peripheral.Peripheral, error) {
// // Find CDashDisplay
// {
// ds.Messages <- "looking for " + cdashdisplay.Name + "...\n"
// ds.Logger.Info("Looking for " + cdashdisplay.Name)
//
// cdashdisplay.SetLogger(ds.Logger.With("[device]", cdashdisplay.Name))
//
// // Create a cdashdisplay
// display, err := cdashdisplay.Discover()
// if err == nil {
// ds.Devices[cdashdisplay.Name] = display
// ds.Logger.Info("found " + cdashdisplay.Name + " on: " + display.WT.Cfg.Name)
// ds.Messages <- "found " + cdashdisplay.Name + " on: " + display.WT.Cfg.Name + "\n"
// return
// }
//
// ds.Logger.Info("didn't find " + cdashdisplay.Name)
// ds.Messages <- "didn't find " + cdashdisplay.Name + "\n"
// // No CDashDisplay available for one reason or another, so we don't set the
// // key
// }
return nil, errors.New("not implemented yet")
}
+53
View File
@@ -0,0 +1,53 @@
package uidevice
import (
"esdi/peripheral"
"esdi/telemetry"
)
type UIDevice struct {
dataChan chan telemetry.TelemetryData
}
const NAME = "UIView"
func NewUIDevice() (peripheral.Peripheral, error) {
return &UIDevice{
dataChan: make(chan telemetry.TelemetryData, 1),
}, nil
}
func (uid *UIDevice) SendData(data *telemetry.TelemetryData) {
if data == nil {
return
}
select {
case uid.dataChan <- *data:
default:
// Drop frame if buffer is full
}
}
func (uid *UIDevice) Name() string {
return NAME
}
func (uid *UIDevice) DataChannel() <-chan telemetry.TelemetryData {
return uid.dataChan
}
func (uid *UIDevice) RequiredFields() map[int16]telemetry.FieldID {
subscribeTo := []telemetry.FieldID{
telemetry.Speed,
telemetry.Gear,
telemetry.RPM,
}
fields := make(map[int16]telemetry.FieldID, len(subscribeTo))
for k, field := range subscribeTo {
fields[int16(k)] = field
}
return fields
}