Ejemplo n.º 1
0
        /// <summary>
        /// Completes the task gracefully when canceled.
        /// </summary>
        /// <param name="sender">Background task instance.</param>
        /// <param name="reason">Cancellation reason.</param>
        private void OnTaskCancelled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
        {
            // Release hardware resources
            Debug.WriteLine("Disconnecting from Navio board.");
            _board?.Dispose();

            // End execution
            Debug.WriteLine("Application finished.");
            _taskDeferral.Complete();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns the current <see cref="INavioBoard"/> or creates it the first time.
        /// </summary>
        /// <param name="model">Hardware model.</param>
        /// <returns>Hardware interface for the requested model when successful.</returns>
        /// <remarks>
        /// The requested hardware model must be the same, otherwise any existing board
        /// is disposed and an attempt made to create a board of the new model.
        /// </remarks>
        public static INavioBoard Connect(NavioHardwareModel model)
        {
            // Thread-safe lock
            lock (_lock)
            {
                // Check existing board when present
                if (_board != null)
                {
                    // Return existing board when present
                    if (_board.Model == model)
                    {
                        return(_board);
                    }

                    // Dispose existing board when not null and different model (just in case)
                    _board.Dispose();
                    _board = null;
                }

                // Create new board
                switch (model)
                {
                case NavioHardwareModel.Navio1:
                    return(_board = new Navio1Board());

                case NavioHardwareModel.Navio1Plus:
                    return(_board = new Navio1PlusBoard());

                case NavioHardwareModel.Navio2:
                    return(_board = new Navio2Board());

                default:
                    // Invalid value or unsupported
                    throw new ArgumentOutOfRangeException(nameof(model));
                }
            }
        }