/*
 * 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 First test of full-up 12 x 12 array
 *          V2 Add (X,Y) cross reference to pixel number
 */


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

#define TICK                            10 // Main cycle clock = 10 milliseconds (not used in this sketch)

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

#define COLOR_WHITE              0xFFFFFFU
#define COLOR_BLACK              0x000000U

#define COLOR_RED                0xFF0000U
#define COLOR_GREEN              0x00FF00U
#define COLOR_BLUE               0x0000FFU

#define COLOR_YELLOW             0xFFFF00U
#define COLOR_CYAN               0x00FFFFU
#define COLOR_HOT_PINK           0xFF00FFU

#define COLOR_DARK_ORANGE        0xFF8000U
#define COLOR_AMBER              0xFFC000U
#define COLOR_LIME_GREEN         0xC0FF00U
#define COLOR_CHARTREUSE         0x80FF00U
#define COLOR_SPRING_GREEN       0x00FFC0U
#define COLOR_AZURE              0x0080FFU
#define COLOR_ELECTRIC_VIOLET    0x8000FFU


// 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
int TestCycleTime = 50;


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

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


void loop ()
{
    LightOneAfterAnother(COLOR_RED);
    LightOneAfterAnother(COLOR_GREEN);
    LightOneAfterAnother(COLOR_BLUE);
}


void LightOneAfterAnother (uint32_t thisColor)
{
    int iNeo;
   
    for (int yInd = 0; yInd < NUM_ROWS; yInd++)
    {
        for (int xInd = 0; xInd < NUM_COLS; xInd++)
        {
            iNeo = GetNeoNum(xInd, yInd);            
            Neos.setPixelColor(iNeo, thisColor);
            Neos.show();
            delay(TestCycleTime);
        }
    }
}


// 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.
 */