Object chaining and methods in the API extension

So, I am looking to extend / adapt the API in my own needs. I am talking about the Lego Mindstorms C # API. I create my own API around it (based on the adapter template), so I can program the robot in the best way OO.

Here's a link on how the API works: Lego Mindstorms EV3 C # API

But now I am stuck very strange that the C # API processes commands for a brick.

Definitely not an OO-way ...

Example: To send a command to a brick, you need to have a brick instance to send the command. But the DirectCommand instance has nothing to do with the brick.

await brick.DirectCommand.TurnMotorAtPowerAsync(OutputPort.A, 50, 5000); 

So, I would like to make the brick and DirectCommand loosely coupled.

Here is another example: Execute a command package. You must write out all the commands and then execute a specific method. There is no way in the current API to loop through an array and add their stack element to execute them at a later point.

 brick.BatchCommand.TurnMotorAtSpeedForTime(OutputPort.A, 50, 1000, false); brick.BatchCommand.TurnMotorAtPowerForTime(OutputPort.C, 50, 1000, false); brick.BatchCommand.PlayTone(50, 1000, 500); await brick.BatchCommand.SendCommandAsync(); 

So, I would like to do the following:

Create a command like PlayTone (..) add it to the list of arrays lists and then skip it ...

 List<Command> toBeExecuted = new List<Command>; toBeExecuted.Add(DirectCommand.PlayTone(50, 1000, 500)); brick.DirectCommand(toBeExecuted[0]); 

So, if someone can help ... I would be very pleased :)

+5
source share
1 answer

Not quite what they are intended for, but can you queue them up as a list of tasks and pass them instead?

Same:

 static void Main(string[] args) { //---- queue commands into a "batch" List<Task> toBeExecuted = new List<Task>(); toBeExecuted.Add(Task.Run(() => dothing())); toBeExecuted.Add(Task.Run(() => dothing())); toBeExecuted.Add(Task.Run(() => dothing())); toBeExecuted.Add(Task.Run(() => dothing())); toBeExecuted.Add(Task.Run(() => dothing())); //---- elsewhere Task.WaitAll(toBeExecuted.ToArray()); //fire off the batch await brick.BatchCommand.SendCommandAsync(); //send to brick } 

Substituting dothing () for the batchcommand command that you want to queue:

.Add(Task.Run(() => brick.BatchCommand...()));

+1
source

Source: https://habr.com/ru/post/1232903/


All Articles