Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.

dustmouse

Member Since 03 Jan 2013
Offline Last Active Mar 18 2015 03:21 PM
-----

#43904 Button triggering multiple LEDs

Posted by dustmouse on 22 January 2013 - 03:43 AM

Hi Dave, thanks a lot for that great example.  It's a very nice implementation for syncing a bunch of blinkers.  I borrowed your use of IDisposable on your LED_Extension class and followed your conservative approach to thread creation.  

 

My final implementation (below) was slightly different because I am using a button event to turn the blinking LEDs on and off.  And because I was able to get the blinking LEDs to look closely synced by only creating the blinking thread once for each LED, I was able to keep with my foreach loop approach for turning the LEDs on/off.  

 

I also need to thank hanzibal for helping me arrive at a similar conclusion about threading.  

 

Oh, I ended up going with a ManualResetEvent because it doesn't automatically set the state to signal when it is encountered.  My Dispose() method looks a little sloppy to me and I think it can be simplified.  Anyway, here is the code (I made a couple edits to it):

 

public sealed class PulseLed : Led, IDisposable
    {
        private Thread pulseThread;
        private ManualResetEvent manualResetEvent;
        private bool isDisposed;
 
        #region Constructors
 
        public PulseLed(Cpu.Pin cpuPin) : base(cpuPin) 
        {
            this.manualResetEvent = new ManualResetEvent(true);
            this.isDisposed = false;
        }
 
        #endregion
 
        #region Public Properties
 
        public int PulseIntervalInMilliseconds { get; set; }
 
        #endregion
 
        #region IDisposable Members
 
        public void Dispose()
        {
            if (this.pulseThread != null && this.pulseThread.IsAlive)
            {
                this.manualResetEvent.Set();
                this.isDisposed = true;
                this.pulseThread.Join();
 
                if (base.State)
                    base.SetState(false);
            }
        }
 
        #endregion
 
        #region IOutputComponent Members
 
        public override bool State { get; protected set; }
 
        public override void SetState(bool state)
        {
            this.State = state;
 
            if (this.PulseIntervalInMilliseconds > 0)
            {
                if (this.State)
                {
                    if ((this.pulseThread == null || !this.pulseThread.IsAlive))
                    {
                        this.pulseThread = new Thread(new ThreadStart(this.pulse));
                        this.pulseThread.Start();
                    }
                    else
                        this.manualResetEvent.Set();
                }  
                else
                {
                    this.manualResetEvent.Reset();
 
                    if (base.State)
                        base.SetState(false);
                }
            }
            else
                base.SetState(state);
        }
 
        #endregion
 
        #region Private Methods
 
        private void pulse()
        {
            while (!this.isDisposed)
            {
                base.State = !base.State;
                base.outputPort.Write(base.State);
                Thread.Sleep(this.PulseIntervalInMilliseconds / 2);
 
                this.manualResetEvent.WaitOne();
            }
        }
 
        #endregion
    }



home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.