void SensorReading(OscMessage oscM) { int value = (int)oscM.Values[0]; if (value < 300) AwayCount++; else AwayCount = 0; if (AwayCount > 5) Working = false; else Working = true; }
private void timer1_Tick(object sender, EventArgs e) { if (!SpeedSet) { OscMessage oscMS = new OscMessage(); oscMS.Address = "/servo/0/speed"; oscMS.Values.Add((int)200); osc.Send(oscMS); SpeedSet = true; } float cpu = getCurrentCpuUsage(); CPU.Text = cpu.ToString(); int cpuSpeed = ((int)cpu) * 10; if (cpuSpeed != lastCpuSpeed) { OscMessage oscM = new OscMessage(); oscM.Address = "/servo/0/position"; oscM.Values.Add(((int)cpu) * 10); osc.Send(oscM); lastCpuSpeed = cpuSpeed; } }
private void timer1_Tick(object sender, EventArgs e) { OscMessage oscMS = new OscMessage(); oscMS.Address = "/analogin/7/value"; osc.Send(oscMS); }
private void LedSet(int index, bool value) { OscMessage oscMS = new OscMessage(); oscMS.Address = "/appled/" + index.ToString() + "/state"; oscMS.Values.Add(value?1:0); osc.Send(oscMS); }
void TrimPotReading(OscMessage oscM) { int value = (int)oscM.Values[0]; SetIndicator( value ); }
/// <summary> /// Creates an array of bytes from a single OscMessage. Used internally. /// </summary> /// <remarks>Can specify where in the array of bytes the OscMessage should be put.</remarks> /// <param name="oscM">The OscMessage to be turned into an array of bytes.</param> /// <param name="packet">The array of bytes to be populated with the OscMessage.</param> /// <param name="start">The start index in the packet where the OscMessage should be put.</param> /// <param name="length">The length of the array of bytes.</param> /// <returns>The index into the packet after the last OscMessage.</returns> private static int OscMessageToPacket(OscMessage oscM, byte[] packet, int start, int length) { int index = start; index = InsertString(oscM.Address, packet, index, length); //if (oscM.Values.Count > 0) { StringBuilder tag = new StringBuilder(); tag.Append(","); int tagIndex = index; index += PadSize(1 + oscM.Values.Count); foreach (object o in oscM.Values) { if (o is int) { int i = (int)o; tag.Append("i"); packet[index++] = (byte)((i >> 24) & 0xFF); packet[index++] = (byte)((i >> 16) & 0xFF); packet[index++] = (byte)((i >> 8) & 0xFF); packet[index++] = (byte)((i) & 0xFF); } else { if (o is float) { float f = (float)o; tag.Append("f"); byte[] buffer = new byte[4]; MemoryStream ms = new MemoryStream(buffer); BinaryWriter bw = new BinaryWriter(ms); bw.Write(f); packet[index++] = buffer[3]; packet[index++] = buffer[2]; packet[index++] = buffer[1]; packet[index++] = buffer[0]; } else { if (o is string) { tag.Append("s"); index = InsertString(o.ToString(), packet, index, length); } else { if ( o is byte[] ) { tag.Append("b"); byte[] byteArray = (byte[])o; int len = byteArray.Length; packet[index++] = (byte)((len >> 24) & 0xFF); packet[index++] = (byte)((len >> 16) & 0xFF); packet[index++] = (byte)((len >> 8) & 0xFF); packet[index++] = (byte)((len) & 0xFF); Array.Copy(byteArray, 0, packet, index, len); index += len; int pad = len % 4; if (pad != 0) { pad = 4 - pad; while (pad-- > 0) packet[index++] = 0; } } else tag.Append("?"); } } } } InsertString(tag.ToString(), packet, tagIndex, length); } return index; }
/// <summary> /// Extracts a messages from a packet. /// </summary> /// <param name="messages">An ArrayList to be populated with the OscMessage.</param> /// <param name="packet">The packet of bytes to be parsed.</param> /// <param name="start">The index of where to start looking in the packet.</param> /// <param name="length">The length of the packet.</param> /// <returns>The index after the OscMessage is read.</returns> private static int ExtractMessage(ArrayList messages, byte[] packet, int start, int length) { OscMessage oscM = new OscMessage(); oscM.Address = ExtractString(packet, start, length); int index = start + PadSize(oscM.Address.Length+1); string typeTag = ExtractString(packet, index, length); index += PadSize(typeTag.Length + 1); //oscM.Values.Add(typeTag); foreach (char c in typeTag) { switch (c) { case ',': break; case 's': { string s = ExtractString(packet, index, length); index += PadSize(s.Length + 1); oscM.Values.Add(s); break; } case 'b': { byte[] b = ExtractBlob(packet, index, length); index += PadSize(b.Length); oscM.Values.Add(b); break; } case 'i': { int i = ( packet[index++] << 24 ) + ( packet[index++] << 16 ) + ( packet[index++] << 8 ) + packet[index++]; oscM.Values.Add(i); break; } case 'f': { byte[] buffer = new byte[4]; buffer[3] = packet[index++]; buffer[2] = packet[index++]; buffer[1] = packet[index++]; buffer[0] = packet[index++]; MemoryStream ms = new MemoryStream(buffer); BinaryReader br = new BinaryReader(ms); float f = br.ReadSingle(); oscM.Values.Add(f); break; } } } messages.Add( oscM ); return index; }
/// <summary> /// Send an individual OSC message. Internally takes the OscMessage object and /// serializes it into a byte[] suitable for sending to the PacketIO. /// </summary> /// <param name="oscMessage">The OSC Message to send.</param> public void Send( OscMessage oscMessage ) { byte[] packet = new byte[1000]; int length = Osc.OscMessageToPacket( oscMessage, packet, 1000 ); OscPacketIO.SendPacket( packet, length); }
/// <summary> /// Creates an OscMessage from a string - extracts the address and determines each of the values. /// </summary> /// <param name="message">The string to be turned into an OscMessage</param> /// <returns>The OscMessage.</returns> public static OscMessage StringToOscMessage(string message) { OscMessage oM = new OscMessage(); // Console.WriteLine("Splitting " + message); string[] ss = message.Split(new char[] { ' ' }); IEnumerator sE = ss.GetEnumerator(); if (sE.MoveNext()) oM.Address = (string)sE.Current; while ( sE.MoveNext() ) { string s = (string)sE.Current; // Console.WriteLine(" <" + s + ">"); // Check for a string - as indicated by an open quote if (s.StartsWith("\"")) { // Get the whole string StringBuilder quoted = new StringBuilder(); bool looped = false; if (s.Length > 1) quoted.Append(s.Substring(1)); else looped = true; while (sE.MoveNext()) { string a = (string)sE.Current; // Console.WriteLine(" q:<" + a + ">"); if (looped) quoted.Append(" "); if (a.EndsWith("\"")) { quoted.Append(a.Substring(0, a.Length - 1)); break; } else { if (a.Length == 0) quoted.Append(" "); else quoted.Append(a); } looped = true; } oM.Values.Add(quoted.ToString()); } else { if ( s.StartsWith( "<" ) ) { ArrayList byteList = new ArrayList(); if (s.Length > 1) ExtractBytesFromString(byteList, s.Substring(1)); while (sE.MoveNext()) { string a = (string)sE.Current; ExtractBytesFromString(byteList, a); if ( a.EndsWith( ">" ) ) break; } byte[] byteArray = new byte[ byteList.Count ]; int index = 0; foreach ( byte b in byteList ) byteArray[ index++ ] = (byte)b; oM.Values.Add( byteArray ); } else { if (s.Length > 0) { try { int i = int.Parse(s); // Console.WriteLine(" i:" + i); oM.Values.Add(i); } catch { try { float f = float.Parse(s); // Console.WriteLine(" f:" + f); oM.Values.Add(f); } catch { // Console.WriteLine(" s:" + s); oM.Values.Add(s); } } } } } } return oM; }
/// <summary> /// General static helper that returns a string suitable for printing representing the supplied /// OscMessage. /// </summary> /// <param name="message">The OscMessage to be stringified.</param> /// <returns>The OscMessage as a string.</returns> public static string OscMessageToString(OscMessage message) { StringBuilder s = new StringBuilder(); s.Append(message.Address); foreach( object o in message.Values ) { s.Append(" "); if (o is byte[]) { byte[] byteArray = (byte[])o; s.Append("<"); bool first = true; foreach (byte b in byteArray) { if (!first) s.Append(" "); else first = false; s.Append( String.Format( "{0:X2}",b)); } s.Append(">"); } else s.Append(o.ToString()); } return s.ToString(); }
/// <summary> /// Creates a packet (an array of bytes) from a single OscMessage. /// </summary> /// <remarks>A convenience method, not requiring a start index.</remarks> /// <param name="oscM">The OscMessage to be returned as a packet.</param> /// <param name="packet">The packet to be populated with the OscMessage.</param> /// <param name="length">The usable size of the array of bytes.</param> /// <returns>The length of the packet</returns> public static int OscMessageToPacket(OscMessage oscM, byte[] packet, int length) { return OscMessageToPacket(oscM, packet, 0, length); }
private void Timer_Tick(object sender, EventArgs e) { if (Working) { WorkingTime += 1.0F; SetTime(WorkingText, WorkingTime); } else { AwayTime += 1.0F; SetTime(AwayText, AwayTime); } OscMessage oscM = new OscMessage(); oscM.Address = "/analogin/0/value"; osc.Send(oscM); }