from ctypes import *
from secg40 import *
import sys
import time

device = SECG40(get_lib_path())


def printLeads():
    if 0 != device.get_output_lead(OutputLead_E.Lead_RA.value):
        print(OutputLead_E.Lead_RA.name, '  ON')
    else:
        print(OutputLead_E.Lead_RA.name, '  OFF')

    if 0 != device.get_output_lead(OutputLead_E.Lead_LA.value):
        print(OutputLead_E.Lead_LA.name, '  ON')
    else:
        print(OutputLead_E.Lead_LA.name, '  OFF')

    if 0 != device.get_output_lead(OutputLead_E.Lead_LL.value):
        print(OutputLead_E.Lead_LL.name, '  ON')
    else:
        print(OutputLead_E.Lead_LL.name, '  OFF')


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

print('device is connected... ({})'.format(
    device.get_serial_number()))

for lead in range(0, 3):  # Loop RA/LA/LL
    print('### Test Iteration: ', lead, '###')

    # Reset All Setting to Default
    device.reset()

    # Set Output Lead
    if 0 != device.set_output_lead(lead, 1):
        print('set_output_lead(', lead, '1) failed')
        sys.exit()

    printLeads()

    # Set Output Function
    outFunc = lead + 1
    if 0 != device.set_output_function(outFunc):
        print('set_output_function(', outFunc, ' 1) failed')
        sys.exit()

    print('Output Function: ', OutputFunction_E(outFunc).name)

    # Set Amplitude : 0.5 / 1.0 / 1.5 mV
    amp = c_double(outFunc * 0.5)
    if 0 != device.set_amplitude(amp):
        print('set_amplitude(', amp, ') failed')
        sys.exit()

    print('Amplitude is: ', device.get_amplitude(), ' mV')

    # Set Frequency : 1.0 / 2.0 / 3.0 Hz
    freq = c_double(outFunc)
    if 0 != device.set_frequency(freq):
        print('set_frequency(', freq, ') failed')
        sys.exit()

    print('Frequency is: ', device.get_frequency(), ' Hz')

    print('---------------------- Output Signal for 5 seconds ------------------------')
    time.sleep(5)

device.close()
