Skip to content

Commit

Permalink
Merge pull request #1079 from WildernessLabs/ws28212_hackin
Browse files Browse the repository at this point in the history
WS2812 driver cleanup
  • Loading branch information
adrianstevens authored Oct 4, 2024
2 parents 40b193d + cb070ea commit 8371be0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 62 deletions.
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=>
}
}

0 comments on commit 8371be0

Please sign in to comment.