It feels like helping Windows IoT to land again, so, serious write a blog. In fact, about half a year ago, want to write, then want to do a Windows IoT-based car, but the Raspberry Pi native does not support PWM AH. Baidu also search, on GitHub turned a circle, in @ms-iot that found Lightning, and then see the last update time, or 2016 mid ... Windows IoT is so miserable in China that no one has ever written a tutorial for such a long time ... Don't talk nonsense ...
Example of this article address: https://github.com/ZhangGaoxing/windows-iot-demo/tree/master/RgbLed
Lightning Project Address: https://github.com/ms-iot/lightning
:
First, change the default controller driver
Open the Raspberry Pi Windows Device Portal and change the default Controller Driver option from the Devices menu to direct Memory Mapped D River, reboot.
Second, change the package.appxmanifest configuration
Create a new UWP project, this article is called "Rgbleddemo". Open Package.appxmanifest as "View Code".
Add a namespace under the Package tab and change the Ignorablenamespaces property.
xmlns:iot= "Http://schemas.microsoft.com/appx/manifest/iot/windows10" ignorablenamespaces= "UAP MP IoT"
In capabilities add the following:
<name= "lowleveldevices"/><name = "109b86ad-f53d-4b76-aa5f-821e2ddf2141" />
Iii. citation of Microsoft.Iot.Lightning
Search for Lightning under NuGet Package Manager.
And the Windows IoT Extensions for the UWP
Iv. Use of Lightning
Note reference
using WINDOWS.DEVICES.PWM; using Microsoft.IoT.Lightning.Providers;
1. Judging the activation of Lightning
This step is necessary because using Lightning must turn off the system's default controller driver and throw an exception if it is not enabled.
if (! lightningprovider.islightningenabled) { thrownew NullReferenceException ("Lightning isn ' t enabled! " );}
2. Obtaining the Software PWM controller
Under all normal circumstances it is time to obtain the software PWM controller in the Lightning. Other hardware PWM controllers are also integrated in the Lightning, so a collection is returned when calling Pwmcontroller.getcontrollersasync (), where the second item is the software PWM controller we need. It is also necessary to set the PWM frequency when the controller is obtained, the frequency range of this software PWM controller is between 40-1000 Hz (Low pitiful ...). ), a number that is not within this range throws an exception.
Pwmcontroller controller = (await Pwmcontroller.getcontrollersasync (Lightningpwmprovider.getpwmprovider () )) [1];controller. Setdesiredfrequency (+);
3. Setting the PWM Pin
Take the Red pin as an example. The PIN is opened by the controller first, and here is the GPIO 17-bit pin. Then need to set Duty Cycle Percentage, popular point is the ratio of voltage, 0-1 decimal between.
Pwmpin Redpin = controller. Openpin (+); Redpin.setactivedutycyclepercentage (0
Then change the brightness of the LEDs or change the value of Setactivedutycyclepercentage (value).
Turn off the PWM first if you release it.
Redpin.stop (); Redpin.dispose ();
Five, attention
With Lightning, the I2C,SPI code that was previously written based on the default controller driver will get an error. But the Lightning integrates the controller of I²c, SPI, GPIO and so on, replace it.
The Project parsing section of this article is over. Here is a test code for a breathing lamp, I use a common cathode RGB LEDs. The code is in the GitHub project.
/// <summary>///Breathing LED/// </summary>/// <param name= "delay" >Delay Time</param> Public AsyncTask Breathingasync (intdelay) { DoubleRed =255; DoubleGreen =0; DoubleBlue =0; while(Red! =0&& Green! =255) {redpin.setactivedutycyclepercentage (red/255.0); Greenpin.setactivedutycyclepercentage (Green/255.0); Red--; Green++; awaitTask.delay (Delay); } while(Green! =0&& Blue! =255) {greenpin.setactivedutycyclepercentage (green/255.0); Bluepin.setactivedutycyclepercentage (Blue/255.0); Green--; Blue++; awaitTask.delay (Delay); } while(Blue! =0&& Red! =255) {bluepin.setactivedutycyclepercentage (blue/255.0); Redpin.setactivedutycyclepercentage (Red/255.0); Blue--; Red++; awaitTask.delay (Delay); }}
Happy Windows IoT Development Note: Using the software PWM driver for RGB LEDs in Lightning