private void HandleLed(PFSwPanelLeds led, PFSwPanelLedState state) { if (led == PFSwPanelLeds.GEAR_NONE) { return; } m_pfspManager.SetLed(led, state); }
public static void HandleLed(string ledString, PFSwPanelManager manager) { PFSwPanelLedState ledColor; PFSwPanelLeds led; if (string.IsNullOrEmpty(ledString)) { ledString = "n"; // none is default if nothing is given } switch (ledString.ToUpperInvariant( )) { case "NO": // Nose Off led = PFSwPanelLeds.GEAR_N; ledColor = PFSwPanelLedState.Led_Off; break; case "NG": // Nose Green led = PFSwPanelLeds.GEAR_N; ledColor = PFSwPanelLedState.Led_Green; break; case "NR": // Nose Red led = PFSwPanelLeds.GEAR_N; ledColor = PFSwPanelLedState.Led_Red; break; case "NA": // Nose Amber led = PFSwPanelLeds.GEAR_N; ledColor = PFSwPanelLedState.Led_Amber; break; case "LO": // Left Off led = PFSwPanelLeds.GEAR_L; ledColor = PFSwPanelLedState.Led_Off; break; case "LG": // Left Green led = PFSwPanelLeds.GEAR_L; ledColor = PFSwPanelLedState.Led_Green; break; case "LR": // Left Red led = PFSwPanelLeds.GEAR_L; ledColor = PFSwPanelLedState.Led_Red; break; case "LA": // Left Amber led = PFSwPanelLeds.GEAR_L; ledColor = PFSwPanelLedState.Led_Amber; break; case "RO": // Right Off led = PFSwPanelLeds.GEAR_R; ledColor = PFSwPanelLedState.Led_Off; break; case "RG": // Right Green led = PFSwPanelLeds.GEAR_R; ledColor = PFSwPanelLedState.Led_Green; break; case "RR": // Right Red led = PFSwPanelLeds.GEAR_R; ledColor = PFSwPanelLedState.Led_Red; break; case "RA": // Right Amber led = PFSwPanelLeds.GEAR_R; ledColor = PFSwPanelLedState.Led_Amber; break; default: // none led = PFSwPanelLeds.GEAR_NONE; ledColor = PFSwPanelLedState.Led_Off; break; } if (led == PFSwPanelLeds.GEAR_NONE) { return; } manager.SetLed(led, ledColor); }
/// <summary> /// Main Background worker thread entry /// </summary> /// <param name="bgwContext">The command and control struct</param> public void DoWork(object sender, BgwSwitchPanelContext bgwContext) { m_worker = sender as BackgroundWorker; m_worker.ReportProgress(0, "SwitchPanelSupport - About to start"); // first message.. m_bgwContext = bgwContext; m_pfspManager = new PFSwPanelManager( ); bool abort = false; bool eventsHooked = false; // Try to connect to the HID driver if (m_pfspManager.OpenDevice( )) { // attach to the events from the HID device m_pfspManager.DeviceAttached += PfspManager_DeviceAttached; m_pfspManager.DeviceRemoved += PfspManager_DeviceRemoved; m_pfspManager.ButtonUp += PfspManager_ButtonUp; m_pfspManager.ButtonDown += PfspManager_ButtonDown; m_pfspManager.RotaryChanged += PfspManager_RotaryChanged; eventsHooked = true; m_pfspManager.SetLed(PFSwPanelLeds.GEAR_L, PFSwPanelLedState.Led_Off); m_pfspManager.SetLed(PFSwPanelLeds.GEAR_R, PFSwPanelLedState.Led_Off); m_pfspManager.SetLed(PFSwPanelLeds.GEAR_N, PFSwPanelLedState.Led_Off); // Try to load the config file m_panelConfig = SwitchPanelConfig.FromJson(m_bgwContext.ConfigFile); if (m_panelConfig.Valid) { m_worker.ReportProgress(0, $" Config: {m_panelConfig.ConfigName}"); // first message.. } else { m_worker.ReportProgress(0, $" Invalid Config File - aborting"); // first message.. m_worker.ReportProgress(0, $" {SwitchPanelConfig.ErrorMsg}"); // first message.. abort = true; } } else { m_worker.ReportProgress(0, "SwitchPanelSupport - Device open failed"); // first message.. abort = true; } // Try to connect the JoystickServer if (int.TryParse(m_bgwContext.ServerPort, out int portNo)) { if (UdpMessenger.CheckIP(m_bgwContext.ServerIP)) { m_udp = new UdpMessenger(m_bgwContext.ServerIP, portNo); } else { m_worker.ReportProgress(0, $"SwitchPanelSupport - Invalid IP # {m_bgwContext.ServerIP}"); } } else { m_worker.ReportProgress(0, $"SwitchPanelSupport - Invalid Port number # {m_bgwContext.ServerPort}"); } // Report about incidents.. // Task loop - wait until killed - handling happens in the events while (!abort) { if (m_bgwContext.LedChanged) { m_pfspManager.SetLed(PFSwPanelLeds.GEAR_L, m_bgwContext.L_Led); m_pfspManager.SetLed(PFSwPanelLeds.GEAR_N, m_bgwContext.N_Led); m_pfspManager.SetLed(PFSwPanelLeds.GEAR_R, m_bgwContext.R_Led); m_bgwContext.LedChanged = false; // commit } Thread.Sleep(200); // check every 200 ms for abort or LED changes abort = m_worker.CancellationPending; // check once in a while } // clean UDP m_udp = null; // clean HID up // if the device is still attached if (m_devAttached) { // final message... m_pfspManager.SetLed(PFSwPanelLeds.GEAR_L, PFSwPanelLedState.Led_Red); m_pfspManager.SetLed(PFSwPanelLeds.GEAR_R, PFSwPanelLedState.Led_Green); m_pfspManager.SetLed(PFSwPanelLeds.GEAR_N, PFSwPanelLedState.Led_Amber); m_pfspManager.CloseDevice( ); } // events if (eventsHooked) { m_pfspManager.DeviceAttached -= PfspManager_DeviceAttached; m_pfspManager.DeviceRemoved -= PfspManager_DeviceRemoved; m_pfspManager.ButtonUp -= PfspManager_ButtonUp; m_pfspManager.ButtonDown -= PfspManager_ButtonDown; m_pfspManager.RotaryChanged -= PfspManager_RotaryChanged; } // the HID object m_pfspManager.Dispose( ); m_pfspManager = null; m_worker.ReportProgress(100, "SwitchPanelSupport - Ending now"); // final message.. }
/// <summary> /// Main Background worker thread entry /// </summary> /// <param name="bgwContext">The command and control struct</param> public void DoWork(object sender, BgwSwitchPanelContext bgwContext) { m_worker = sender as BackgroundWorker; m_worker.ReportProgress(0, "SwitchPanelSupport - About to start"); // first message.. m_bgwContext = bgwContext; m_pfspManager = new PFSwPanelManager( ); bool abort = false; bool eventsHooked = false; // Try to connect to the HID driver if (m_pfspManager.OpenDevice( )) { // attach to the events from the HID device m_pfspManager.DeviceAttached += PfspManager_DeviceAttached; m_pfspManager.DeviceRemoved += PfspManager_DeviceRemoved; m_pfspManager.ButtonUp += PfspManager_ButtonUp; m_pfspManager.ButtonDown += PfspManager_ButtonDown; m_pfspManager.RotaryChanged += PfspManager_RotaryChanged; eventsHooked = true; m_pfspManager.SetLed(PFSwPanelLeds.GEAR_L, PFSwPanelLedState.Led_Off); m_pfspManager.SetLed(PFSwPanelLeds.GEAR_R, PFSwPanelLedState.Led_Off); m_pfspManager.SetLed(PFSwPanelLeds.GEAR_N, PFSwPanelLedState.Led_Off); // Try to load the config file m_panelConfig = SwitchPanelConfig.FromJson(m_bgwContext.ConfigFile); if (m_panelConfig.Valid) { m_worker.ReportProgress(0, $" Config: {m_panelConfig.ConfigName}"); // first message.. } else { m_worker.ReportProgress(0, $" Invalid Config File - aborting"); // first message.. m_worker.ReportProgress(0, $" {SwitchPanelConfig.ErrorMsg}"); // first message.. abort = true; } } else { m_worker.ReportProgress(0, "SwitchPanelSupport - Device open failed"); // first message.. abort = true; } if (!VJoyHandler.Instance.AreLibrariesLoaded( )) { m_worker.ReportProgress(0, $"SwitchPanelSupport - Libraries are not loaded"); // first message.. abort = true; } else { // First try to connect the Joystick interface if ((m_bgwContext.JoystickNo > 0) && (m_bgwContext.JoystickNo <= 16)) { if (!VJoyHandler.Instance.Connect(m_bgwContext.JoystickNo)) { m_worker.ReportProgress(0, $"SwitchPanelSupport - Cannot connect joystick # {m_bgwContext.JoystickNo}"); // first message.. // we continue with keyboard support only } } else { m_worker.ReportProgress(0, $"SwitchPanelSupport - Invalid Joystick # {m_bgwContext.JoystickNo}"); // first message.. // we continue with keyboard support only } } // Task loop - wait until killed - handling happens in the events while (!abort) { if (m_bgwContext.LedChanged) { m_pfspManager.SetLed(PFSwPanelLeds.GEAR_L, m_bgwContext.L_Led); m_pfspManager.SetLed(PFSwPanelLeds.GEAR_N, m_bgwContext.N_Led); m_pfspManager.SetLed(PFSwPanelLeds.GEAR_R, m_bgwContext.R_Led); m_bgwContext.LedChanged = false; // commit } Thread.Sleep(200); // check every 200 ms for abort or LED changes abort = m_worker.CancellationPending; // check once in a while } // clean vJoy up VJoyHandler.Instance.Disconnect( ); // shut the Joystick handler // clean HID up // if the device is still attached if (m_devAttached) { // final message... m_pfspManager.SetLed(PFSwPanelLeds.GEAR_L, PFSwPanelLedState.Led_Red); m_pfspManager.SetLed(PFSwPanelLeds.GEAR_R, PFSwPanelLedState.Led_Green); m_pfspManager.SetLed(PFSwPanelLeds.GEAR_N, PFSwPanelLedState.Led_Amber); m_pfspManager.CloseDevice( ); } // events if (eventsHooked) { m_pfspManager.DeviceAttached -= PfspManager_DeviceAttached; m_pfspManager.DeviceRemoved -= PfspManager_DeviceRemoved; m_pfspManager.ButtonUp -= PfspManager_ButtonUp; m_pfspManager.ButtonDown -= PfspManager_ButtonDown; m_pfspManager.RotaryChanged -= PfspManager_RotaryChanged; } // the HID object m_pfspManager.Dispose( ); m_pfspManager = null; m_worker.ReportProgress(100, "SwitchPanelSupport - Ending now"); // final message.. }