diff options
| author | Christian Kolset <christian.kolset@gmail.com> | 2026-04-21 22:58:30 -0600 |
|---|---|---|
| committer | Christian Kolset <christian.kolset@gmail.com> | 2026-04-21 22:58:30 -0600 |
| commit | 4db119fb9c04428e7aef6f1a278c1639aa867baf (patch) | |
| tree | e0e0645559637e593f1bbfb9d713256028668527 /devices/analog_input.py | |
| parent | 97e15f69b835af03b071c4eeb4547c65a02e6f80 (diff) | |
Major update to Output control
Including:
- Control panel
- Change status of simulation after devices creation.
- Arduino serial conflict with multiple channels.
- Claude instructions.
- Update to arduino on board code.
Diffstat (limited to 'devices/analog_input.py')
| -rw-r--r-- | devices/analog_input.py | 36 |
1 files changed, 24 insertions, 12 deletions
diff --git a/devices/analog_input.py b/devices/analog_input.py index 508804f..e1b8b09 100644 --- a/devices/analog_input.py +++ b/devices/analog_input.py @@ -90,11 +90,13 @@ class AnalogInputDevice(BaseDevice): ni_device, ni_min_v, ni_max_v, ard_port, ard_baud, channel_ids): if backend == "arduino": - return ArduinoLayer( + # Use shared layer so two devices on the same port don't conflict + from api_layers.port_registry import port_registry + return port_registry.get_layer( port=ard_port, baud=ard_baud, - analog_pins=channel_ids, simulate=simulate, + extra_pins=channel_ids, ) else: return NidaqmxLayer( @@ -110,13 +112,17 @@ class AnalogInputDevice(BaseDevice): def connect(self) -> bool: self._last_error = "" - # ArduinoLayer uses connect(); NidaqmxLayer uses start() + # ArduinoLayer: use connect_if_needed so shared layers aren't + # opened twice when multiple devices share the same port. + # NidaqmxLayer: uses start() if hasattr(self._layer, "connect"): - ok = self._layer.connect() + if self._layer.is_connected: + ok = True # already open — shared with another device + else: + ok = self._layer.connect() else: ok = self._layer.start() - # Surface the underlying error message if available if not ok and hasattr(self._layer, "last_error"): self._last_error = self._layer.last_error @@ -128,13 +134,19 @@ class AnalogInputDevice(BaseDevice): return ok def disconnect(self) -> None: - try: - if hasattr(self._layer, "disconnect"): - self._layer.disconnect() - elif hasattr(self._layer, "stop"): - self._layer.stop() - except Exception: - pass + # For shared Arduino layers, release our ref count via port_registry. + # The layer stays open until the last device using it disconnects. + if self.backend == "arduino" and not self.simulate: + from api_layers.port_registry import port_registry + port_registry.release(self._ard_port, self._ard_baud) + else: + try: + if hasattr(self._layer, "stop"): + self._layer.stop() + elif hasattr(self._layer, "disconnect"): + self._layer.disconnect() + except Exception: + pass self.status = DeviceStatus.DISCONNECTED def read_channels(self) -> Dict[str, float]: |
