Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WS2812 driver cleanup #1079

Merged
merged 8 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,10 @@ public SpiClockConfiguration.Mode SpiBusMode
/// </summary>
/// <param name="spiBus">SPI bus</param>
/// <param name="numberOfLeds">Number of leds</param>
/// <param name="chipSelectPort">SPI chip select port (optional)</param>
public Ws2812(ISpiBus spiBus, int numberOfLeds, IDigitalOutputPort? chipSelectPort = null)
public Ws2812(ISpiBus spiBus, int numberOfLeds)
{
spiComms = new SpiCommunications(spiBus, chipSelectPort, spiBus.Configuration.Speed);

spiComms = new SpiCommunications(spiBus, null, DefaultSpiBusSpeed, DefaultSpiBusMode);
this.numberOfLeds = numberOfLeds;

// To transmit 8 bits of color we need 4 bytes and there are 3 colors
buffer = new byte[numberOfLeds * BytesPerColorPart * 3];
}
Expand Down Expand Up @@ -137,4 +134,4 @@ public void Show()
{
spiComms.Write(buffer);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,81 +1,40 @@
using Meadow;
using Meadow.Devices;
using Meadow.Foundation.Leds;
using Meadow.Units;
using System;
using System.Threading.Tasks;

namespace MeadowApp
{
public class MeadowApp : App<F7FeatherV2>
{
private Ws2812 _ws2812;
//<!=SNIP=>

private readonly int ledCount = 20;
private Ws2812 neoPixels;

private readonly int ledCount = 24;

public override Task Initialize()
{
Resolver.Log.Info("Initialize");
var _spiBus = Device.CreateSpiBus(new Frequency(3, Frequency.UnitType.Megahertz));
_ws2812 = new Ws2812(_spiBus, ledCount);
var spiBus = Device.CreateSpiBus();
neoPixels = new Ws2812(spiBus, ledCount);

return base.Initialize();
}

public override async Task Run()
{
Resolver.Log.Info("Run...");

var rand = new Random();

var colors = new Color[]
{
Color.Red,
Color.Orange,
Color.Yellow,
Color.Green,
Color.Blue,
Color.Violet,
};

var c = 0;

while (true)
{
var color = colors[c];

Resolver.Log.Info($"Change to {color.ToString()}");

_ws2812.SetLed(9, Color.Black);
_ws2812.SetLed(8, Color.Black);
_ws2812.SetLed(7, Color.Black);
_ws2812.SetLed(6, Color.Black);
_ws2812.SetLed(5, Color.Black);
_ws2812.SetLed(4, Color.Black);
_ws2812.SetLed(3, Color.Black);
_ws2812.SetLed(2, Color.Black);
_ws2812.SetLed(1, Color.Black);
_ws2812.SetLed(0, color);
_ws2812.Show();
await Task.Delay(1000);

_ws2812.SetLed(9, Color.Black);
_ws2812.SetLed(8, Color.Black);
_ws2812.SetLed(7, Color.Black);
_ws2812.SetLed(6, Color.Black);
_ws2812.SetLed(5, Color.Black);
_ws2812.SetLed(4, Color.Black);
_ws2812.SetLed(3, Color.Black);
_ws2812.SetLed(2, Color.Black);
_ws2812.SetLed(1, Color.Black);
_ws2812.SetLed(0, Color.Black);
_ws2812.Show();

c++;
if (c > 5) c = 0;

await Task.Delay(1000);
for (int i = 0; i < neoPixels.NumberOfLeds; i++)
{
neoPixels.SetAllLeds(Color.Black);
neoPixels.SetLed(i, Color.Blue);
neoPixels.Show();
await Task.Delay(100);
}
}
}

//<!=SNOP=>
}
}
Loading