Esempio n. 1
0
 public static void setDiodeColorBuffers(IniReader tagFile, string section, IniReader buttonFile, IniReader colorFile)
 {
     foreach (String key in tagFile.GetKeys(section))
     {
         string[] diodes = buttonFile.GetValue(key, "Diodes").Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
         foreach (string diode in diodes)
         {
             int index = int.Parse(diode);
             string[] rgb = colorFile.GetValue(tagFile.GetValue(key, section), "Colors").Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
             trinket.setRed(index, byte.Parse(rgb[0]));
             trinket.setGreen(index, byte.Parse(rgb[1]));
             trinket.setBlue(index, byte.Parse(rgb[2]));
         }
     }
 }
Esempio n. 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();
                }
            }
        }