Difference between revisions of "Blog 24"

From BitWizard WIKI
Jump to: navigation, search
(Useful links)
(RGB lighted paper Tree)
Line 13: Line 13:
  
 
The c-program RGBTree.c can be downloaded: [http://bitwizard.nl/source/ here].
 
The c-program RGBTree.c can be downloaded: [http://bitwizard.nl/source/ here].
 +
 +
#include <stdio.h>
 +
#include <stdlib.h>
 +
#include <unistd.h>
 +
 +
#define WHITE  0xffffff
 +
#define YELLOW 0xffff00
 +
#define RED    0x800000
 +
#define GREEN  0x008000
 +
#define BLUE  0x0000ff
 +
#define BLACK  0x000000
 +
 +
int nfadesteps = 30;
 +
int delayms = 30;
 +
 +
int interpolate (int c1, int c2, int shift, int pos, int end)
 +
{
 +
  c1 >>= shift;
 +
  c2 >>= shift;
 +
  c1 &= 0xff;
 +
  c2 &= 0xff;
 +
 +
  return c1 * (end-pos) / end + c2 * pos / end;
 +
}
 +
 +
void fadeto (int pixnum, int col1, int col2)
 +
{
 +
    int i;
 +
    int r, g, b;
 +
 +
    for (i=0;i <= nfadesteps;i++) {
 +
      r = interpolate (col1, col2, 16, i, nfadesteps);
 +
      g = interpolate (col1, col2,  8, i, nfadesteps);
 +
      b = interpolate (col1, col2,  0, i, nfadesteps);
 +
      printf ("pix %d %06x\n", pixnum,
 +
(r << 16) | (g << 8) | (b << 0));
 +
 +
      usleep (delayms*1000);
 +
    }
 +
}
 +
 +
int main (int argc, char **argv)
 +
{
 +
  int nleds = 10;
 +
  int *pixels;
 +
  int pixnum, newcolor;
 +
  int i;
 +
 
 +
  if (argc > 1)
 +
    nleds = atoi (argv[1]);
 +
 +
  pixels = calloc (nleds, sizeof(int));
 +
 
 +
  printf ("pix %d %06x\n", nleds, WHITE); 
 +
 +
  for (i=0;i < nleds;i++){ 
 +
    if (random () % 2 == 0)
 +
        pixels[i] = RED;
 +
    else
 +
        pixels[i] = GREEN;
 +
    printf ("pix %d %06x\n", i, pixels[i]); 
 +
  }
 +
 +
  while (1) {
 +
      pixnum = random () % nleds;
 +
      if (pixels[pixnum] == RED)
 +
newcolor = GREEN;
 +
      else
 +
        newcolor = RED;
 +
      fadeto (pixnum, pixels[pixnum], newcolor);
 +
      pixels[pixnum] = newcolor;
 +
  }
 +
  exit (0);
 +
 +
}
 +
 +
  
  

Revision as of 17:52, 6 January 2016

Working with RGB leds on the WS2812

For Linux/Raspberry:

apt-get install ckermit

If you use and other device, or want to know more:

C-Kermit

For Windows users it's optional to use:

PuTTY 

RGB lighted paper Tree

The c-program RGBTree.c can be downloaded: here.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define WHITE  0xffffff
#define YELLOW 0xffff00
#define RED    0x800000
#define GREEN  0x008000
#define BLUE   0x0000ff
#define BLACK  0x000000 

int nfadesteps = 30;
int delayms = 30; 

int interpolate (int c1, int c2, int shift, int pos, int end)
{
  c1 >>= shift;
  c2 >>= shift;
  c1 &= 0xff;
  c2 &= 0xff;

  return c1 * (end-pos) / end + c2 * pos / end;
}

void fadeto (int pixnum, int col1, int col2)
{
   int i; 
   int r, g, b; 

   for (i=0;i <= nfadesteps;i++) {
      r = interpolate (col1, col2, 16, i, nfadesteps);
      g = interpolate (col1, col2,  8, i, nfadesteps);
      b = interpolate (col1, col2,  0, i, nfadesteps);
      printf ("pix %d %06x\n", pixnum, 
	 (r << 16) | (g << 8) | (b << 0)); 

      usleep (delayms*1000);
   }
}

int main (int argc, char **argv)
{
  int nleds = 10;
  int *pixels;
  int pixnum, newcolor;
  int i;
  
  if (argc > 1) 
    nleds = atoi (argv[1]);

  pixels = calloc (nleds, sizeof(int));
  
  printf ("pix %d %06x\n", nleds, WHITE);  

  for (i=0;i < nleds;i++){  
    if (random () % 2 == 0)
       pixels[i] = RED;
    else
       pixels[i] = GREEN;
    printf ("pix %d %06x\n", i, pixels[i]);  
  }

  while (1) {
     pixnum = random () % nleds; 
     if (pixels[pixnum] == RED) 
	newcolor = GREEN;
     else 
       newcolor = RED; 
     fadeto (pixnum, pixels[pixnum], newcolor);
     pixels[pixnum] = newcolor;
  }
  exit (0);

}



RGBTree1.jpg


RGBTree2.jpg


RGBThree.jpg

Useful links