Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import socket
- import struct
- import numpy as np
- import pyqtgraph.opengl as gl
- from pyqtgraph.Qt import QtCore, QtWidgets
- class NeuralTelemetryFeed(QtWidgets.QMainWindow):
- def __init__(self, channels=5120):
- super().__init__()
- self.channels = channels
- self.view = gl.GLViewWidget()
- self.setCentralWidget(self.view)
- self.view.setCameraPosition(distance=45, elevation=20, azimuth=45)
- self.setWindowTitle('1U Dock: Real-Time Telemetry (Awaiting Signal...)')
- self.pos = self._load_electrode_geometry()
- self.colors = np.array([[0.1, 0.1, 0.3, 0.5]] * self.channels) # Default: Idle Blue
- self.scatter = gl.GLScatterPlotItem(pos=self.pos, color=self.colors, size=1.5, pxMode=True)
- self.view.addItem(self.scatter)
- self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- self.socket.setblocking(False) # Non-blocking for UI fluidity
- self.timer = QtCore.QTimer()
- self.timer.timeout.connect(self.poll_hardware_stream)
- self.timer.start(10) # 100Hz Polling
- def _load_electrode_geometry(self):
- t = np.linspace(0, 2*np.pi, self.channels)
- cos_t, sin_t = np.cos(t), np.sin(t)
- return np.vstack([15*cos_t, 15*sin_t, 10*np.sin(t*5)]).T
- def poll_hardware_stream(self):
- try:
- # Format: 5120 floats (4 bytes each) representing variation/activity
- data = self.socket.recv(self.channels * 4)
- if data:
- activity = np.frombuffer(data, dtype=np.float32)
- self.update_visualization(activity)
- except (BlockingIOError, ConnectionResetError):
- pass # No data yet, keep the display static
- def update_visualization(self, activity):
- norm_activity = np.clip(activity, 0, 1)
- self.colors[:, 0] = norm_activity # Red
- self.colors[:, 1] = norm_activity * 0.5 # Green (Warmth)
- self.colors[:, 2] = 1 - norm_activity # Blue
- self.scatter.setData(color=self.colors)
- if __name__ == '__main__':
- app = QtWidgets.QApplication(sys.argv)
- feed = NeuralTelemetryFeed()
- feed.show()
- sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment