Checkpoint
This commit is contained in:
138 files changed
+1898
-57607
No files matched your search
+49
-110
@@ -1,74 +1,32 @@
|
||||
// Package config contains the configuration structure and parser.
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"periph.io/x/conn/v3/gpio"
|
||||
"periph.io/x/conn/v3/gpio/gpioreg"
|
||||
)
|
||||
|
||||
/*
|
||||
// Load the configuration file.
|
||||
func Load(name string) (*Configuration, error) {
|
||||
var (
|
||||
cfg Configuration
|
||||
md, err = toml.DecodeFile(name, &cfg)
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if u := md.Undecoded(); len(u) > 0 {
|
||||
return nil, fmt.Errorf("bragi: unknown configuration keys: %v", u)
|
||||
}
|
||||
return &cfg, nil
|
||||
}
|
||||
*/
|
||||
|
||||
// Structure holds the configuration settings for the application.
|
||||
type Structure struct {
|
||||
Audio *Audio `hcl:"audio,block"`
|
||||
Player *Player `hcl:"player,block"`
|
||||
Page []*Page `hcl:"page,block"`
|
||||
Display []*Display `hcl:"display,block"`
|
||||
Input []*Input `hcl:"input,block"`
|
||||
Visual []*Visual `hcl:"visual,block"`
|
||||
// Config structure.
|
||||
type Config struct {
|
||||
Display Display `hcl:"display,block"`
|
||||
Input Input `hcl:"input,block"`
|
||||
Audio Audio `hcl:"audio,block"`
|
||||
Player Player `hcl:"player,block"`
|
||||
}
|
||||
|
||||
// Audio stream configuration.
|
||||
type Audio struct {
|
||||
Type string `hcl:"type"`
|
||||
Host *string `hcl:"host"`
|
||||
Path *string `hcl:"path"`
|
||||
}
|
||||
|
||||
// Player configuration for the music player.
|
||||
type Player struct {
|
||||
Type string `hcl:"type"`
|
||||
Host *string `hcl:"host"`
|
||||
Password *string `hcl:"password"`
|
||||
}
|
||||
|
||||
// Page configuration.
|
||||
type Page struct {
|
||||
Name string `hcl:"name,label"`
|
||||
Visual []string `hcl:"visual"`
|
||||
Action *PageAction `hcl:"action,block"`
|
||||
}
|
||||
|
||||
type PageAction struct {
|
||||
RotaryCW *string `hcl:"rotary_cw"`
|
||||
RotaryCCW *string `hcl:"rotary_ccw"`
|
||||
RotaryClick *string `hcl:"rotary_click"`
|
||||
RotaryHold *string `hcl:"rotary_hold"`
|
||||
}
|
||||
|
||||
// Display configuration for the display(s).
|
||||
// Display configuration for OLED and TFT screens.
|
||||
type Display struct {
|
||||
Name string `hcl:"name,label"`
|
||||
FPS int `hcl:"fps,optional"`
|
||||
Left DisplayConfig `hcl:"left,block"`
|
||||
Right DisplayConfig `hcl:"right,block"`
|
||||
Color DisplayConfig `hcl:"color,block"`
|
||||
}
|
||||
|
||||
// DisplayConfig is the configuration for one display.
|
||||
type DisplayConfig struct {
|
||||
Name string
|
||||
|
||||
// Type of display.
|
||||
Type string `hcl:"type"`
|
||||
|
||||
// Mode of operation; i2c or spi (defaults to i2c).
|
||||
Mode string `hcl:"mode,optional"`
|
||||
|
||||
// Size of the display in pixels.
|
||||
Size string `hcl:"size"`
|
||||
|
||||
@@ -101,60 +59,41 @@ type Display struct {
|
||||
|
||||
// Rotate the screen.
|
||||
Rotate string `hcl:"rotate,optional"`
|
||||
|
||||
// Flip the screen vertically (if supported by hardware).
|
||||
Flip bool `hcl:"flip,optional"`
|
||||
|
||||
// Mirror the screen horizontally (if supported by hardware).
|
||||
Mirror bool `hcl:"flip,optional"`
|
||||
}
|
||||
|
||||
// Input configuration for the input devices.
|
||||
// Input peripherals.
|
||||
type Input struct {
|
||||
Name string `hcl:"name,label"`
|
||||
Encoder Encoder `hcl:"encoder,block"`
|
||||
}
|
||||
|
||||
// Encoder is a rotary encoder.
|
||||
type Encoder struct {
|
||||
Type string `hcl:"type"`
|
||||
|
||||
CW string `hcl:"cw,optional"`
|
||||
CCW string `hcl:"ccw,optional"`
|
||||
Switch string `hcl:"sw,optional"`
|
||||
}
|
||||
|
||||
// InputRotary configuration for a rotary encoder.
|
||||
type InputRotary struct {
|
||||
// Name of the rotary encoder.
|
||||
Name string `hcl:"name,label"`
|
||||
|
||||
// ID is the device identifier, if left empty the device is the first rotary found.
|
||||
ID *string `hcl:"id,omitempty"`
|
||||
|
||||
// CCW is the counter-clockwise action.
|
||||
CCW string `hcl:"ccw"`
|
||||
|
||||
// CW is the clockwise action.
|
||||
CW string `hcl:"cw"`
|
||||
|
||||
// Click is the switch action.
|
||||
Click string `hcl:"click"`
|
||||
// Audio source provides PCM samples.
|
||||
type Audio struct {
|
||||
Type string `hcl:"type"`
|
||||
Path string `hcl:"path,optional"`
|
||||
Host string `hcl:"host,optional"`
|
||||
Rate float64 `hcl:"rate,optional"`
|
||||
Channels int `hcl:"channels,optional"`
|
||||
Bits int `hcl:"bits,optional"`
|
||||
}
|
||||
|
||||
// Visual configuration.
|
||||
type Visual struct {
|
||||
Name string `hcl:"name,label"`
|
||||
Type string `hcl:"type"`
|
||||
Config map[string]string `hcl:"config,optional"`
|
||||
}
|
||||
|
||||
// BusType is the bus type (i2c or spi).
|
||||
type BusType string
|
||||
|
||||
func (t *BusType) UnmarshalText(text []byte) error {
|
||||
switch string(text) {
|
||||
case "i2c", "spi":
|
||||
*t = BusType(string(text))
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("bragi: invalid bus type %q", text)
|
||||
}
|
||||
}
|
||||
|
||||
// PinOut is the GPIO output pin.
|
||||
type PinOut struct {
|
||||
Pin gpio.PinOut
|
||||
}
|
||||
|
||||
func (c *PinOut) UnmarshalText(text []byte) error {
|
||||
if c.Pin = gpioreg.ByName(string(text)); c.Pin == nil {
|
||||
return fmt.Errorf("bragi: invalid GPIO pin %q", text)
|
||||
}
|
||||
return nil
|
||||
// Player controls the audio.
|
||||
type Player struct {
|
||||
Type string `hcl:"type"`
|
||||
Host string `hcl:"host,optional"`
|
||||
Password string `hcl:"password,optional"`
|
||||
}
|
||||
+4
-109
@@ -3,33 +3,16 @@ package config
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
"github.com/hashicorp/hcl/v2"
|
||||
"github.com/hashicorp/hcl/v2/gohcl"
|
||||
"github.com/hashicorp/hcl/v2/hclsyntax"
|
||||
"github.com/hashicorp/hcl/v2/json"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/zclconf/go-cty/cty"
|
||||
"github.com/zclconf/go-cty/cty/function"
|
||||
)
|
||||
|
||||
var evalContext = &hcl.EvalContext{
|
||||
Functions: map[string]function.Function{
|
||||
"hex": hexFunc,
|
||||
"bounds": boundsFunc,
|
||||
"rotary": rotaryFunc,
|
||||
"volume": volumeFunc,
|
||||
//"volume": volumeFunc,
|
||||
//"goto": gotoFunc,
|
||||
},
|
||||
}
|
||||
|
||||
// Load the configuration file.
|
||||
func Load(name string) (*Structure, error) {
|
||||
func Load(name string) (*Config, error) {
|
||||
log := logrus.WithField("path", name)
|
||||
log.Debug("loading configuration")
|
||||
|
||||
@@ -39,46 +22,14 @@ func Load(name string) (*Structure, error) {
|
||||
}
|
||||
|
||||
var (
|
||||
config = new(Structure)
|
||||
config = new(Config)
|
||||
ctx *hcl.EvalContext
|
||||
file *hcl.File
|
||||
meta toml.MetaData
|
||||
diag hcl.Diagnostics
|
||||
)
|
||||
|
||||
switch ext := strings.ToLower(filepath.Ext(name)); ext {
|
||||
case ".json":
|
||||
log.Trace("parsing as JSON")
|
||||
file, diag = json.Parse(source, name)
|
||||
|
||||
case ".hcl", ".cfg", ".conf":
|
||||
log.Trace("parsing as HCL")
|
||||
start := hcl.Pos{Line: 1, Column: 1}
|
||||
file, diag = hclsyntax.ParseConfig(source, name, start)
|
||||
|
||||
case ".toml":
|
||||
log.Trace("parsing as TOML")
|
||||
if meta, err = toml.Decode(string(source), config); err != nil {
|
||||
return nil, err
|
||||
} else if undecoded := meta.Undecoded(); len(undecoded) > 0 {
|
||||
diag = diag.Append(&hcl.Diagnostic{
|
||||
Severity: hcl.DiagError,
|
||||
Summary: "Undecoded contents",
|
||||
Detail: fmt.Sprintf("Cannot read from %s: undecoded items in configuration: %v.", name, undecoded),
|
||||
})
|
||||
return nil, diag
|
||||
}
|
||||
|
||||
default:
|
||||
diag = diag.Append(&hcl.Diagnostic{
|
||||
Severity: hcl.DiagError,
|
||||
Summary: "Unsupported file format",
|
||||
Detail: fmt.Sprintf("Cannot read from %s: unrecognized file format suffix %q.", name, ext),
|
||||
})
|
||||
return nil, diag
|
||||
}
|
||||
|
||||
if diag.HasErrors() {
|
||||
start := hcl.Pos{Line: 1, Column: 1}
|
||||
if file, diag = hclsyntax.ParseConfig(source, name, start); diag.HasErrors() {
|
||||
return nil, diag
|
||||
}
|
||||
|
||||
@@ -130,59 +81,3 @@ func explainSubject(diag *hcl.Diagnostic) bool {
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
var (
|
||||
hexFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{{
|
||||
Name: "number",
|
||||
Type: cty.String,
|
||||
}},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
v := args[0].AsString()
|
||||
if strings.HasPrefix(strings.ToLower(v), "0x") {
|
||||
v = v[2:]
|
||||
}
|
||||
i, err := strconv.ParseUint(v, 16, 64)
|
||||
if err != nil {
|
||||
return cty.NumberUIntVal(0), err
|
||||
}
|
||||
return cty.NumberUIntVal(i), nil
|
||||
},
|
||||
})
|
||||
|
||||
rotaryFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{{
|
||||
Name: "action",
|
||||
Type: cty.String,
|
||||
}},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
})
|
||||
|
||||
boundsFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{{
|
||||
Name: "width",
|
||||
Type: cty.Number,
|
||||
}, {
|
||||
Name: "height",
|
||||
Type: cty.Number,
|
||||
}},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
w := args[0].AsString()
|
||||
h := args[1].AsString()
|
||||
return cty.StringVal(w + "x" + h), nil
|
||||
},
|
||||
})
|
||||
|
||||
volumeFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{{
|
||||
Name: "level",
|
||||
Type: cty.Number,
|
||||
}},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
return cty.StringVal("volume:" + args[0].AsString()), nil
|
||||
},
|
||||
})
|
||||
)
|
||||
Reference in new issue
Block a user