private void disconnect() { if (transmitter != null) { transmitter.Close(); } if (receiver != null) { receiver.Close(); } if (listenerThread != null) { listenerThread.Abort(); } listenerThread = null; receiver = null; transmitter = null; }
private void oscListenerLoop() { OSCReceiver oscReceiver = null; OSCMessage oscWiimoteData = null; oscStopNow = false; double[] beams = new double[200]; beams[0] = 0.0d; beams[1] = 0.0d; beams[2] = 0.0d; double accel = 0.0d; double accelBase = 0.0d; double speed = 0.0d; double dist = 0.0d; double dT = 0.0d; double par4 = 0.0d; long currTick; long prevTick; currTick = prevTick = DateTime.Now.Ticks; try { int oscPort = Convert.ToInt32(oscPortTextBox.Text); oscReceiver = new OSCReceiver(oscPort); oscReceiver.Connect(); Tracer.Trace("OSC connected and listening on port " + oscPort); while (!oscStopNow && (oscWiimoteData = (OSCMessage)oscReceiver.Receive()) != null) { string addr = oscWiimoteData.Address; if (wiiValuesControls.ContainsKey(addr)) { wiimoteValuesUserControl = (WiimoteValuesUserControl)wiiValuesControls[addr]; } else { this.Invoke(new MethodInvoker(createWiimoteValuesUserControl)); wiiValuesControls.Add(addr, wiimoteValuesUserControl); } wiimoteValuesUserControl.values = oscWiimoteData; switch (addr) { case "/wiimote-g": currTick = DateTime.Now.Ticks; dT = (double)(currTick - prevTick) / 10000000.0d; // sec // string str = DateTime.Now.ToLongTimeString() + " OSC packet: " + oscWiimoteData.Address + " "; int i = 0; foreach (object obj in oscWiimoteData.Values) { // str += obj.ToString() + " "; if (i == 0) { try { beams[i] = Convert.ToDouble((float)obj); } catch { beams[i] = 0.0d; } accel = beams[i]; // m/c2 } if (i == 4) { try { beams[i] = Convert.ToDouble((float)obj); } catch { beams[i] = 0.0d; } par4 = beams[i]; } i++; } if (par4 > 0.0d) { accelBase = accel; speed = 0.0d; dist = 0.0d; } else { accel -= accelBase; speed += accel * dT; dist += speed * dT; } // oscilloscope.AddData(beams[0], beams[1], beams[2]); oscilloscope.AddData(accel, speed * 100.0d, dist * 100.0d); // Tracer.Trace(str.Trim()); prevTick = currTick; break; } } } catch (Exception exc) { Tracer.Error(exc.ToString()); } finally { if (oscReceiver != null) { oscReceiver.Close(); } Tracer.Trace("OSC finished and closed"); } }