This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // setMeshCommands sets the commands that can be executed on the mesh | |
| func setMeshCommands(mesh *fmesh.FMesh, commands step_sim.MeshCommandMap) { | |
| timeComponent := mesh.ComponentByName("time") | |
| // Print current time | |
| commands["time:now"] = step_sim.NewMeshCommandDescriptor("Print current time", func(_ *fmesh.FMesh) { | |
| tickCount := timeComponent.State().Get("tick_count") | |
| simTime := timeComponent.State().Get("sim_time") | |
| simWallTime := timeComponent.State().Get("sim_wall_time") | |
| fmt.Println("Current tick count:", tickCount) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // I like my main func to be clean and short | |
| func main() { | |
| simMesh := getSimulationMesh() | |
| // Run the mesh in a step simulation | |
| step_sim.NewApp(simMesh, initSim).Run() | |
| } | |
| // Here is our step_sim wrapper, it provides REPL and some built-in commands: pause, resume, exit, help | |
| func NewApp(fm *fmesh.FMesh, simInitFunc SimInitFunc) *Application { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // getSimulationMesh returns the main mesh of the simulation | |
| func getSimulationMesh() *fmesh.FMesh { | |
| // Set up the world | |
| habitat := env.NewHabitat(component.NewCollection().Add( | |
| factor.GetTimeComponent(), | |
| factor.GetAirComponent(), | |
| factor.GetSunComponent(), | |
| )) | |
| // Add human beings |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func main() { | |
| fm := getMesh() | |
| // Initialize the mesh: set diagnostic frames to USB port, so the laptop will send them | |
| laptopInstance.SendDataToUSB( | |
| diagnostics.FrameGetEngineDTCs, | |
| diagnostics.FrameGetSpeed, | |
| diagnostics.FrameGetRPM, | |
| diagnostics.FrameGetCoolantTemperature, | |
| diagnostics.FrameGetCalibrationID, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Frame represents a simplified CAN frame | |
| type Frame struct { | |
| // CAN identifier (11-bit standard ID) | |
| // Determines priority during arbitration: lower ID = higher priority | |
| Id uint32 | |
| // Data Length Code (DLC) | |
| // Indicates how many bytes of data are in the payload (0–8 for classic CAN, up to 64 for CAN FD) | |
| DLC uint8 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Builder pattern (all chained calls are invoked on the same object): | |
| p := component.New("processor"). | |
| WithDescription("main processor"). | |
| AddLabel("env", "prod"). | |
| AddLabel("tier", "backend"). | |
| AddInputs("in1", "in2"). | |
| AddOutputs("out1", "out2"). | |
| WithActivationFunc(func(this *component.Component) error { | |
| return nil | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "github.com/trustmaster/goflow" | |
| ) | |
| // Greeter sends greetings | |
| type Greeter struct { | |
| Name <-chan string // input port |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func main() { | |
| // Build mesh | |
| numbersCruncher := getMesh() | |
| // Init with data | |
| numbersCruncher.ComponentByName("first-stage").InputByName("num").PutSignals( | |
| signal.New(1), | |
| signal.New(2), | |
| signal.New(3), | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func(this *component.Component) error { | |
| num := this.InputByName("num").Signals().FirstPayloadOrDefault(0) // Read inputs | |
| this.OutputByName("res").PutSignals(signal.New(num.(int) * 3)) // Provide outputs | |
| return nil // Report possible errors | |
| } |