//Portal Cube - 2017 Kevin Rye - V1.0
//Download Adafruit Neopixel and MMA8451 libraries from Adafruit.com

/*
    @file     Adafruit_MMA8451.h
    @author   K. Townsend (Adafruit Industries)
    @license  BSD (see license.txt)

    This is an example for the Adafruit MMA8451 Accel breakout board
    ----> https://www.adafruit.com/products/2019

    NeoPixel Ring simple sketch (c) 2013 Shae Erisson
    released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library

    Adafruit invests time and resources providing this open source code,
    please support Adafruit and open-source hardware by purchasing
    products from Adafruit!
*/

#include <Wire.h>
#include <Adafruit_MMA8451.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_NeoPixel.h>

Adafruit_MMA8451 mma = Adafruit_MMA8451();

#define PIN            4

#define NUMPIXELS      72

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int delayval = 0; // delay

int i = 0;
int R = 0;
int G = 0;
int B = 0;

long randNumber1;
long randNumber2;
long randNumber3;

void setup(void) {
  //comment out serial data when not connected to a computer, or sketch will not work when using an Arduino micro!
  // Serial.begin(9600);
  // while (!Serial); //needed for Arduino micro serial communication
  //  Serial.println("Adafruit MMA8451 test!");

  pixels.begin(); // This initializes the NeoPixel library.

  if (! mma.begin()) {
    Serial.println("Couldnt start");
    while (1);
  }
  //  Serial.println("MMA8451 found!");

  mma.setRange(MMA8451_RANGE_2_G);

  // Serial.print("Range = "); Serial.print(2 << mma.getRange());
  //  Serial.println("G");

}

void loop() {

  randNumber1 = random(0, 255);
  randNumber2 = random(0, 255);
  randNumber3 = random(0, 255);

  // Read the 'raw' data in 14-bit counts
  mma.read();
  //Serial.print("X:\t"); Serial.print(mma.x);
  //Serial.print("\tY:\t"); Serial.print(mma.y);
  // Serial.print("\tZ:\t"); Serial.print(mma.z);
  //Serial.println();

  /* Get a new sensor event */
  sensors_event_t event;
  mma.getEvent(&event);

  /* Display the results (acceleration is measured in m/s^2) */
  // Serial.print("X: \t"); Serial.print(event.acceleration.x); Serial.print("\t");
  //Serial.print("Y: \t"); Serial.print(event.acceleration.y); Serial.print("\t");
  //Serial.print("Z: \t"); Serial.print(event.acceleration.z); Serial.print("\t");
  //Serial.println("m/s^2 ");

  /* Get the orientation of the sensor */
  uint8_t o = mma.getOrientation();

  switch (o) {
    case MMA8451_PL_PUF:
      //  Serial.println("Portrait Up Front");
      R = 0;
      G = 0;
      B = 255;
      delayval = 0;
      break;
    case MMA8451_PL_PUB:
      //   Serial.println("Portrait Up Back");
      R = 0;
      G = 0;
      B = 255;
      delayval = 0;
      break;
    case MMA8451_PL_PDF:
      //  Serial.println("Portrait Down Front");
      R = 0;
      G = 0;
      B = 255;
      delayval = 0;
      break;
    case MMA8451_PL_PDB:
      //   Serial.println("Portrait Down Back");
      R = 0;
      G = 0;
      B = 255;
      delayval = 0;
      break;
    case MMA8451_PL_LRF:
      //  Serial.println("Landscape Right Front");
      R = 255;
      G = 255;
      B = 0;
      delayval = 0;
      break;
    case MMA8451_PL_LRB:
      //  Serial.println("Landscape Right Back");
      R = 255;
      G = 255;
      B = 0;
      delayval = 0;
      break;
    case MMA8451_PL_LLF:
      //  Serial.println("Landscape Left Front");
      R = 255;
      G = 255;
      B = 0;
      delayval = 0;
      break;
    case MMA8451_PL_LLB:
      //  Serial.println("Landscape Left Back");

      R = randNumber1; //this is my little Easter Egg. In this orientation, random colors are displayed.
      G = randNumber2;
      B = randNumber3;
      delayval = 0;
      break;
  }
  //Serial.println();
  //delay(500);

  for ( i = 0; i < NUMPIXELS; i++) {

    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(R, G, B));
    pixels.show(); // This sends the updated pixel color to the hardware.

    delay(delayval); // Delay for a period of time (in milliseconds).

  }
}
