Beispiel #1
0
        /// <summary>
        ///     Learns an IR code asynchronously.
        /// </summary>
        /// <param name="codeFormat">The format of the IR code to use in learning.</param>
        /// <param name="learnCodeFormat">The modifier used for the code format.</param>
        /// <param name="forcedFrequency">The frequency to use in learning.</param>
        /// <param name="userState">
        ///     An optional user state object that will be passed to the
        ///     Learning and LearnCompleted events and which can be used when calling LearnAsyncCancel().
        /// </param>
        public void LearnAsync(CodeFormat codeFormat, LearnCodeModifier learnCodeFormat,
                               uint forcedFrequency, object userState)
        {
            CheckDisposed();
            if (LearnCodeModifier.ForceFrequency == learnCodeFormat)
            {
                if (0 == forcedFrequency)
                {
                    throw new ArgumentException("forcedFrequency", "forcedFrequency must be specified when using LearnCodeModifier.ForceFrequency");
                }
            }
            else
            {
                if (0 != forcedFrequency)
                {
                    throw new ArgumentException("forcedFrequency", "forcedFrequency can only be specified when using LearnCodeModifier.ForceFrequency");
                }
            }

            object learnStatesKey = null == userState ? this : userState;
            var    learnState     = new LearnState(codeFormat, learnCodeFormat, forcedFrequency, userState);

            _learnStates[learnStatesKey] = learnState;

            if (false == ThreadPool.QueueUserWorkItem(DoLearn, learnState))
            {
                throw new ApplicationException("Unable to QueueUserWorkItem");
            }
        }
Beispiel #2
0
 public Learner(Driver driver,
                CodeFormat defaultCodeFormat = CodeFormat.Pronto,
                LearnCodeModifier defaultLearnCodeModifier = LearnCodeModifier.Default)
     : base(driver)
 {
     _defaultLearnCodeFormat   = defaultCodeFormat;
     _defaultLearnCodeModifier = defaultLearnCodeModifier;
 }
 public Task LearnAsync(CodeFormat? codeFormat = null,
     LearnCodeModifier? learnCodeModifier = null,
     uint? forcedFrequency = null,
     object userState = null)
 {
     _waitHandle.WaitOne();
     return _learner.LearnAsync(codeFormat, learnCodeModifier, forcedFrequency, userState);
 }
 public void Learn(CodeFormat? codeFormat = null,
     LearnCodeModifier? learnCodeFormat = null,
     uint? forcedFrequency = null,
     TimeSpan? timeout = null)
 {
     _waitHandle.WaitOne();
     _learner.Learn(codeFormat, learnCodeFormat, forcedFrequency, timeout);
 }
 public LearnHelper(Driver driver,
     CodeFormat defaultCodeFormat = CodeFormat.Pronto,
     LearnCodeModifier defaultLearnCodeModifier = LearnCodeModifier.Default)
     : base(driver)
 {
     SetUpLearner(driver, defaultCodeFormat, defaultLearnCodeModifier);
     SetUpReceiver(driver);
 }
 public Learner(Driver driver,
     CodeFormat defaultCodeFormat = CodeFormat.Pronto,
     LearnCodeModifier defaultLearnCodeModifier = LearnCodeModifier.Default)
     : base(driver)
 {
     _defaultLearnCodeFormat = defaultCodeFormat;
     _defaultLearnCodeModifier = defaultLearnCodeModifier;
 }
Beispiel #7
0
 public LearnHelper(Driver driver,
                    CodeFormat defaultCodeFormat = CodeFormat.Pronto,
                    LearnCodeModifier defaultLearnCodeModifier = LearnCodeModifier.Default)
     : base(driver)
 {
     SetUpLearner(driver, defaultCodeFormat, defaultLearnCodeModifier);
     SetUpReceiver(driver);
 }
