Color pixels library using WS2812B and nRF51822
Dependents: BLE_Color_Pixels Grove_Node BLE_HeartRate
Color pixels library using WS2812B and nRF51822 (16Hz)
It's for
- http://www.seeedstudio.com/depot/Digital-RGB-LED-FlexiStrip-60-LED-1-Meter-p-1666.html
- http://www.seeedstudio.com/depot/WS2812B-Digital-RGB-LED-Waterproof-FlexiStrip-144-LEDmeter-2-meter-p-1869.html
- http://www.seeedstudio.com/depot/WS2812B-RGB-LED-with-Integrated-Driver-Chip-10-PCs-pack-p-1675.html
color_pixels.h@4:16ef874fa57f, 2015-05-05 (annotated)
- Committer:
- yihui
- Date:
- Tue May 05 05:37:20 2015 +0000
- Revision:
- 4:16ef874fa57f
- Parent:
- 3:91af7b451005
update header file name
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yihui | 1:0322642447e2 | 1 | /** Color pixels library using WS2812B and nRF51822 (16Hz) |
yihui | 1:0322642447e2 | 2 | * It's for |
yihui | 1:0322642447e2 | 3 | * + http://www.seeedstudio.com/depot/Digital-RGB-LED-FlexiStrip-60-LED-1-Meter-p-1666.html |
yihui | 1:0322642447e2 | 4 | * + http://www.seeedstudio.com/depot/WS2812B-Digital-RGB-LED-Waterproof-FlexiStrip-144-LEDmeter-2-meter-p-1869.html |
yihui | 1:0322642447e2 | 5 | * + http://www.seeedstudio.com/depot/WS2812B-RGB-LED-with-Integrated-Driver-Chip-10-PCs-pack-p-1675.html |
yihui | 1:0322642447e2 | 6 | * |
yihui | 1:0322642447e2 | 7 | * The MIT License (MIT) |
yihui | 1:0322642447e2 | 8 | * |
yihui | 1:0322642447e2 | 9 | * Copyright (c) 2014 Seeed Technology Inc. |
yihui | 1:0322642447e2 | 10 | * |
yihui | 1:0322642447e2 | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
yihui | 1:0322642447e2 | 12 | * of this software and associated documentation files (the "Software"), to deal |
yihui | 1:0322642447e2 | 13 | * in the Software without restriction, including without limitation the rights |
yihui | 1:0322642447e2 | 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
yihui | 1:0322642447e2 | 15 | * copies of the Software, and to permit persons to whom the Software is |
yihui | 1:0322642447e2 | 16 | * furnished to do so, subject to the following conditions: |
yihui | 1:0322642447e2 | 17 | |
yihui | 1:0322642447e2 | 18 | * The above copyright notice and this permission notice shall be included in |
yihui | 1:0322642447e2 | 19 | * all copies or substantial portions of the Software. |
yihui | 1:0322642447e2 | 20 | |
yihui | 1:0322642447e2 | 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
yihui | 1:0322642447e2 | 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
yihui | 1:0322642447e2 | 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
yihui | 1:0322642447e2 | 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
yihui | 1:0322642447e2 | 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
yihui | 1:0322642447e2 | 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
yihui | 1:0322642447e2 | 27 | * THE SOFTWARE. |
yihui | 1:0322642447e2 | 28 | */ |
yihui | 1:0322642447e2 | 29 | |
yihui | 1:0322642447e2 | 30 | #ifndef __COLOR_PIXELS_H__ |
yihui | 1:0322642447e2 | 31 | #define __COLOR_PIXELS_H__ |
yihui | 1:0322642447e2 | 32 | |
yihui | 1:0322642447e2 | 33 | #include <stdint.h> |
yihui | 1:0322642447e2 | 34 | |
Yihui Xiong | 2:5c805323a2c0 | 35 | typedef union { |
Yihui Xiong | 2:5c805323a2c0 | 36 | uint32_t rgb; |
Yihui Xiong | 2:5c805323a2c0 | 37 | struct { |
Yihui Xiong | 2:5c805323a2c0 | 38 | uint8_t r; |
Yihui Xiong | 2:5c805323a2c0 | 39 | uint8_t g; |
Yihui Xiong | 2:5c805323a2c0 | 40 | uint8_t b; |
Yihui Xiong | 2:5c805323a2c0 | 41 | }; |
Yihui Xiong | 2:5c805323a2c0 | 42 | } color_t; |
Yihui Xiong | 2:5c805323a2c0 | 43 | |
yihui | 1:0322642447e2 | 44 | /** Color pixels class using WS2812B and nRF51822 (16Hz) |
yihui | 1:0322642447e2 | 45 | * |
yihui | 1:0322642447e2 | 46 | * Example: |
yihui | 1:0322642447e2 | 47 | * @code |
yihui | 1:0322642447e2 | 48 | * #include "mbed.h" |
yihui | 1:0322642447e2 | 49 | * #include "color_pixels.h" |
yihui | 1:0322642447e2 | 50 | * |
yihui | 1:0322642447e2 | 51 | * ColorPixels pixels(1, 32); |
yihui | 1:0322642447e2 | 52 | * |
yihui | 1:0322642447e2 | 53 | * int main() { |
yihui | 1:0322642447e2 | 54 | * pixels.set_color(0, 0, 255, 0); |
yihui | 1:0322642447e2 | 55 | * pixels.update(); |
yihui | 1:0322642447e2 | 56 | * |
yihui | 1:0322642447e2 | 57 | * while(1) { |
yihui | 1:0322642447e2 | 58 | * } |
yihui | 1:0322642447e2 | 59 | * } |
yihui | 1:0322642447e2 | 60 | * @endcode |
yihui | 1:0322642447e2 | 61 | */ |
yihui | 1:0322642447e2 | 62 | class ColorPixels |
yihui | 1:0322642447e2 | 63 | { |
yihui | 1:0322642447e2 | 64 | public: |
yihui | 1:0322642447e2 | 65 | /** Initilaze |
yihui | 1:0322642447e2 | 66 | * \param pin number of GPIO |
yihui | 1:0322642447e2 | 67 | * \param num number of pixels |
yihui | 1:0322642447e2 | 68 | */ |
yihui | 1:0322642447e2 | 69 | ColorPixels(uint8_t pin, uint16_t num); |
yihui | 1:0322642447e2 | 70 | |
yihui | 1:0322642447e2 | 71 | ~ColorPixels(); |
yihui | 1:0322642447e2 | 72 | |
yihui | 1:0322642447e2 | 73 | /** Set the color of a pixel (without update) |
yihui | 1:0322642447e2 | 74 | * \param index index of a pixel |
yihui | 1:0322642447e2 | 75 | * \param r red |
yihui | 1:0322642447e2 | 76 | * \param g green |
yihui | 1:0322642447e2 | 77 | * \param b blue |
yihui | 1:0322642447e2 | 78 | */ |
yihui | 1:0322642447e2 | 79 | void set_color(uint16_t index, uint8_t r, uint8_t g, uint8_t b); |
yihui | 1:0322642447e2 | 80 | |
Yihui Xiong | 2:5c805323a2c0 | 81 | void set_color(uint16_t index, uint32_t rgb); |
Yihui Xiong | 2:5c805323a2c0 | 82 | |
yihui | 3:91af7b451005 | 83 | void set_all_color(uint8_t r, uint8_t g, uint8_t b); |
yihui | 3:91af7b451005 | 84 | |
Yihui Xiong | 2:5c805323a2c0 | 85 | void rainbow(uint8_t r, uint8_t g, uint8_t b); |
Yihui Xiong | 2:5c805323a2c0 | 86 | |
Yihui Xiong | 2:5c805323a2c0 | 87 | void rainbow(uint32_t rgb = 0x0000FF); |
Yihui Xiong | 2:5c805323a2c0 | 88 | |
yihui | 1:0322642447e2 | 89 | /** Update |
yihui | 1:0322642447e2 | 90 | */ |
yihui | 1:0322642447e2 | 91 | void update(); |
yihui | 1:0322642447e2 | 92 | |
yihui | 1:0322642447e2 | 93 | /** Turn off all pixels |
yihui | 1:0322642447e2 | 94 | */ |
yihui | 1:0322642447e2 | 95 | void clear(); |
yihui | 1:0322642447e2 | 96 | |
yihui | 1:0322642447e2 | 97 | private: |
yihui | 1:0322642447e2 | 98 | typedef union { |
yihui | 1:0322642447e2 | 99 | struct { |
yihui | 1:0322642447e2 | 100 | uint8_t g, r, b; |
yihui | 0:1e1ce549ad91 | 101 | }; |
yihui | 1:0322642447e2 | 102 | uint32_t grb; |
Yihui Xiong | 2:5c805323a2c0 | 103 | } grb_t; |
yihui | 1:0322642447e2 | 104 | |
yihui | 1:0322642447e2 | 105 | uint8_t pin; |
yihui | 1:0322642447e2 | 106 | uint16_t num; |
Yihui Xiong | 2:5c805323a2c0 | 107 | grb_t *colors; |
yihui | 1:0322642447e2 | 108 | |
yihui | 1:0322642447e2 | 109 | }; |
yihui | 1:0322642447e2 | 110 | |
yihui | 1:0322642447e2 | 111 | #endif // __COLOR_PIXELS_H__ |