/* * public coor_axis_t LeftFingerUp; * public coor_axis_t LeftFingerForward; * public coor_axis_t LeftRightDirection;*/ // Sends a packet to the TCP server private static void sendPacket(ApolloPacketBinary packet, bool destroyPacket = true) { _apolloTCP.Send(ApolloSDK.WrapPacket(packet)); // Dispose of the packet (pointer) if requested if (destroyPacket) { ApolloSDK.apolloDisposePacket(packet); } }
public static void rumble(GloveLaterality side, ushort duration, ushort power) { // try to find a glove with the inserted handedness foreach (var glove in sourceList) { if (glove.Value.handedness == side) { var eventId = randomEventId; var packet = ApolloSDK.generateDeviceVibrate(_session, glove.Key, duration, power); sendPacket(packet); eventList.Add(eventId, new eventData(EventType.RUMBLE)); } } }
// Reset every variable related to connecting with Apollo private void resetApolloState() { apolloState = ApolloState.APOLLO_0_CONNECTING; // empty the source and event List sourceList.Clear(); eventList.Clear(); // reset the apollo session ApolloSDK.apolloCloseSession(_session); CreateApolloSession(); // make sure the TCP server is reset ApolloTCP.StopClient(); _connectionAttempts = 0; }
// Creates a new Apollo session void CreateApolloSession() { // register error handler first because OpenSession can give errors ApolloSDK.registerErrorHandler(new IntPtr(), errorHandler); // start new instance of the apollo Network SDK _session = ApolloSDK.apolloOpenSession(_clientId); log("Started new ApolloSDK session: " + _session); // register all the callback functions from the NetSDK ApolloSDK.registerHandshakePacketHandler(_session, new IntPtr(), handshakeHandler); ApolloSDK.registerSuccessHandler(_session, new IntPtr(), successHandler); ApolloSDK.registerFailHandler(_session, new IntPtr(), failHandler); ApolloSDK.registerDeviceIdListHandler(_session, new IntPtr(), deviceListHandler); ApolloSDK.registerDongleIdListHandler(_session, new IntPtr(), dongleIdListHandler); ApolloSDK.registerDataStreamHandler(_session, new IntPtr(), dataStreamHandler); ApolloSDK.registerRawStreamHandler(_session, new IntPtr(), rawStreamHandler); ApolloSDK.registerSourceInfoHandler(_session, new IntPtr(), sourceInfoHandler); ApolloSDK.registerDeviceInfoHandler(_session, new IntPtr(), deviceInfoHandler); }
// Checks all the gloves status and handles their connection with Apollo private static void setupGloves() { foreach (var glove in sourceList) { // generate a random eventID and prepare the variables var eventId = randomEventId; var gloveId = glove.Key; var endpointID = glove.Value.endpointID; switch (glove.Value.state) { case GloveState.GLOVE_0_DISCONNECTED: log(gloveId + ": Unused state, should not happen"); break; case GloveState.GLOVE_1_GET_INFO: log(gloveId + ": GET_INFO: " + eventId); // request the information about this source to identify it sendPacket(ApolloSDK.generateGetSourceInfo(_session, gloveId, eventId)); // create the event eventList.Add(eventId, new eventData(EventType.GLOVE_IDENTIFY, gloveId)); break; case GloveState.GLOVE_2_FILTER_GESTURE: if (!_usePinchFilter) { sourceList[gloveId].state = GloveState.GLOVE_3_FILTER_CONVERT; log(gloveId + ": SKIPPED FILTER GESTURE: " + eventId); break; } log(gloveId + ": SETUP FILTER GESTURE: " + eventId); // prepare the arguments for the gesture filter var gestureArray = new ApolloGestureArray(new apollo_gesture_t[] { apollo_gesture_t.GESTURE_INDEX_PINCH }); // create a filter handle for the packet generation var gestureFilterHandle = ApolloSDK.generateGestureFilter(_session, gestureArray.numGestures, gestureArray.gestures, 0, 1); // create the unmanaged sources/filters lists var gestureSources = new U64Array(endpointID); var gestureFilters = new U64Array(gestureFilterHandle); // Add the filters in Apollo sendPacket(ApolloSDK.generateAddFilters(_session, gestureSources.pointer, gestureSources.size, gestureFilters.pointer, gestureFilters.size, eventId)); // create an event eventList.Add(eventId, new eventData(EventType.GLOVE_ADD_FILTER_GESTURE, gloveId)); _filterGestureAttempts++; // Skip this fase if it was not able to setup the filter if (_filterGestureAttempts >= 3) { sourceList[gloveId].state = GloveState.GLOVE_3_FILTER_CONVERT; } break; case GloveState.GLOVE_3_FILTER_CONVERT: log(gloveId + ": SETUP FILTER CONVERT: " + eventId); // create a filter handle for the packet generation var convertFilterHandle = ApolloSDK.generateMeshMappingFilter(_session, getUE4MannequinMeshConfig()); // create the unmanaged sources/filters lists var convertSources = new U64Array(endpointID); var convertFilters = new U64Array(convertFilterHandle); // Add the filters in Apollo sendPacket(ApolloSDK.generateAddFilters(_session, convertSources.pointer, convertSources.size, convertFilters.pointer, convertFilters.size, eventId)); // create an event eventList.Add(eventId, new eventData(EventType.GLOVE_ADD_FILTER_CONVERT, gloveId)); break; case GloveState.GLOVE_4_ADD_TO_STREAM: // add this glove to the stream // create a packet with one gloveId and send the packet out //var glovesList = new U64Array(new ulong[] { endpointID, gloveId }); var glovesList = new U64Array(endpointID); sendPacket(ApolloSDK.generateAddStreams(_session, glovesList.pointer, glovesList.size, eventId)); // create an event in the list eventList.Add(eventId, new eventData(EventType.GLOVE_ADD_STREAM, gloveId)); log(gloveId + ": ADD_TO_STREAM: " + eventId + " | endpointID: " + endpointID); break; case GloveState.GLOVE_5_SET_STREAM_DATA: // enable the data-stream for this glove // create and send the packet sendPacket(ApolloSDK.generateSetStreamData(_session, endpointID, true, eventId)); // create an event in the list eventList.Add(eventId, new eventData(EventType.GLOVE_SET_STREAM_DATA, gloveId)); log(gloveId + ": SET_STREAM_DATA: " + eventId + " | endpointID: " + endpointID); break; case GloveState.GLOVE_6_SET_STREAM_RAW: // enable the raw-stream for this glove // create and send the packet sendPacket(ApolloSDK.generateSetStreamRaw(_session, endpointID, true, eventId)); // create an event in the list eventList.Add(eventId, new eventData(EventType.GLOVE_SET_STREAM_RAW, gloveId)); log(gloveId + ": SET_STREAM_RAW: " + eventId + " | endpointID: " + endpointID); break; case GloveState.GLOVE_7_SETUP: log(gloveId + ": SETUP"); // glove is completely setup, don't do anything break; default: log("Unknow state '" + glove.Value.state + "' for source: " + glove.Key); break; } } }
// Constantly running check for the connection with Apollo private IEnumerator ApolloCheck() { while (true) { // init the connection delay default fast float connectionDelay = fastConnectionDelay; // if the connection gets broken somehow and not already in disconnected state if (apolloState > ApolloState.APOLLO_0_CONNECTING && !ApolloTCP.connected) { resetApolloState(); log("Connnection reset by server"); } // if apollo is properly connected/handshaken if (apolloState > ApolloState.APOLLO_1_HANDSHAKING) { // check available dongles/sources sendPacket(ApolloSDK.generateListDongleIDs(_session)); // If we found a dongle, start requesting all devices if (dongleId != 0) { sendPacket(ApolloSDK.generateListDeviceIDs(_session, dongleId)); } // request device information foreach (var source in sourceList) { // if a glove in setup is found, request device info if (source.Value.state == GloveState.GLOVE_7_SETUP) { sendPacket(ApolloSDK.generateGetDeviceInfo(_session, source.Key)); } } } switch (apolloState) { case ApolloState.APOLLO_0_CONNECTING: // slow the check down to give Apollo time to init if (_connectionAttempts >= 3 / fastConnectionDelay) { connectionDelay = slowConnectionDelay; } // try connecting and skip if already connected if (!ApolloTCP.connected) { _connectionAttempts++; _apolloTCP.ConnectClient(); // (re)try to get a connection } else { apolloState = ApolloState.APOLLO_1_HANDSHAKING; } break; case ApolloState.APOLLO_1_HANDSHAKING: // slow the check down to give Apollo time to init // if this is not the first attempt if (_connectionAttempts > 3 / fastConnectionDelay) { connectionDelay = slowConnectionDelay; } // Connecting is succesfull so reset connectionAttempts _connectionAttempts = 0; log("Start handshake"); // initiate handshake sendPacket(ApolloSDK.generateHandshake(_session)); break; case ApolloState.APOLLO_2_GLOVE_SETUP: // setup the gloves setupGloves(); // start the stream if all gloves are setup bool startStream = false; foreach (var info in sourceList.Values) { // if a glove still in setup is found, never start the stream if (info.state >= GloveState.GLOVE_1_GET_INFO && info.state < GloveState.GLOVE_7_SETUP) { startStream = false; break; } // if a glove that is set is found, we can start the stream if (info.state == GloveState.GLOVE_7_SETUP) { startStream = true; } } if (startStream) { log("(New) gloves discovered and all are setup, start streaming"); var eventId = randomEventId; // send out the packet to Apollo sendPacket(ApolloSDK.generateStartStreams(_session, eventId)); // register am event in the list eventList.Add(eventId, new eventData(EventType.APOLLO_START_STREAM)); } break; case ApolloState.APOLLO_3_STREAMING: // if apollo is streaming and a new glove is found, turn off the stream and set all active gloves to discovered bool stopStream = false; // if a glove that is new is found, we can stop the stream foreach (var info in sourceList.Values) { if (info.state == GloveState.GLOVE_1_GET_INFO) { stopStream = true; } } if (stopStream) { log("(New) gloves discovered, start streaming"); var eventId = randomEventId; // send out the packet to Apollo sendPacket(ApolloSDK.generateStopStreams(_session, eventId)); // register am event in the list eventList.Add(eventId, new eventData(EventType.APOLLO_STOP_STREAM)); } break; } // stop executing the coroutine if not running if (running) { yield return(new WaitForSeconds(connectionDelay)); } else { yield break; } } }
// Callback from the TCP server when a full packet is received private static void receivePacket(byte[] data) { ApolloSDK.apolloProcessPacket(_session, new ApolloPacketBinary(data)); }