/* * Initializes form values upon load and loads previous settings * into UI, if available. If not, settings are initialized to * defaults. */ private void FrmMain_Load(object sender, EventArgs e) { // get UI button references uiButtons = new Button[7]; uiButtons[0] = btnJoystick; uiButtons[1] = btnButton1; uiButtons[2] = btnButton2; uiButtons[3] = btnButton3; uiButtons[4] = btnButton4; uiButtons[5] = btnButton5; uiButtons[6] = btnButton6; // scroll wheel event handlers so the scroll wheel cannot control the trackbars in joystick view trkJoystick.MouseWheel += new MouseEventHandler(trackBar_MouseWheel); trkCursor.MouseWheel += new MouseEventHandler(trackBar_MouseWheel); // load settings settings = DeviceSettings.Instance; buttonSettings = new ButtonSetting[6]; // if previous settings were loaded, update the UI to reflect them if (settings.LoadedPreviousSession) { buttonSettings[0] = settings.GetButtonSetting(1); buttonSettings[1] = settings.GetButtonSetting(2); buttonSettings[2] = settings.GetButtonSetting(3); buttonSettings[3] = settings.GetButtonSetting(4); buttonSettings[4] = settings.GetButtonSetting(5); buttonSettings[5] = settings.GetButtonSetting(6); currentJoystickSetting = settings.JoystickSetting; currentJoystickSetting.AssignedButton = uiButtons[0]; for (int i = 0; i < 6; i++) { buttonSettings[i].AssignedButton = uiButtons[i + 1]; } mnuStartWithWindows.Checked = settings.RunAtStartup; trayNotifShown = true; } else // set default values if not { currentJoystickSetting = new JoystickSetting(uiButtons[0]); settings.JoystickSetting = currentJoystickSetting; for (int i = 0; i < 6; i++) { buttonSettings[i] = new ButtonSetting(uiButtons[i + 1]); settings.SetButtonSetting(i + 1, buttonSettings[i]); } trayNotifShown = false; // since we don't know if the application is to run at startup // from device settings since a previous session was not loaded, // check if the task exists and update device settings accordingly using (TaskService scheduler = new TaskService()) { if (scheduler.GetTask("Custom Mouse Controller") == null) { settings.RunAtStartup = false; mnuStartWithWindows.Checked = false; } else { settings.RunAtStartup = true; mnuStartWithWindows.Checked = true; } } } //show warning if programs assigned last session have not been found if (settings.NotAllProgramsLoaded) { MessageBox.Show(this, "One or more programs that were assigned to the buttons have been uninstalled or moved since " + "last run. Their assignments have been removed.", "Custom Mouse Controller - Missing Programs", MessageBoxButtons.OK, MessageBoxIcon.Information); if (Visible) { Focus(); } } // start out on joystick view since it is the first button btnJoystick.PerformClick(); isJoystickView = true; }
/* * Starts the communcation process between the computer and * Arduino. This method runs a loop until Dispose() is called * and processes all communication to and from the Arduino. */ public void Start() { if (connected) // do not start a new loop if Start() is called again { return; } while (!disposed) { if (!connected) // while not connected, search for Arduino on each serial port and attempt to connect if it is found { ManagementObjectSearcher portSearcher = new ManagementObjectSearcher("Select * From Win32_SerialPort"); ManagementObjectCollection ports = portSearcher.Get(); foreach (ManagementObject port in ports) { string portName = port.GetPropertyValue("Description").ToString(); if (portName.Contains("Arduino Leonardo")) { if (device == null) { device = new SerialPort(port.GetPropertyValue("DeviceID").ToString()); device.BaudRate = 9600; device.DtrEnable = true; device.RtsEnable = true; device.Handshake = Handshake.None; device.Parity = Parity.None; device.StopBits = StopBits.Two; device.ReadTimeout = 10000; // setting to 10000 here for handshake device.DataBits = 8; try { device.Open(); } catch // device was probably just plugged in and is still open by Windows, so try again on next iteration of loop { try { device.Dispose(); } catch // no need to handle serial port exceptions here { ; } device = null; continue; } // perform a handshake to ensure we have the right Arduino and that it is working properly connected = PerformHandshake(); } // initialize cursorSpeedTick if connected, shorten read timeout and break the loop if (connected) { cursorSpeedTick = 12 - settings.JoystickSetting.SpeedMultiplier; device.ReadTimeout = 1000; device.WriteTimeout = 1000; break; } else { try { device.Dispose(); } catch // no need to handle serial port exceptions here { ; } device = null; } } } } else // connected to Arduino, so read and process data that is being sent { string data = String.Empty; try { data = device.ReadLine(); } catch // if there is any error reading from the Arduino, check if it is still connected { ManagementObjectSearcher portSearcher = new ManagementObjectSearcher("Select * From Win32_SerialPort"); ManagementObjectCollection ports = portSearcher.Get(); bool stillConnected = false; // check to see if port is still in list of ports foreach (ManagementObject port in ports) { if (port.GetPropertyValue("DeviceID").ToString() == device.PortName) { stillConnected = true; } } // disconnect if device is no longer connected if (!stillConnected) { connected = false; try { device.Dispose(); } catch // no need to handle serial port exceptions here { ; } device = null; } continue; } if (data.Contains("X") && data.Contains("Y")) // joystick signal from Arduino { cursorSpeedTick--; // decrement with each iteration of loop if (cursorSpeedTick == 0) // if cursorSpeedTick reaches 0, move cursor accordingly and reset cursorSpeedTick { MoveMouse(data.Substring(0, data.IndexOf("\r"))); cursorSpeedTick = 12 - settings.JoystickSetting.SpeedMultiplier; } } else // button signal from Arduino { switch (data.Substring(0, data.IndexOf("\r"))) { case "1": PerformButtonAction(settings.GetButtonSetting(1)); break; case "2": PerformButtonAction(settings.GetButtonSetting(2)); break; case "3": PerformButtonAction(settings.GetButtonSetting(3)); break; case "4": PerformButtonAction(settings.GetButtonSetting(4)); break; case "5": PerformButtonAction(settings.GetButtonSetting(5)); break; case "6": PerformButtonAction(settings.GetButtonSetting(6)); break; } } } } }