Hexiwear / Mbed OS Hexi_Click_4x4RGB_Example

Dependencies:   PixelArray WS2812

Fork of WS2812_Example by Brian Daniels

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "WS2812.h"
00003 #include "PixelArray.h"
00004 
00005 #define WS2812_BUF 96 // W2812_BUFF >/= NUM_COLORS * NUM_LEDS
00006 #define NUM_COLORS 6
00007 #define NUM_LEDS 16
00008 
00009 // Pin connections
00010 DigitalOut led1(LED_GREEN); // RGB LED
00011 Serial pc(USBTX, USBRX); // Serial interface
00012 WS2812 ws(PTB11, WS2812_BUF, 0, 5, 5, 0); // LED Driver
00013 
00014 // Variables
00015 int i, j, z;
00016 
00017 // Fuctions
00018 PixelArray px(WS2812_BUF);
00019 
00020 int main()
00021 {
00022 
00023     // use per-pixel intensity scaling
00024 //    ws.useII(WS2812::PER_PIXEL); // Enable Pixel diming between first and last row
00025     ws.useII(WS2812::GLOBAL); // Pixel dimming disabled
00026     
00027     // set up the colours we want to draw with
00028     int colorbuf[NUM_COLORS] = {0x2f0000,0x2f2f00,0x002f00,0x002f2f,0x00002f,0x2f002f};
00029     
00030     // 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
00031     for (i = 0; i < WS2812_BUF; i++) {
00032         px.Set(i, colorbuf[(i / NUM_LEDS) % NUM_COLORS]);
00033     }
00034 
00035     // now all the colours are computed, add a fade effect using intensity scaling compute and write the II value for each pixel
00036     for (j=0; j<WS2812_BUF; j++) 
00037     {
00038         // px.SetI(pixel position, II value)
00039         px.SetI(j%WS2812_BUF, 0xf+(0xf*(j%NUM_LEDS)));
00040     }
00041 
00042 
00043     // Now the buffer is written, rotate it by writing it out with an increasing offset
00044     while (true) 
00045     {
00046         for (z=0; z<WS2812_BUF; z++) // Color updated from Top to Bottom
00047 //        for (z=WS2812_BUF; z>=0; z--) // Color updated from Bottom to Top        
00048         {
00049             ws.write_offsets(px.getBuf(),z,z,z);
00050             Thread::wait(100);
00051         }
00052         led1 = !led1;
00053     }
00054 
00055 }