Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: WS281X.cpp
- Revision:
- 28:b452e097da53
- Parent:
- 27:bc79f444883b
- Child:
- 29:a362df191524
diff -r bc79f444883b -r b452e097da53 WS281X.cpp
--- a/WS281X.cpp Tue Sep 06 22:12:59 2016 +0000
+++ b/WS281X.cpp Wed Sep 07 21:07:17 2016 +0000
@@ -18,6 +18,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* Rev 0.97 2016-09-07
+ * Rev 0.98 2016-09-08
*/
#include "WS281X.h"
@@ -56,7 +57,7 @@
}
//----------------------------------------------------------------------------
-WS281X::WS281X(PinName wirePin, PinMode pinMode, int maxPixels, RGBOrder rgbOrder)
+WS281X::WS281X(PinName wirePin, PinMode pinMode, int maxPixels, RGBOrder order)
: _wirePin(wirePin), _gpio(), _buf_owner(false)
{
gpio_init_inout(&_gpio, wirePin, PIN_OUTPUT, pinMode, 0);
@@ -64,13 +65,13 @@
pin_mode_ex(wirePin, pinMode);
#endif
- setRGBOrder(rgbOrder);
+ rgbOrder(order);
_dummyPixel = 0;
setPixelBuffer(0, maxPixels);
}
WS281X::WS281X(PinName wirePin, PinMode pinMode,
- RGBColor *buffer, int maxPixels, RGBOrder rgbOrder)
+ RGBColor *buffer, int maxPixels, RGBOrder order)
: _wirePin(wirePin), _gpio(), _buf_owner(false)
{
gpio_init_inout(&_gpio, wirePin, PIN_OUTPUT, pinMode, 0);
@@ -78,7 +79,7 @@
pin_mode_ex(wirePin, pinMode);
#endif
- setRGBOrder(rgbOrder);
+ rgbOrder(order);
_dummyPixel = 0;
setPixelBuffer(buffer, maxPixels);
}
@@ -152,19 +153,20 @@
}
#endif
-void WS281X::setRGBOrder(RGBOrder rgbOrder)
+WS281X::RGBOrder WS281X::rgbOrder(WS281X::RGBOrder order)
{
- _rgbOrder = rgbOrder;
- switch(_rgbOrder)
+ switch(order)
{
- case RGB: _1st = 0; _2nd = 1; _3rd = 2; break;
- case RBG: _1st = 0; _2nd = 2; _3rd = 1; break;
- case GRB: _1st = 1; _2nd = 0; _3rd = 2; break;
- case GBR: _1st = 2; _2nd = 0; _3rd = 1; break;
- case BRG: _1st = 1; _2nd = 2; _3rd = 0; break;
- case BGR: _1st = 2; _2nd = 1; _3rd = 0; break;
- default: _1st = 0; _2nd = 1; _3rd = 2; break;
+ case RGB: _1st = 0; _2nd = 1; _3rd = 2; _rgbOrder = RGB; break;
+ case RBG: _1st = 0; _2nd = 2; _3rd = 1; _rgbOrder = RGB; break;
+ case GRB: _1st = 1; _2nd = 0; _3rd = 2; _rgbOrder = GRB; break;
+ case GBR: _1st = 2; _2nd = 0; _3rd = 1; _rgbOrder = GBR; break;
+ case BRG: _1st = 1; _2nd = 2; _3rd = 0; _rgbOrder = BRG; break;
+ case BGR: _1st = 2; _2nd = 1; _3rd = 0; _rgbOrder = BGR; break;
+ default:
+ break;
}
+ return _rgbOrder;
}
//#define _nop1() __nop()
@@ -413,7 +415,7 @@
}
// 指定色を指定位置のピクセルから指定サイズ分書き込む
-void WS281X::fillPixels(const RGBColor color, int index, int len)
+void WS281X::fillPixels(int index, const RGBColor color, int len)
{
if (_pixels && len > 0 && (uint16_t)index < _numPixels)
{
@@ -425,9 +427,9 @@
}
// 指定位置のピクセルから指定色を指定サイズ分書き込む
-void WS281X::fillPixels(const HSVColor color, int index, int len)
+void WS281X::fillPixels(int index, const HSVColor color, int len)
{
- fillPixels(color, index, len);
+ fillPixels(index, color, len);
}
// 先頭から指定サイズ分のブロックをバッファの最後までコピーする
@@ -462,6 +464,50 @@
}
}
+void WS281X::makeGradation(int index, RGBColor from, RGBColor to, int len)
+{
+ if (!_pixels || len < 1 || index >= _numPixels || (index + len) < 0)
+ return;
+
+ int end = len;
+ if (index + end > _numPixels)
+ end = _numPixels - index;
+
+ RGBColor color;
+ RGBColor *dest = _pixels;
+ if (index > 0)
+ dest += index;
+ for (int i = (index < 0) ? -index : 0; i < end; ++i)
+ {
+ int j = len - i;
+ color.red = ((from.red * j) + (to.red * i)) / len;
+ color.green = ((from.green * j) + (to.green * i)) / len;
+ color.blue = ((from.blue * j) + (to.blue * i)) / len;
+ *dest++ = GammaColor(color);
+ }
+}
+
+void WS281X::makeRainbow(int index, HSVColor color, int len, int direction)
+{
+ if (!_pixels || len < 1 || index >= _numPixels || (index + len) < 0)
+ return;
+
+ int end = len;
+ if (index + end > _numPixels)
+ end = _numPixels - index;
+
+ HSVColor hsv(color);
+ RGBColor *dest = _pixels;
+ if (index > 0)
+ dest += index;
+ direction = (direction >= 0) ? -3600 : 3600;
+ for (int i = (index < 0) ? -index : 0; i < end; ++i)
+ {
+ hsv.hue = color.hue + direction * i / len;
+ *dest++ = GammaColor(hsv);
+ }
+}
+
// 指定色でバッファを埋めた後表示
void WS281X::show(const RGBColor color)
{