NeoPixel

NeoPixels are Adafruit’s brand of LED strips that can be individually controlled. The MetaWear firmware can communicate with up to 3 strands on the WS2811 protocol with the INeoPixel interface.

using MbientLab.MetaWear.Peripheral;

INeoPixel neopixel = metawear.GetModule<INeoPixel>();

Initializing a Strand

Before we can interact with a NeoPixel strand, we first need to initialize memory on the MetaWear board to store NeoPixel information. To initialize the strand, call the InitializeStrand method. When calling the function, you will need to take into consideration these parameters:

  • Operating frequency (either 800 Hz or 400 Hz)
  • How many LEDs to use
  • What color ordering the strand requires (RGB, GRB, etc.)
  • Which gpio pin the data wire is connected to
using MbientLab.MetaWear.Peripheral.NeoPixel;

// initialize memory for a strand #2 that operates at 800KHz (fast), has 60 leds,
// uses rbg color order, and has a data wire connected to gpio pin 1
IStrand strand = neopixel.InitializeStrand(2, ColorOrdering.WS2811_RBG, StrandSpeed.Fast, 1, 60);

The function returns an IStrand object and can be retrieved at a later time by calling LookupStrand with the same strand number used for InitializeStrand.

strand = neopixel.LookupStrand(2);

Setting Colors

When you have received your strand object, you can start turning on the LEDs using SetRgb. Any changes to the led state will immediately be propogated to the strand unless a hold is enabled with the Hold function. When Release is called, all the LED changes will appear simultaneously.

// Enable hold to lock the strand state
strand.Hold();

strand.SetRgb(0, 255, 0, 0);
strand.SetRgb(1, 0, 255, 0);
strand.SetRgb(2, 0, 0, 255);

// Disable hold, which will now update the strand with any state changes
strand.Release();

Pattern Rotation

Once you have a color pattern on your strand, you can rotate the pattern using Rotate. By default, the pattern will rotate indefinetly however you can set a limit by setting the repetitions parameter.

If an indefinite rotation has started, call StopRotation to manually terminate the rotation.

using MbientLab.MetaWear.Peripheral.NeoPixel;

// Rotate pattern 16 times, moving away from the board with a delay of 500ms
strand.Rotate(RotationDirection.Away, 500, repetitions: 16);

// Rotate pattern indefinitely, moving towards the board with a delay of 250ms
strand.Rotate(RotationDirection.Towards, 250);

// Stop pattern rotation
strand.StopRotation();

Clean Up

When you are down using your NeoPixel strand, make sure you turn off the LEDs and deallocate the memory used for that particular strand. You can discard the IStrand object after calling Free.

// turn off leds from index [0, 59] (60 leds)
strand.Clear(0, 59);

// free
strand.Free();