In this project we're going to learn how easy is to use an SPI capable LED strip using Meadow.Foundation using the Apa102 driver. You'll see in this project a few examples of how to make groovy animations that you could use for a DIY Christmas tree.
Meadow.Foundation a platform for quickly and easily building connected things using.NET on Meadow. Created by Wilderness Labs, it's completely open source and maintained by the Wilderness Labs community.
If you're new working with Meadow, I suggest you go to the Getting Started w/ Meadow by Controlling the Onboard RGB LED project to properly set up your development environment.
Step 1 - Assemble the circuitWire your project like this if you have a small segment of LED strip:
Important Note: if you plan on using long LED strips and turn them all simultaneously, you will need an external power supply (usually 12V), otherwise, drawing lots of voltage from Meadow can burn your board.
Step 2 - Create a Meadow Application projectCreate a new Meadow Application project in Visual Studio 2019 for Windows or macOS and name it MerryXmasLights.
Step 3 - Add the required NuGet packagesFor this project, search and install the following NuGet packages:
Step 4 - Write the code for MeadowMenuCopy the following code below:
public class MeadowApp : App<F7Micro, MeadowApp>
{
Apa102 ledStrip;
public MeadowApp()
{
Initialize();
while (true)
{
WalkXmasColors();
Console.WriteLine("Cycle colors");
CycleColors(5000);
WalkColors(Color.Red);
WalkColors(Color.Green);
WalkColors(Color.Blue);
WalkColors(Color.Yellow);
WalkColors(Color.Cyan);
WalkColors(Color.Violet);
}
}
void Initialize()
{
var onboardLed = new RgbPwmLed(device: Device,
redPwmPin: Device.Pins.OnboardLedRed,
greenPwmPin: Device.Pins.OnboardLedGreen,
bluePwmPin: Device.Pins.OnboardLedBlue,
3.3f, 3.3f, 3.3f,
IRgbLed.CommonType.CommonAnode);
onboardLed.SetColor(Color.Red);
Console.WriteLine("Initialize hardware...");
ISpiBus spiBus = Device.CreateSpiBus();
IDigitalOutputPort spiPeriphChipSelect =
Device.CreateDigitalOutputPort(Device.Pins.D04);
ledStrip = new Apa102(
spiBus: spiBus,
numberOfLeds: 18,
pixelOrder: Apa102.PixelOrder.BGR);
onboardLed.SetColor(Color.Green);
}
void WalkXmasColors(int count = 100)
{
int offset = 0;
Color color;
while (count > 0)
{
if (offset % 4 == 0)
{
color = Color.Red;
}
else if (offset % 4 == 1)
{
color = Color.Blue;
}
else if (offset % 4 == 2)
{
color = Color.Green;
}
else//f (offset % 4 == 1)
{
color = Color.Yellow;
}
for (uint i = 0; i < ledStrip.NumberOfLeds; i++)
{
if ((offset + i) % 4 == 0)
{
ledStrip.SetLed(i, color);
}
else
{
ledStrip.SetLed(i, Color.Black);
}
}
ledStrip.Show();
Thread.Sleep(400);
offset++;
count--;
}
}
void WalkColors(Color color, int count = 25)
{
int offset = 0;
while (count > 0)
{
for (uint i = 0; i < ledStrip.NumberOfLeds; i++)
{
if ((offset + i) % 5 == 0)
{
ledStrip.SetLed(i, color);
}
else
{
ledStrip.SetLed(i, Color.Black);
}
}
ledStrip.Show();
Thread.Sleep(400);
offset++;
count--;
}
}
void CycleColors(int duration)
{
Console.WriteLine("Cycle colors...");
ShowColorPulse(Color.Blue, duration);
ShowColorPulse(Color.Cyan, duration);
ShowColorPulse(Color.Green, duration);
ShowColorPulse(Color.GreenYellow, duration);
ShowColorPulse(Color.Yellow, duration);
ShowColorPulse(Color.Orange, duration);
ShowColorPulse(Color.OrangeRed, duration);
ShowColorPulse(Color.Red, duration);
ShowColorPulse(Color.MediumVioletRed, duration);
ShowColorPulse(Color.Purple, duration);
}
void ShowColorPulse(Color color, int duration = 1000)
{
float brightness = 0.05f;
bool forward = true;
DateTime time = DateTime.Now;
while ((DateTime.Now - time).TotalMilliseconds < duration)
{
for (uint i = 0; i < ledStrip.NumberOfLeds; i++)
{
ledStrip.SetLed(i, color, brightness);
}
if (forward) { brightness += 0.01f; }
else { brightness -= 0.01f; }
if (brightness <= 0.05f)
{
forward = true;
}
if (brightness >= 0.5f)
{
forward = false;
}
ledStrip.Show();
}
}
}
Here's a quick breakdown of each method:
Initialize
- This method (as the name suggests) initializes the LED strip and uses the Onboard LED to indicate when it has started (glowing red) and when its finished (glowing green).WalkXmasColors
- Lights up every fourth LED in the strip with Christmas colors (red, blue, green and yellow) a 100 cycles.CycleColors
- CallsShowColorPulse
multiple times passing different colors and duration of the effect.ShowColorPulse
- The entire LED strip makes pulsing effect with the color passed and duration in milliseconds.WalkColors
- Every fifth LED glow the color passed as they increment each LED position, for 25 cycles.
Click the Run button in Visual Studio. It should look like to the following GIF:
This project is only the tip of the iceberg in terms of the extensive exciting things you can do with Meadow.Foundation.
- It comes with a huge peripheral driver library with drivers for the most common sensors and peripherals.
- The peripheral drivers encapsulate the core logic and expose a simple, clean, modern API.
- This project is backed by a growing community that is constantly working on building cool connected things and are always excited to help new-comers and discuss new projects.
Comments