Beispiel #8
0
 internal LearnState(CodeFormat codeFormat, LearnCodeModifier learnCodeFormat,
                     uint forcedFrequency, object userState)
 {
     _codeFormat      = codeFormat;
     _learnCodeFormat = learnCodeFormat;
     _forcedFrequency = forcedFrequency;
     _userState       = userState;
     _abort           = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Int32)));
     Marshal.WriteInt32(_abort, 0);
 }
        internal LearnState(CodeFormat codeFormat, LearnCodeModifier learnCodeFormat, 
			uint forcedFrequency, object userState)
        {
            _codeFormat = codeFormat;
            _learnCodeFormat = learnCodeFormat;
            _forcedFrequency = forcedFrequency;
            _userState = userState;
            _abort = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Int32)));
            Marshal.WriteInt32(_abort, 0);
        }
Beispiel #10
0
        /// <summary>
        ///     Learns an IR code synchronously.
        /// </summary>
        /// <param name="codeFormat">The format of the IR code to use in learning.</param>
        /// <param name="learnCodeFormat">The modifier used for the code format.</param>
        /// <param name="forcedFrequency">The frequency to use in learning.</param>
        /// <param name="timeout">The timeout after which to abort learning if it has not completed.</param>
        /// <returns>The IR code that was learned, or null if learning failed.</returns>
        public string Learn(CodeFormat codeFormat, LearnCodeModifier learnCodeFormat,
                            uint forcedFrequency, TimeSpan timeout)
        {
            CheckDisposed();
            if (timeout < TimeSpan.Zero)
            {
                throw new ArgumentOutOfRangeException("timeout", "timeout cannot be negative");
            }

            using (var results = new SyncLearnResults()) {
                LearnCompleted += ManagedWrapper_LearnCompleted;

                try {
                    LearnAsync(codeFormat, learnCodeFormat, forcedFrequency, results);
                    if (TimeSpan.Zero == timeout)
                    {
                        results.WaitEvent.WaitOne();
                        return(results.LearnCompletedEventArgs.Code);
                    }
                    else if (results.WaitEvent.WaitOne(timeout, false))
                    {
                        if (null != results.LearnCompletedEventArgs.Error)
                        {
                            throw results.LearnCompletedEventArgs.Error;
                        }
                        else if (results.LearnCompletedEventArgs.Cancelled)
                        {
                            return(null);
                        }
                        return(results.LearnCompletedEventArgs.Code);
                    }
                    else
                    {
                        LearnAsyncCancel(results);
                        return(null);
                    }
                }
                finally {
                    LearnCompleted -= ManagedWrapper_LearnCompleted;
                }
            }
        }
Beispiel #11
0
 /// <summary>
 /// Learns an IR code asynchronously.
 /// </summary>
 /// <param name="codeFormat">The format of the IR code to use in learning.</param>
 /// <param name="learnCodeFormat">The modifier used for the code format.</param>
 /// <param name="userState">An optional user state object that will be passed to the 
 /// Learning and LearnCompleted events and which can be used when calling LearnAsyncCancel().</param>
 public void LearnAsync(CodeFormat codeFormat, LearnCodeModifier learnCodeFormat, object userState)
 {
     CheckDisposed();
     LearnAsync(codeFormat, learnCodeFormat, 0, userState);
 }
Beispiel #12
0
        /// <summary>
        /// Learns an IR code asynchronously.
        /// </summary>
        /// <param name="codeFormat">The format of the IR code to use in learning.</param>
        /// <param name="learnCodeFormat">The modifier used for the code format.</param>
        /// <param name="forcedFrequency">The frequency to use in learning.</param>
        /// <param name="userState">An optional user state object that will be passed to the 
        /// Learning and LearnCompleted events and which can be used when calling LearnAsyncCancel().</param>
        public void LearnAsync(CodeFormat codeFormat, LearnCodeModifier learnCodeFormat,
			uint forcedFrequency, object userState)
        {
            CheckDisposed();
            if (LearnCodeModifier.ForceFrequency == learnCodeFormat)
            {
                if (0 == forcedFrequency)
                {
                    throw new ArgumentException("forcedFrequency", "forcedFrequency must be specified when using LearnCodeModifier.ForceFrequency");
                }
            }
            else
            {
                if (0 != forcedFrequency)
                {
                    throw new ArgumentException("forcedFrequency", "forcedFrequency can only be specified when using LearnCodeModifier.ForceFrequency");
                }
            }

            object learnStatesKey = null == userState ? this : userState;
            LearnState learnState = new LearnState(codeFormat, learnCodeFormat, forcedFrequency, userState);
            _learnStates[learnStatesKey] = learnState;

            if (false == ThreadPool.QueueUserWorkItem(new WaitCallback(DoLearn), learnState))
            {
                throw new ApplicationException("Unable to QueueUserWorkItem");
            }
        }
