import ctypes
from ctypes import *


def initialization():
    lib = ctypes.CDLL('MCSControl.dll')
    out_buffer = ctypes.create_string_buffer(3000)
    buffer_size = ctypes.c_uint(3000)
    opt=ctypes.c_char_p()
    system_index = ctypes.c_uint()

    channel_index = ctypes.c_uint(0)

    direction = ctypes.c_uint(0)
    hold_time = ctypes.c_uint(0)
    auto_zero = ctypes.c_uint(0)

    inizializing_option = b"sync"
    options = ctypes.c_char_p(inizializing_option)

    lib.SA_FindSystems.argtypes = [POINTER(c_char), POINTER(c_char*3000), POINTER(c_uint)]
    result = lib.SA_FindSystems(opt, out_buffer, buffer_size)
    system_locator=out_buffer.value

    lib.SA_OpenSystem.argtypes = [POINTER(ctypes.c_uint), c_char_p, c_char_p]
    result = lib.SA_OpenSystem(system_index, system_locator, options)

    lib.SA_FindReferenceMark_S.argtypes = [ctypes.c_uint, ctypes.c_uint, ctypes.c_uint, ctypes.c_uint, ctypes.c_uint]
    result = lib.SA_FindReferenceMark_S(system_index, channel_index, direction, hold_time, auto_zero)
    #print(result)

    while True:
        status = ctypes.c_int()
        lib.SA_GetStatus_S.argtypes = [ctypes.c_uint, ctypes.c_uint, POINTER(ctypes.c_int)]
        result = lib.SA_GetStatus_S(system_index, channel_index, status)
        #print(status.value)
        if status.value == 0:
            break


    lib.SA_GotoPositionAbsolute_S.argtypes = [ctypes.c_uint, ctypes.c_uint, ctypes.c_int, ctypes.c_uint]
    result = lib.SA_GotoPositionAbsolute_S(system_index, channel_index, 0, 0)
    #print(result)

    return system_index, channel_index;