# -*- coding: utf-8 -*- # # A simple demonstration of connecting to the UPDD and listening for digitiser # events in Python. This uses asyncio in order to process UPDD events in # Python's main thread rather than the UPDD's callback thread import upddapi import asyncio import signal digitiserCallback = None async def configEventCallback(event_type, event): if event.pe.config.configEventType == upddapi.CONFIG_EVENT_CONNECT: print("Connected to UPDD") global digitiserCallback digitiserCallback = upddapi.create_event_callback_async(digitiserEventCallback) upddapi.TBApiRegisterEvent(0, 0, upddapi._EventTypeDigitiserEvent, digitiserCallback) if event.pe.config.configEventType == upddapi.CONFIG_EVENT_DISCONNECT: print("Disconnected from UPDD") async def digitiserEventCallback(event_type, event): print(f"Received digitiser event from device {event.hDevice}") if event.pe.digitiserEvent.digitizerType == upddapi.DIGITIZER_TYPE_PEN: print(f" pen {event.hStylus} {'down' if event.pe.digitiserEvent.de.touchEvent.touchingLeft else 'up'}") else: print(f" touch {event.hStylus} {'down' if event.pe.digitiserEvent.de.touchEvent.touchingLeft else 'up'}") print(f" screen coordinates: {event.pe.digitiserEvent.screenx} , {event.pe.digitiserEvent.screeny}") def startup(): upddapi.TBApiRegisterEvent(0, 0, upddapi._EventConfiguration, upddapi.create_event_callback_async(configEventCallback)) upddapi.TBApiOpen() def shutdown(): if digitiserCallback is not None: upddapi.TBApiUnregisterEvent(digitiserCallback) upddapi.TBApiUnregisterEvent(upddapi.create_event_callback_async(configEventCallback)) upddapi.TBApiClose() async def main(): global loop stop_event = asyncio.Future() def handle_sigint(): if not stop_event.done(): print("\nReceived Ctrl+C, shutting down gracefully...") stop_event.set_result(None) startup() asyncio.get_running_loop().add_signal_handler(signal.SIGINT, handle_sigint) await stop_event shutdown() print("Done.") if __name__ == "__main__": asyncio.run(main())