Writing a Good C# Class

You may skip this step if you simply want to download the pre-compiled DLL files (NeoPixel.dll and NeoPixelNative.dll) from the download page. The DLL files will not work until the firmware is updated as well (using MFDeploy to load ER_CONFIG and ER_FLASH), which is covered in the previous Building Firmware page.
So far our custom function takes raw data and sends the data in sequence to the NeoPixels. It gets the job done with a minimum amount of bells and whistles.

But I thought it would be nice if we can represent each NeoPixel as a class object named NeoPixel, with three values that represent the R G and B color intensities. I used overloads so you can create an instance of NeoPixel using many data types.

I then created another class that organizes a chain of NeoPixels, named it "NeoPixelChain". It inherits from a generic list interface, so you can access it as it was an array of NeoPixels. You can then call "Write" on the entire chain, and the byte sequence will be automatically rearranged into the GRB sequence that the WS2812 expects, then sent out of the output pin using our native function.

(note: this is just to illustrate "inheritance", which is a feature of object oriented programming languages such as C#, Java, C++, and many more)

I also wrote a few more wrapper functions under the class name "NeoPixelWriter". The most impressive one can directly draw a bitmap image directly onto a grid of NeoPixels (such as the NeoPixel 8x8)

(note: NeoPixelWriter makes heavy use of "overloading")
(note: although "NeoPixelWriter" has the word "Writer" in it, it is does not inherit from "Stream" or even resemble "Stream" at all, sorry about that)

Remember, on the Netduino, you can easily load a bitmap from a SD card or download from the network, or draw one during runtime using graphic functions. Have fun with this one!

All the files are provided on the download page, see the navigation panel.

All documentation about the API is done within the code, which means Visual Studio can automatically show you the documentation when:
  • you use "Intellisense" (aka autocomplete)
  • use "Object Browser"
But in order to do this, you need both the DLL and documentation XML files (also provided in downloads) together.

Below is a class diagram of my "NeoPixel" namespace.
Here is "Object Browser" in action, showing you how to view the code documentation
Last updated on May 04, 2015 Published on Sep 12, 2013