Displays a gradient between two colors

Dependencies:   mbed WS2812 PixelArray

main.cpp

Committer:
radmir102
Date:
2021-04-19
Revision:
1:3fa4e131f518
Parent:
0:3bb97062a703
Child:
2:c4a43cc93ca8

File content as of revision 1:3fa4e131f518:

#include "mbed.h"
#include "WS2812.h"
#include "PixelArray.h"

#define WS2812_BUF 16   //number of LEDs in the array
#define NUM_COLORS 6  //number of colors to store in the array
#define PIN_NUM A2
#define LIGHT_INTENSITY 30

using namespace std;
DigitalOut usrLed(LED1);
PixelArray px(WS2812_BUF);
DigitalIn usrBtn(USER_BUTTON);


// See the program page for information on the timing numbers
WS2812 ws(PIN_NUM, WS2812_BUF,6,17,9,14);   //nucleo-f411re

int color_set(uint8_t red,uint8_t green, uint8_t blue)
{
  return ((red<<16) + (green<<8) + blue);   
}

// 0 <= stepNumber <= lastStepNumber
int interpolate(int startValue, int endValue, int stepNumber, int lastStepNumber)
{
    return (endValue - startValue) * stepNumber / lastStepNumber + startValue;
}

int main()
{    
    uint8_t ir = 0;
    uint8_t ig = 0;
    uint8_t ib = 0;
   int btnState = 0;
    
    ws.useII(WS2812::PER_PIXEL); // use per-pixel intensity scaling
    
    // set up the colours we want to draw with
    int colorbuf[NUM_COLORS] = {0xff0000,0x0000ff,0x0002f0,0x00002f,0x00002f,0x2f002f};
    
    //get starting RGB components for interpolation
    size_t c1 = colorbuf[0];
    size_t r1 = (c1 & 0xff0000) >> 16;
    size_t g1 = (c1 & 0x00ff00) >> 8;
    size_t b1 = (c1 & 0x0000ff);
    
    //get ending RGB components for interpolation
    size_t c2 = colorbuf[1];
    size_t r2 = (c2 & 0xff0000) >> 16;
    size_t g2 = (c2 & 0x00ff00) >> 8;
    size_t b2 = (c2 & 0x0000ff);
    
    for (int i = 0; i <= WS2812_BUF; i++)
    {
        ir = interpolate(r1, r2, i, WS2812_BUF);
        ig = interpolate(g1, g2, i, WS2812_BUF);
        ib = interpolate(b1, b2, i, WS2812_BUF);
        
        //write the color value for each pixel
        px.Set(i, color_set(ir,ig,ib));
        
        //write the II value for each pixel
        /*px.SetI(4, 30);
        px.SetI(1, 30);
        px.SetI(2, 30);
        px.SetI(7, 30);
        px.SetI(12, 30);
        px.SetI(15, 30);*/// смайлик убийца 
        
        px.SetI(i, 10);
 
        /*px.SetI(10, 15);

        px.SetI(15, 20);

        px.SetI(3, 25);
 
        px.SetI(6, 30);

        px.SetI(9, 15);
        
        px.SetI(12, 5);*/
        
    }
    /*
    for (int i = WS2812_BUF; i >= 0; i--) 
    {
        ws.write(px.getBuf());
    }*/
    
        usrLed = 1;
    
    

    
   /*
        while (1) 
    {
        if (usrBtn == 0)    //button is pressed
        { 
         //write the color value for each pixel
        px.SetAll(colorbuf[btnState]);
        //write the II value for each pixel
        px.SetAllI(64);
        
            usrLed = 1;
            btnState = btnState++;
            
            if (btnState == NUM_COLORS) {
                btnState = 0;
            }
        } 
        else 
        {
        px.SetAll(colorbuf[btnState]);
        px.SetI(0, LIGHT_INTENSITY);
        
        px.SetI(5, LIGHT_INTENSITY);
 
        px.SetI(10, LIGHT_INTENSITY);

        px.SetI(15, LIGHT_INTENSITY);

        px.SetI(3, LIGHT_INTENSITY);
 
        px.SetI(6, LIGHT_INTENSITY);

        px.SetI(9, LIGHT_INTENSITY);
        
        px.SetI(12, LIGHT_INTENSITY);
            
            usrLed = 0;
        }
        
       

        
        for (int i = WS2812_BUF; i >= 0; i--) 
        {
            ws.write(px.getBuf());
        }
    
    }*/

}