コード例 #1
0
ファイル: Brick.cs プロジェクト: PeterOrneholm/LegoEv3Core
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="comm">Object implementing the <see cref="ICommunication"/> interface for talking to the brick</param>
        /// <param name="alwaysSendEvents">Send events when data changes, or at every poll</param>
        public Brick(ICommunication comm, bool alwaysSendEvents)
        {
            DirectCommand = new DirectCommand(this);
            SystemCommand = new SystemCommand(this);
            BatchCommand  = new Command(this);

            Buttons = new BrickButtons();

            _alwaysSendEvents = alwaysSendEvents;

            var index = 0;

            _comm = comm;
            _comm.ReportReceived += ReportReceived;

            Ports = new Dictionary <InputPort, Port>();

            foreach (InputPort i in Enum.GetValues(typeof(InputPort)))
            {
                Ports[i] = new Port
                {
                    InputPort = i,
                    Index     = index++,
                    Name      = i.ToString(),
                };
            }
        }
コード例 #2
0
ファイル: Brick.cs プロジェクト: PeterOrneholm/LegoEv3Core
        /// <summary>
        /// Connect to the EV3 brick with a specified polling time.
        /// </summary>
        /// <param name="pollingTime">The period to poll the device status.  Set to TimeSpan.Zero to disable polling.</param>
        /// <returns></returns>
        public async Task ConnectAsync(TimeSpan pollingTime)
        {
            _tokenSource = new CancellationTokenSource();

            await _comm.ConnectAsync();

            await DirectCommand.StopMotorAsync(OutputPort.All, false);

            if (pollingTime != TimeSpan.Zero)
            {
                var t = Task.Factory.StartNew(async() =>
                {
                    while (!_tokenSource.IsCancellationRequested)
                    {
                        await PollSensorsAsync();
                        await Task.Delay(pollingTime, _tokenSource.Token);
                    }

                    await DirectCommand.StopMotorAsync(OutputPort.All, false);
                }, _tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Current);
            }
        }