Simple Click 4x4 RGB example for Hexiwear

Dependencies:   PixelArray WS2812

Fork of WS2812_Example by Brian Daniels

Committer:
GregC
Date:
Wed Oct 19 17:29:58 2016 +0000
Revision:
3:8ac839958132
Parent:
2:cb82a3dc4031
Simple Click 4x4 RGB example for Hexiwear

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bridadan 0:12cb6f0c2788 1 #include "mbed.h"
bridadan 0:12cb6f0c2788 2 #include "WS2812.h"
bridadan 0:12cb6f0c2788 3 #include "PixelArray.h"
bridadan 0:12cb6f0c2788 4
GregC 3:8ac839958132 5 #define WS2812_BUF 96 // W2812_BUFF >/= NUM_COLORS * NUM_LEDS
bridadan 2:cb82a3dc4031 6 #define NUM_COLORS 6
GregC 3:8ac839958132 7 #define NUM_LEDS 16
bridadan 0:12cb6f0c2788 8
GregC 3:8ac839958132 9 // Pin connections
GregC 3:8ac839958132 10 DigitalOut led1(LED_GREEN); // RGB LED
GregC 3:8ac839958132 11 Serial pc(USBTX, USBRX); // Serial interface
GregC 3:8ac839958132 12 WS2812 ws(PTB11, WS2812_BUF, 0, 5, 5, 0); // LED Driver
GregC 3:8ac839958132 13
GregC 3:8ac839958132 14 // Variables
GregC 3:8ac839958132 15 int i, j, z;
GregC 3:8ac839958132 16
GregC 3:8ac839958132 17 // Fuctions
bridadan 0:12cb6f0c2788 18 PixelArray px(WS2812_BUF);
bridadan 0:12cb6f0c2788 19
bridadan 0:12cb6f0c2788 20 int main()
bridadan 0:12cb6f0c2788 21 {
bridadan 0:12cb6f0c2788 22
GregC 3:8ac839958132 23 // use per-pixel intensity scaling
GregC 3:8ac839958132 24 // ws.useII(WS2812::PER_PIXEL); // Enable Pixel diming between first and last row
GregC 3:8ac839958132 25 ws.useII(WS2812::GLOBAL); // Pixel dimming disabled
bridadan 0:12cb6f0c2788 26
bridadan 0:12cb6f0c2788 27 // set up the colours we want to draw with
bridadan 2:cb82a3dc4031 28 int colorbuf[NUM_COLORS] = {0x2f0000,0x2f2f00,0x002f00,0x002f2f,0x00002f,0x2f002f};
GregC 3:8ac839958132 29
GregC 3:8ac839958132 30 // for each of the colours (j) write out 16 of them the pixels are written at the colour*16, plus the colour position all modulus 60 so it wraps around
GregC 3:8ac839958132 31 for (i = 0; i < WS2812_BUF; i++) {
GregC 3:8ac839958132 32 px.Set(i, colorbuf[(i / NUM_LEDS) % NUM_COLORS]);
bridadan 0:12cb6f0c2788 33 }
bridadan 0:12cb6f0c2788 34
GregC 3:8ac839958132 35 // now all the colours are computed, add a fade effect using intensity scaling compute and write the II value for each pixel
GregC 3:8ac839958132 36 for (j=0; j<WS2812_BUF; j++)
GregC 3:8ac839958132 37 {
bridadan 0:12cb6f0c2788 38 // px.SetI(pixel position, II value)
GregC 3:8ac839958132 39 px.SetI(j%WS2812_BUF, 0xf+(0xf*(j%NUM_LEDS)));
bridadan 0:12cb6f0c2788 40 }
bridadan 0:12cb6f0c2788 41
bridadan 0:12cb6f0c2788 42
GregC 3:8ac839958132 43 // Now the buffer is written, rotate it by writing it out with an increasing offset
GregC 3:8ac839958132 44 while (true)
GregC 3:8ac839958132 45 {
GregC 3:8ac839958132 46 for (z=0; z<WS2812_BUF; z++) // Color updated from Top to Bottom
GregC 3:8ac839958132 47 // for (z=WS2812_BUF; z>=0; z--) // Color updated from Bottom to Top
GregC 3:8ac839958132 48 {
bridadan 0:12cb6f0c2788 49 ws.write_offsets(px.getBuf(),z,z,z);
GregC 3:8ac839958132 50 Thread::wait(100);
bridadan 0:12cb6f0c2788 51 }
GregC 3:8ac839958132 52 led1 = !led1;
bridadan 0:12cb6f0c2788 53 }
bridadan 0:12cb6f0c2788 54
GregC 3:8ac839958132 55 }