diff options
| author | sam-anthony <samanthony6@protonmail.com> | 2022-03-26 14:51:43 -0230 |
|---|---|---|
| committer | sam-anthony <samanthony6@protonmail.com> | 2022-03-26 14:51:43 -0230 |
| commit | 713b65d414e327db7b530ce0be8403bc0ff4ff3b (patch) | |
| tree | 6e4a32fb43ca3e5b26d9cfb61c693ffd76391796 | |
| parent | 16235e23bda445f0bd0b664c3738c77e786a1cbf (diff) | |
| download | volute-713b65d414e327db7b530ce0be8403bc0ff4ff3b.zip | |
engine displacement
| -rw-r--r-- | main.go | 18 | ||||
| -rw-r--r-- | ui.go | 22 | ||||
| -rw-r--r-- | volume.go | 54 |
3 files changed, 85 insertions, 9 deletions
@@ -16,18 +16,17 @@ func check(err error) { } } +var ( + displacement = volume{2000, cubicCentimetre} + // selectedVolumeUnit is used to index volumeUnitStrings. + selectedVolumeUnit = defaultVolumeUnitIndex +) + var engineSpeed = [numPoints]int32{2000, 3000, 4000, 5000, 6000, 7000} var volumetricEfficiency = [numPoints]int32{100, 100, 100, 100, 100, 100} var ( - manifoldPressure [numPoints]pressure - - // selectedPressureUnit is used to index pressureUnits - selectedPressureUnit int32 -) - -func init() { manifoldPressure = [numPoints]pressure{ newPressure(), newPressure(), @@ -37,12 +36,13 @@ func init() { newPressure(), } - // selectedPressureUnit is used to index pressureUnitStrings + // selectedPressureUnit is used to index pressureUnitStrings. selectedPressureUnit = defaultPressureUnitIndex -} +) func loop() { g.SingleWindow().Layout( + engineDisplacementRow(), g.Table(). Rows( engineSpeedRow(), @@ -2,6 +2,28 @@ package main import g "github.com/AllenDang/giu" +func engineDisplacementRow() *g.RowWidget { + return g.Row( + g.Label("Engine Displacement"), + g.InputFloat(&displacement.val).Format("%.2f"), + g.Combo( + "", + volumeUnitStrings()[selectedVolumeUnit], + volumeUnitStrings(), + &selectedVolumeUnit, + ). + OnChange(func() { + s := volumeUnitStrings()[selectedVolumeUnit] + u, err := volumeUnitFromString(s) + check(err) + displacement = volume{ + displacement.asUnit(u), + u, + } + }), + ) +} + func engineSpeedRow() *g.TableRowWidget { return g.TableRow( g.Label("Engine Speed"), diff --git a/volume.go b/volume.go new file mode 100644 index 0000000..cb7098a --- /dev/null +++ b/volume.go @@ -0,0 +1,54 @@ +package main + +import ( + "errors" + "fmt" +) + +type volumeUnit float32 + +const ( + cubicCentimetre volumeUnit = 1 + litre volumeUnit = 1_000 + cubicMetre volumeUnit = 1_000_000 + cubicInch volumeUnit = 16.38706 +) + +// volumeUnitStrings returns a slice of strings, each representing a +// volumeUnit. +// This is necessary because giu.Combo only works with strings. +func volumeUnitStrings() []string { + return []string{"cc", "L", "m³", "in³"} +} + +const ( + defaultVolumeUnit volumeUnit = cubicCentimetre + // Used to index volumeUnitStrings + defaultVolumeUnitIndex int32 = 0 // cc +) + +func volumeUnitFromString(s string) (volumeUnit, error) { + // Each case corresponds to a value in volumeUnitStrings + switch s { + case "cc": + return cubicCentimetre, nil + case "L": + return litre, nil + case "m³": + return cubicMetre, nil + case "in³": + return cubicInch, nil + default: + return *new(volumeUnit), errors.New(fmt.Sprintf("invalid volumeUnit: '%s'", s)) + } +} + +type volume struct { + val float32 + unit volumeUnit +} + +func (v volume) asUnit(u volumeUnit) float32 { + cc := v.val * float32(v.unit) // Convert to cubic centimetres. + return cc / float32(u) // Convert to desired unit. +} |