Code used in RGB-123 Kickstarter. Conways Game of Life

Dependencies:   WS2811_RY_A mbed

Fork of WS2811_RY_A by ohararp@...

main.cpp

Committer:
ohararp
Date:
2013-01-04
Revision:
0:f19c80146d55
Child:
1:1b91ef74cc9c

File content as of revision 0:f19c80146d55:

#include "mbed.h"
Serial pc(USBTX, USBRX);
DigitalOut dat(p5);


//*******************************************************************************    
// DEFINE WS2811 Strip Parameters
#define numLEDs 64*5
uint8_t *pixels;
Timer guardtime;
uint32_t bogocal;

//*******************************************************************************
void setup () {
    if ((pixels = (uint8_t *)malloc(numLEDs * 3))) {
        memset(pixels, 0x00, numLEDs * 3); // Init to RGB 'off' state
    }
    // calibrate delay loops for NRZ 
    int i;
    guardtime.start();
    for (i=0; i<1000; i++)
        /* do nothing */;
    i=guardtime.read_us();
    printf("ws2811:  1000 iters took %d usec.\n", i);
    bogocal = (1000 / (i*4));//2.4)); // iterations per bitcell (417 nsec)
    printf("ws2811:  calibrating to %d bogojiffies.\n", bogocal);
}
//*******************************************************************************
inline void celldelay(void) {
    for (int i = 0; i<bogocal; i++)
        /* do nothing */ ;
}
//*******************************************************************************
void writebit(bool bit) {
    // first cell is always 1
    dat = 1;
    celldelay();
    if (bit) {
        celldelay();
    } else {
        dat=0;
        celldelay();
    }
    // last cell is always 0
    dat=0;
    celldelay();
}
//*******************************************************************************
void write(uint8_t byte) {
    __disable_irq();
    for (int i=0; i<8; i++) {
        if (byte & 0x80)
            writebit(1);
        else
            writebit(0);
        byte <<= 1;
    }
    __enable_irq();
}

uint16_t numPixels(void) {
    return numLEDs;
}

//*******************************************************************************
void show(void) {
    uint16_t i, nl3 = numLEDs * 3; // 3 bytes per LED
    while (guardtime.read_us() < 50)
        /* spin */;
    for (i=0; i<nl3; i++ ) {
        write(pixels[i]);
    }
    guardtime.reset();
}
//*******************************************************************************
void blank(void) {
    memset(pixels, 0x00, numLEDs * 3);
    show();
}
//*******************************************************************************
void blankDelay(int n) {
    memset(pixels, 0x00, numLEDs * 3);
    show();
    wait_ms(n);
}
//*******************************************************************************
uint32_t total_luminance(void) {
    uint32_t running_total;
    running_total = 0;
    for (int i=0; i<numLEDs*3; i++)
        running_total += pixels[i];
    return running_total;
}
//*******************************************************************************
// Convert R,G,B to combined 32-bit color
uint32_t Color(uint8_t r, uint8_t g, uint8_t b) {
    // Take the lowest 7 bits of each value and append them end to end
    // We have the top bit set high (its a 'parity-like' bit in the protocol
    // and must be set!)
    return ((uint32_t)g << 16) | ((uint32_t)r << 8) | (uint32_t)b;
}
//*******************************************************************************
// store the rgb component in our array
void setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b) {
    if (n >= numLEDs) return; // '>=' because arrays are 0-indexed
    pixels[n*3  ] = g;
    pixels[n*3+1] = r;
    pixels[n*3+2] = b;
    //pc.printf("setPixelColor-4 inputs\n");   
}
//*******************************************************************************
void setPixelR(uint16_t n, uint8_t r) {
    if (n >= numLEDs) return; // '>=' because arrays are 0-indexed
    pixels[n*3+1] = r;
}
//*******************************************************************************
void setPixelG(uint16_t n, uint8_t g) {
    if (n >= numLEDs) return; // '>=' because arrays are 0-indexed
    pixels[n*3] = g;
}
//*******************************************************************************
void setPixelB(uint16_t n, uint8_t b) {
    if (n >= numLEDs) return; // '>=' because arrays are 0-indexed
    pixels[n*3+2] = b;
}
//*******************************************************************************
void setPixelColor(uint16_t n, uint32_t c) {
    if (n >= numLEDs) return; // '>=' because arrays are 0-indexed
    pixels[n*3  ] = (c >> 16);
    pixels[n*3+1] = (c >>  8);
    pixels[n*3+2] =  c;
    //pc.printf("setPixelColor-2 inputs\n");   
}
//*******************************************************************************
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<numPixels(); i++) {
      setPixelColor(i, c);
      show();
      wait_ms(wait);
  }
}
//*******************************************************************************
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(int WheelPos) {
  if(WheelPos < 85) {
   return Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if(WheelPos < 170) {
   WheelPos -= 85;
   return Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
   WheelPos -= 170;
   return Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}
//*******************************************************************************
void rainbow(uint8_t rwait) {
  uint16_t i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<numPixels(); i++) {
      setPixelColor(i, Wheel((i+j) & 255));
    }
    show();
    wait_ms(rwait);
  }
}
//*******************************************************************************
// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t rwait) {
  uint16_t i, j;

  for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
    for(i=0; i< numPixels(); i++) {
      setPixelColor(i, Wheel(((i * 256 / numPixels()) + j) & 255));
    }
    show();
    wait_ms(rwait);
  }
}
//*******************************************************************************
int main() {
    pc.baud(38400);
    setup();
    while (1) {
        pc.printf("Top of the Loop\n");
        colorWipe(Color(64, 0, 0), 50); // Red
        blankDelay(250);
        colorWipe(Color(0, 64, 0), 50); // Grn
        blankDelay(250);
        colorWipe(Color(0, 0, 64), 50); // Blu
        blankDelay(250);
        //rainbow(20);
        //blankDelay(250);
        //rainbowCycle(10);
        //blankDelay(250);
        
    }
}

//write(128);
//wait_us(10);
//write(0);
//wait_us(10);