Beispiel #13
0
 /// <summary>
 /// Learns an IR code synchronously.
 /// </summary>
 /// <param name="codeFormat">The format of the IR code to use in learning.</param>
 /// <param name="learnCodeFormat">The modifier used for the code format.</param>
 /// <param name="timeout">The timeout after which to abort learning if it has not completed.</param>
 /// <returns>The IR code that was learned, or null if learning failed.</returns>
 public string Learn(CodeFormat codeFormat, LearnCodeModifier learnCodeFormat, TimeSpan timeout)
 {
     CheckDisposed();
     return  Learn(codeFormat, learnCodeFormat, 0, timeout);
 }
Beispiel #14
0
        /// <summary>
        /// Learns an IR code synchronously.
        /// </summary>
        /// <param name="codeFormat">The format of the IR code to use in learning.</param>
        /// <param name="learnCodeFormat">The modifier used for the code format.</param>
        /// <param name="forcedFrequency">The frequency to use in learning.</param>
        /// <param name="timeout">The timeout after which to abort learning if it has not completed.</param>
        /// <returns>The IR code that was learned, or null if learning failed.</returns>
        public string Learn(CodeFormat codeFormat, LearnCodeModifier learnCodeFormat, 
			uint forcedFrequency, TimeSpan timeout)
        {
            CheckDisposed();
            if (timeout < TimeSpan.Zero)
            {
                throw new ArgumentOutOfRangeException("timeout", "timeout cannot be negative");
            }

            using (SyncLearnResults results = new SyncLearnResults())
            {
                this.LearnCompleted += new LearnCompletedEventHandler(ManagedWrapper_LearnCompleted);

                try
                {
                    LearnAsync(codeFormat, learnCodeFormat, forcedFrequency, results);
                    if (TimeSpan.Zero == timeout)
                    {
                        results.WaitEvent.WaitOne();
                        return results.LearnCompletedEventArgs.Code;
                    }
                    else if (results.WaitEvent.WaitOne(timeout, false))
                    {
                        if (null != results.LearnCompletedEventArgs.Error)
                        {
                            throw results.LearnCompletedEventArgs.Error;
                        }
                        else if (false != results.LearnCompletedEventArgs.Cancelled)
                        {
                            return null;
                        }
                        return results.LearnCompletedEventArgs.Code;
                    }
                    else
                    {
                        LearnAsyncCancel(results);
                        return null;
                    }
                }
                finally
                {
                    this.LearnCompleted -= new LearnCompletedEventHandler(ManagedWrapper_LearnCompleted);
                }
            }
        }
 public Learner(CodeFormat defaultCodeFormat = CodeFormat.Pronto, 
     LearnCodeModifier defaultLearnCodeModifier = LearnCodeModifier.Default)
 {
     _defaultLearnCodeFormat = defaultCodeFormat;
     _defaultLearnCodeModifier = defaultLearnCodeModifier;
 }
 public LearnHelper(CodeFormat defaultCodeFormat = CodeFormat.Pronto, 
     LearnCodeModifier defaultLearnCodeModifier = LearnCodeModifier.Default)
 {
     SetUpLearner(null, defaultCodeFormat, defaultLearnCodeModifier);
     SetUpReceiver();
 }
        /// <summary>
        /// Learns an IR code synchronously.
        /// </summary>
        /// <param name="codeFormat">The format of the IR code to use in learning.</param>
        /// <param name="learnCodeFormat">The modifier used for the code format.</param>
        /// <param name="forcedFrequency">The frequency to use in learning.</param>
        /// <param name="timeout">The timeout after which to abort learning if it has not completed.</param>
        /// <returns>The IR code that was learned, or null if learning failed.</returns>
        public string Learn(
            CodeFormat? codeFormat = null,
            LearnCodeModifier? learnCodeFormat = null,
            uint? forcedFrequency = null,
            TimeSpan? timeout = null)
        {
            CheckDisposed();
            timeout = timeout ?? TimeSpan.Zero;
            if (timeout < TimeSpan.Zero)
            {
                throw new ArgumentOutOfRangeException("timeout", "timeout cannot be negative");
            }

            var learnTask = LearnAsync(codeFormat, learnCodeFormat, forcedFrequency);
            if (TimeSpan.Zero == timeout)
            {
                return learnTask.Result;
            }
            if (learnTask.Wait(timeout.Value))
            {
                if (learnTask.Exception != null)
                {
                    throw learnTask.Exception;
                }
                if (learnTask.IsCanceled)
                {
                    return null;
                }
                return learnTask.Result;
            }
            LearnAsyncCancel();
            return null;
        }
        /// <summary>
        /// Learns an IR code asynchronously.
        /// </summary>
        /// <param name="codeFormat">The format of the IR code to use in learning.</param>
        /// <param name="learnCodeModifier">The modifier used for the code format.</param>
        /// <param name="forcedFrequency">The frequency to use in learning.</param>
        /// <param name="userState">An optional user state object that will be passed to the 
        /// Learning and LearnCompleted events and which can be used when calling LearnAsyncCancel().</param>
        public Task<string> LearnAsync(
            CodeFormat? codeFormat = null,
            LearnCodeModifier? learnCodeModifier = null,
            uint? forcedFrequency = null,
            object userState = null)
        {
            CheckDisposed();
            if (learnCodeModifier == LearnCodeModifier.ForceFrequency)
            {
                if (forcedFrequency == 0)
                {
                    throw new ArgumentException("forcedFrequency must be specified when using LearnCodeModifier.ForceFrequency",
                        "forcedFrequency");
                }
            }
            else
            {
                if (forcedFrequency != null && forcedFrequency != 0)
                {
                    throw new ArgumentException("forcedFrequency can only be specified when using LearnCodeModifier.ForceFrequency",
                        "forcedFrequency");
                }
            }

            object learnStatesKey = userState ?? this;
            var cancellationSource = new CancellationTokenSource();
            var learnState = new LearnState(
                    codeFormat ?? _defaultLearnCodeFormat,
                    learnCodeModifier ?? _defaultLearnCodeModifier,
                    cancellationSource,
                    forcedFrequency ?? 0,
                    userState);
            _learnStates[learnStatesKey] = learnState;

            var learnTask = Task<string>.Factory.StartNew(LearnInternal, learnState, cancellationSource.Token);
            learnTask.ContinueWith(t =>
            {
                try
                {
                    var temp = LearnCompleted;
                    if (temp != null)
                    {
                        temp(this,
                            new LearnCompletedEventArgs(t.Exception,
                                learnState.WasAborted,
                                t.Result,
                                learnState.UserState));
                    }
                }
                finally
                {
                    learnState.Dispose();
                    learnStatesKey = null == learnState.UserState ? this : t.AsyncState;
                    _learnStates[learnStatesKey] = null;
                }
            });

            return learnTask;
        }
 private void SetUpLearner(Driver driver, CodeFormat defaultCodeFormat, LearnCodeModifier defaultLearnCodeModifier)
 {
     _learner = driver == null ? new Learner(defaultCodeFormat, defaultLearnCodeModifier) : new Learner(driver, defaultCodeFormat, defaultLearnCodeModifier);
     _learner.LearnCompleted += OnLearnComplete;
 }
Beispiel #20
0
 public LearnHelper(CodeFormat defaultCodeFormat = CodeFormat.Pronto,
                    LearnCodeModifier defaultLearnCodeModifier = LearnCodeModifier.Default)
 {
     SetUpLearner(null, defaultCodeFormat, defaultLearnCodeModifier);
     SetUpReceiver();
 }
Beispiel #21
0
        private static void TestLearn(Controller mc, CodeFormat learnFormat, LearnCodeModifier learnCodeModifier)
        {
            learnCompletedEventArgs = null;
            Console.WriteLine("<Press x to abort Learn>");
            mc.Learning += new UsbUirt.Controller.LearningEventHandler(mc_Learning);
            mc.LearnCompleted += new UsbUirt.Controller.LearnCompletedEventHandler(mc_LearnCompleted);

            try
            {
                try
                {
                    mc.LearnAsync(learnFormat, learnCodeModifier, learnCompletedEventArgs);
                }
                catch(Exception ex)
                {
                    Console.WriteLine("*** ERROR calling LearnAsync! ***");
                    throw;
                }

                while (learnCompletedEventArgs == null)
                {
                    string s = Console.ReadLine();
                    if (s.Length != 0 && s[0] == 'x')
                    {
                        if (learnCompletedEventArgs == null)
                        {
                            Console.WriteLine("Calling LearnAsyncCancel...");
                            mc.LearnAsyncCancel(learnCompletedEventArgs);
                            Thread.Sleep(1000);
                            break;
                        }
                    }
                    else
                    {
                        Console.WriteLine("<Press x to abort Learn>");
                    }
                }

                if (learnCompletedEventArgs != null &&
                    learnCompletedEventArgs.Cancelled == false &&
                    learnCompletedEventArgs.Error == null)
                {
                    irCode = learnCompletedEventArgs.Code;
                    Console.WriteLine("...Done...IRCode = {0}", irCode);
                    transmitFormat = learnFormat;
                }

            }
            finally
            {
                mc.Learning -= new UsbUirt.Controller.LearningEventHandler(mc_Learning);
                mc.LearnCompleted -= new UsbUirt.Controller.LearnCompletedEventHandler(mc_LearnCompleted);
            }
        }
