Ejemplo n.º 1
0
        private void ThreadProc()
        {
            ushort[] data  = new ushort[5];
            ushort   edges = 0;
            float    inputToOutputRatio;
            short    MinPWMFrequency, MaxPWMFrequency;

            while (!terminated)
            {
                queryProcedure.InvokeEx(data);

                if (data[0] > 0)
                {
                    if (opMode == OperatingMode.AdaptiveMobile)
                    {
                        inputToOutputRatio = MobileInOutRatio;
                        MinPWMFrequency    = MobileMinFrequency;
                        MaxPWMFrequency    = MobileMaxFrequency;
                        edges = data[1];
                    }
                    else
                    {
                        inputToOutputRatio = PortalInOutRatio;
                        MinPWMFrequency    = PortalMinFrequency;
                        MaxPWMFrequency    = PortalMaxFrequency;
                        edges = (data[2] > data[3]) ? data[2] : data[3];
                    }

                    // speed in miles per hour
                    CurrentSpeedMPH = ((float)edges / (float)data[0]) * 10.0f;

                    // update PWM rate
                    if ((opMode == OperatingMode.AdaptiveMobile) || (opMode == OperatingMode.AdaptivePortal))
                    {
                        int frequency =
                            (int)System.Math.Round(CurrentSpeedMPH * 100.0f * inputToOutputRatio);

                        if (frequency < MinPWMFrequency)
                        {
                            frequency = (int)MinPWMFrequency;
                        }
                        if (frequency > MaxPWMFrequency)
                        {
                            frequency = (int)MaxPWMFrequency;
                        }

                        UpdatePWMFrequency(frequency);
                    }

                    // Debug.Print("Time: " + data[0].ToString() + "ms Edges: " + edges.ToString() + " Speed: " + CurrentSpeedMPH.ToString());
                }

                Thread.Sleep(300);
            }
        }
Ejemplo n.º 2
0
        public static void Init(out RLP.Procedure lcdInit, out RLP.Procedure lcdSet, byte[] loadPlugin = null)
        {
            if (initialized)
            {
                throw new Exception("Already initialized");
            }

            //NOTE: use your own unlock code
            RLP.Unlock("[email protected]", new byte[]
            {
                0xFB, 0xCC, 0x7D, 0xDA, 0x8E, 0x87, 0x58, 0xAB,
                0x95, 0x8E, 0x68, 0x02, 0x1F, 0xE6, 0xD6, 0x14,
                0x3C, 0xBD, 0x3E, 0xD1, 0xBE, 0xF0, 0xCE, 0xFB,
                0x1D, 0xD9, 0xC4, 0xEC, 0xE2, 0x2A, 0xFD, 0xCD
            });

            byte[] elf_file = Extension.GetBytes(Extension.BinaryResources.Extension);
            RLP.LoadELF(elf_file);
            RLP.InitializeBSSRegion(elf_file);
            RLP.Procedure VsInit = RLP.GetProcedure(elf_file, "VsInit");
            vsStreamData       = RLP.GetProcedure(elf_file, "VsStreamData");
            vsNoMoreData       = RLP.GetProcedure(elf_file, "VsNoMoreData");
            vsLoadPlugin       = RLP.GetProcedure(elf_file, "VsLoadPluginFromArray");
            vsSetVolume        = RLP.GetProcedure(elf_file, "VsSetVolume");
            vsSetBassAndTreble = RLP.GetProcedure(elf_file, "VsSetBassAndTreble");

            lcdInit = RLP.GetProcedure(elf_file, "LcdInit");
            lcdSet  = RLP.GetProcedure(elf_file, "LcdSetLevel");

            elf_file = null;

            int result;

            if ((result = loadPlugin == null ? VsInit.Invoke() : VsInit.InvokeEx(loadPlugin, loadPlugin.Length)) != 0)
            {
                throw new Exception("Could not init VS Driver. Reason " + result.ToString());
            }
            loadedPlugin = loadPlugin;

            RLP.RLPEvent += (data, time) =>
            {
                switch (data)
                {
                case 0x01:
                    //data was streamed
                    wait.Set();     //notify any waiter
                    break;

                case 0x02:
                    //error when closing stream
                    //this means the VS was reseted,
                    //reload plugin, set volume, bass and treble
                    if (loadedPlugin != null)
                    {
                        vsLoadPlugin.InvokeEx(loadedPlugin, loadedPlugin.Length);
                    }
                    RaiseException("Error closing stream!");
                    break;
                }
            };
            initialized = true;

            balanceGeneral = 0;
            Volume         = 255; //max volume
            trebleGeneral  = 0;
            bassGeneral    = 0;
        }