static void TestTimeoutWaitAsync() { Console.WriteLine("Testing async wait with a timeout of 10 seconds"); var button = Pi.Gpio.Pin(buttonPin, PinKind.InputPullUp); // Request a 10 second task to wait for the button pin rising edge var task = button.WaitForEdgeAsync(PinEdge.Rising, 10000); var i = 1; var timer = PreciseTimer.Every(1000, () => { if (task.IsCompleted) { return(false); } Console.WriteLine(i++); return(i < 10 && !task.IsCompleted); }); // Check if the task detected a rising edge before timeout was reached if (task.Result) { Console.WriteLine("Rising edge found"); } else { Console.WriteLine("Rising edge timed out after 10 seconds"); } timer.Wait(); }
public MainWindow() { InitializeComponent(); string name = Microsoft.VisualBasic.Interaction.InputBox("Select name", "Select name", Environment.UserName); gameConnection.Connect(name, SERVER_IP, SERVER_PORT); gameConnection.StateReceived += MovePlayers; gameConnection.PlayerDataReceived += UpdatePlayers; keyManager = new KeyManager(this); preciseTimer = new PreciseTimer(25); preciseTimer.Tick += GameLoop; Closing += OnClose; Random rnd = new Random(); Color startColor = Color.FromRgb( (byte)rnd.Next(0, 255), (byte)rnd.Next(0, 255), (byte)rnd.Next(0, 255)); colorPicker = new ColorPicker(paintCanvas, startColor); colorPicker.OnUpdate += ColorPickerOnOnUpdate; gameConnection.UpdatePlayerColor(startColor); }
static void TestTimeoutWait() { Console.WriteLine("Testing wait with a timeout of 10 seconds"); var button = Pi.Gpio.Pin(buttonPin, PinKind.InputPullUp); var done = false; var i = 1; var timer = PreciseTimer.Every(1000, () => { if (done) { return(false); } Console.WriteLine(i++); return(i < 10 && !done); }); // Wait 10 seconds for the button pin rising edge if (button.WaitForEdge(PinEdge.Rising, 10000)) { Console.WriteLine("Rising edge found"); } else { Console.WriteLine("Rising edge timed out after 10 seconds"); } done = true; timer.Wait(); }
public void NetLoop() { var enetEvent = new Event(); _lastMapDurationWatch = new Stopwatch(); _lastMapDurationWatch.Start(); using (PreciseTimer.SetResolution(1)) { while (!Program.IsSetToExit) { while (_server.Service(0, out enetEvent) > 0) { switch (enetEvent.Type) { case EventType.Connect: // Set some defaults enetEvent.Peer.Mtu = PEER_MTU; enetEvent.Data = 0; break; case EventType.Receive: PacketHandlerManager.handlePacket(enetEvent.Peer, enetEvent.Packet, (Channel)enetEvent.ChannelID); // Clean up the packet now that we're done using it. enetEvent.Packet.Dispose(); break; case EventType.Disconnect: HandleDisconnect(enetEvent.Peer); break; } } if (IsPaused) { _lastMapDurationWatch.Stop(); _pauseTimer.Enabled = true; if (PauseTimeLeft <= 0 && !_autoResumeCheck) { PacketHandlerManager.GetHandler(PacketCmd.PKT_UnpauseGame, Channel.CHL_C2S) .HandlePacket(null, new byte[0]); _autoResumeCheck = true; } continue; } if (_lastMapDurationWatch.Elapsed.TotalMilliseconds + 1.0 > REFRESH_RATE) { var sinceLastMapTime = _lastMapDurationWatch.Elapsed.TotalMilliseconds; _lastMapDurationWatch.Restart(); if (IsRunning) { Map.Update((float)sinceLastMapTime); _gameScriptTimers.ForEach(gsTimer => gsTimer.Update((float)sinceLastMapTime)); _gameScriptTimers.RemoveAll(gsTimer => gsTimer.IsDead()); } } Thread.Sleep(1); } } }
public void GetElapsedTimeTest() { PreciseTimer preciseTimer = new PreciseTimer(); //not sure how to test if this gives a correct result //since it is all based on CPU time between the frame preciseTimer.GetElapsedTime(); }
public void GetElapsedTimeTest() { PreciseTimer target = new PreciseTimer(); // TODO: Initialize to an appropriate value double expected = 0F; // TODO: Initialize to an appropriate value double actual; actual = target.GetElapsedTime(); Assert.AreEqual(expected, actual); Assert.Inconclusive("Verify the correctness of this test method."); }
public void Should_fire_once() { int fireCount = 0; var timer = new PreciseTimer(state => fireCount++, null); timer.FireAt(DateTime.Now.AddMilliseconds(100)); Thread.Sleep(1000); timer.Dispose(); Assert.That(fireCount, Is.EqualTo(1)); }
public void NetLoop() { var enetEvent = new Event(); var lastMapDurationWatch = new Stopwatch(); lastMapDurationWatch.Start(); using (PreciseTimer.SetResolution(1)) { while (!Program.IsSetToExit) { while (_server.Service(0, out enetEvent) > 0) { switch (enetEvent.Type) { case EventType.Connect: // Set some defaults enetEvent.Peer.Mtu = PEER_MTU; enetEvent.Data = 0; break; case EventType.Receive: if (!PacketHandlerManager.handlePacket(enetEvent.Peer, enetEvent.Packet, (Channel)enetEvent.ChannelID)) { //enet_peer_disconnect(event.peer, 0); } // Clean up the packet now that we're done using it. enetEvent.Packet.Dispose(); break; case EventType.Disconnect: HandleDisconnect(enetEvent.Peer); break; } } if (lastMapDurationWatch.Elapsed.TotalMilliseconds + 1.0 > REFRESH_RATE) { double sinceLastMapTime = lastMapDurationWatch.Elapsed.TotalMilliseconds; lastMapDurationWatch.Restart(); if (IsRunning) { Map.Update((float)sinceLastMapTime); } } Thread.Sleep(1); } } }
public void Test_StartStop() { int ms = 200; int iterations = 5; using (PreciseTimer timer = new PreciseTimer(new TimerCallback(IncrementCount), TimeSpan.FromMilliseconds(ms), PreciseTimerCallbackMode.Async)) { timer.Start(); Thread.Sleep(ms * iterations); timer.Stop(); Trace.WriteLine("Timer stopped"); Assert.AreEqual(iterations, _Count); } }
static void TestPreciseWait() { Console.WriteLine("PreciseTimer wait resolution = {0:0.0000}", PreciseTimer.WaitResolution); PreciseTimer.Wait(100); double t; PreciseTimer.Wait(1); t = PreciseTimer.Now; PreciseTimer.Wait(100); t = PreciseTimer.Now - t; Console.WriteLine("wait 100ms = {0:0.0000}", t); PreciseTimer.Wait(1); t = PreciseTimer.Now; PreciseTimer.Wait(10); t = PreciseTimer.Now - t; Console.WriteLine("wait 10ms = {0:0.0000}", t); PreciseTimer.Wait(1); t = PreciseTimer.Now; PreciseTimer.Wait(1); t = PreciseTimer.Now - t; Console.WriteLine("wait 1ms = {0:0.0000}", t); PreciseTimer.Wait(1); t = PreciseTimer.Now; PreciseTimer.Wait(0.5); t = PreciseTimer.Now - t; Console.WriteLine("wait 500μs = {0:0.0000}", t); PreciseTimer.Wait(1); t = PreciseTimer.Now; PreciseTimer.Wait(0.1); t = PreciseTimer.Now - t; Console.WriteLine("wait 100μs = {0:0.0000}", t); PreciseTimer.Wait(1); t = PreciseTimer.Now; PreciseTimer.Wait(0.01); t = PreciseTimer.Now - t; Console.WriteLine("wait 10μs = {0:0.0000}", t); PreciseTimer.Wait(1); t = PreciseTimer.Now; PreciseTimer.Wait(0.001); t = PreciseTimer.Now - t; Console.WriteLine("wait 1μs = {0:0.0000}", t); PreciseTimer.Wait(1); t = PreciseTimer.Now; PreciseTimer.Wait(0.0001); t = PreciseTimer.Now - t; Console.WriteLine("wait 100ns = {0:0.0000}", t); }
public static void TimerElapsedTest() { var timer = new PreciseTimer(); int i = 0; double a = 0; timer.OnElapsed += (sender, e) => { i++; if (i == 1000) { a = PreciseTimer.Now; } else if (i == 2000) { a = (PreciseTimer.Now - a) / 1000d; } }; i = 0; a = 0; timer.Interval = 1; timer.Enabled = true; PreciseTimer.Wait(5000); timer.Enabled = false; Console.WriteLine("expected interval = {0:0.000}, actual = {1:0.000}", timer.Interval, a); i = 0; a = 0; timer.Interval = 0.1; timer.Enabled = true; PreciseTimer.Wait(500); timer.Enabled = false; Console.WriteLine("expected interval = {0:0.000}, actual = {1:0.000}", timer.Interval, a); i = 0; a = 0; timer.Interval = 0.01; timer.Enabled = true; PreciseTimer.Wait(500); timer.Enabled = false; Console.WriteLine("expected interval = {0:0.000}, actual = {1:0.000}", timer.Interval, a); i = 0; a = 0; timer.Interval = 0.001; timer.Enabled = true; PreciseTimer.Wait(500); timer.Enabled = false; Console.WriteLine("expected interval = {0:0.000}, actual = {1:0.000}", timer.Interval, a); }
public void Should_fire_multiple_times() { int fireCount = 0; var timer = new PreciseTimer(state => { if (fireCount < 10) { fireCount++; } }, null); timer.FireAndRepeat(DateTime.Now, TimeSpan.FromMilliseconds(100)); Thread.Sleep(1000); timer.Dispose(); Assert.That(fireCount, Is.EqualTo(10)); }
public static void TimerEveryTest() { var a = new PreciseTimer(); long i = 0; var running = true; var every = PreciseTimer.Every(1, () => { i++; if (i % 1000 == 0) { Console.WriteLine("elapsed = {0:0.000}, i = {1}", a.ElapsedMilliseconds, i); } return(running); }); Console.ReadLine(); running = false; every.Wait(); }
//constructor public Command(double delay = 0.0d) { if (delay < 0.0d) { throw new InvalidOperationException("delay cannot be less than zero."); } else if (delay == 0.0d) { return; } else if (delay < 17.0d) { preciseTimer = new PreciseTimer(delay); preciseTimer.AutoReset = false; preciseTimer.Elapsed += onPreciseElapsed; } else { timer = new System.Timers.Timer(delay); timer.AutoReset = false; timer.Elapsed += onElapsed; } }
public void Test_Precision() { int ms = 1; int iterations = 100; using (PreciseTimer timer = new PreciseTimer(new TimerCallback(AddTicks), TimeSpan.FromMilliseconds(ms), PreciseTimerCallbackMode.Synchronized)) { timer.Start(); Thread.Sleep(ms * iterations); timer.Stop(); Trace.WriteLine("Timer stopped"); int length = _Ticks.Count; long difference; for (int i = 1; i < length; i++) { difference = _Ticks[i] - _Ticks[i - 1]; Trace.WriteLine(difference); Assert.GreaterOrEqual(difference, ms * 10000); Assert.LessOrEqual(difference, ms * 10000 + 120); // we should have at least a 120 nano second precision } } }
public void CreateFastLoop() { PreciseTimer preciseTimer = new PreciseTimer(); FastLoop fastloop = new FastLoop(GameLoopTest); }
public void CreatePreciseTimer() { PreciseTimer preciseTimer = new PreciseTimer(); }
public void PreciseTimerConstructorTest() { PreciseTimer target = new PreciseTimer(); Assert.Inconclusive("TODO: Implement code to verify target"); }
static void TestSweep() { Console.WriteLine("Testing a servo motor in a sweeping motion"); var angle = 0d; var step = 0.1d; var done = false; using (var left = Pi.Gpio.Pin(leftPin, PinKind.InputPullUp)) using (var right = Pi.Gpio.Pin(rightPin, PinKind.InputPullUp)) using (var servo = new ServoMotor(maxAngle)) { servo.PulseWidths(0.5, 2.5); servo.Start(); var timer = PreciseTimer.Every(1, () => { if (done) { return(false); } angle += step; var a = angle % (maxAngle * 2); if (a > maxAngle) { a = (maxAngle * 2) - a; } servo.Angle = a; return(true); }); Console.WriteLine("Click the button to stop this test"); // Connect rotate and click events left.OnRisingEdge += Rotate; Pi.Gpio.Pin(buttonPin, PinKind.InputPullUp).WaitForEdge(PinEdge.Rising); left.OnRisingEdge -= Rotate; done = true; timer.Wait(); servo.Angle = 0; Pi.Wait(2000); // Called on the falling edge of the left pin void Rotate(object sender, PinEventHandlerArgs args) { if (args.Bounced) { return; } var s = step; if (left.Value == right.Value) { s += 0.1; } else { s -= 0.1; } if (s < 0) { s = 0; } step = s; Console.WriteLine("servo step {0:0.0}°", step); } } }