virtual protected void ExecuteCommand(ArduinoCommand command, ExecutionArguments xargs) { switch (command.CommandAlias.ToLower()) { case "enable": bool enable = xargs != null && xargs.Arguments.Count > 0 ? Utilities.Convert.ToBoolean(xargs.Arguments[0]) : true; Enable(enable); break; case "disable": Enable(false); break; default: if (command.Commands.Count > 0) { for (int i = 0; i < command.Repeat; i++) { foreach (var ccommand in command.Commands) { ExecuteCommand(ccommand, xargs != null && xargs.Deep ? xargs : null); if (command.Delay > 0) { System.Threading.Thread.Sleep(command.Delay); } } } //end command repeat } else { SendCommand(command, xargs); } break; } }
virtual public byte ThreadExecuteCommand(String command, int repeat, int delay, List <Object> args) { //check has command ArduinoCommand acmd = GetCommand(command); if (acmd == null) { throw new Exception(String.Format("Device {0} does not have command {1}", ID, command)); } //pass an empty array rather than null ... safety measure here just for the ThreadExecution Manager if (args == null) { args = new List <Object>(); } byte tag = acmd.ExpectsResponse ? Mgr.MessageTags.CreateTag() : (byte)0; ExecutionArguments xargs = new ExecutionArguments(args, tag); //Use ThreadExecutionManager to allow for multi-threading by device int prevSize = ThreadExecutionManager.MaxQueueSize; ThreadExecutionManager.MaxQueueSize = acmd.IsCompound ? 1 : 256; ThreadExecutionState xs = ThreadExecutionManager.Execute <ExecutionArguments>(ID, repeat, delay, ExecuteCommand, command, xargs); ThreadExecutionManager.MaxQueueSize = prevSize; if (xs == null) { Mgr.MessageTags.Release(tag); tag = 0; } return(tag); }
public ArduinoCommand TryAddCommand(String commandAlias, String[] commandAliases, int delay = 1, int repeat = 1, ArduinoCommand.CommandType commandType = ArduinoCommand.CommandType.NOT_SET, bool expectsResponse = false) { try { ArduinoCommand cmd = AddCommand(commandAlias, commandAliases, delay, repeat, commandType, expectsResponse); return(cmd); } catch (Exception) { return(null); } }
//messaging virtual protected void SendCommand(ArduinoCommand command, ExecutionArguments xargs = null) { if (Mgr == null) { throw new Exception(String.Format("Device {0} has not yet been added to a device manager", ToString())); } if (!IsConnected) { throw new Exception(String.Format("Device {0} is not 'connected' to board", ToString())); } if (xargs == null) { Mgr.SendCommand(BoardID, command); } else { Mgr.SendCommand(BoardID, command, xargs.Arguments, xargs.Tag); } LastCommandSent = command; LastCommandSentOn = DateTime.Now.Ticks; }
public ArduinoCommand AddCommand(String commandAlias, String[] commandAliases, int delay = 1, int repeat = 1, ArduinoCommand.CommandType commandType = ArduinoCommand.CommandType.NOT_SET, bool expectsResponse = false) { var command = new ArduinoCommand(commandAlias, commandType); command.Delay = delay; command.Repeat = repeat; command.ExpectsResponse = expectsResponse; if (commandAliases != null) { for (int i = 0; i < commandAliases.Length; i++) { var c = GetCommand(commandAliases[i]); if (c == null) { throw new Exception("No command found with alias " + commandAliases[i]); } command.Commands.Add(c); } } return(AddCommand(command)); }
public ArduinoCommand AddCommand(ArduinoCommand command) { var key = command.CommandAlias.ToLower(); if (_commands.ContainsKey(key)) { throw new Exception("Already contains a command with alias " + command.CommandAlias); } //we auto generate a byte ID which uses the lower 4 bits for the type and the upper 5 bits for a count byte count = 0; foreach (ArduinoCommand cmd in _commands.Values) { if (command.Type == cmd.Type) { count++; } } command.AssignID(count); _commands[key] = command; return(command); }
public byte SendCommand(byte targetID, ArduinoCommand command, List <Object> extraArgs = null, byte tag = 0) { var message = new ADMMessage(); message.LittleEndian = LittleEndian; message.Type = Messaging.MessageType.COMMAND; message.TargetID = targetID; message.Tag = tag == 0 ? MessageTags.CreateTag() : tag; message.CommandID = command.ID; List <Object> allArgs = command.Arguments; if (extraArgs != null && extraArgs.Count > 0) { allArgs.AddRange(extraArgs); } foreach (Object arg in allArgs) { byte[] b; if (arg is String) { b = Chetch.Utilities.Convert.ToBytes((String)arg); } else if (arg.GetType().IsValueType) { b = Chetch.Utilities.Convert.ToBytes((ValueType)arg, LittleEndian); } else { throw new Exception("Unable to process type " + arg.GetType()); } message.AddArgument(b); } return(SendMessage(message)); }