Remarks
Apa102 | |
---|---|
Status | |
Source code | GitHub |
Datasheet(s) | GitHub |
NuGet package |
Code Example
Apa102 apa102;
int numberOfLeds = 256;
float maxBrightness = 0.25f;
public override Task Initialize()
{
Resolver.Log.Info("Initialize...");
apa102 = new Apa102(Device.CreateSpiBus(Apa102.DefaultSpiBusSpeed), numberOfLeds, Apa102.PixelOrder.BGR);
return base.Initialize();
}
public override Task Run()
{
apa102.Clear();
apa102.SetLed(index: 0, color: Color.Red, brightness: 0.5f);
apa102.SetLed(index: 1, color: Color.Purple, brightness: 0.6f);
apa102.SetLed(index: 2, color: Color.Blue, brightness: 0.7f);
apa102.SetLed(index: 3, color: Color.Green, brightness: 0.8f);
apa102.SetLed(index: 4, color: Color.Yellow, brightness: 0.9f);
apa102.SetLed(index: 5, color: Color.Orange, brightness: 1.0f);
apa102.Show();
Apa102Tests();
return Task.CompletedTask;
}
Sample project(s) available on GitHub
Wiring Example
To wire a Apa102 to your Meadow board, connect the following:
Apa102 | Meadow Pin |
---|---|
GND | GND |
CI | SCK |
DI | MOSI |
VCC | 5V |
It should look like the following diagram:
Characteristic | Locus |
---|---|
Inheritance | System.Object > Apa102 |
Implements | IApa102 IGraphicsDisplay IPixelBuffer |
Namespace | Meadow.Foundation.Leds |
Assembly | Apa102.dll |
Syntax
public class Apa102 : object, IApa102, IGraphicsDisplay, IPixelBuffer
Constructors
Apa102(ISpiBus, IPin, Int32, Apa102.PixelOrder)
Creates a new APA102 object
Declaration
public Apa102(ISpiBus spiBus, IPin chipSelectPin, int numberOfLeds, Apa102.PixelOrder pixelOrder = Apa102.PixelOrder.BGR)
Parameters
Type | Name | Description |
---|---|---|
ISpiBus | spiBus | SPI bus |
IPin | chipSelectPin | Chip select pin |
System.Int32 | numberOfLeds | Number of leds |
Apa102.PixelOrder | pixelOrder | Pixel color order |
Apa102(ISpiBus, IPin, Int32, Int32, Apa102.PixelOrder)
Creates a new APA102 object
Declaration
public Apa102(ISpiBus spiBus, IPin chipSelectPin, int width, int height, Apa102.PixelOrder pixelOrder = Apa102.PixelOrder.BGR)
Parameters
Type | Name | Description |
---|---|---|
ISpiBus | spiBus | SPI bus |
IPin | chipSelectPin | Chip select pin |
System.Int32 | width | Width of led array |
System.Int32 | height | Height of led array |
Apa102.PixelOrder | pixelOrder | Pixel color order |
Apa102(ISpiBus, Int32, Apa102.PixelOrder, IDigitalOutputPort)
Creates a new APA102 object
Declaration
public Apa102(ISpiBus spiBus, int numberOfLeds, Apa102.PixelOrder pixelOrder = Apa102.PixelOrder.BGR, IDigitalOutputPort chipSelectPort = null)
Parameters
Type | Name | Description |
---|---|---|
ISpiBus | spiBus | SPI bus |
System.Int32 | numberOfLeds | Number of leds |
Apa102.PixelOrder | pixelOrder | Pixel color order |
IDigitalOutputPort | chipSelectPort | SPI chip select port (optional) |
Apa102(ISpiBus, Int32, Int32, Apa102.PixelOrder, IDigitalOutputPort)
Creates a new APA102 object
Declaration
public Apa102(ISpiBus spiBus, int width, int height, Apa102.PixelOrder pixelOrder = Apa102.PixelOrder.BGR, IDigitalOutputPort chipSelectPort = null)
Parameters
Type | Name | Description |
---|---|---|
ISpiBus | spiBus | SPI bus |
System.Int32 | width | Width of led array |
System.Int32 | height | Height of led array |
Apa102.PixelOrder | pixelOrder | Pixel color order |
IDigitalOutputPort | chipSelectPort | SPI chip select port (optional) |
Fields
DefaultSpiBusSpeed
Default SPI bus speed
Declaration
public static Frequency DefaultSpiBusSpeed
Field Value
Type | Description |
---|---|
Frequency |
spiPeripheral
SpiPeripheral object
Declaration
protected ISpiPeripheral spiPeripheral
Field Value
Type | Description |
---|---|
ISpiPeripheral |
Properties
BitDepth
The color bit depth of the display
Declaration
public int BitDepth { get; }
Property Value
Type | Description |
---|---|
System.Int32 |
Brightness
Brightness used for LEDs Default is 0.5
Declaration
public float Brightness { get; set; }
Property Value
Type | Description |
---|---|
System.Single |
Buffer
The display buffer - not implemented for the Apa102 driver
Declaration
public byte[] Buffer { get; }
Property Value
Type | Description |
---|---|
System.Byte[] |
ByteCount
The size of the display buffer in bytes
Declaration
public int ByteCount { get; }
Property Value
Type | Description |
---|---|
System.Int32 |
ColorMode
Color mode of leds - 24bpp
Declaration
public ColorMode ColorMode { get; }
Property Value
Type | Description |
---|---|
ColorMode |
Height
Height of pixel array
Declaration
public int Height { get; }
Property Value
Type | Description |
---|---|
System.Int32 |
NumberOfLeds
Total number of leds
Declaration
public int NumberOfLeds { get; }
Property Value
Type | Description |
---|---|
System.Int32 |
PixelBuffer
PixelBuffer representation of the Apa102
Declaration
public IPixelBuffer PixelBuffer { get; }
Property Value
Type | Description |
---|---|
IPixelBuffer |
SupportedColorModes
The Color mode supported by the display
Declaration
public ColorMode SupportedColorModes { get; }
Property Value
Type | Description |
---|---|
ColorMode |
Width
Width of pixel array
Declaration
public int Width { get; }
Property Value
Type | Description |
---|---|
System.Int32 |
Methods
Clear()
Clear the display buffer
Declaration
public void Clear()
Clear(Boolean)
Clear the display buffer
Declaration
public void Clear(bool update = false)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | update | Update the leds if true |
DrawPixel(Int32, Int32, Color)
Draw pixel at location
Declaration
public void DrawPixel(int x, int y, Color color)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | x | x position of pixel |
System.Int32 | y | y position of pixel |
Color | color | color of pixel |
DrawPixel(Int32, Int32, Boolean)
Draw pixel at a location Primarily used for monochrome displays, prefer overload that accepts a Color
Declaration
public void DrawPixel(int x, int y, bool enabled)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | x | x position of pixel |
System.Int32 | y | y position of pixel |
System.Boolean | enabled | if true draw white, if false draw black |
Fill(Color)
Fill the display buffer with a color
Declaration
public void Fill(Color color)
Parameters
Type | Name | Description |
---|---|---|
Color | color | The fill color |
Fill(Color, Boolean)
Fill the entire display buffer with a color
Declaration
public void Fill(Color clearColor, bool updateDisplay = false)
Parameters
Type | Name | Description |
---|---|---|
Color | clearColor | color to fill |
System.Boolean | updateDisplay | update after fill |
Fill(Int32, Int32, Int32, Int32, Color)
Fill a color in the specified region
Declaration
public void Fill(int x, int y, int width, int height, Color fillColor)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | x | x position |
System.Int32 | y | y position |
System.Int32 | width | width to fill |
System.Int32 | height | height to fill |
Color | fillColor | color to fill |
GetPixel(Int32, Int32)
Get the color of pixel at at given coordinate
Declaration
public Color GetPixel(int x, int y)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | x | X position in pixels |
System.Int32 | y | Y position in pixels |
Returns
Type | Description |
---|---|
Color |
InvertPixel(Int32, Int32)
Invert pixel at location
Declaration
public void InvertPixel(int x, int y)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | x | x position of pixel |
System.Int32 | y | y position of pixel |
SetLed(Int32, Color)
Set the color of the specified LED using the global brightness balue
Declaration
public virtual void SetLed(int index, Color color)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | index | Index of the LED to change |
Color | color | The color |
SetLed(Int32, Color, Single)
Set the color of the specified LED
Declaration
public virtual void SetLed(int index, Color color, float brightness = 1F)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | index | Index of the LED to change |
Color | color | The color |
System.Single | brightness | The brighrness 0.0 - 1.0f |
SetLed(Int32, Byte[])
Set the color of the specified LED
Declaration
public virtual void SetLed(int index, byte[] rgb)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | index | Index of the LED to change |
System.Byte[] | rgb | Byte array representing the color RGB values. byte[0] = Red, byte[1] = Green, byte[2] = Blue |
SetLed(Int32, Byte[], Single)
Set the color of the specified LED
Declaration
public virtual void SetLed(int index, byte[] rgb, float brightness = 1F)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | index | Index of the LED to change |
System.Byte[] | rgb | Byte array representing the color RGB values. byte[0] = Red, byte[1] = Green, byte[2] = Blue |
System.Single | brightness | The brighrness 0.0 - 1.0f |
SetPixel(Int32, Int32, Color)
Set the pixel for a given coordindate
Declaration
public void SetPixel(int x, int y, Color color)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | x | X position in pixels |
System.Int32 | y | Y position in pixels |
Color | color | Color of pixel |
Show()
Transmit the buffer to the LEDs
Declaration
public void Show()
Show(Int32, Int32, Int32, Int32)
Update APA102 with data in display buffer
Declaration
public void Show(int left, int top, int right, int bottom)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | left | left |
System.Int32 | top | top |
System.Int32 | right | right |
System.Int32 | bottom | bottom |
WriteBuffer(Int32, Int32, IPixelBuffer)
Draw a buffer at the specified location
Declaration
public void WriteBuffer(int x, int y, IPixelBuffer displayBuffer)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | x | x position |
System.Int32 | y | y position |
IPixelBuffer | displayBuffer | buffer to draw |