public void Pulse(ushort strength, ushort duration) { if (isConnected) { OutputPacket pack = new OutputPacket(); pack.restart = false; pack.hapticStr = strength; pack.hapticDur = duration; Compose(pack); tx.StoreAsync(); } else { throw new System.InvalidOperationException("Controller not connected"); } }
public void Restart() { if (isConnected) { OutputPacket pack = new OutputPacket(); pack.restart = true; pack.hapticStr = 0; pack.hapticDur = 0; Compose(pack); tx.StoreAsync(); } else { throw new System.InvalidOperationException("Controller not connected"); } }
private void Compose(OutputPacket pack) { // Convert the packet to a byte array byte[] buf = new byte[OUTPUT_LENGTH]; buf[0] = (byte)((pack.restart) ? 1 : 0); buf[1] = (byte)((pack.hapticStr & 0xFF00) >> 8); buf[2] = (byte)(pack.hapticStr & 0x00FF); buf[3] = (byte)((pack.hapticDur & 0xFF00) >> 8); buf[4] = (byte)(pack.hapticDur & 0x00FF); int sz = buf.Length + 2; // Size of the actual data we send // Find out how many extra bytes to send foreach (byte b in buf) { if (b == START || b == END || b == ESCAPE) { sz++; } } // Allocate data to send byte[] data = new byte[sz]; data[0] = START; data[sz - 1] = END; // Fill the data int ind = 1; foreach (byte b in buf) { if (b == START || b == END || b == ESCAPE) { // If a byte is a delim char, send an escape code data[ind] = ESCAPE; ind++; } data[ind] = b; ind++; } // Finally, write the data to the sream tx.WriteBytes(data); }