/*
 * Board:   Seeeduino XAIO
 * Target:  12 x 12 NeoPixel-powered ping pong ball array
 * Author:  Clive "Max" Maxfield (max@clivemaxfield.com) 
 * License: The MIT License (See full license at the bottom of this file)
 *
 * Notes: V1 Display classic Adafruit rainbow effect from left to right columns
 *        V2 Modify effect to display on diagonals
 */

#include <Adafruit_NeoPixel.h>             // Library for NeoPixels

#define NUM_NEOS                       145 // 144 in array plus "sacrificial" pixel as voltage level shifter
#define NUM_ROWS                        12
#define NUM_COLS                        12

#define NUM_DIAGS                       23 // Number of diagonals = (NUM_ROWS + NUM_COLS - 1)

#define RAINBOW_INTERVAL                15
#define RAINBOW_STEPS                  255


// Assign any input/output (I/O) pins
const int PinNeos  = 10;


// Instantiate NeoPixel(s)
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel Neos(NUM_NEOS, PinNeos, NEO_GRB + NEO_KHZ800);


// Define any global variables
uint16_t RainbowIndex;
uint32_t LastRainbowUpdateTime;
uint32_t RainbowColors[NUM_DIAGS];


void setup ()
{
//    Serial.begin (9600);
    pinMode(PinNeos, OUTPUT);

    Neos.begin();
    Neos.show();

    RainbowIndex = 0;
    LastRainbowUpdateTime = millis();
}


void loop ()
{
    RainbowEffect();
}


void RainbowEffect ()
{
    uint32_t currentTime;

    currentTime = millis();
    if ( (currentTime - LastRainbowUpdateTime) > RAINBOW_INTERVAL )
    {
        LastRainbowUpdateTime = currentTime;

        GenerateRainbowColors();
        LoadRainbowColors();
        Neos.show();
    }
}


void GenerateRainbowColors ()
{
    // Generate the number of colors to match the number of diagonals
    for (int iDiag = 0; iDiag < NUM_DIAGS; iDiag++)
    {
        RainbowColors[iDiag] = RainbowColorWheel( ( ( (iDiag * 256) / NUM_DIAGS ) + RainbowIndex) & 255);
    }
    RainbowIndex++;

    if (RainbowIndex >= RAINBOW_STEPS)
    {
       RainbowIndex = 0;
    }
}


// Input a value 0 to 255 to get a color value (colours transition R > B > G and back to R)
uint32_t RainbowColorWheel (uint8_t wheelPosition)
{
    uint32_t newColor;

    if (wheelPosition < 85)
    {
        newColor = BuildColor( (wheelPosition * 3), (255 - (wheelPosition * 3)), 0 );
    }
    else if (wheelPosition < 170)
    {
        wheelPosition -= 85;
        newColor = BuildColor( (255 - (wheelPosition * 3)), 0, (wheelPosition * 3) );
    }
    else
    {
        wheelPosition -= 170;
        newColor = BuildColor( 0, (wheelPosition * 3), (255 - (wheelPosition * 3)) );
    }

    return newColor;
}


// Returns a 32-bit color built out of 8-bit RGB elements (the most-significant byte is 0)
uint32_t BuildColor (uint8_t red, uint8_t green, uint8_t blue)
{
    return ((uint32_t)red << 16) | ((uint32_t)green << 8) | (uint32_t)blue;
}


// Main diagonal runs from upper-left corner to lower right ([0,11] to [11,0])
void LoadRainbowColors ()
{
    int iNeo;
    int xStart;
    int xEnd;
    int yStart;
    int halfDiags = NUM_DIAGS / 2;

    for (int iDiag = 0; iDiag < NUM_DIAGS; iDiag++)
    {
        if (iDiag <= halfDiags )
        {
            // Diagonal is on lower left-hand-side of array
            xStart = 0;
            xEnd   = iDiag;
            yStart = iDiag;
        }
        else
        {
            // Diagonal is on upper right-hand side of array
            xStart = iDiag - halfDiags;
            xEnd   = halfDiags;
            yStart = halfDiags;
        }

        for (int xInd = xStart, yInd = yStart; xInd <= xEnd; xInd++, yInd--)
        {
            iNeo = GetNeoNum(xInd, yInd);            
            Neos.setPixelColor(iNeo, RainbowColors[iDiag]);
        }
    }
}


// Accept an x/y (column/row) value and return the corresponding pixel number
int GetNeoNum (int xInd, int yInd)
{
    int iNeo;

    iNeo = yInd * NUM_COLS;

    if ( (yInd % 2) == 0)
    {   // Even row
        iNeo = iNeo + (12 - xInd);
    }
    else
    {   // Odd row
        iNeo = iNeo + (xInd + 1);
    }

    return iNeo;
}


/*
 * Copyright (c) 2020 Clive "Max" Maxfield
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and any associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHOR(S) OR COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */