private static Command getCommand(Hashtable result)
        {
            Command answer = new Command();
            answer.CommandType = GetCommandType(result, COMMAND_TYPE, CommandType.Parent);
            answer.PauseAfter = GetInt(result, PAUSE_AFTER);
            answer.StepTime = GetInt(result, STEP_TIME);
            answer.Commands = new ArrayList();
            var commands = (ArrayList)result[getKey(result, COMMANDS)];
            if (commands != null)
            {
                foreach (Hashtable c in commands)
                {
                    answer.Commands.Add(getCommand(c));
                }
            }
            answer.Repetitions = GetInt(result, REPETITIONS);
            answer.PauseBetween = GetInt(result, PAUSE_BETWEEN);
            answer.ColourSet = GetPixelColourList(result, COLOUR_SET);
            answer.PrimaryColour = GetPixelColour(result, PRIMARY_COLOUR);
            answer.SecondaryColour = GetPixelColour(result, SECONDARY_COLOUR);
            answer.StartingPosition = GetInt(result, STARTING_POSITION);
            answer.PixelPositions = GetIntArray(result, PIXEL_POSITIONS);
            answer.RotateIncrement = GetInt(result, ROTATE_INCREMENT);
            answer.Cycles = GetInt(result, CYCLES);

            return answer;
        }
        private static Command getCommand(Hashtable result, JSONParser parser)
        {
            CommandType commandType;
            int commandTypeInt;
            int pauseAfter;
            int stepTime;
            ArrayList commands;
            int repetitions;
            int pauseBetween;
            ArrayList colourSet;
            int primaryColourInt;
            int secondaryColourInt;
            int startingPosition;
            ArrayList pixelPositions;
            int rotateIncrement;
            int cycles;

            Command answer = new Command();

            if (parser.Find("CommandType", result, out commandTypeInt))
            {
                commandType = (CommandType)commandTypeInt;
            }
            else
            {
                commandType = (CommandType)0;
            }

            if (parser.Find("PauseAfter", result, out pauseAfter))
                answer.PauseAfter = pauseAfter;

            if (parser.Find("StepTime", result, out stepTime))
                answer.StepTime = stepTime;

            answer.Commands = new ArrayList();
            if(parser.Find("Commands", result, out commands))
            {
                foreach (Hashtable c in commands)
                {
                    answer.Commands.Add(getCommand(c, parser));
                }

            }

            if (parser.Find("Repetitions", result, out repetitions))
                answer.Repetitions = repetitions;

            if (parser.Find("PauseBetween", result, out pauseBetween))
                answer.PauseBetween = pauseBetween;

            if(parser.Find("ColourSet", result, out colourSet))
            {
                answer.ColourSet = new PixelColour[colourSet.Count];
                int pos = 0;
                foreach (int i in colourSet)
                {
                    answer.ColourSet[pos] = (PixelColour)(int)colourSet[pos];
                    pos++;

                }
            }

            if (parser.Find("PrimaryColour", result, out primaryColourInt))
                answer.PrimaryColour = (PixelColour)primaryColourInt;

            if (parser.Find("SecondaryColour", result, out secondaryColourInt))
                answer.SecondaryColour = (PixelColour)secondaryColourInt;

            if (parser.Find("StartingPosition", result, out startingPosition))
                answer.StartingPosition = startingPosition;

            if (parser.Find("PixelPositions", result, out pixelPositions))
            {
                answer.PixelPositions = new int[pixelPositions.Count];
                int pos = 0;
                foreach (int i in pixelPositions)
                {
                    answer.PixelPositions[pos] = (int)pixelPositions[pos];
                    pos++;

                }
            }

            if (parser.Find("RotateIncrement", result, out rotateIncrement))
                answer.RotateIncrement = rotateIncrement;

            if (parser.Find("Cycles", result, out cycles))
                answer.Cycles = cycles;

            return answer;
        }
 public void RunCommand(Command command)
 {
     switch (command.CommandType) {
         case CommandType.Parent:
             foreach (Command child in command.Commands) {
                 RunCommand(child);
             }
             break;
         case CommandType.Repeat:
             for (int i = 0; i < command.Repetitions; i++) {
                 foreach (Command child in command.Commands) {
                     RunCommand(child);
                 }
                 Util.Delay(command.PauseBetween);
             }
             break;
         case CommandType.Light1Pixel:
             FrameClear();
             FrameSet(getPixel(command.PrimaryColour), new ushort[] { (ushort)command.StartingPosition });
             FrameDraw();
             break;
         case CommandType.LightMultiPixel:
             FrameClear();
             FrameSet(getPixel(command.PrimaryColour), UShortArrayFromIntArray(command.PixelPositions));
             FrameDraw();
             break;
         case CommandType.Set1Pixel:
             FrameSet(getPixel(command.PrimaryColour), new ushort[] { (ushort)command.PixelPositions[0] });
             FrameDraw();
             break;
         case CommandType.SetMultiPixel:
             for (int i = 0; i < command.PixelPositions.Length; i++) {
                 FrameSet(getPixel(command.PrimaryColour), (ushort)command.PixelPositions[i]);
             }
             FrameDraw();
             break;
         case CommandType.Rotate:
             FrameShift(command.RotateIncrement == 0 ? 1 : command.RotateIncrement);
             FrameDraw();
             break;
         case CommandType.AllOff:
             FrameClear();
             FrameDraw();
             break;
         case CommandType.AllOn:
             FrameSet(getPixel(command.PrimaryColour));
             FrameDraw();
             break;
         case CommandType.Blink:
             Blink(command.PauseBetween, command.Repetitions);
             break;
         case CommandType.Spin:
             SpinColour(getPixel(command.PrimaryColour), command.Cycles, command.StepTime);
             break;
         case CommandType.SpinOnBackground:
             SpinColourOnBackground(getPixel(command.PrimaryColour), getPixel(command.SecondaryColour), command.Cycles, command.StepTime);
             break;
         case CommandType.AlternateColours:
             var colourList = GetColourListFromColourSet(command.ColourSet);
             FrameSet(colourList);
             FrameDraw();
             break;
         case CommandType.ColourBlocks:
             var thecolourList = GetColourListFromColourSet(command.ColourSet);
             FrameSetBlocks(thecolourList);
             FrameDraw();
             break;
         case CommandType.Wait:
             break;
         default:
             break;
     }
     if (command.CommandType != CommandType.Parent)
         Util.Delay(command.PauseAfter);
 }
        public void Finale()
        {
            Command command = new Command() {
                CommandType = CommandType.Parent,
                Commands = new ArrayList()
                {
                    new Command()
                    {
                        CommandType = CommandType.AllOff
                    },
                    new Command()
                    {
                        CommandType = CommandType.AllOn,
                        PrimaryColour= PixelColour.Aquamarine,
                        PauseAfter=500
                    },
                    new Command()
                    {
                        CommandType = CommandType.LightMultiPixel,
                        PrimaryColour = PixelColour.Crimson,
                        PixelPositions = new int[] {3, 4, 5, 9, 10, 11},
                        PauseAfter=500
                    },
                    new Command()
                    {
                        CommandType=CommandType.Rotate,
                        StepTime=50,
                        Cycles=6,
                        PauseAfter=500
                    },
                    new Command()
                    {
                        CommandType = CommandType.Repeat,
                        PauseBetween = 200,
                        Repetitions = 5,
                        Commands = new ArrayList()
                        {
                            new Command()
                            {
                                CommandType = CommandType.AllOff,
                                PauseAfter=250
                            },
                            new Command()
                            {
                                CommandType = CommandType.AllOn,
                                PrimaryColour = PixelColour.LightGreen,
                                PauseAfter = 250
                            },

                        }
                    },
                    new Command()
                    {
                        CommandType = CommandType.Wait,
                        PauseAfter = 1000
                    },
                    new Command()
                    {
                        CommandType = CommandType.ColourBlocks,
                        ColourSet = new PixelColour[]
                        {
                            PixelColour.Red,
                            PixelColour.Green,
                            PixelColour.Blue
                        },
                        PauseAfter = 0
                    },
                    new Command()
                    {
                        CommandType = CommandType.Repeat,
                        Repetitions = 36,
                        PauseBetween = 50,
                        PauseAfter = 1000,
                        Commands = new ArrayList()
                        {
                            new Command()
                            {
                                CommandType = CommandType.Rotate,
                                RotateIncrement = -1
                            }
                        }
                    },
                    new Command()
                    {
                        CommandType = CommandType.AllOff,
                        PauseAfter = 1000
                    }
                }
            };

            RunCommand(command);
        }