Fork of AndyA/cylon, with support for STM32F4 and LPC8xx

Dependencies:   mbed wsDrive

Fork of cylon by Andy A

main.cpp

Committer:
JojoS
Date:
2020-04-24
Revision:
4:4482500a0e5c
Parent:
2:c658370b1a08

File content as of revision 4:4482500a0e5c:

#include "mbed.h"
#include "wsDrive.h"

DigitalOut led1(LED1);

// time period between each movement
#define updatePeriodMS 10

// number of LEDs in chain
#define chainLen 300

// set the pulldown and then create the driver
//wsDrive ledDriver(P1_22, P0_22, P1_15);
wsDrive ledDriver(PC_12, NC, NC);

Timer updateRateTimer;

// pixel storage buffer
pixelInfo16 pixelData[chainLen];

const uint8_t trailCount = 3;

// info for each trail
struct trailInfo {
    float start;  // location of the trail from 0 to chainLen-1
    int length;   // length of the trail
    float speed;  // speed in moves at in LEDs per update
    int backgroundRatio; // background glow level
    pixelInfo colour;  // colour (and brightness) at the start of the chain
    bool dir;       // direction of travel - true = increasing location
};


struct trailInfo lines[trailCount];


void blankBuffer(pixelInfo *Ptr)
{
    memset( (void *)Ptr, 0, chainLen*sizeof(pixelInfo) );
}

void blankBuffer(pixelInfo16 *Ptr)
{
    memset( (void *)Ptr, 0, chainLen*sizeof(pixelInfo16) );
}

void setPixel (pixelInfo *pixel, pixelInfo *colour, float level)
{
    pixel->R = (colour->R * level);
    pixel->G = (colour->G * level);
    pixel->B = (colour->B * level);
}

void addPixel (pixelInfo16 *pixel, pixelInfo *colour, float level)
{
    pixel->R = pixel->R + (int)(colour->R * level);
    pixel->G = pixel->G + (int)(colour->G * level);
    pixel->B = pixel->B + (int)(colour->B * level);
}

void subtractPixel (pixelInfo16 *pixel, pixelInfo *colour, float level)
{
    pixel->R = pixel->R - (int)(colour->R * level);
    pixel->G = pixel->G - (int)(colour->G * level);
    pixel->B = pixel->B - (int)(colour->B * level);
}


void setTrail(bool add, pixelInfo16* buffer, pixelInfo *colour, int peakPoint, bool increasing, int len)
{
    int pixelToUpdate = peakPoint;
    for (int pixel = 0; pixel < len; pixel++) {

        if (add)
            addPixel((buffer+pixelToUpdate), colour, 1.0f - (float)pixel/(float)len);
        else
            subtractPixel((buffer+pixelToUpdate), colour, 1.0f - (float)pixel/(float)len);

        increasing ? pixelToUpdate-- : pixelToUpdate++;

        if (pixelToUpdate == chainLen) {
            increasing = false;
            pixelToUpdate = chainLen-2;
        }
        if (pixelToUpdate == -1) {
            increasing = true;
            pixelToUpdate = 1;
        }
    }
}

void removeTrail (pixelInfo16* buffer, pixelInfo *colour, int peakPoint, bool increasing, int len)
{
    setTrail (false, buffer, colour, peakPoint, increasing, len);
}

void addTrail (pixelInfo16* buffer, pixelInfo *colour, int peakPoint, bool increasing, int len)
{
    setTrail (true, buffer, colour, peakPoint, increasing, len);
}


int main ()
{
    //LEDs = 0;


    // set up the lights.
    lines[0].start = 0;
    lines[0].speed = 1.6;
    lines[0].length = 30;
    lines[0].backgroundRatio = 40;
    lines[0].colour.R = 200;
    lines[0].colour.G = 0;
    lines[0].colour.B = 0;
    lines[0].dir = true;

#if 1
    lines[1].start = 150;
    lines[1].speed = 1.4;
    lines[1].length = 30;
    lines[1].backgroundRatio = 40;
    lines[1].colour.R = 0;
    lines[1].colour.G = 0;
    lines[1].colour.B = 200;
    lines[1].dir = true;

    lines[2].start = 299;
    lines[2].speed = 1.1;
    lines[2].length = 30;
    lines[2].backgroundRatio = 40;
    lines[2].colour.R = 0;
    lines[2].colour.G = 200;
    lines[2].colour.B = 0;
    lines[2].dir = false;
#endif

    // clear the buffer
    blankBuffer(pixelData);

    // add the optional background
    /*
        for (int i = 0; i< chainLen; i++) {
            for (int j = 0; j <trailCount; j++) {
                addPixel((pixelData+i), &(lines[j].colour), 1.0/lines[j].backgroundRatio);
            }
        }
    */

// add the initial lines
    for (int j = 0; j <trailCount; j++) {
        addTrail (pixelData, &(lines[j].colour), lines[j].start, lines[j].dir, lines[j].length); // set the LED data
    }
    // give the LED driver the buffer to use.
    ledDriver.setData(pixelData, chainLen);

    //LEDs = 1;

    updateRateTimer.start();
    while (true) {
        ledDriver.sendData(); // send the LED data

        led1 = !led1;
        //LEDs = LEDs+1;

        // subtract the current trail locations and then add the new locations.
        for (int j = 0; j <trailCount; j++) {
            removeTrail (pixelData, &(lines[j].colour), lines[j].start, lines[j].dir, lines[j].length); // set the LED data

            lines[j].dir ? lines[j].start+=lines[j].speed : lines[j].start-=lines[j].speed;
            if ((int)lines[j].start >= chainLen) {
                lines[j].dir = false;
                lines[j].start = chainLen-1 - lines[j].speed;
            }
            if ((int)lines[j].start <= -1) {
                lines[j].dir = true;
                lines[j].start = lines[j].speed;
            }
            addTrail (pixelData, &(lines[j].colour), lines[j].start, lines[j].dir, lines[j].length); // set the LED data
        }
        // wait for the next update time.
        while (updateRateTimer.read_ms() < updatePeriodMS) {
        }
        updateRateTimer.reset();
    }

}