Skip to content

Instantly share code, notes, and snippets.

@RoadieRich
Created June 15, 2012 18:51
Show Gist options
  • Save RoadieRich/2938148 to your computer and use it in GitHub Desktop.
Save RoadieRich/2938148 to your computer and use it in GitHub Desktop.
SoftwarePWMPort
using System.Threading;
using Microsoft.SPOT.Hardware;
public class SoftwarePWMPort : OutputPort
{
private bool OffState;
public SoftwarePWMPort(Cpu.Pin portID, bool initialState)
: base(portID, initialState)
{
_t = new Thread(modulate);
OffState = initialState;
}
private uint _b;
public uint Brightness
{
get
{
return _b;
}
set
{
_b = value <= 100 ? value : 100;
if (!_t.IsAlive)
{
_t = new Thread(modulate);
_t.Start();
}
}
}
#region /* Legacy methods */
public new void Write(bool state)
{
Brightness = state ? 100u : 0;
}
public new bool Read()
{
return _b > 0;
}
#endregion
private Thread _t;
private int i;
///Actual method run within the thread
private void modulate()
{
while (_b > 0 && _b < 100)
{
base.Write(!OffState);
for (i = 0; i < _b; i++)
{
Thread.Sleep(0);
}
base.Write(OffState);
for (; i < 100; i++)
{
Thread.Sleep(0);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment