# -*- coding: utf-8 -*-
"""
Created on Mon Apr 11 10:30:34 2022

@author: CW
"""

def loadDLL():
    """This function loads the HSDC Pro 64-bit DLL.
    \nArgs: {None}
    \nReturn: None
    """
    
    if not 'HSDCProDLL' in locals():
        loadedDLL = cdll.LoadLibrary("C:/Program Files (x86)/Texas Instruments/High Speed Data Converter Pro/HSDCPro Automation DLL/64Bit DLL/HSDCProAutomation_64Bit.dll")
        return loadedDLL
    else: 
        pass


if __name__ == '__main__':
    
    import ctypes
    from ctypes import cdll
    import numpy as np
    
    default_timeout = 10000
    NumChannels = 2
    Number_Of_Samples_Per_Channel = 4096
    
    
    HSDCProDLL = loadDLL()
    HSDCProDLL.ADC_Analysis_Window_Length(ctypes.c_ulong(Number_Of_Samples_Per_Channel),default_timeout)
    
    # Save data using CaptureData_32Bits approach:    
    Capture_Data_Array_Len = NumChannels * Number_Of_Samples_Per_Channel
    CaptureData_32bits = (ctypes.c_ulonglong*Capture_Data_Array_Len)()
    HSDCProDLL.Get_Capture_Data_32bits(Number_Of_Samples_Per_Channel,0,CaptureData_32bits,Capture_Data_Array_Len,default_timeout)
    
    data = np.frombuffer(CaptureData_32bits, dtype = np.int32)
    data_from_CaptureData_32bits = data.reshape(Number_Of_Samples_Per_Channel*2,2)[:][:Number_Of_Samples_Per_Channel]
    data_from_CaptureData_32bits = data_from_CaptureData_32bits -(2**(16-1))   # convert from unsigned to signed
    
    
    # Save data using the CSV save approach:
    SaveCSVFilePathWithName = b"C:/Users/Public/Documents/Texas Instruments/High Speed Data Converter Pro/Example Files/Python Files/ADCdataNEW.csv"
    HSDCProDLL.Save_Raw_Data_As_CSV(SaveCSVFilePathWithName,default_timeout)
    
    #load CSV data to np array
    import pandas as pd
    csv_data = pd.read_csv(SaveCSVFilePathWithName.decode("utf-8"),header=None).to_numpy()
    
    #compare results:
    print(np.array_equal(data_from_CaptureData_32bits, csv_data))