gotelem/skylab/templates/golang.go.tmpl

168 lines
3.7 KiB
Cheetah
Raw Permalink Normal View History

2023-05-17 05:01:40 +00:00
{{ define "packet" }}
{{- $structName := camelCase .Name true}}
{{- /* generate any bitfield structs */ -}}
{{range .Data -}}
{{ if .Bits -}}
{{- $bfname := (printf "%s%s" $structName (camelCase .Name true)) }}
type {{$bfname}} struct {
{{- range $el := .Bits}}
{{camelCase $el.Name true}} bool `json:"{{$el.Name}}"`
{{- end}}
}
2023-05-29 00:39:03 +00:00
func (p *{{$bfname}}) MarshalByte() byte {
2023-05-17 05:01:40 +00:00
var b byte
{{- range $idx, $el := .Bits}}
{{- $bitName := camelCase $el.Name true}}
if p.{{$bitName}} {
b |= 1 << {{$idx}}
}
{{- end}}
return b
}
2023-05-29 00:39:03 +00:00
func (p *{{$bfname}}) UnmarshalByte(b byte) {
2023-05-17 05:01:40 +00:00
{{- range $idx, $el := .Bits}}
{{- $bitName := camelCase $el.Name true }}
p.{{$bitName}} = (b & (1 << {{ $idx }})) != 0
{{- end}}
}
{{end}}
{{- end}}
// {{$structName}} is {{.Description}}
type {{$structName}} struct {
{{- range .Data}}
2023-05-18 22:47:14 +00:00
{{- if .Units }}
// {{.Conversion}} {{.Units}}
{{- end }}
2023-05-17 05:01:40 +00:00
{{ .ToStructMember $structName }} `json:"{{.Name}}"`
{{- end}}
{{- if .Repeat }}
// Idx is the packet index. The accepted range is 0-{{.Repeat}}
2023-05-22 13:41:37 +00:00
Idx uint32 `json:"idx"`
2023-05-17 05:01:40 +00:00
{{- end }}
}
func (p *{{$structName}}) CanId() (can.CanID, error) {
2024-03-05 02:41:15 +00:00
c := can.CanID{Extended: {{.IsExtended}}}
2023-05-17 05:01:40 +00:00
{{- if .Repeat }}
if p.Idx >= {{.Repeat}} {
return c, &UnknownIdError{ {{ printf "0x%X" .Id }} }
2023-05-17 05:01:40 +00:00
}
c.Id = {{ printf "0x%X" .Id }} + p.Idx
2023-05-17 05:01:40 +00:00
{{- else }}
c.Id = {{ printf "0x%X" .Id }}
2023-05-17 05:01:40 +00:00
{{- end }}
return c, nil
2023-05-17 05:01:40 +00:00
}
func (p *{{$structName}}) Size() uint {
return {{.CalcSize}}
}
func (p *{{$structName}}) MarshalPacket() ([]byte, error) {
b := make([]byte, {{ .CalcSize }})
{{.MakeMarshal}}
return b, nil
}
func (p *{{$structName}}) UnmarshalPacket(b []byte) error {
if len(b) != {{.CalcSize}} {
return &BadLengthError{expected: {{.CalcSize}}, actual: uint32(len(b))}
}
2023-05-17 05:01:40 +00:00
{{.MakeUnmarshal}}
return nil
}
func (p *{{$structName}}) String() string {
2023-05-22 17:16:48 +00:00
return "{{ .Name }}"
2023-05-17 05:01:40 +00:00
}
{{ end }}
{{- /* begin actual file template */ -}}
// generated by gen_skylab.go at {{ Time }} DO NOT EDIT!
package skylab
import (
"errors"
2023-05-17 05:01:40 +00:00
"encoding/binary"
"github.com/kschamplin/gotelem/internal/can"
2023-05-17 05:01:40 +00:00
"encoding/json"
)
type SkylabId uint32
const (
{{- range .Packets }}
{{camelCase .Name true}}Id SkylabId = {{.Id | printf "0x%X"}}
{{- end}}
)
2024-02-28 20:10:40 +00:00
// list of every packet ID. Can be used for O(1) checks.
var idMap = map[can.CanID]bool{
2023-05-17 05:01:40 +00:00
{{ range $p := .Packets -}}
{{ if $p.Repeat }}
{{ range $idx := Nx (int $p.Id) $p.Repeat $p.Offset -}}
2024-03-05 02:41:15 +00:00
{ Id: {{ $idx | printf "0x%X"}}, Extended: {{$p.IsExtended}} }: true,
2023-05-17 05:01:40 +00:00
{{ end }}
{{- else }}
2024-03-05 02:41:15 +00:00
{ Id: {{ $p.Id | printf "0x%X" }}, Extended: {{$p.IsExtended}} }: true,
2023-05-17 05:01:40 +00:00
{{- end}}
{{- end}}
}
2023-05-22 13:41:37 +00:00
// FromCanFrame creates a Packet from a given CAN ID and data payload.
// If the CAN ID is unknown, it will return an error.
func FromCanFrame(f can.Frame) (Packet, error) {
id := f.Id
2023-05-17 05:01:40 +00:00
if !idMap[id] {
return nil, &UnknownIdError{ id.Id }
2023-05-17 05:01:40 +00:00
}
switch id {
{{- range $p := .Packets }}
{{- if $p.Repeat }}
case {{ $p | idToString -}}:
2023-05-20 19:54:10 +00:00
var res = &{{camelCase $p.Name true}}{}
res.UnmarshalPacket(f.Data)
res.Idx = id.Id - {{$p.Id | printf "0x%X" }}
2023-05-17 05:01:40 +00:00
return res, nil
{{- else }}
case {{ $p | idToString }}:
2023-05-20 19:54:10 +00:00
var res = &{{camelCase $p.Name true}}{}
res.UnmarshalPacket(f.Data)
2023-05-17 05:01:40 +00:00
return res, nil
{{- end}}
{{- end}}
}
panic("This should never happen. CAN ID didn't match but was in ID map")
2023-05-17 05:01:40 +00:00
}
2023-05-29 00:39:03 +00:00
func FromJson (name string, raw []byte) (Packet, error) {
switch name {
2023-05-17 05:01:40 +00:00
{{- range $p := .Packets }}
case "{{ $p.Name }}":
2023-05-19 21:57:05 +00:00
var res = &{{camelCase $p.Name true}}{}
2023-05-29 00:39:03 +00:00
err := json.Unmarshal(raw, res)
2023-05-17 05:01:40 +00:00
return res, err
{{- end }}
}
return nil, errors.New("unknown packet name")
2023-05-17 05:01:40 +00:00
}
{{range .Packets -}}
{{template "packet" .}}
{{- end}}
// The json representation that was used to generate this data.
// can be used to share the parsing data for i.e dynamic python gui.
2023-06-28 01:39:57 +00:00
const SkylabDefinitions = `{{json . | printf "%s" }}`