diff options
Diffstat (limited to 'main.go')
| -rw-r--r-- | main.go | 252 |
1 files changed, 116 insertions, 136 deletions
@@ -1,158 +1,138 @@ package main import ( - "fmt" - g "github.com/AllenDang/giu" "image" - "image/draw" - _ "image/jpeg" - "os" -) - -const ( - gasConstant = 8.314472 - airMolarMass = 0.0289647 // kg/mol -) - -var ( - defaultDisplacement = 2 * Litre - defaultSpeed int32 = 2000 - defaultVE int32 = 80 - defaultTemperature = Temperature{25, Celcius} -) + "image/color" -var ( - defaultManufacturer = "borgwarner" - defaultSeries = "efr" - defaultModel = "6258" + "github.com/faiface/mainthread" + "volute/gui" + "volute/gui/layout" + "volute/gui/widget" + "volute/gui/win" ) -// Number of data points on the compressor map. -var numPoints = 1 - -var ( - displacement = defaultDisplacement - volumeUnitIndex int32 - - // Angular crankshaft speed in RPM. - speed = []int32{defaultSpeed} - - volumetricEfficiency = []int32{defaultVE} - - intakeAirTemperature = []Temperature{defaultTemperature} - temperatureUnitIndex int32 - - manifoldPressure = []Pressure{AtmosphericPressure()} - pressureUnitIndex int32 -) - -var pressureRatio []float32 - -func pressureRatioAt(point int) float32 { - u := Pascal - m := manifoldPressure[point] / u - a := AtmosphericPressure() / u - return float32(m / a) -} -func init() { - pressureRatio = append(pressureRatio, pressureRatioAt(0)) -} +const ( + WIDTH = 800 + HEIGHT = 600 -var ( - massFlowRateAir []MassFlowRate - massFlowRateUnitIndex int32 + POINTS = 6 ) -func massFlowRateAt(point int) MassFlowRate { - rpm := float32(speed[point]) - disp := float32(displacement / CubicMetre) - ve := float32(volumetricEfficiency[point]) / 100.0 - cubicMetresPerMin := (rpm / 2.0) * disp * ve - - iat, err := intakeAirTemperature[point].AsUnit(Kelvin) - Check(err) - pres := manifoldPressure[point] / Pascal - molsPerMin := (float32(pres) * cubicMetresPerMin) / (gasConstant * iat) - - kgPerMin := molsPerMin * airMolarMass - - mfr := MassFlowRate(kgPerMin/60.0) * KilogramsPerSecond - return mfr -} -func init() { - massFlowRateAir = append(massFlowRateAir, massFlowRateAt(0)) -} +func run() { + w, err := win.New(win.Title("volute"), win.Size(WIDTH, HEIGHT)) + if err != nil { + panic(err) + } + mux, env := gui.NewMux(w) -var ( - compressorImage *image.RGBA - compressorTexture *g.Texture - selectedCompressor Compressor -) + var ( + displacementChan = make(chan uint) + rpmChan = make([]chan uint, POINTS) + veChan = make([]chan uint, POINTS) + focus = NewFocus([]int{1, POINTS, POINTS}) + ) + for i := 0; i < POINTS; i++ { + rpmChan[i] = make(chan uint) + veChan[i] = make(chan uint) + } -func init() { - manufacturer := defaultManufacturer - series := defaultSeries - model := defaultModel - c, ok := Compressors[manufacturer][series][model] - if !ok { - fmt.Printf("compressor.Compressors()[\"%s\"][\"%s\"][\"%s\"] does not exist.\n", - manufacturer, series, model, + bounds := layout.Grid{ + Rows: []int{2, 7, 7}, + Background: color.Gray{255}, + Gap: 1, + Split: split, + SplitRows: splitRows, + Margin: 0, + Border: 0, + BorderColor: color.Gray{16}, + Flip: false, + }.Lay(image.Rect(0, 0, WIDTH, HEIGHT)) + + go widget.Label("displacement (cc)", bounds[0], mux.MakeEnv()) + go widget.Input( + displacementChan, + bounds[1], + focus.widgets[0][0], + mux.MakeEnv(), + ) + go widget.Label("speed (rpm)", bounds[2], mux.MakeEnv()) + go widget.Label("VE (%)", bounds[3+POINTS], mux.MakeEnv()) + for i := 0; i < POINTS; i++ { + go widget.Input( + rpmChan[i], + bounds[3+i], + focus.widgets[1][i], + mux.MakeEnv(), + ) + go widget.Input( + veChan[i], + bounds[3+POINTS+1+i], + focus.widgets[2][i], + mux.MakeEnv(), ) - os.Exit(1) } - setCompressor(c) + focus.widgets[focus.p.Y][focus.p.X] <- true + +Loop: + for { + select { + case _ = <-displacementChan: + case _ = <-rpmChan[0]: + case _ = <-veChan[0]: + case event, ok := <-env.Events(): + if !ok { // channel closed + break Loop + } + switch event := event.(type) { + case win.WiClose: + break Loop + case win.KbType: + switch event.Rune { + case 'q': + break Loop + case 'h': + focus.Left() + case 'j': + focus.Down() + case 'k': + focus.Up() + case 'l': + focus.Right() + } + } + } + } + close(env.Draw()) + close(displacementChan) + for i := range rpmChan { + close(rpmChan[i]) + } } -func main() { - wnd := g.NewMasterWindow("volute", 400, 200, 0) - - go updateCompImg() - m := <-updatedCompImg - g.EnqueueNewTextureFromRgba(m, func(tex *g.Texture) { - compressorTexture = tex - }) - - wnd.Run(loop) +func split(elements int, space int) []int { + bounds := make([]int, elements) + widths := []int{ + widget.TextSize("displacement (cc)").X, + widget.TextSize("123456").X, + } + for i := 0; i < elements && space > 0; i++ { + bounds[i] = min(widths[min(i, len(widths)-1)], space) + space -= bounds[i] + } + return bounds } -func setCompressor(c Compressor) { - f, err := os.Open(c.FileName) - Check(err) - defer f.Close() - - j, _, err := image.Decode(f) - Check(err) - - b := j.Bounds() - m := image.NewRGBA(image.Rect(0, 0, b.Dx(), b.Dy())) - draw.Draw(m, m.Bounds(), j, b.Min, draw.Src) - - selectedCompressor = c - compressorImage = m - - go updateCompImg() +func splitRows(elements int, space int) []int { + bounds := make([]int, elements) + height := widget.TextSize("1").Y + for i := 0; i < elements && space > 0; i++ { + bounds[i] = min(height, space) + space -= bounds[i] + } + return bounds } -func loop() { - g.SingleWindow().Layout( - displacementRow(), - g.Table(). - Size(g.Auto, 190). - Rows( - speedRow(), - volumetricEfficiencyRow(), - intakeAirTemperatureRow(), - manifoldPressureRow(), - pressureRatioRow(), - massFlowRateRow(), - duplicateDeleteRow(), - ). - Columns( - columns()..., - ). - Flags(g.TableFlagsSizingFixedFit), - selectCompressor(), - g.Custom(compressorWidget), - ) +func main() { + mainthread.Run(run) } |