TLIGHT_PRODUCTS / WS281X
Committer:
mutech
Date:
Wed Sep 07 22:21:27 2016 +0000
Revision:
29:a362df191524
Parent:
28:b452e097da53
Child:
30:59b70f91b471
WS2811/WS2812 support Library;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mutech 27:bc79f444883b 1 /* PixelBuffer.h
mutech 27:bc79f444883b 2 * mbed Microcontroller Library
mutech 27:bc79f444883b 3 * Copyright (c) 2016 muetch, t.kuroki
mutech 27:bc79f444883b 4 * Allrights reserved.
mutech 27:bc79f444883b 5 *
mutech 27:bc79f444883b 6 * Rev 0.97 2016-09-07
mutech 28:b452e097da53 7 * Rev 0.98 2016-09-08
mutech 27:bc79f444883b 8 */
mutech 27:bc79f444883b 9
mutech 27:bc79f444883b 10 #pragma once
mutech 27:bc79f444883b 11
mutech 27:bc79f444883b 12 #ifndef PIXELBUFFER_H
mutech 27:bc79f444883b 13 #define PIXELBUFFER_H
mutech 27:bc79f444883b 14
mutech 28:b452e097da53 15 #ifdef _WIN32
mutech 28:b452e097da53 16 #include <stdint.h>
mutech 28:b452e097da53 17 #include <string.h>
mutech 28:b452e097da53 18 #else
mutech 27:bc79f444883b 19 #include "mbed.h"
mutech 28:b452e097da53 20 #endif
mutech 27:bc79f444883b 21 #include "ColorLib.h"
mutech 27:bc79f444883b 22
mutech 27:bc79f444883b 23 //----------------------------------------------------------------------------
mutech 27:bc79f444883b 24 #ifndef MAX_PIXELS
mutech 27:bc79f444883b 25 #define MAX_PIXELS 170
mutech 27:bc79f444883b 26 #endif
mutech 27:bc79f444883b 27
mutech 27:bc79f444883b 28 #ifndef nullptr
mutech 27:bc79f444883b 29 #define nullptr (0)
mutech 27:bc79f444883b 30 #endif
mutech 27:bc79f444883b 31
mutech 27:bc79f444883b 32 //----------------------------------------------------------------------------
mutech 27:bc79f444883b 33 /**
mutech 27:bc79f444883b 34 * RGBPixels
mutech 27:bc79f444883b 35 */
mutech 27:bc79f444883b 36 class RGBPixels
mutech 27:bc79f444883b 37 {
mutech 27:bc79f444883b 38 public:
mutech 27:bc79f444883b 39 /**
mutech 27:bc79f444883b 40 * Initializes the addressable led bus
mutech 27:bc79f444883b 41 *
mutech 27:bc79f444883b 42 * @param Buffer - The Pixel array buffer address.
mutech 27:bc79f444883b 43 * @param maxPixels - Number of the addressable leds
mutech 27:bc79f444883b 44 */
mutech 27:bc79f444883b 45 RGBPixels(RGBColor *buffer = nullptr, int maxPixels = MAX_PIXELS);
mutech 27:bc79f444883b 46 RGBPixels(int maxPixels = MAX_PIXELS);
mutech 27:bc79f444883b 47 ~RGBPixels();
mutech 27:bc79f444883b 48
mutech 27:bc79f444883b 49 void setPixelBuffer(RGBColor *buffer, int maxPixels);
mutech 27:bc79f444883b 50 int maxPixels() { return _maxPixels; }
mutech 27:bc79f444883b 51 int numPixels(int value = -1);
mutech 27:bc79f444883b 52
mutech 27:bc79f444883b 53 void setPixels(int index, RGBColor *color, int len);
mutech 27:bc79f444883b 54 void setPixels(int index, HSVColor *color, int len);
mutech 27:bc79f444883b 55 void setPixels(RGBColor *color, int len) { setPixels(0, color, len); }
mutech 27:bc79f444883b 56 void setPixels(HSVColor *color, int len) { setPixels(0, color, len); }
mutech 28:b452e097da53 57
mutech 29:a362df191524 58 void fillPixels(int index, const RGBColor color, int len);
mutech 29:a362df191524 59 void fillPixels(int index, const HSVColor color, int len);
mutech 28:b452e097da53 60 void fillPixels(int index, const int color, int len) { fillPixels(index, (RGBColor)color, len); }
mutech 28:b452e097da53 61 void fillPixels(const RGBColor color, int len = MAX_PIXELS) { fillPixels(0, color, len); }
mutech 28:b452e097da53 62 void fillPixels(const HSVColor color, int len = MAX_PIXELS) { fillPixels(0, color, len); }
mutech 28:b452e097da53 63 void fillPixels(const int color, int len = MAX_PIXELS) { fillPixels(0, (RGBColor)color, len); }
mutech 28:b452e097da53 64
mutech 27:bc79f444883b 65 void clear(const RGBColor color) { fillPixels(color); }
mutech 27:bc79f444883b 66 void clear(const HSVColor color) { fillPixels(color); }
mutech 27:bc79f444883b 67 void clear(const int color = 0) { fillPixels((RGBColor)color); }
mutech 28:b452e097da53 68
mutech 27:bc79f444883b 69 void repeatPixels(int block_size);
mutech 27:bc79f444883b 70 void repeatPixels(RGBColor *source, int size);
mutech 27:bc79f444883b 71 void repeatPixels(HSVColor *source, int size);
mutech 27:bc79f444883b 72
mutech 28:b452e097da53 73 void makeGradation(int index, RGBColor from, RGBColor to, int len);
mutech 28:b452e097da53 74 void makeGradation(RGBColor from, RGBColor to, int len = MAX_PIXELS) { makeGradation(0, from, to, len); }
mutech 28:b452e097da53 75
mutech 28:b452e097da53 76 void makeRainbow(int index, HSVColor color, int len, int direction);
mutech 28:b452e097da53 77 void makeRainbow(HSVColor color, int len = MAX_PIXELS, int direction = 1) { makeRainbow(0, color, len, direction); }
mutech 28:b452e097da53 78
mutech 27:bc79f444883b 79 RGBColor operator[](int index) const
mutech 27:bc79f444883b 80 {
mutech 27:bc79f444883b 81 if ((uint16_t)index < _numPixels)
mutech 27:bc79f444883b 82 return _pixels[index];
mutech 27:bc79f444883b 83 return _dummyPixel;
mutech 27:bc79f444883b 84 }
mutech 27:bc79f444883b 85
mutech 27:bc79f444883b 86 RGBColor& operator[](int index)
mutech 27:bc79f444883b 87 {
mutech 27:bc79f444883b 88 if ((uint16_t)index < _numPixels)
mutech 27:bc79f444883b 89 return _pixels[index];
mutech 27:bc79f444883b 90 return _dummyPixel;
mutech 27:bc79f444883b 91 }
mutech 27:bc79f444883b 92
mutech 27:bc79f444883b 93 RGBPixels& operator=(const RGBPixels& rhs);
mutech 27:bc79f444883b 94
mutech 27:bc79f444883b 95 operator RGBColor*() const { return _pixels; }
mutech 27:bc79f444883b 96
mutech 27:bc79f444883b 97 protected:
mutech 27:bc79f444883b 98 uint16_t _maxPixels;
mutech 27:bc79f444883b 99 uint16_t _numPixels;
mutech 27:bc79f444883b 100 RGBColor *_pixels;
mutech 27:bc79f444883b 101
mutech 27:bc79f444883b 102 private:
mutech 27:bc79f444883b 103 bool _buf_owner;
mutech 27:bc79f444883b 104 RGBColor _dummyPixel;
mutech 27:bc79f444883b 105
mutech 27:bc79f444883b 106 };
mutech 27:bc79f444883b 107
mutech 27:bc79f444883b 108 //----------------------------------------------------------------------------
mutech 27:bc79f444883b 109 /**
mutech 27:bc79f444883b 110 * HSVPixels
mutech 27:bc79f444883b 111 */
mutech 27:bc79f444883b 112 class HSVPixels
mutech 27:bc79f444883b 113 {
mutech 27:bc79f444883b 114 public:
mutech 27:bc79f444883b 115 /**
mutech 27:bc79f444883b 116 * Initializes the addressable led bus
mutech 27:bc79f444883b 117 *
mutech 27:bc79f444883b 118 * @param Buffer - The Pixel array buffer address.
mutech 27:bc79f444883b 119 * @param maxPixels - Number of the addressable leds
mutech 27:bc79f444883b 120 */
mutech 27:bc79f444883b 121 HSVPixels(HSVColor *buffer = nullptr, int maxPixels = MAX_PIXELS);
mutech 27:bc79f444883b 122 HSVPixels(int maxPixels = MAX_PIXELS);
mutech 27:bc79f444883b 123 ~HSVPixels();
mutech 27:bc79f444883b 124
mutech 27:bc79f444883b 125 void setPixelBuffer(HSVColor *buffer, int maxPixels);
mutech 27:bc79f444883b 126 int maxPixels() { return _maxPixels; }
mutech 27:bc79f444883b 127 int numPixels(int value = -1);
mutech 27:bc79f444883b 128
mutech 27:bc79f444883b 129 void setPixels(int index, HSVColor *color, int len);
mutech 27:bc79f444883b 130 void setPixels(int index, RGBColor *color, int len);
mutech 27:bc79f444883b 131 void setPixels(HSVColor *color, int len) { setPixels(0, color, len); }
mutech 27:bc79f444883b 132 void setPixels(RGBColor *color, int len) { setPixels(0, color, len); }
mutech 28:b452e097da53 133
mutech 29:a362df191524 134 void fillPixels(int index, const HSVColor color, int len);
mutech 29:a362df191524 135 void fillPixels(int index, const RGBColor color, int len);
mutech 29:a362df191524 136 void fillPixels(int index, const int color, int len) { fillPixels(index, (RGBColor)color, len); }
mutech 29:a362df191524 137 void fillPixels(const HSVColor color, int len = MAX_PIXELS) { fillPixels(0, color, len); }
mutech 29:a362df191524 138 void fillPixels(const RGBColor color, int len = MAX_PIXELS) { fillPixels(0, color, len); }
mutech 29:a362df191524 139 void fillPixels(const int color, int len = MAX_PIXELS) { fillPixels(0, (RGBColor)color, len); }
mutech 28:b452e097da53 140
mutech 27:bc79f444883b 141 void clear(const HSVColor color) { fillPixels(color); }
mutech 27:bc79f444883b 142 void clear(const RGBColor color) { fillPixels(color); }
mutech 28:b452e097da53 143 void clear(const int color = 0) { fillPixels((RGBColor)color); }
mutech 28:b452e097da53 144
mutech 27:bc79f444883b 145 void repeatPixels(int block_size);
mutech 27:bc79f444883b 146 void repeatPixels(HSVColor *source, int size);
mutech 27:bc79f444883b 147 void repeatPixels(RGBColor *source, int size);
mutech 27:bc79f444883b 148
mutech 28:b452e097da53 149 void makeGradation(int index, HSVColor from, HSVColor to, int len);
mutech 28:b452e097da53 150 void makeGradation(HSVColor from, HSVColor to, int len = MAX_PIXELS) { makeGradation(0, from, to, len); }
mutech 28:b452e097da53 151
mutech 28:b452e097da53 152 void makeRainbow(int index, HSVColor color, int len, int direction);
mutech 28:b452e097da53 153 void makeRainbow(HSVColor color, int len = MAX_PIXELS, int direction = 1) { makeRainbow(0, color, len, direction); }
mutech 28:b452e097da53 154
mutech 27:bc79f444883b 155 HSVColor operator[](int index) const
mutech 27:bc79f444883b 156 {
mutech 27:bc79f444883b 157 if ((uint16_t)index < _numPixels)
mutech 27:bc79f444883b 158 return _pixels[index];
mutech 27:bc79f444883b 159 return _dummyPixel;
mutech 27:bc79f444883b 160 }
mutech 27:bc79f444883b 161
mutech 27:bc79f444883b 162 HSVColor& operator[](int index)
mutech 27:bc79f444883b 163 {
mutech 27:bc79f444883b 164 if ((uint16_t)index < _numPixels)
mutech 27:bc79f444883b 165 return _pixels[index];
mutech 27:bc79f444883b 166 return _dummyPixel;
mutech 27:bc79f444883b 167 }
mutech 27:bc79f444883b 168
mutech 27:bc79f444883b 169 HSVPixels& operator=(const HSVPixels& rhs);
mutech 27:bc79f444883b 170
mutech 27:bc79f444883b 171 operator HSVColor*() const { return _pixels; }
mutech 27:bc79f444883b 172
mutech 27:bc79f444883b 173 protected:
mutech 27:bc79f444883b 174 uint16_t _maxPixels;
mutech 27:bc79f444883b 175 uint16_t _numPixels;
mutech 27:bc79f444883b 176 HSVColor *_pixels;
mutech 27:bc79f444883b 177
mutech 27:bc79f444883b 178 private:
mutech 27:bc79f444883b 179 bool _buf_owner;
mutech 27:bc79f444883b 180 HSVColor _dummyPixel;
mutech 27:bc79f444883b 181
mutech 27:bc79f444883b 182 };
mutech 27:bc79f444883b 183
mutech 27:bc79f444883b 184 //----------------------------------------------------------------------------
mutech 27:bc79f444883b 185 #endif // end of PIXELBUFFER_H