Created
June 15, 2012 18:51
-
-
Save RoadieRich/2938148 to your computer and use it in GitHub Desktop.
SoftwarePWMPort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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