open method
Implementation
@override
Future<void> open() async {
if (_handle != null) return;
final pHandle = calloc<bindings.EncoderHandle>();
try {
bindings.checkPhidgetError(
bindings.encoderCreate(pHandle), "Encoder Create");
_handle = pHandle.value;
final int instanceId = identityHashCode(this);
Phidget.registerInstance(instanceId, this);
if (deviceSerialNumber != null) {
bindings.checkPhidgetError(
bindings.phidgetSetDeviceSerialNumber(
_handle!, deviceSerialNumber!),
'SetDeviceSerialNumber');
}
if (hubPort != null) {
bindings.checkPhidgetError(
bindings.phidgetSetHubPort(_handle!, hubPort!), 'SetHubPort');
}
bindings.checkPhidgetError(
bindings.setAttachHandlerShim(_handle!), 'set_attach_handler_shim');
bindings.checkPhidgetError(
bindings.setDetachHandlerShim(_handle!), 'set_detach_handler_shim');
bindings.checkPhidgetError(
bindings.setErrorHandlerShim(_handle!), 'set_error_handler_shim');
bindings.checkPhidgetError(
bindings.setEncoderPositionChangeHandlerShim(_handle!),
'set_encoder_position_change_handler_shim');
super.startListeningToShimEvents(_handle!);
_listenForShimEvents();
int openResult =
bindings.phidgetOpenWaitForAttachment(_handle!, openTimeoutMs);
bindings.checkPhidgetError(openResult, 'Phidget_openWaitForAttachment');
final pIsAttached = calloc<Int32>();
try {
bindings.phidgetGetAttached(_handle!, pIsAttached);
if (pIsAttached.value == 1) {
super.internalProcessAttach();
}
} finally {
calloc.free(pIsAttached);
}
if (isAttached) {
if (!attachCompleter.isCompleted) {
attachCompleter.complete();
}
} else {
await attachCompleter.future.timeout(
Duration(milliseconds: openTimeoutMs + 500), onTimeout: () {
if (!isAttached) {
throw TimeoutException(
"Polling timed out waiting for Encoder attachment.");
}
});
}
} catch (e) {
logger.e("Error during Encoder.open: $e");
Phidget.unregisterInstance(identityHashCode(this));
if (_handle != null) {
final pDelete = calloc<bindings.EncoderHandle>();
pDelete.value = _handle!;
try {
bindings.encoderDelete(pDelete);
} catch (_) {}
calloc.free(pDelete);
_handle = null;
}
rethrow;
} finally {
calloc.free(pHandle);
}
}