From 7c0f17d94d5c09b13fe6fc62005243c443ab16ae Mon Sep 17 00:00:00 2001 From: Adrian Stevens Date: Fri, 4 Oct 2024 13:06:44 -0700 Subject: [PATCH 1/7] WS2812 driver cleanup --- .../Leds.Ws2812/Driver/Ws2812.cs | 52 ++----------------- .../Samples/Ws2812_Sample/MeadowApp.cs | 5 +- 2 files changed, 7 insertions(+), 50 deletions(-) diff --git a/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Driver/Ws2812.cs b/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Driver/Ws2812.cs index 141b0bc2e1..a2af1f3908 100644 --- a/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Driver/Ws2812.cs +++ b/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Driver/Ws2812.cs @@ -8,12 +8,12 @@ namespace Meadow.Foundation.Leds /// /// Represents WS2812/Neopixel Led(s) /// - public class Ws2812 : ISpiPeripheral, IDisposable + public class Ws2812 : ISpiPeripheral { /// /// The default SPI bus speed for the device /// - public Frequency DefaultSpiBusSpeed => new Frequency(3, Frequency.UnitType.Megahertz); + public Frequency DefaultSpiBusSpeed => new(3, Frequency.UnitType.Megahertz); /// /// The SPI bus speed for the device @@ -48,11 +48,6 @@ public SpiClockConfiguration.Mode SpiBusMode /// public bool IsDisposed { get; private set; } - /// - /// Did we create the port(s) used by the peripheral - /// - readonly bool createdPort = false; - private static readonly byte[] ws2812Bytes = new byte[] { 0x44, 0x46, 0x64, 0x66 }; private const int BytesPerColorPart = 4; @@ -66,27 +61,14 @@ public SpiClockConfiguration.Mode SpiBusMode readonly IDigitalOutputPort? chipSelectPort; - /// - /// Creates a new WS2812 object - /// - /// SPI bus - /// Chip select pin - /// Number of leds - public Ws2812(ISpiBus spiBus, IPin chipSelectPin, int numberOfLeds) - : this(spiBus, numberOfLeds, chipSelectPin.CreateDigitalOutputPort()) - { - createdPort = true; - } - /// /// Creates a new WS2812 object /// /// SPI bus /// Number of leds - /// SPI chip select port (optional) - public Ws2812(ISpiBus spiBus, int numberOfLeds, IDigitalOutputPort? chipSelectPort = null) + public Ws2812(ISpiBus spiBus, int numberOfLeds) { - spiComms = new SpiCommunications(spiBus, this.chipSelectPort = chipSelectPort, DefaultSpiBusSpeed, DefaultSpiBusMode); + 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]; @@ -148,29 +130,5 @@ public void Show() { spiComms.Write(buffer); } - - /// - public void Dispose() - { - Dispose(disposing: true); - GC.SuppressFinalize(this); - } - - /// - /// Dispose of the object - /// - /// Is disposing - protected virtual void Dispose(bool disposing) - { - if (!IsDisposed) - { - if (disposing && createdPort) - { - chipSelectPort?.Dispose(); - } - - IsDisposed = true; - } - } } -} +} \ No newline at end of file diff --git a/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Samples/Ws2812_Sample/MeadowApp.cs b/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Samples/Ws2812_Sample/MeadowApp.cs index 0b14f92e6b..2879032ea2 100644 --- a/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Samples/Ws2812_Sample/MeadowApp.cs +++ b/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Samples/Ws2812_Sample/MeadowApp.cs @@ -1,6 +1,5 @@ using Meadow; using Meadow.Devices; -using Meadow.Foundation; using Meadow.Foundation.Leds; using Meadow.Units; using System.Threading.Tasks; @@ -27,11 +26,11 @@ public override Task Run() { if (i % 2 == 0) { - _ws2812.SetLed(i, Color.Blue); + _ws2812.SetLed(i, Color.Green); } else { - _ws2812.SetLed(i, Color.Red); + _ws2812.SetLed(i, Color.Blue); } _ws2812.Show(); } From e72d0cf3ed68cfadcbd67eaa456865d9422ffd00 Mon Sep 17 00:00:00 2001 From: Adrian Stevens Date: Fri, 4 Oct 2024 13:08:57 -0700 Subject: [PATCH 2/7] Filescoped namespace update --- .../Leds.Ws2812/Driver/Ws2812.cs | 203 +++++++++--------- 1 file changed, 101 insertions(+), 102 deletions(-) diff --git a/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Driver/Ws2812.cs b/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Driver/Ws2812.cs index a2af1f3908..653ac516f8 100644 --- a/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Driver/Ws2812.cs +++ b/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Driver/Ws2812.cs @@ -3,132 +3,131 @@ using System; using System.Collections.Generic; -namespace Meadow.Foundation.Leds +namespace Meadow.Foundation.Leds; + +/// +/// Represents WS2812/Neopixel Led(s) +/// +public class Ws2812 : ISpiPeripheral { /// - /// Represents WS2812/Neopixel Led(s) + /// The default SPI bus speed for the device + /// + public Frequency DefaultSpiBusSpeed => new(3, Frequency.UnitType.Megahertz); + + /// + /// The SPI bus speed for the device /// - public class Ws2812 : ISpiPeripheral + public Frequency SpiBusSpeed { - /// - /// The default SPI bus speed for the device - /// - public Frequency DefaultSpiBusSpeed => new(3, Frequency.UnitType.Megahertz); - - /// - /// The SPI bus speed for the device - /// - public Frequency SpiBusSpeed - { - get => spiComms.BusSpeed; - set => spiComms.BusSpeed = value; - } + get => spiComms.BusSpeed; + set => spiComms.BusSpeed = value; + } - /// - /// The default SPI bus mode for the device - /// - public SpiClockConfiguration.Mode DefaultSpiBusMode => SpiClockConfiguration.Mode.Mode0; + /// + /// The default SPI bus mode for the device + /// + public SpiClockConfiguration.Mode DefaultSpiBusMode => SpiClockConfiguration.Mode.Mode0; - /// - /// The SPI bus mode for the device - /// - public SpiClockConfiguration.Mode SpiBusMode - { - get => spiComms.BusMode; - set => spiComms.BusMode = value; - } + /// + /// The SPI bus mode for the device + /// + public SpiClockConfiguration.Mode SpiBusMode + { + get => spiComms.BusMode; + set => spiComms.BusMode = value; + } - /// - /// Total number of leds - /// - public int NumberOfLeds => numberOfLeds; + /// + /// Total number of leds + /// + public int NumberOfLeds => numberOfLeds; - /// - /// Is the object disposed - /// - public bool IsDisposed { get; private set; } + /// + /// Is the object disposed + /// + public bool IsDisposed { get; private set; } + + private static readonly byte[] ws2812Bytes = new byte[] { 0x44, 0x46, 0x64, 0x66 }; + private const int BytesPerColorPart = 4; + + /// + /// SPI Communication bus used to communicate with the peripheral + /// + protected ISpiCommunications spiComms; - private static readonly byte[] ws2812Bytes = new byte[] { 0x44, 0x46, 0x64, 0x66 }; - private const int BytesPerColorPart = 4; + readonly int numberOfLeds; + readonly byte[] buffer; - /// - /// SPI Communication bus used to communicate with the peripheral - /// - protected ISpiCommunications spiComms; + readonly IDigitalOutputPort? chipSelectPort; - readonly int numberOfLeds; - readonly byte[] buffer; + /// + /// Creates a new WS2812 object + /// + /// SPI bus + /// Number of leds + public Ws2812(ISpiBus spiBus, int numberOfLeds) + { + 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]; + } - readonly IDigitalOutputPort? chipSelectPort; + /// + /// Set the color of the specified LED + /// + /// Index of the LED to change + /// The color + public void SetLed(int index, Color color) + { + SetLed(index, new byte[] { color.R, color.G, color.B }); + } - /// - /// Creates a new WS2812 object - /// - /// SPI bus - /// Number of leds - public Ws2812(ISpiBus spiBus, int numberOfLeds) + private static IEnumerable ByteToWs2812Byte(byte theByte) + { + for (int counter = 0; counter < 4; ++counter) { - 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]; + yield return ws2812Bytes[(theByte & 0b1100_0000) >> 6]; + theByte <<= 2; } + } - /// - /// Set the color of the specified LED - /// - /// Index of the LED to change - /// The color - public void SetLed(int index, Color color) + /// + /// Set the color of the specified LED + /// + /// Index of the LED to change + /// Byte array representing the color RGB values. byte[0] = Red, byte[1] = Green, byte[2] = Blue + public void SetLed(int index, byte[] rgb) + { + if (index > numberOfLeds || index < 0) { - SetLed(index, new byte[] { color.R, color.G, color.B }); + throw new ArgumentOutOfRangeException("Index must be less than the number of leds specified"); } - private static IEnumerable ByteToWs2812Byte(byte theByte) + // 4 bytes per color and 3 colors + int position = index * BytesPerColorPart * 3; + + // The on-the-wire format is GRB, the input is RGB + foreach (var theByte in ByteToWs2812Byte(rgb[1])) { - for (int counter = 0; counter < 4; ++counter) - { - yield return ws2812Bytes[(theByte & 0b1100_0000) >> 6]; - theByte <<= 2; - } + buffer[position++] = theByte; } - - /// - /// Set the color of the specified LED - /// - /// Index of the LED to change - /// Byte array representing the color RGB values. byte[0] = Red, byte[1] = Green, byte[2] = Blue - public void SetLed(int index, byte[] rgb) + foreach (var theByte in ByteToWs2812Byte(rgb[0])) { - if (index > numberOfLeds || index < 0) - { - throw new ArgumentOutOfRangeException("Index must be less than the number of leds specified"); - } - - // 4 bytes per color and 3 colors - int position = index * BytesPerColorPart * 3; - - // The on-the-wire format is GRB, the input is RGB - foreach (var theByte in ByteToWs2812Byte(rgb[1])) - { - buffer[position++] = theByte; - } - foreach (var theByte in ByteToWs2812Byte(rgb[0])) - { - buffer[position++] = theByte; - } - foreach (var theByte in ByteToWs2812Byte(rgb[2])) - { - buffer[position++] = theByte; - } + buffer[position++] = theByte; } - - /// - /// Transmit the buffer to the LEDs - /// - public void Show() + foreach (var theByte in ByteToWs2812Byte(rgb[2])) { - spiComms.Write(buffer); + buffer[position++] = theByte; } } + + /// + /// Transmit the buffer to the LEDs + /// + public void Show() + { + spiComms.Write(buffer); + } } \ No newline at end of file From 4c91dcaf3c5038b45d3adcc3fcf7e6a3fa9cb245 Mon Sep 17 00:00:00 2001 From: Adrian Stevens Date: Fri, 4 Oct 2024 13:11:39 -0700 Subject: [PATCH 3/7] Readd SetAllLeds method --- .../Leds.Ws2812/Driver/Ws2812.cs | 12 ++++++++++++ .../Leds.Ws2812/Samples/Ws2812_Sample/MeadowApp.cs | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Driver/Ws2812.cs b/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Driver/Ws2812.cs index 653ac516f8..3230979ad8 100644 --- a/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Driver/Ws2812.cs +++ b/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Driver/Ws2812.cs @@ -84,6 +84,18 @@ public void SetLed(int index, Color color) SetLed(index, new byte[] { color.R, color.G, color.B }); } + /// + /// Set the color of all LEDs + /// + /// The color + public void SetAllLeds(Color color) + { + for (int i = 0; i < numberOfLeds; i++) + { + SetLed(i, new byte[] { color.R, color.G, color.B }); + } + } + private static IEnumerable ByteToWs2812Byte(byte theByte) { for (int counter = 0; counter < 4; ++counter) diff --git a/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Samples/Ws2812_Sample/MeadowApp.cs b/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Samples/Ws2812_Sample/MeadowApp.cs index 2879032ea2..9ea7e90975 100644 --- a/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Samples/Ws2812_Sample/MeadowApp.cs +++ b/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Samples/Ws2812_Sample/MeadowApp.cs @@ -26,7 +26,7 @@ public override Task Run() { if (i % 2 == 0) { - _ws2812.SetLed(i, Color.Green); + _ws2812.SetLed(i, Color.Yellow); } else { From 944a95146de4970f7ae9daf3874e0291556047b4 Mon Sep 17 00:00:00 2001 From: Adrian Stevens Date: Fri, 4 Oct 2024 13:18:30 -0700 Subject: [PATCH 4/7] Remove IsDisposed property --- .../Leds.Ws2812/Driver/Ws2812.cs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Driver/Ws2812.cs b/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Driver/Ws2812.cs index 3230979ad8..538bf58915 100644 --- a/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Driver/Ws2812.cs +++ b/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Driver/Ws2812.cs @@ -43,11 +43,6 @@ public SpiClockConfiguration.Mode SpiBusMode /// public int NumberOfLeds => numberOfLeds; - /// - /// Is the object disposed - /// - public bool IsDisposed { get; private set; } - private static readonly byte[] ws2812Bytes = new byte[] { 0x44, 0x46, 0x64, 0x66 }; private const int BytesPerColorPart = 4; From e4620b28f05b7989b63a160dac189725aa49a93d Mon Sep 17 00:00:00 2001 From: Adrian Stevens Date: Fri, 4 Oct 2024 13:31:07 -0700 Subject: [PATCH 5/7] sample cleanup --- .../Samples/Ws2812_Sample/MeadowApp.cs | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Samples/Ws2812_Sample/MeadowApp.cs b/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Samples/Ws2812_Sample/MeadowApp.cs index 9ea7e90975..31e8e73255 100644 --- a/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Samples/Ws2812_Sample/MeadowApp.cs +++ b/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Samples/Ws2812_Sample/MeadowApp.cs @@ -1,40 +1,37 @@ using Meadow; using Meadow.Devices; using Meadow.Foundation.Leds; -using Meadow.Units; +using System.Threading; using System.Threading.Tasks; namespace MeadowApp { public class MeadowApp : App { - private Ws2812 _ws2812; + private Ws2812 neoPixels; - private readonly int ledCount = 10; + private readonly int ledCount = 24; public override Task Initialize() { - var _spiBus = Device.CreateSpiBus(new Frequency(3.2, Frequency.UnitType.Megahertz)); - _ws2812 = new Ws2812(_spiBus, ledCount); + var spiBus = Device.CreateSpiBus(); + neoPixels = new Ws2812(spiBus, ledCount); return base.Initialize(); } public override Task Run() { - for (var i = 0; i < ledCount; i++) + while (true) { - if (i % 2 == 0) + for (int i = 0; i < neoPixels.NumberOfLeds; i++) { - _ws2812.SetLed(i, Color.Yellow); + neoPixels.SetAllLeds(Color.Black); + neoPixels.SetLed(i, Color.Purple); + neoPixels.Show(); + Thread.Sleep(100); } - else - { - _ws2812.SetLed(i, Color.Blue); - } - _ws2812.Show(); } - return Task.CompletedTask; } } } \ No newline at end of file From 2423b8a2afe06ff70be7a5babb5bb302c265e9be Mon Sep 17 00:00:00 2001 From: Adrian Stevens Date: Fri, 4 Oct 2024 13:38:42 -0700 Subject: [PATCH 6/7] Minor cleanup --- .../Leds.Ws2812/Driver/Ws2812.cs | 7 ------- .../Leds.Ws2812/Samples/Ws2812_Sample/MeadowApp.cs | 8 +++----- 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Driver/Ws2812.cs b/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Driver/Ws2812.cs index a57df81f41..f9764a07e4 100644 --- a/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Driver/Ws2812.cs +++ b/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Driver/Ws2812.cs @@ -53,13 +53,6 @@ public SpiClockConfiguration.Mode SpiBusMode /// public int NumberOfLeds => numberOfLeds; - /// - /// SPI Communication bus used to communicate with the peripheral - /// - protected ISpiCommunications spiComms; - - readonly IDigitalOutputPort? chipSelectPort; - /// /// Creates a new WS2812 object /// diff --git a/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Samples/Ws2812_Sample/MeadowApp.cs b/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Samples/Ws2812_Sample/MeadowApp.cs index 6712b0c5b2..53b831e2e3 100644 --- a/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Samples/Ws2812_Sample/MeadowApp.cs +++ b/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Samples/Ws2812_Sample/MeadowApp.cs @@ -1,9 +1,7 @@ using Meadow; using Meadow.Devices; using Meadow.Foundation.Leds; -using System.Threading; -using Meadow.Units; -using System; +using System.Threading.Tasks; namespace MeadowApp { @@ -28,9 +26,9 @@ public override async Task Run() for (int i = 0; i < neoPixels.NumberOfLeds; i++) { neoPixels.SetAllLeds(Color.Black); - neoPixels.SetLed(i, Color.Purple); + neoPixels.SetLed(i, Color.Blue); neoPixels.Show(); - Thread.Sleep(100); + await Task.Delay(100); } } } From cb070ea1ab26c348c6273af49a411898194a2924 Mon Sep 17 00:00:00 2001 From: Adrian Stevens Date: Fri, 4 Oct 2024 13:40:01 -0700 Subject: [PATCH 7/7] Add docs marker tags to sample --- .../Leds.Ws2812/Samples/Ws2812_Sample/MeadowApp.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Samples/Ws2812_Sample/MeadowApp.cs b/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Samples/Ws2812_Sample/MeadowApp.cs index 53b831e2e3..2f47c9580f 100644 --- a/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Samples/Ws2812_Sample/MeadowApp.cs +++ b/Source/Meadow.Foundation.Peripherals/Leds.Ws2812/Samples/Ws2812_Sample/MeadowApp.cs @@ -7,6 +7,8 @@ namespace MeadowApp { public class MeadowApp : App { + // + private Ws2812 neoPixels; private readonly int ledCount = 24; @@ -32,5 +34,7 @@ public override async Task Run() } } } + + // } } \ No newline at end of file