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:
- 27:bc79f444883b
- Parent:
- 24:f93a61e727a3
- Child:
- 28:b452e097da53
--- a/WS281X.cpp Sun Sep 04 19:00:24 2016 +0000
+++ b/WS281X.cpp Tue Sep 06 22:12:59 2016 +0000
@@ -16,6 +16,8 @@
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * Rev 0.97 2016-09-07
*/
#include "WS281X.h"
@@ -34,6 +36,26 @@
// TARGET_LPC824
//----------------------------------------------------------------------------
+// 指定されたバッファの先頭からblock_size分をbuf_sizeが満杯になるまで繰り返しコピーする
+template <class T>
+static void repeat_buffer(T *buffer, int buf_size, int block_size = 1)
+{
+ if (buffer && block_size > 0 && (uint16_t)block_size < buf_size)
+ {
+ T *dest = buffer + block_size;
+ int left = buf_size - block_size;
+ while (left > block_size)
+ {
+ memcpy(dest, buffer, block_size * sizeof(T));
+ dest += block_size;
+ left -= block_size;
+ block_size <<= 1; // 次回は2倍のサイズの転送
+ }
+ memcpy(dest, buffer, left * sizeof(T));
+ }
+}
+
+//----------------------------------------------------------------------------
WS281X::WS281X(PinName wirePin, PinMode pinMode, int maxPixels, RGBOrder rgbOrder)
: _wirePin(wirePin), _gpio(), _buf_owner(false)
{
@@ -48,7 +70,7 @@
}
WS281X::WS281X(PinName wirePin, PinMode pinMode,
- RGBColor *Buffer, int maxPixels, RGBOrder rgbOrder)
+ RGBColor *buffer, int maxPixels, RGBOrder rgbOrder)
: _wirePin(wirePin), _gpio(), _buf_owner(false)
{
gpio_init_inout(&_gpio, wirePin, PIN_OUTPUT, pinMode, 0);
@@ -58,7 +80,7 @@
setRGBOrder(rgbOrder);
_dummyPixel = 0;
- setPixelBuffer(Buffer, maxPixels);
+ setPixelBuffer(buffer, maxPixels);
}
WS281X::~WS281X()
@@ -67,13 +89,13 @@
delete[] _pixels;
}
-void WS281X::setPixelBuffer(RGBColor *Buffer, int maxPixels)
+void WS281X::setPixelBuffer(RGBColor *buffer, int maxPixels)
{
if (_buf_owner && _pixels)
delete[] _pixels;
_buf_owner = false;
- _pixels = Buffer;
+ _pixels = buffer;
_maxPixels = (maxPixels < 0) ? 0 : (maxPixels > MAX_PIXELS) ? MAX_PIXELS : maxPixels;
if (!_pixels && _maxPixels > 0)
@@ -85,9 +107,11 @@
clear();
}
-void WS281X::setNumPixels(int numPixels)
+int WS281X::numPixels(int value)
{
- _numPixels = (numPixels < 0) ? 0 : (numPixels > _maxPixels) ? _maxPixels : numPixels;
+ if (value >= 0)
+ _numPixels = (value > _maxPixels) ? _maxPixels : value;
+ return _numPixels;
}
#if defined(TARGET_STM)
@@ -364,7 +388,7 @@
}
// 指定位置のピクセルへ色配列を指定サイズ分をコピーする
-void WS281X::setColor(int index, RGBColor *color, int len)
+void WS281X::setPixels(int index, RGBColor *color, int len)
{
if (_pixels && len > 0 && (uint16_t)index < _numPixels)
{
@@ -374,7 +398,7 @@
}
}
-void WS281X::setColor(int index, HSVColor *color, int len)
+void WS281X::setPixels(int index, HSVColor *color, int len)
{
if (_pixels && len > 0 && (uint16_t)index < _numPixels)
{
@@ -388,51 +412,45 @@
}
}
-// 指定位置のピクセルから指定色を指定サイズ分書き込む
-void WS281X::fillColor(int index, const RGBColor color, int len)
+// 指定色を指定位置のピクセルから指定サイズ分書き込む
+void WS281X::fillPixels(const RGBColor color, int index, int len)
{
if (_pixels && len > 0 && (uint16_t)index < _numPixels)
{
if (index + len > _numPixels)
len = _numPixels - index;
- RGBColor *dest = &_pixels[index];
- do
- {
- *dest++ = color;
- } while (--len);
+ _pixels[index] = color;
+ repeat_buffer<RGBColor>(_pixels + index, len, 1);
}
}
+// 指定位置のピクセルから指定色を指定サイズ分書き込む
+void WS281X::fillPixels(const HSVColor color, int index, int len)
+{
+ fillPixels(color, index, len);
+}
+
// 先頭から指定サイズ分のブロックをバッファの最後までコピーする
-void WS281X::repeatBlock(int block_size)
+void WS281X::repeatPixels(int block_size)
{
if (_pixels && block_size > 0 && block_size < _numPixels)
{
- RGBColor *dest = _pixels + block_size;
- int left = _numPixels - block_size;
- while (left > block_size)
- {
- memcpy(dest, _pixels, block_size * sizeof(_pixels[0]));
- dest += block_size;
- left -= block_size;
- block_size <<= 1; // 次回は2倍のサイズの転送
- }
- memcpy(dest, _pixels, left * sizeof(_pixels[0]));
+ repeat_buffer<RGBColor>(_pixels, _numPixels, block_size);
}
}
-void WS281X::repeatBlock(RGBColor *source, int size)
+void WS281X::repeatPixels(RGBColor *source, int size)
{
if (_pixels && source && size > 0)
{
if (size > _numPixels)
size = _numPixels;
memcpy(_pixels, source, size * sizeof(_pixels[0]));
- repeatBlock(size);
+ repeat_buffer<RGBColor>(_pixels, _numPixels, size);
}
}
-void WS281X::repeatBlock(HSVColor *source, int size)
+void WS281X::repeatPixels(HSVColor *source, int size)
{
if (_pixels && source && size > 0)
{
@@ -440,17 +458,7 @@
size = _numPixels;
for (int i = 0; i < size; ++i)
_pixels[i] = *source++;
- repeatBlock(size);
- }
-}
-
-// 指定色でバッファを埋める
-void WS281X::clear(const RGBColor color)
-{
- if (_pixels)
- {
- _pixels[0] = color;
- repeatBlock(1);
+ repeat_buffer<RGBColor>(_pixels, _numPixels, size);
}
}