feat: render minimal flight OSD from supervisor snapshots (#3)
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"git.piksar.eu/piotr/groundstation/internal/osd"
|
||||
"git.piksar.eu/piotr/groundstation/internal/protocol"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
@@ -15,6 +18,16 @@ type DisplayConfig struct {
|
||||
Xauthority string `yaml:"xauthority"`
|
||||
}
|
||||
|
||||
// OSDConfig configures the supervisor's flight OSD snapshot publication.
|
||||
type OSDConfig struct {
|
||||
// StatePath is the snapshot state file the display process reads.
|
||||
StatePath string `yaml:"state_path"`
|
||||
// ArmIndex and ArmedThreshold derive the arm state from the final RC
|
||||
// channels: armed while that channel stays above the threshold.
|
||||
ArmIndex int `yaml:"arm_index"`
|
||||
ArmedThreshold int16 `yaml:"armed_threshold"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Display DisplayConfig `yaml:"display"`
|
||||
Serial struct {
|
||||
@@ -26,6 +39,7 @@ type Config struct {
|
||||
Threshold int16 `yaml:"threshold"`
|
||||
Command string `yaml:"command"`
|
||||
} `yaml:"video"`
|
||||
OSD OSDConfig `yaml:"osd"`
|
||||
Logging struct {
|
||||
Enabled bool `yaml:"enabled"`
|
||||
Path string `yaml:"path"`
|
||||
@@ -40,6 +54,7 @@ func Default() Config {
|
||||
c.Video.AuxIndex = 5 // AUX2, zero-based
|
||||
c.Video.Threshold = -512
|
||||
c.Video.Command = "fpv-video"
|
||||
c.OSD = DefaultOSD()
|
||||
c.Logging.Enabled = true
|
||||
return c
|
||||
}
|
||||
@@ -56,17 +71,41 @@ func DefaultDisplay() DisplayConfig {
|
||||
}
|
||||
}
|
||||
|
||||
// DefaultOSD publishes snapshots to the default tmpfs path and derives the
|
||||
// arm state from CH5/AUX1, armed above the midpoint.
|
||||
func DefaultOSD() OSDConfig {
|
||||
return OSDConfig{
|
||||
StatePath: osd.DefaultStatePath,
|
||||
ArmIndex: 4,
|
||||
ArmedThreshold: 0,
|
||||
}
|
||||
}
|
||||
|
||||
func Load(path string) (Config, error) {
|
||||
config := Default()
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return config, nil
|
||||
return config, config.validate()
|
||||
}
|
||||
return config, err
|
||||
}
|
||||
if err := yaml.Unmarshal(data, &config); err != nil {
|
||||
return config, err
|
||||
}
|
||||
return config, nil
|
||||
return config, config.validate()
|
||||
}
|
||||
|
||||
// validate rejects channel indexes outside the final RC channel range: an
|
||||
// out-of-range index would panic on the first decoded frame.
|
||||
func (c Config) validate() error {
|
||||
for name, index := range map[string]int{
|
||||
"video.aux_index": c.Video.AuxIndex,
|
||||
"osd.arm_index": c.OSD.ArmIndex,
|
||||
} {
|
||||
if index < 0 || index >= protocol.Channels {
|
||||
return fmt.Errorf("%s %d outside the RC channel range 0..%d", name, index, protocol.Channels-1)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -48,3 +48,56 @@ func TestLoadOverridesDisplaySettings(t *testing.T) {
|
||||
t.Fatalf("display=%q want default :0", cfg.Display.Display)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultConfigPublishesOSDSnapshotsFromAUX1(t *testing.T) {
|
||||
cfg := Default()
|
||||
|
||||
if cfg.OSD.StatePath != "/run/groundstation/osd-state.bin" {
|
||||
t.Fatalf("state_path=%q want the default tmpfs path", cfg.OSD.StatePath)
|
||||
}
|
||||
// CH5/AUX1 is the arming channel; the fail-closed convention reads it
|
||||
// armed above the midpoint.
|
||||
if cfg.OSD.ArmIndex != 4 {
|
||||
t.Fatalf("arm_index=%d want 4 (CH5/AUX1, zero-based)", cfg.OSD.ArmIndex)
|
||||
}
|
||||
if cfg.OSD.ArmedThreshold != 0 {
|
||||
t.Fatalf("armed_threshold=%d want 0", cfg.OSD.ArmedThreshold)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadOverridesOSDSettings(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "config.yml")
|
||||
body := "osd:\n state_path: /tmp/osd-state.bin\n arm_index: 6\n armed_threshold: 256\n"
|
||||
if err := os.WriteFile(path, []byte(body), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cfg, err := Load(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if cfg.OSD.StatePath != "/tmp/osd-state.bin" {
|
||||
t.Fatalf("state_path=%q want the override", cfg.OSD.StatePath)
|
||||
}
|
||||
if cfg.OSD.ArmIndex != 6 || cfg.OSD.ArmedThreshold != 256 {
|
||||
t.Fatalf("arm config=%+v want the overrides", cfg.OSD)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadRejectsArmAndVideoIndexesOutsideTheChannelRange(t *testing.T) {
|
||||
for name, body := range map[string]string{
|
||||
"arm index below range": "osd:\n arm_index: -1\n",
|
||||
"arm index above range": "osd:\n arm_index: 16\n",
|
||||
"video index below range": "video:\n aux_index: -1\n",
|
||||
"video index above range": "video:\n aux_index: 16\n",
|
||||
} {
|
||||
path := filepath.Join(t.TempDir(), "config.yml")
|
||||
if err := os.WriteFile(path, []byte(body), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := Load(path); err == nil {
|
||||
t.Fatalf("%s must be rejected: an out-of-range index would panic on the first frame", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user