From 558e0de315c99f489967f0fe1163a25d54cfc5af Mon Sep 17 00:00:00 2001
From: wendtalexander <wendtalexander@protonmail.com>
Date: Fri, 20 Sep 2024 11:08:20 +0200
Subject: [PATCH] abstract class, currently handly reading analog input

---
 pyrelacs/repros/repos.py | 61 +++++++++++++++++++++++++++++++++++-----
 1 file changed, 54 insertions(+), 7 deletions(-)

diff --git a/pyrelacs/repros/repos.py b/pyrelacs/repros/repos.py
index 3aaecbb..0de3d1b 100644
--- a/pyrelacs/repros/repos.py
+++ b/pyrelacs/repros/repos.py
@@ -1,5 +1,6 @@
+from ctypes import Array, c_double
 import uldaq
-from IPython import embed
+import numpy as np
 
 from pyrelacs.util.logging import config_logging
 
@@ -8,6 +9,58 @@ log = config_logging()
 
 class Repos:
     def __init__(self) -> None:
+        devices = uldaq.get_daq_device_inventory(uldaq.InterfaceType.USB)
+        log.debug(f"Found daq devices {len(devices)}, connecting to the first one")
+        if len(devices) == 0:
+            log.error("Did not found daq devices, please connect one")
+            exit(1)
+        self.daq_device = uldaq.DaqDevice(devices[0])
+        self.daq_device.connect()
+        log.debug("Connected")
+
+    def read_analog_daq(
+        self,
+        channels: list[int],
+        duration: int,
+        samplerate: float,
+        AiInputMode: uldaq.AiInputMode = uldaq.AiInputMode.SINGLE_ENDED,
+        Range: uldaq.Range = uldaq.Range.BIP10VOLTS,
+        ScanOption: uldaq.ScanOption = uldaq.ScanOption.DEFAULTIO,
+        AInScanFlag: uldaq.AInScanFlag = uldaq.AInScanFlag.DEFAULT,
+    ) -> Array[c_double]:
+
+        if len(channels) > 2:
+            log.error("Please provide only two channels")
+        # Get the Ananlog In device and Analog Info
+
+        ai_device = self.daq_device.get_ai_device()
+        ai_info = ai_device.get_info()
+
+        buffer_len = np.shape(np.arange(0, duration, 1 / samplerate))[0]
+        data_analog_input = uldaq.create_float_buffer(1, buffer_len)
+
+        er = ai_device.a_in_scan(
+            channels[0],
+            channels[1],
+            AiInputMode,
+            Range,
+            buffer_len,
+            samplerate,
+            ScanOption,
+            AInScanFlag,
+            data=data_analog_input,
+        )
+        ai_device.scan_wait(uldaq.WaitType.WAIT_UNTIL_DONE, timeout=-1)
+        log.info(f"Sampled with {er}")
+        log.debug("Scanning")
+
+        return data_analog_input
+
+    def disconnect_dac(self):
+        self.daq_device.disconnect()
+        self.daq_device.release()
+
+    def write_daq(self) -> None:
         pass
 
     def run_repo(self) -> None:
@@ -18,9 +71,3 @@ class Repos:
 
     def reload_repo(self) -> None:
         pass
-
-    def read_daq(self) -> None:
-        pass
-
-    def write_daq(self) -> None:
-        pass