from wecg400 import *
import numpy as np
import matplotlib.pyplot as plot


ac = None
dc = None
def load_raw_data_file(file_name):
    global ac, dc
    
    play_raw_data = PLAY_RAW_DATA()
    lineNumber = 0
    f = open(file_name)
    for x in f:
        if lineNumber == 0:
            play_raw_data.SampleRate = int(x)
        elif lineNumber == 1:
            play_raw_data.Size = int(x)
            ac = (c_double * play_raw_data.Size)()
            dc = (c_double * play_raw_data.Size)()
        elif lineNumber >= 4:
            ac[lineNumber-4] = float(x)
            dc[lineNumber-4] = 0

        lineNumber = lineNumber + 1

    f.close()

    play_raw_data.AC = addressof(ac)
    play_raw_data.DC = addressof(dc)
    return play_raw_data

raw_data = PLAY_RAW_DATA()


device = WECG400(get_lib_path())
if not device.connect(-1, 5000):
    print('Error: device is not connected')
    device.free()
    sys.exit()

device.reset()

print('play raw data ...', end='')
sys.stdout.flush()

raw_data = load_raw_data_file('ecg-1hz-5mv.txt')


print (device.load_raw_data(pointer(raw_data)))

device.set_output_lead(OutputDut.DUT1.value, OutputLead.Lead_LA.value)
device.set_output_function(OutputFunction.Output_RawData.value)

time.sleep(10)
device.set_output_function(OutputFunction.Output_Off.value)  # stop output
device.free()

print('done')


