public void Stop() { running = false; var stopwatch = Stopwatch.StartNew(); while (thread.IsAlive && stopwatch.ElapsedMilliseconds < 5000) { Thread.Sleep(50); } connector.Close(); }
//evento de click para desligar o sphero private void m_BtnSleep_Click(object sender, EventArgs e) { //enviar o pacote com o comando para desligar ao sphero m_SpheroConnector.Sphero.SendPacket(new SpheroCommandPacket(CoreCommand.CMD_SLEEP, CommandSequencer.Next, 0x00, 0x00, 0x00, 0x00, 0x00)); m_SpheroConnector.Close(); m_BtnSleep.Enabled = false; m_LblSpheroStatus.Text = "Sphero: Procurando..."; printToLog("Procurando Sphero..."); //recomeçar a procura... m_SearchSpheroWorker.RunWorkerAsync(); }
static void Main(string[] args) { SpheroConnector spheroConnector = new SpheroConnector(); Sphero sphero = null; string[] parameters = new string[] { "none" }; string command = "none"; while (!string.IsNullOrEmpty(command)) { command = parameters[0]; switch (command) { case "find": spheroConnector.Scan(); var deviceNames = spheroConnector.DeviceNames; for (int i = 0; i < deviceNames.Count; i++) { Console.WriteLine("{0}: {1}", i, deviceNames[i]); } break; case "connect": if (parameters.Length < 2) { break; } int index = -1; if (int.TryParse(parameters[1], out index)) { try { sphero = spheroConnector.Connect(index); } catch (Exception ex) { Console.WriteLine(ex.Message); } } else { Console.WriteLine("'{0}' is not a valid device index.", parameters[1]); } break; case "close": spheroConnector.Close(); break; case "sleep": sphero.Sleep(); break; case "setcolor": if (parameters.Length < 2) { break; } ChangeColor(sphero, parameters[1]); break; case "getresponses": { if (parameters.Length < 2) { break; } int count; if (int.TryParse(parameters[1], out count)) { lock (sphero.Listener.SyncRoot) { IEnumerable <SpheroResponsePacket> packets = sphero.Listener.GetLastResponsePackets(count); foreach (var packet in packets) { Console.WriteLine(packet); } } } } break; case "getasync": { if (parameters.Length < 2) { break; } int count; if (int.TryParse(parameters[1], out count)) { lock (sphero.Listener.SyncRoot) { IEnumerable <SpheroAsyncPacket> packets = sphero.Listener.GetLastAsyncPackets(count); foreach (var packet in packets) { Console.WriteLine(packet); } } } } break; case "exit": spheroConnector.Close(); Console.WriteLine("See you soon ... ;) "); Thread.Sleep(1500); return; case "sendprogram": { var area = StorageArea.Temporary; IEnumerable <string> programLines = GetOrbBasicLinesFromFile("orbbasic.txt"); foreach (var programLine in programLines) { Console.WriteLine(programLine); } sphero.EraseOrbBasicStorage(area); sphero.SendOrbBasicProgram(area, programLines); } break; case "runprogram": { StorageArea area = StorageArea.Temporary; sphero.ExecuteOrbBasicProgram(area, 10); } break; case "abortprogram": sphero.AbortOrbBasicProgram(); break; case "diagnostics": { sphero.PerformLevelOneDiagnostics(); } break; case "none": Console.WriteLine("Welcome to Sphero CMD controll application"); break; case "help": Console.WriteLine("HELP will be realized soon."); break; case "goForward": if (parameters.Length < 3) { break; } ChangeColor(sphero, "Red"); GoForward(sphero, Byte.Parse(parameters[1]), int.Parse(parameters[2])); break; default: Console.WriteLine("Unknown command. Please type 'help' for getting list of available commands."); break; } Console.Write("> "); parameters = Console.ReadLine().Split(new char[] { ' ' }); } }