Checkpoint
This commit is contained in:
125 files changed
+59376
No files matched your search
@@ -0,0 +1,160 @@
|
||||
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"`
|
||||
}
|
||||
|
||||
// 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).
|
||||
type Display struct {
|
||||
Name string `hcl:"name,label"`
|
||||
|
||||
// Type of display.
|
||||
Type string `hcl:"type"`
|
||||
|
||||
// Size of the display in pixels.
|
||||
Size string `hcl:"size"`
|
||||
|
||||
// Bus is the I²C or SPI bus name.
|
||||
Bus string `hcl:"bus,optional"`
|
||||
|
||||
// Address is the I²C address.
|
||||
Address string `hcl:"addr,optional"`
|
||||
|
||||
// Frequency is the I²C or SPI bus frequency.
|
||||
Freq string `hcl:"frequency,optional"`
|
||||
|
||||
// Reset is the reset pin.
|
||||
Reset string `hcl:"reset,optional"`
|
||||
|
||||
// DC is the data/command pin.
|
||||
DC string `hcl:"dc,optional"`
|
||||
|
||||
// CS is the chip select pin.
|
||||
CS string `hcl:"cs,optional"`
|
||||
|
||||
// BL is the backlight pin.
|
||||
BL string `hcl:"bl,optional"`
|
||||
|
||||
// Host name.
|
||||
Host string `hcl:"host,optional"`
|
||||
|
||||
// Port name or number.
|
||||
Port string `hcl:"port,optional"`
|
||||
|
||||
// Rotate the screen.
|
||||
Rotate string `hcl:"rotate,optional"`
|
||||
}
|
||||
|
||||
// Input configuration for the input devices.
|
||||
type Input struct {
|
||||
Name string `hcl:"name,label"`
|
||||
Type string `hcl:"type"`
|
||||
}
|
||||
|
||||
// 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"`
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
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) {
|
||||
log := logrus.WithField("path", name)
|
||||
log.Debug("loading configuration")
|
||||
|
||||
source, err := os.ReadFile(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var (
|
||||
config = new(Structure)
|
||||
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() {
|
||||
return nil, diag
|
||||
}
|
||||
|
||||
if diag = gohcl.DecodeBody(file.Body, ctx, config); diag.HasErrors() {
|
||||
return nil, diag
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
||||
func ExplainError(name string, err error) bool {
|
||||
fmt.Printf("error type %T\n", err)
|
||||
if diags, ok := err.(hcl.Diagnostics); ok {
|
||||
for _, diag := range diags {
|
||||
if diag.Subject != nil {
|
||||
return explainSubject(diag)
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func explainSubject(diag *hcl.Diagnostic) bool {
|
||||
if diag.Subject == nil || diag.Subject.Filename == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
source, err := os.ReadFile(diag.Subject.Filename)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
fmt.Printf("Fatal error parsing %s:\n", diag.Subject)
|
||||
lines := strings.Split(string(source), "\n")
|
||||
start := diag.Subject.Start.Line - 3
|
||||
if start < 0 {
|
||||
start = 0
|
||||
}
|
||||
for line := start; line < diag.Subject.Start.Line; line++ {
|
||||
fmt.Printf("%4d| %s\n", line+1, lines[line])
|
||||
}
|
||||
fmt.Print(" ...|")
|
||||
if diag.Subject.Start.Column > 1 {
|
||||
fmt.Print(strings.Repeat(" ", diag.Subject.Start.Column))
|
||||
}
|
||||
fmt.Print("^- ")
|
||||
fmt.Println(diag.Summary + "\n")
|
||||
fmt.Println(diag.Detail)
|
||||
|
||||
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