Skip to content

Instantly share code, notes, and snippets.

@willpatera
Created October 1, 2019 01:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willpatera/e8a338d8f97bd139d21d9405f66c6d4f to your computer and use it in GitHub Desktop.
Save willpatera/e8a338d8f97bd139d21d9405f66c6d4f to your computer and use it in GitHub Desktop.
import time
# https://github.com/pupil-labs/pyndsi/tree/v1.0
import ndsi # Main requirement
GAZE_TYPE = "gaze" # Type of sensors that we are interested in
SENSORS = {} # Will store connected sensors
def main():
# Start auto-discovery of Pupil Invisible Companion devices
network = ndsi.Network(formats={ndsi.DataFormat.V4}, callbacks=(on_network_event,))
network.start()
try:
# Event loop, runs until interrupted
while network.running:
# Check for recently connected/disconnected devices
if network.has_events:
network.handle_event()
# Iterate over all connected devices
for gaze_sensor in SENSORS.values():
# Fetch recent sensor configuration changes,
# required for pyndsi internals
while gaze_sensor.has_notifications:
gaze_sensor.handle_notification()
# Fetch recent gaze data
for gaze in gaze_sensor.fetch_data():
# Output: GazeValue(x, y, ts)
print(gaze_sensor, gaze)
time.sleep(0.1)
# Catch interruption and disconnect gracefully
except (KeyboardInterrupt, SystemExit):
network.stop()
def on_network_event(network, event):
# Handle gaze sensor attachment
if event["subject"] == "attach" and event["sensor_type"] == GAZE_TYPE:
# Create new sensor, start data streaming,
# and request current configuration
sensor = network.sensor(event["sensor_uuid"])
sensor.set_control_value("streaming", True)
sensor.refresh_controls()
# Save sensor s.t. we can fetch data from it in main()
SENSORS[event["sensor_uuid"]] = sensor
print(f"Added sensor {sensor}...")
# Handle gaze sensor detachment
if event["subject"] == "detach" and event["sensor_uuid"] in SENSORS:
# Known sensor has disconnected, remove from list
SENSORS[event["sensor_uuid"]].unlink()
del SENSORS[event["sensor_uuid"]]
print(f"Removed sensor {event['sensor_uuid']}...")
main() # Execute example
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment