Exemple #1
0
        public static void Main(string[] args)
        {
            //ErrorCode ec = ErrorCode.None;
            try
            {
                trinket.open();

                if (args.Length != 0)
                {
                    // open usb communications

                    if (args.Length == numberOfDiodes)
                    {
                        for (int i = 0; i < numberOfDiodes; i++)
                        {
                            String trimmed = args[i];
                            if (args[i].Substring(0, 2) == "0x")
                                trimmed = args[i].Substring(2, 6);
                            trinket.setRed(i, byte.Parse(trimmed.Substring(0, 2), System.Globalization.NumberStyles.HexNumber));
                            trinket.setGreen(i, byte.Parse(trimmed.Substring(2, 2), System.Globalization.NumberStyles.HexNumber));
                            trinket.setBlue(i, byte.Parse(trimmed.Substring(4, 2), System.Globalization.NumberStyles.HexNumber));
                        }
                        Console.Write(trinket.update().ToString());
                    }
                    else if (args.Length == 1) // one argument means either a game file or an animation
                    {
                        if (args[0].EndsWith(".lwax", true, null)) // animation, ignore case, default culture
                        {
                            XmlDocument anim = new XmlDocument();
                            anim.Load(args[0]);

                            XmlNodeList intensityList, stateList;
                            intensityList = anim.SelectNodes(".//Intensity[@LedHwType='6']"); // there are more premade animations for LEDWiz

                            stateList = anim.SelectNodes(".//State[@LedHwType='6']");

                            KeyFrame[] animation = new KeyFrame[stateList.Count];

                            String currentIntensity = new String("".ToCharArray());
                            String currentState = new String("".ToCharArray());
                            for (int i = 0; i < stateList.Count; i++)
                            {
                                XmlElement frameparent = (XmlElement)stateList[i].ParentNode;
                                // find out current intensity setting
                                for (int j = 0; j < intensityList.Count; j++) // index from one, because thats what the animation files do
                                {
                                   XmlElement parent = (XmlElement)intensityList[j].ParentNode;
                                   if(parent.GetAttribute("Number")==frameparent.GetAttribute("Number"))
                                   {
                                       XmlElement intensityElement = (XmlElement)intensityList[j];
                                       currentIntensity = intensityElement.GetAttribute("Value");
                                   }
                                }
                                XmlElement stateElement = (XmlElement)stateList[i];
                                currentState = stateElement.GetAttribute("Value");
                                // build frame
                                animation[i] = new KeyFrame(int.Parse(frameparent.GetAttribute("Duration")), currentIntensity,currentState);
                             }

                            foreach (KeyFrame frame in animation)
                            {
                                for (int i = 0; i < numberOfDiodes; i++)
                                {
                                    trinket.setRed(i,frame.intensity[i*3]);
                                    trinket.setGreen(i,frame.intensity[i*3 + 1]);
                                    trinket.setBlue(i, frame.intensity[i*3 + 2]);
                                }
                                Thread.Sleep(frame.duration);
                                //trinket.update();
                                Console.WriteLine(trinket.update().ToString());
                            }

                            Console.ReadKey();
                        }
                        else //gamefile
                        {
                            // Most games don't use all buttons, so lets paint it black
                            trinket.setToBlack();

                            // read game ini file
                            IniReader games = new IniReader("../../data/Colors.ini");

                            // supersede allows defining new games or redefining games already in colors.ini to new colors.
                            IniReader supersede = new IniReader("../../data/supersede.ini");

                            // buttons.ini define which diode goes to which button
                            IniReader buttons = new IniReader("../../data/Buttons.ini");

                            // colordefinitions.ini have name definitions for different colors.
                            IniReader colors = new IniReader("../../data/ColorDefinitions.ini");

                            // check if the argument matches any of the game files
                            if (games.ContainsSection(args[0]) && !supersede.ContainsSection(args[0])) // allow supersede to override the colors.ini settings.
                            {
                                setDiodeColorBuffers(games, args[0], buttons, colors);
                            }
                            else if (supersede.ContainsSection(args[0]))
                            {
                                setDiodeColorBuffers(supersede, args[0], buttons, colors);
                            }
                            else
                            {
                                throw new Exception("Game file " + args[0] + " not found in colors.ini nor in supersede.ini");
                            }
                            // set system button colors
                            setDiodeColorBuffers(buttons, "SystemButtonColors", buttons, colors);
                            Console.Write(trinket.update().ToString());
                        }

                    }
                    else
                    {
                        throw new Exception("Need exactly 28 arguments in hex (0xffffff) format or 1 game file argument");
                    }

                }
                else
                {
                    throw new Exception("Usage: " + Environment.NewLine +
                                        "   L3CLI 0xffffff 0xffffff 0xffffff 0xffffff ..." + Environment.NewLine +
                                        "       - Set all 28 diode colors using hex values, 0xRRGGBB" + Environment.NewLine +
                                        "   L3CLI [gametag]" + Environment.NewLine +
                                        "       - Set diode colors according to the colors.ini for that game tag. (diode to button mapping in buttons.ini)");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine();
                Console.WriteLine(ex.Message);
            }
            finally
            {
                // close down usb connection
                trinket.close();
            }
        }
Exemple #2
0
        static void Main(string[] args)
        {
            // handle closing of the window correctly (see #region cleanup)
            HandlerRoutine hr = new HandlerRoutine(ConsoleCtrlCheck);
            SetConsoleCtrlHandler(hr, true);

            using (var server = new MailslotServer("LagomLitenLedMailSlot"))
            {
                try
                {
                    trinket.open();

                    // Game lists etc:
                    // read game ini file
                    IniReader games = new IniReader("../../../configuration/Colors.ini");

                    // supersede allows defining new games or redefining games already in colors.ini to new colors.
                    IniReader supersede = new IniReader("../../../configuration/supersede.ini");

                    // buttons.ini define which diode goes to which button
                    IniReader buttons = new IniReader("../../../configuration/Buttons.ini");

                    // colordefinitions.ini have name definitions for different colors.
                    IniReader colors = new IniReader("../../../configuration/ColorDefinitions.ini");

                    while (!stop)
                    {
                        var msg = server.GetNextMessage();
                        if (msg != null) // incoming message
                        {
                            var arguments = msg.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                            if (arguments.Length == 1) // one argument means either a game file or an animation
                            {
                                if (arguments[0].Equals("stop")) // time to end this
                                {
                                    stop = true;
                                }
                                else if (arguments[0].EndsWith(".lwax", true, null)) // animation, ignore case, default culture
                                {
                                    XmlDocument anim = new XmlDocument();
                                    //TODO: add error handling
                                    anim.Load(arguments[0]);

                                    XmlNodeList intensityList, stateList;
                                    intensityList = anim.SelectNodes(".//Intensity[@LedHwType='6']"); // there are more premade animations for LEDWiz

                                    stateList = anim.SelectNodes(".//State[@LedHwType='6']");

                                    KeyFrame[] animation = new KeyFrame[stateList.Count];

                                    String currentIntensity = new String("".ToCharArray());
                                    String currentState = new String("".ToCharArray());
                                    for (int i = 0; i < stateList.Count; i++)
                                    {
                                        XmlElement frameparent = (XmlElement)stateList[i].ParentNode;
                                        // find out current intensity setting
                                        for (int j = 0; j < intensityList.Count; j++) // index from one, because thats what the animation files do
                                        {
                                            XmlElement parent = (XmlElement)intensityList[j].ParentNode;
                                            if (parent.GetAttribute("Number") == frameparent.GetAttribute("Number"))
                                            {
                                                XmlElement intensityElement = (XmlElement)intensityList[j];
                                                currentIntensity = intensityElement.GetAttribute("Value");
                                            }
                                        }
                                        XmlElement stateElement = (XmlElement)stateList[i];
                                        currentState = stateElement.GetAttribute("Value");
                                        // build frame
                                        animation[i] = new KeyFrame(int.Parse(frameparent.GetAttribute("Duration")), currentIntensity, currentState);
                                    }

                                    foreach (KeyFrame frame in animation)
                                    {
                                        for (int i = 0; i < numberOfDiodes; i++)
                                        {
                                            trinket.setRed(i, frame.intensity[i * 3]);
                                            trinket.setGreen(i, frame.intensity[i * 3 + 1]);
                                            trinket.setBlue(i, frame.intensity[i * 3 + 2]);
                                        }
                                        Thread.Sleep(frame.duration);
                                        //trinket.update();
                                        Console.WriteLine(trinket.update().ToString());
                                    }
                                }
                                else //gamefile
                                {
                                    // Most games don't use all buttons, so lets paint it black
                                    trinket.setToBlack();

                                    // check if the argument matches any of the game files
                                    if (games.ContainsSection(arguments[0]) && !supersede.ContainsSection(arguments[0])) // allow supersede to override the colors.ini settings.
                                    {
                                        setDiodeColorBuffers(games, arguments[0], buttons, colors);
                                    }
                                    else if (supersede.ContainsSection(arguments[0]))
                                    {
                                        setDiodeColorBuffers(supersede, arguments[0], buttons, colors);
                                    }
                                    else
                                    {
                                        Console.WriteLine("Game file " + arguments[0] + " not found in colors.ini nor in supersede.ini");
                                    }
                                    // set system button colors
                                    setDiodeColorBuffers(buttons, "SystemButtonColors", buttons, colors);
                                    Console.Write(trinket.update().ToString());
                                }
                            }
                            else if (arguments.Length == 2) // probably keyboardsender sends (Index,KeyboardEventName)
                            {
                                // check buttons.ini if the outputs are grouped (i.e four LEDs for one joystick)
                                foreach (string key in buttons.GetKeys("Diodes"))// expensive going through them all
                                {
                                    string[] diodes = buttons.GetValue(key, "Diodes").Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                                    if (diodes.Contains(arguments[0]))
                                    {
                                        foreach (string diode in diodes)
                                        {
                                            // most cases diodes is  only 1 long, in all other cases leds are grouped.
                                            // invert color rgb wise (keydown inverts once, keyup on same index will invert back to initial values)
                                            int index = int.Parse(diode);
                                            byte red = trinket.getRed(index);
                                            byte green = trinket.getGreen(index);
                                            byte blue = trinket.getBlue(index);
                                            trinket.setRed(index, (byte)(255 - red));
                                            trinket.setGreen(index, (byte)(255 - green));
                                            trinket.setBlue(index, (byte)(255 - blue));
                                        }
                                    }
                                }
                                Console.Write(trinket.update().ToString());
                            }
                            else if (arguments.Length == numberOfDiodes) // set colors directly
                            {
                                for (int i = 0; i < numberOfDiodes; i++)
                                {
                                    String trimmed = arguments[i];
                                    if (arguments[i].Substring(0, 2) == "0x")
                                        trimmed = arguments[i].Substring(2, 6);
                                    trinket.setRed(i, byte.Parse(trimmed.Substring(0, 2), System.Globalization.NumberStyles.HexNumber));
                                    trinket.setGreen(i, byte.Parse(trimmed.Substring(2, 2), System.Globalization.NumberStyles.HexNumber));
                                    trinket.setBlue(i, byte.Parse(trimmed.Substring(4, 2), System.Globalization.NumberStyles.HexNumber));
                                }
                                Console.Write(trinket.update().ToString());
                            }
                        }
                        Thread.Sleep(1);
                    }

                }
                catch (Exception ex)
                {
                    Console.WriteLine();
                    Console.WriteLine(ex.Message);
                    Console.ReadKey();
                }
                finally
                {
                    trinket.close();
                }
            }
        }