Color pixels library using WS2812B and nRF51822

Dependents:   BLE_Color_Pixels Grove_Node BLE_HeartRate

Color pixels library using WS2812B and nRF51822 (16Hz)

http://www.seeedstudio.com/depot/bmz_cache/4/4f346dc15724a7b5a5c1383253aeefc9.image.530x397.jpg

It's for

Committer:
yihui
Date:
Thu Jul 31 12:02:23 2014 +0000
Revision:
1:0322642447e2
Parent:
0:1e1ce549ad91
Child:
2:5c805323a2c0
add example code

Who changed what in which revision?

UserRevisionLine numberNew 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 1:0322642447e2 35 /** Color pixels class using WS2812B and nRF51822 (16Hz)
yihui 1:0322642447e2 36 *
yihui 1:0322642447e2 37 * Example:
yihui 1:0322642447e2 38 * @code
yihui 1:0322642447e2 39 * #include "mbed.h"
yihui 1:0322642447e2 40 * #include "color_pixels.h"
yihui 1:0322642447e2 41 *
yihui 1:0322642447e2 42 * ColorPixels pixels(1, 32);
yihui 1:0322642447e2 43 *
yihui 1:0322642447e2 44 * int main() {
yihui 1:0322642447e2 45 * pixels.set_color(0, 0, 255, 0);
yihui 1:0322642447e2 46 * pixels.update();
yihui 1:0322642447e2 47 *
yihui 1:0322642447e2 48 * while(1) {
yihui 1:0322642447e2 49 * }
yihui 1:0322642447e2 50 * }
yihui 1:0322642447e2 51 * @endcode
yihui 1:0322642447e2 52 */
yihui 1:0322642447e2 53 class ColorPixels
yihui 1:0322642447e2 54 {
yihui 1:0322642447e2 55 public:
yihui 1:0322642447e2 56 /** Initilaze
yihui 1:0322642447e2 57 * \param pin number of GPIO
yihui 1:0322642447e2 58 * \param num number of pixels
yihui 1:0322642447e2 59 */
yihui 1:0322642447e2 60 ColorPixels(uint8_t pin, uint16_t num);
yihui 1:0322642447e2 61
yihui 1:0322642447e2 62 ~ColorPixels();
yihui 1:0322642447e2 63
yihui 1:0322642447e2 64 /** Set the color of a pixel (without update)
yihui 1:0322642447e2 65 * \param index index of a pixel
yihui 1:0322642447e2 66 * \param r red
yihui 1:0322642447e2 67 * \param g green
yihui 1:0322642447e2 68 * \param b blue
yihui 1:0322642447e2 69 */
yihui 1:0322642447e2 70 void set_color(uint16_t index, uint8_t r, uint8_t g, uint8_t b);
yihui 1:0322642447e2 71
yihui 1:0322642447e2 72 /** Update
yihui 1:0322642447e2 73 */
yihui 1:0322642447e2 74 void update();
yihui 1:0322642447e2 75
yihui 1:0322642447e2 76 /** Turn off all pixels
yihui 1:0322642447e2 77 */
yihui 1:0322642447e2 78 void clear();
yihui 1:0322642447e2 79
yihui 1:0322642447e2 80 private:
yihui 1:0322642447e2 81 typedef union {
yihui 1:0322642447e2 82 struct {
yihui 1:0322642447e2 83 uint8_t g, r, b;
yihui 0:1e1ce549ad91 84 };
yihui 1:0322642447e2 85 uint32_t grb;
yihui 1:0322642447e2 86 } color_t;
yihui 1:0322642447e2 87
yihui 1:0322642447e2 88 uint8_t pin;
yihui 1:0322642447e2 89 uint16_t num;
yihui 1:0322642447e2 90 color_t *colors;
yihui 1:0322642447e2 91
yihui 1:0322642447e2 92 };
yihui 1:0322642447e2 93
yihui 1:0322642447e2 94 #endif // __COLOR_PIXELS_H__