/
DotstarWing.cs
147 lines (128 loc) · 5.66 KB
/
DotstarWing.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using Meadow.Foundation.Leds;
using Meadow.Hardware;
using Meadow.Peripherals.Displays;
using static Meadow.Foundation.Leds.Apa102;
namespace Meadow.Foundation.FeatherWings
{
/// <summary>
/// Represents Adafruit's Dotstar feather wing 12x6
/// </summary>
public class DotstarWing : IPixelDisplay
{
readonly Apa102 ledMatrix;
/// <summary>
/// Returns the color mode
/// </summary>
public ColorMode ColorMode => ColorMode.Format12bppRgb444;
/// <summary>
/// Color modes supported by the device
/// </summary>
public ColorMode SupportedColorModes => ColorMode.Format12bppRgb444;
/// <summary>
/// Returns the width of the RGB LED matrix
/// </summary>
public int Width => 12;
/// <summary>
/// Returns the height of the RGB LED matrix
/// </summary>
public int Height => 6;
/// <summary>
/// Get/Sets the RGB LED Matrix brightness
/// </summary>
public float Brightness
{
get => ledMatrix.Brightness;
set => ledMatrix.Brightness = value;
}
/// <summary>
/// Get the offscreen pixel buffer
/// </summary>
public IPixelBuffer PixelBuffer => ledMatrix.PixelBuffer;
/// <summary>
/// Creates a DotstarWing driver
/// </summary>
/// <param name="spiBus">The SPI bus used by the Dotstar Wing</param>
public DotstarWing(ISpiBus spiBus)
{
ledMatrix = new Apa102(spiBus, 72, PixelOrder.BGR);
}
/// <summary>
/// Clear the RGB LED Matrix buffer
/// </summary>
/// <param name="updateDisplay">If true, update the display, if false, only clear the buffer</param>
public void Clear(bool updateDisplay = false)
{
ledMatrix.Clear(updateDisplay);
}
/// <summary>
/// Turn on an RGB LED with the specified color on (x,y) coordinates
/// </summary>
/// <param name="x">The x position in pixels 0 indexed from the left</param>
/// <param name="y">The y position in pixels 0 indexed from the top</param>
/// <param name="color">The color to draw normalized to black/off or white/on</param>
public void DrawPixel(int x, int y, Color color)
{
int minor = x;
int major = Height - 1 - y;
int pixelOffset = (major * Width) + minor;
if (pixelOffset >= 0 && pixelOffset < Height * Width)
{
ledMatrix.SetLed(pixelOffset, color);
}
}
/// <summary>
/// Turn on a LED on (x,y) coordinates
/// </summary>
/// <param name="x">The x position in pixels 0 indexed from the left</param>
/// <param name="y">The y position in pixels 0 indexed from the top</param>
/// <param name="colored">Led is on if true, off if false</param>
public void DrawPixel(int x, int y, bool colored)
=> DrawPixel(x, y, colored ? Color.White : Color.Black);
/// <summary>
/// Invert the color of the pixel at the given location
/// </summary>
/// <param name="x">The x position in pixels 0 indexed from the left</param>
/// <param name="y">The y position in pixels 0 indexed from the top</param>
public void InvertPixel(int x, int y)
=> ledMatrix.InvertPixel(x, y);
/// <summary>
/// Draw a buffer to the display
/// </summary>
/// <param name="x">The x position in pixels 0 indexed from the left</param>
/// <param name="y">The y position in pixels 0 indexed from the top</param>
/// <param name="displayBuffer">The display buffer to draw to the CharlieWing</param>
public void WriteBuffer(int x, int y, IPixelBuffer displayBuffer)
=> ledMatrix.WriteBuffer(x, y, displayBuffer);
/// <summary>
/// Fill the display buffer to a normalized color
/// </summary>
/// <param name="fillColor">The clear color which will be normalized to black/off or white/on</param>
/// <param name="updateDisplay">Force a display update if true, false to clear the buffer</param>
public void Fill(Color fillColor, bool updateDisplay = false)
=> ledMatrix.Fill(fillColor, updateDisplay);
/// <summary>
/// Fill the display
/// </summary>
/// <param name="x">The x position in pixels 0 indexed from the left</param>
/// <param name="y">The y position in pixels 0 indexed from the top</param>
/// <param name="width">The width to fill in pixels</param>
/// <param name="height">The height to fill in pixels</param>
/// <param name="fillColor">The fillColor color which will be normalized to black/off or white/on</param>
public void Fill(int x, int y, int width, int height, Color fillColor)
=> ledMatrix.Fill(x, y, width, height, fillColor);
/// <summary>
/// Update the display from the offscreen buffer
/// </summary>
public void Show()
=> ledMatrix.Show();
/// <summary>
/// Update a region of the display from the offscreen buffer
/// </summary>
/// <param name="left">The left bounding position in pixels</param>
/// <param name="top">The top bounding position in pixels</param>
/// <param name="right">The right bounding position in pixels</param>
/// <param name="bottom">The bottom bounding position in pixels</param>
public void Show(int left, int top, int right, int bottom)
=> ledMatrix.Show(left, top, right, bottom);
}
}