Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add method ..Degrees() and ..Uint32() in mesgdefs for convenient #194

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions internal/cmd/fitgen/profile/mesgdef/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func (b *mesgdefBuilder) Build() ([]builder.Data, error) {
InvalidValue: b.invalidValueOf(field.Type, field.Array),
Comment: field.Comment,
Array: field.Array != "",
Units: field.Units,
}

if _, ok := canExpandMap[field.Name]; ok {
Expand Down Expand Up @@ -201,6 +202,9 @@ func (b *mesgdefBuilder) Build() ([]builder.Data, error) {
strings.Contains(f.ComparableValue, "unsafe") {
imports["unsafe"] = struct{}{}
}
if f.Units == "semicircles" {
imports["github.com/muktihari/fit/kit/semicircles"] = struct{}{}
}
}

// Optimize memory usage by aligning the memory in the struct.
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/fitgen/profile/mesgdef/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Field struct {
ComparableValue string
InvalidValue string
Comment string
Units string
Scale float64
Offset float64
Array bool
Expand Down
21 changes: 18 additions & 3 deletions internal/cmd/fitgen/profile/mesgdef/mesgdef.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ func (m *{{ .Name }}) ToMesg(options *Options) proto.Message {

fac := options.Factory

fieldsArray := fieldsPool.Get().(*[256]proto.Field)
defer fieldsPool.Put(fieldsArray)
arr := pool.Get().(*[256]proto.Field)
defer pool.Put(arr)

fields := (*fieldsArray)[:0] // Create slice from array with zero len.
fields := arr[:0] // Create slice from array with zero len.
mesg := proto.Message{ Num: typedef.MesgNum{{ .Name }} }

{{ range .Fields -}}
Expand All @@ -114,6 +114,13 @@ func (m *{{ .Name }}) ToMesg(options *Options) proto.Message {
return mesg
}

{{ range .Fields -}}
{{ if eq .Type "time.Time" }}
// {{ .Name }}Uint32 returns {{ .Name }} in uint32 (seconds since FIT's epoch) instead of time.Time.
func (m *{{ $.Name }}) {{ .Name }}Uint32() uint32 { return datetime.ToUint32(m.{{ .Name }}) }
{{ end }}
{{ end }}

{{ range .Fields -}}
{{ if not (and (eq .Scale 1.0) (eq .Offset 0.0)) }}
// {{ .Name }}Scaled return {{ .Name }} in its scaled value [{{ .Comment }}].
Expand All @@ -130,6 +137,14 @@ func (m *{{ $.Name }}) {{ .Name }}Scaled() {{ if eq .Array true }} []float64 {{
{{ end }}
{{ end }}


{{ range .Fields -}}
{{ if eq .Units "semicircles" }}
// {{ .Name }}Degrees returns {{ .Name }} in degrees instead of semicircles.
func (m *{{ $.Name }}) {{ .Name }}Degrees() float64 { return semicircles.ToDegrees(m.{{ .Name }}) }
{{ end }}
{{ end }}

{{ range .Fields -}}
// Set{{ .Name }} sets {{ $.Name }} value.
//
Expand Down
9 changes: 6 additions & 3 deletions profile/mesgdef/aad_accel_features_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions profile/mesgdef/accelerometer_data_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions profile/mesgdef/activity_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions profile/mesgdef/ant_channel_id_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions profile/mesgdef/ant_rx_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions profile/mesgdef/ant_tx_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions profile/mesgdef/aviation_attitude_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions profile/mesgdef/barometer_data_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions profile/mesgdef/beat_intervals_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions profile/mesgdef/bike_profile_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions profile/mesgdef/blood_pressure_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions profile/mesgdef/cadence_zone_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions profile/mesgdef/camera_event_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions profile/mesgdef/capabilities_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions profile/mesgdef/chrono_shot_data_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading