import numpy as np
import struct
import xml.etree.ElementTree as ET
from xml.dom import minidom

def main():

    pretime = 500 * 10 ** -6
    uptime = 50 * 10 ** -6
    posttime = 60 * 10 ** -6
    sample_freq = 2
    pulse_number = 1
    
    #add = (pulse_width * pulse_number * sample_freq * (10**6) * 2)
    counter = 0
    n_pre = round(pretime * sample_freq * (10 ** 6))
    n_up = round(uptime * sample_freq * (10 ** 6))
    n_post = round(posttime * sample_freq * (10 ** 6))
    size_1 =(n_pre + n_post + n_up) * 2
    print(f'rf array size_1 = {size_1}')

    size_2 = np.int32(size_1) * np.int32(pulse_number)
    print(f'rf array size_2 = {size_2}')
    rf_array = np.zeros(size_2, dtype=np.int8)

    for _ in range(0, pulse_number, 1):

        for _ in range(0, n_pre, 1):
            rf_array[counter] = np.int8(0)
            rf_array[counter + 1] = np.int8(0)
            counter += 2
        for _ in range(0, n_up, 1):
            rf_array[counter] = np.int8(127)
            rf_array[counter + 1] = np.int8(0)
            counter += 2
        for _ in range(0, n_up, 1):
            rf_array[counter] = np.int8(0)
            rf_array[counter + 1] = np.int8(0)
            counter += 2

    print(rf_array)

    f = open(f'test3_22_08_25.bin', 'wb')  # открываем файл на чтение

    for elem in rf_array:  # берём каждую строчку из файла f
        f.write(struct.pack('<b',elem))
    f.close()

    root = ET.Element('root')
    rf = ET.SubElement(root, 'RF')
    sw = ET.SubElement(root, 'SW')
    adc = ET.SubElement(root, 'ADC')
    gru1 = ET.SubElement(root, 'GR')

    first_pulse_delay = 17 * 10 ** -6
    if int(sample_freq) == 2:
        first_pulse_delay = 17 * 10 ** -6
    elif int(sample_freq) == 8:
        first_pulse_delay = 4.12 * 10 ** -6
    elif int(sample_freq) == 10:
        first_pulse_delay = 3.26 * 10 ** -6
    elif (sample_freq) == 20:
        first_pulse_delay = 1.5 * 10 ** -6

    cl = ET.SubElement(root, 'CL')
    ET.SubElement(rf,'RF1').text = '0'
    ET.SubElement(sw,'SW1').text = '0'
    ET.SubElement(adc,'ADC1').text = '0'
    ET.SubElement(gru1,'GR1').text = '1'

    ET.SubElement(cl, 'CL1').text = str(round(first_pulse_delay / (20*(10**-9))))

    param_count = 1

    param_count += 1
    ET.SubElement(rf, 'RF' + str(param_count)).text = "0"
    ET.SubElement(sw, 'SW' + str(param_count)).text = "0"
    ET.SubElement(adc, 'ADC' + str(param_count)).text = "0"
    ET.SubElement(gru1, 'GR' + str(param_count)).text = "0"
    ET.SubElement(cl, 'CL' + str(param_count)).text = str(round(pretime / (20*(10**-9))))

    param_count += 1
    ET.SubElement(rf, 'RF' + str(param_count)).text = "0"
    ET.SubElement(sw, 'SW' + str(param_count)).text = "0"
    ET.SubElement(adc, 'ADC' + str(param_count)).text = "0"
    ET.SubElement(gru1, 'GR' + str(param_count)).text = "0"
    ET.SubElement(cl, 'CL' + str(param_count)).text = str(round(uptime / (20*(10**-9))))

    param_count += 1
    ET.SubElement(rf, 'RF' + str(param_count)).text = "0"
    ET.SubElement(sw, 'SW' + str(param_count)).text = "0"
    ET.SubElement(adc, 'ADC' + str(param_count)).text = "0"
    ET.SubElement(gru1, 'GR' + str(param_count)).text = "0"
    ET.SubElement(cl, 'CL' + str(param_count)).text = str(round(posttime / (20*(10**-9))))

    ET.SubElement(root, 'ParamCount').text = str(param_count)
    xml_str = ET.tostring(root, encoding='utf-8', method='xml')
    parsed_str = minidom.parseString(xml_str)  # Парсим строку
    pretty_xml_str = parsed_str.toprettyxml(indent="  ")  # Добавляем отступы

    # Сохраняем форматированный XML в файл
    with open('Sync_param_test3.xml', 'w', encoding='utf-8') as f:
        f.write(pretty_xml_str)

main()
