<ui_settings>
    <version>2.8</version>
    <model_locked>False</model_locked>
    <canvas_bg_color>#ffffff</canvas_bg_color>
    <initialization_code><![CDATA[# NOTE: Variables and functions defined here will be
# available for use in all Macro and Expression scripts.
# This code is always executed prior simulation start.
# HIL API


# Variable 'SETTINGS_DIR' holds directory where loaded Panel .cus file is located.
# Also you can call 'get_settings_dir_path()' function in any
# Macro and Expression scripts to get the same directory.
SETTINGS_DIR = get_settings_dir_path()

# Numpy module is imported as 'np'
# Scipy module is imported as 'sp'
# Schematic Editor model namespace is imported as 'scm'

]]></initialization_code>
    <components>
        <component_data>
                <id>75b75af4d23111ed8839a0cec8edaff4</id>
                <name><![CDATA[XCP over CAN]]></name>
                <type>Macro</type>
                <description><![CDATA[]]></description>
                <on_click_code active="True"><![CDATA[import sys
from time import sleep
import os.path

from pyxcp.cmdline import ArgumentParser
from pyxcp.daq_stim import DaqList, DaqToCsv, DaqRecorder

modulepath = r"d:\\work\\t_sw\\t_sw\\tests\\90_manual\\704_xcp_over_can\\"
sys.path.append(r"d:\work\t_sw\t_sw\tests\90_manual\704_xcp_over_can")
dll_path = r"d:\work\t_sw\t_sw\tests\90_manual\704_xcp_over_can\SeedNKeyXcp.dll"

from xcp_script import XCPclass, open_db_session, create_measurement_list, parse_csv
old_working_dir = os.getcwd()

try:
    os.chdir(modulepath)

    a2l_folder_path = modulepath
    a2l_file_name = "XCPsim.a2l"
    a2l_file_path = a2l_folder_path + a2l_file_name
    db, session = open_db_session(a2l_file_path)
    data_dict = {}
    
    measurement_names = ["channel_d", "TestDWord_501", "TestDWord_502", "TestWord_992", "TestWord_991",
                         "TestWord_989"]
    daq_measurements = create_measurement_list(measurement_names, session)

    daq_list = [DaqList(
        name="test",
        event_num=2,
        stim=False,
        enable_timestamps=False,
        measurements=daq_measurements,
        priority=0,
        prescaler=1
    )]

    daq_parser = DaqToCsv(daq_list)
    # daq_parser = DaqRecorder(daq_list, "demo_measurement", 1)
    ap = ArgumentParser()

    with ap.run(policy=daq_parser) as xm:
        xcp = XCPclass(master=xm)
        timestamp_enable = False
        conn = xm.connect()
        mode_info = xm.getCommModeInfo()
        gid = xm.getId(0x1)
        status = xm.getStatus()
        xm.synch()

        xcp.get_seed_and_unlock(xm, 0x04, dll_path=dll_path)
        DAQ_proc_info = xm.getDaqProcessorInfo()
        DAQ_res_info = xm.getDaqResolutionInfo()
        DAQ_info = xm.getDaqInfo()
        for i in range(0, len(DAQ_info["channels"])):
            DAQ_event_info = xm.getDaqEventInfo(i)
            upload_rcv = xm.upload(DAQ_event_info.eventChannelNameLength)

        xcp.get_seed_and_unlock(xm, 0x01, dll_path=dll_path)
        scp = xm.setCalPage(0x83, 0, 0)

        xcp.build_checksum_for_slave(xm, session)
        xcp.set_mta_and_build_checksum(xm, session)

        for i in range(0, DAQ_proc_info.maxEventChannel - 1):
            DAQ_event_info = xm.getDaqEventInfo(i)
            upload_rcv = xm.upload(DAQ_event_info.eventChannelNameLength)

        pwm = xcp.get_measurement(xm, "PWM", session)
        print(f"PWM = {pwm}")
        pwm_filter = xcp.get_measurement(xm, "PWMFiltered", session)
        print(f"PWMFiltered = {pwm_filter}")

        value = 5
        PWM_Level = xcp.get_characteristic_value(xm, "PWM_Level", session)
        print(f"PWM_Level = {PWM_Level}")

        xcp.set_characteristic_value(xm, value, "PWM_Level", session)

        PWM_Level = xcp.get_characteristic_value(xm, "PWM_Level", session)
        print(f"PWM_Level = {PWM_Level}")

        daq_parser.setup()
        daq_parser.start()
        sleep(1)
        daq_parser.stop()
        session.close()
        xm.disconnect()

    data_dict = parse_csv("test.csv")
    for i in range(len(data_dict)):
        print(f"{measurement_names[i]}: {data_dict[measurement_names[i]]}")

finally:
    os.chdir(old_working_dir)]]></on_click_code>
                <on_start_code active="True"><![CDATA[# NOTE: The code specified in this handler will be executed on simulation start.
# NOTE: Variables specified here will be available in other handlers.
# HIL API is imported as 'hil'
# SCADA API is imported as 'panel'
# SCADA API constants are imported as 'api_const'
# 'WIDGET_HANDLE' constant holds the WidgetHandle object of this widget.
# This object can be used as an argument in SCADA API functions.

]]></on_start_code>
                <on_timer_code active="True"><![CDATA[# NOTE: The code specified in this handler will be executed on timer event.
# HIL API is imported as 'hil'
# SCADA API is imported as 'panel'
# SCADA API constants are imported as 'api_const'
# 'WIDGET_HANDLE' constant holds the WidgetHandle object of this widget.
# This object can be used as an argument in SCADA API functions.

]]></on_timer_code>
                <run_each>250</run_each>
                <on_stop_code active="True"><![CDATA[# NOTE: The code specified in this handler will be executed after simulation is stopped.
# HIL API is imported as 'hil'
# SCADA API is imported as 'panel'
# SCADA API constants are imported as 'api_const'
# 'WIDGET_HANDLE' constant holds the WidgetHandle object of this widget.
# This object can be used as an argument in SCADA API functions.

]]></on_stop_code>
                <widget_settings>
                    <x>176</x>
                    <y>48</y>
                    <width>144</width>
                    <height>40</height>
                    <appearance><![CDATA[Flat]]></appearance>
                    <hierarchy_position><![CDATA[1]]></hierarchy_position>
                    <is_background><![CDATA[False]]></is_background>
                    <label><![CDATA[]]></label>
                    <use_label>False</use_label>
                </widget_settings>
            </component_data>
        </components>
</ui_settings>