Beispiel #22
0
 /// <summary>
 ///     Learns an IR code synchronously.
 /// </summary>
 /// <param name="codeFormat">The format of the IR code to use in learning.</param>
 /// <param name="learnCodeFormat">The modifier used for the code format.</param>
 /// <param name="timeout">The timeout after which to abort learning if it has not completed.</param>
 /// <returns>The IR code that was learned, or null if learning failed.</returns>
 public string Learn(CodeFormat codeFormat, LearnCodeModifier learnCodeFormat, TimeSpan timeout)
 {
     CheckDisposed();
     return(Learn(codeFormat, learnCodeFormat, 0, timeout));
 }
Beispiel #23
0
 private void SetUpLearner(Driver driver, CodeFormat defaultCodeFormat, LearnCodeModifier defaultLearnCodeModifier)
 {
     _learner = driver == null ? new Learner(defaultCodeFormat, defaultLearnCodeModifier) : new Learner(driver, defaultCodeFormat, defaultLearnCodeModifier);
     _learner.LearnCompleted += OnLearnComplete;
 }
Beispiel #24
0
 /// <summary>
 ///     Learns an IR code asynchronously.
 /// </summary>
 /// <param name="codeFormat">The format of the IR code to use in learning.</param>
 /// <param name="learnCodeFormat">The modifier used for the code format.</param>
 /// <param name="userState">
 ///     An optional user state object that will be passed to the
 ///     Learning and LearnCompleted events and which can be used when calling LearnAsyncCancel().
 /// </param>
 public void LearnAsync(CodeFormat codeFormat, LearnCodeModifier learnCodeFormat, object userState)
 {
     CheckDisposed();
     LearnAsync(codeFormat, learnCodeFormat, 0, userState);
 }
Beispiel #25
0
 public Learner(CodeFormat defaultCodeFormat = CodeFormat.Pronto,
                LearnCodeModifier defaultLearnCodeModifier = LearnCodeModifier.Default)
 {
     _defaultLearnCodeFormat   = defaultCodeFormat;
     _defaultLearnCodeModifier = defaultLearnCodeModifier;
 }