 // Add the pattern library.
 #include "patterns.h"
 
+/*
+Available commands over Serial:
+- <n>         = Set Value for all Motors : Returns nothing
+0 <n>         = Set Value for Motor 1 : Return current Value
+1 <n>         = Set Value for Motor 2 : Return current Value
+2 <n>         = Set Value for Motor 3 : Return current Value
+c             = Cycle Pattern : Returns running pattern
+g             = Get number of currently running pattern : Returns running pattern
+i             = Read input 0 or 1 : Returns value of input
+l <n>         = Set LED power : Return current Value
+p             = Decrease Power : Returns new Powerlevel
+P             = Increase Power : Returns new Powerlevel
+r <n>         = Run a specific pattern, r,0; r,1; etc. : Returns running pattern
+s             = Stop all Motors, reset Patternnumber, Power & Timescale : Returns nothing
+t             = Decrease pattern time, as in everything goes faster : Returns new Timescale
+T             = Increase pattern time, as in everything goes slower : Returns new Timescale
+{<n>,<n>,<n>} = Set Value for Motor1,2 and 3 in a single line : Returns nothing
+
+New from v0.2.2:
+G = get Powerlevel : Returns Powerlevel
+v = get Version : Returns Version information
+
+New from v0.2.4:
+U ... = Play the User transmitted pattern, for example:
+U 3,255 0 0 500, 0 0 255 500, 0 255 0 500
+3 Entries
+First Motor 255, Second 0, Third 0, for 500ms
+First Motor 0, Second 0, Third 255, for 500ms
+First Motor 0, Second 255, Third 0, for 500ms
+
+allStop Pattern added
+
+New from v0.2.5:
+u = Returns current Userpattern
+*/
+
+const String txtVersion = "Mod-Sketch v0.2.5";
+int* userPattern; // Will dynamicly be allocated/freed
+int userPatternSize = 0;
+int userPatternNumber = 0; // Number of the Userpattern
+int currentPatternNumber = 0; // Currently added Pattern number
+
 void setup() {
+  Toy.setID(); // Set Toy ID using default Value, currently 1 = MOD
+  
   // Button will increase/decrease power by 20%
   Toy.setPowerScaleStep(0.2);
   

  Serial.begin(9600);
}



void loop() {
  serialProcessor();
}

// Serial console. Read a character in to command[1], and a value in to val
void serialProcessor() { 
  char command[1];
  byte val;

  if (Serial.available() > 0) {
    Serial.readBytes(command,1);
    if (command[0] == 'l') { // Set LED power
      val = Serial.parseInt();
      Toy.setLED(0,val);
      Serial.println(val);
    } else if (command[0] == '0' || command[0] == '1' || command[0] == '2') { // Set power of individual motor
      val = Serial.parseInt();
      Toy.setOutput(command[0], val);
      Serial.println(val);
    } else if (command[0] == '-') { // Catch '-1', set power of all motors
      int out = Serial.parseInt();
      out *= -1;
      val = Serial.parseInt();
      Toy.setOutput(out,val);
    } else if (command[0] == '{') {
      int motors[3];
      motors[0] = Serial.parseInt();
      motors[1] = Serial.parseInt();
      motors[2] = Serial.parseInt();
      for (int i = 0; i < 3; i++) {
        if (motors[i] >= 0) {
          Toy.setOutput(i, motors[i]);
        }
      }
    } else if (command[0] == 'p') {
      Serial.println(Toy.decreasePower());
    } else if (command[0] == 'P') {
      Serial.println(Toy.increasePower());
    } else if (command[0] == 't') { // Decrease pattern time, as in everything goes faster
      Serial.println(Toy.decreaseTime());
    } else if (command[0] == 'T') {
      Serial.println(Toy.increaseTime());
    } else if (command[0] == 'r') { // Run a specific pattern, r,0; r,1; etc.
      val = Serial.parseInt();
      Toy.runPattern(val);
      Serial.println(Toy.getPattern());
    } else if (command[0] == 'g') {  // Get number of currently running pattern
      Serial.println(Toy.getPattern());
    } else if (command[0] == 's') {
      Toy.stop();
    } else if (command[0] == 'c') {
      Toy.cyclePattern();
      Serial.println(Toy.getPattern());
    } else if (command[0] == 'i') { // Read input 0 or 1 and print it to serial port
      int in = Serial.parseInt();
      in %= Toy.device.inCount;
      Serial.println(Toy.getInput(in));
    }
  }
}


// Cycle through all the outputs, turn the LED on and leave it on to show that we're on
void startupSequence() {
  int outs = Toy.device.outCount;
  int outInterval = 100 / outs;

  Toy.setLED(0,128);

  for (int i = 0; i < outs; i++) {
    Toy.setOutput(i, 100+outInterval*i);
    delay(100);
    Toy.setOutput(i, 0);
    delay(50);
  }
  for (int i = 0; i < 3; i++) {
    Toy.setLED(0,0);
    delay(100);
    Toy.setLED(0,128);
    delay(100);
  }
}

/* Mod Sketch v0.2.5 -- written by Craig Durkin / Comingle. */
/* Additional Commands & Documentation added by Holger 'Lynxx' Hippenstiel */
/* This software comes pre-loaded on Comingle Mod sex toys */

// Include necessary libraries: Wire.h and WiiChuck.h for the nunchuck, OSSex.h for the Mod library
