Beispiel #1
0
        protected void OperateDoor(bool open)
        {
            // if we're already in process
            // TODO: maybe we make this cancellable
            if (_operatingDoor)
            {
                return;
            }
            _operatingDoor = true;

            // spin up a new thread so we don't block anything.
            // this is especially important with the end stops.
            // without a new thread, we don't get those events. :D
            Thread th = new Thread(() =>
            {
                Debug.Print("OperateDoor: " + (open ? "open" : "close"));

                // open the door
                if (open)
                {
                    // rotate the winch servo until the end stop is triggered
                    while (!_openEndStopTriggered)
                    {
                        // rotate the winch servo in the open direction
                        _doorServo.Rotate(_openDirection, 1.0f);
                    }
                    // stop the winch
                    _doorServo.Stop();
                    Debug.Print("Open end stop hit.");

                    // update our door state
                    _doorState = DoorState.Open;
                }
                else
                { // close the door
                  // rotate the winch servo until the end stop is triggered
                    while (!_closeEndStopTriggered)
                    {
                        // close
                        _doorServo.Rotate(_closeDirection, 1.0f);
                    }
                    // stop the winch
                    _doorServo.Stop();
                    Debug.Print("Open end stop hit.");

                    // update our door state
                    _doorState = DoorState.Closed;
                }

                //
                _operatingDoor = false;
            });

            th.Start();
        }
        static void ToggleServo()
        {
            if (!_running)
            {
                Thread th = new Thread(() => {
                    _running = true;
                    while (_running)
                    {
                        Debug.Print("Rotating clockwise at increasing speeds.");
                        for (float speed = 1; speed <= 10; speed++)
                        {
                            if (!_running)
                            {
                                break;
                            }
                            _servo.Rotate(RotationDirection.Clockwise, (speed / 10.0f));
                            Thread.Sleep(500);
                        }

                        if (!_running)
                        {
                            break;
                        }
                        Debug.Print("Stopping for half a sec.");
                        _servo.Stop();
                        Thread.Sleep(500);

                        Debug.Print("Rotating counter-clockwise at increasing speeds.");
                        for (float speed = 1; speed <= 10; speed++)
                        {
                            if (!_running)
                            {
                                break;
                            }
                            _servo.Rotate(RotationDirection.CounterClockwise, (speed / 10.0f));
                            Thread.Sleep(500);
                        }
                    }
                });
                th.Start();
            }
            else
            {
                Debug.Print("Stopping.");
                _running = false;
                Thread.Sleep(550); // wait for the loop to break
                _servo.Stop();
            }
        }