Fezzer LED Driver Bug

The LED driver has a bit of a bug in it that some people may not catch. The problem arises when you are blinking the LED then you call either TurnOn() or ShutOff() without calling StopBlinking() first, then start blinking the LED again. You will get a null reference exception or an object disposed exception (can’t remember which one).

The problem line and fix is here: (problem is at line 53 if copied directly from the Fezzer page)


            [MethodImpl(MethodImplOptions.Synchronized)]
            public void SetState(bool LED_state)
            {
                if (disposed)
                    throw new ObjectDisposedException();
 
                if (timerRunning)
                {
                    //The Timer should NOT be disposed because it is never
                    //recreated elsewhere (aka, the StartBlinking routine)
                    //timer.Dispose();
                    //Change the above line to:
                    timer.Change(Timeout.Infinite, Timeout.Infinite);
                    timerRunning = false;
                }
 
                led.Write(LED_state);
            }


Also…

StopBlinking should probably not call StartBlinking because of the way it sets up the flags. When it calls StartBlinking, the timerRunning flag is never set to false (makes the bug above worse because you don’t need to be blinking to cause the exception, you just have to had been blinking it at one time). StopBlinking would work better if it was modified to be:


public void StopBlinking()
{
  timerRunning = false;
  timer.change(Timeout.Infinite, 0);
}

Thank you Ron2 :wink: