open method
Implementation
@override
Future<void> open() async {
if (_handle != null) return;
final pHandle = calloc<bindings.StepperHandle>();
try {
bindings.checkPhidgetError(
bindings.stepperCreate(pHandle), 'Stepper 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.setStepperPositionChangeHandlerShim(_handle!),
'set_stepper_position_change_handler_shim');
bindings.checkPhidgetError(
bindings.setStepperStoppedHandlerShim(_handle!),
'set_stepper_stopped_handler_shim');
bindings.checkPhidgetError(
bindings.setStepperVelocityChangeHandlerShim(_handle!),
'set_stepper_velocity_change_handler_shim');
super.startListeningToShimEvents(_handle!);
_listenForStepperShimEvents();
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 Stepper attachment");
}
});
}
} catch (e) {
logger.e("Error during Stepper.open (polling): $e");
Phidget.unregisterInstance(identityHashCode(this));
if (_handle != null) {
final pDelete = calloc<bindings.StepperHandle>();
pDelete.value = _handle!;
try {
bindings.stepperDelete(pDelete);
} catch (_) {}
calloc.free(pDelete);
_handle = null;
}
rethrow;
} finally {
calloc.free(pHandle);
}
}