import rtconfig
from building import *

# get current directory
cwd = GetCurrentDir()

# The set of source files associated with this SConscript file.

src = Split('''

''')

path = []
CPPDEFINES = []

# tinyusb src and inc
if GetDepend(['TINYUSB_USING_DEVICE']) or GetDepend(['TINYUSB_USING_HOST']):
    src += ['../../tinyusb/src/tusb.c']
    path += [cwd + '/../../tinyusb/src/']

# tinyusb common src and inc
if GetDepend(['TINYUSB_USING_DEVICE']) or  GetDepend(['TINYUSB_USING_HOST']):
    src += ['../../tinyusb/src/common/tusb_fifo.c']
    path += [cwd + '/../../tinyusb/src/common/']

# tinyusb device src and inc
if GetDepend(['TINYUSB_USING_DEVICE']):
    src += ['../../tinyusb/src/device/usbd_control.c']
    src += ['../../tinyusb/src/device/usbd.c']
    path += [cwd + '/../../tinyusb/src/device/']

# tinyusb hw inc
if GetDepend(['TINYUSB_USING_DEVICE']) or GetDepend(['TINYUSB_USING_HOST']):
    path += [cwd + '/../../tinyusb/hw']

# audio src and inc
if GetDepend(['TUSBD_USING_AUDIO']):
    # audio class
    src += ['../../tinyusb/src/class/audio/audio_device.c']
    path += [cwd + '/../../tinyusb/src/class/audio']
    # audio example
    if GetDepend(['TUSBD_USING_AUDIO_4_CHANNLE_MIC']):
        src += ['../../tinyusb/examples/device/audio_4_channel_mic/src/main.c']
        src += ['../../tinyusb/examples/device/audio_4_channel_mic/src/usb_descriptors.c']
        path += [cwd + '/../../tinyusb/examples/device/audio_4_channel_mic/src'] # tusb_config.h
    elif GetDepend(['TUSBD_USING_AUDIO_TEST']):
        src += ['../../tinyusb/examples/device/audio_test/src/main.c']
        src += ['../../tinyusb/examples/device/audio_test/src/usb_descriptors.c']
        path += [cwd + '/../../tinyusb/examples/device/audio_test/src'] # tusb_config.h
    elif GetDepend(['TUSBD_USING_AUDIO_TEST_MULTI_RATE']):
        src += ['../../tinyusb/examples/device/audio_test_multi_rate/src/main.c']
        src += ['../../tinyusb/examples/device/audio_test_multi_rate/src/usb_descriptors.c']
        path += [cwd + '/../../tinyusb/examples/device/audio_test_multi_rate/src'] # tusb_config.h
    elif GetDepend(['TUSBD_USING_AUDIO_UAC2_HEADSET']):
        src += ['../../tinyusb/examples/device/uac2_headset/src/main.c']
        src += ['../../tinyusb/examples/device/uac2_headset/src/usb_descriptors.c']
        path += [cwd + '/../../tinyusb/examples/device/uac2_headset/src'] # tusb_config.h

# board test src and inc
if GetDepend(['TUSBD_USING_BOARD_TEST']):
    src += ['../../tinyusb/examples/device/board_test/src/main.c']
    path += [cwd + '/../../tinyusb/examples/device/board_test/src'] # tusb_config.h

# bth src and inc
if GetDepend(['TUSBD_USING_BTH']):
    # bth class
    src += ['../../tinyusb/src/class/bth/bth_device.c']
    path += [cwd + '/../../tinyusb/src/class/bth']

# cdc dual ports src and inc
if GetDepend(['TUSBD_USING_CDC']):
    # cdc class
    src += ['../../tinyusb/src/class/cdc/cdc_device.c']
    path += [cwd + '/../../tinyusb/src/class/cdc']
    # cdc example
    if GetDepend(['TUSBD_USING_CDC_DUAL_PORTS']):
        src += ['../../tinyusb/examples/device/cdc_dual_ports/src/main.c']
        src += ['../../tinyusb/examples/device/cdc_dual_ports/src/usb_descriptors.c']
        path += [cwd + '/../../tinyusb/examples/device/cdc_dual_ports/src'] # tusb_config.h
    elif GetDepend(['TUSBD_USING_CDC_MSC']):
        src += ['../../tinyusb/examples/device/cdc_msc/src/main.c']
        src += ['../../tinyusb/examples/device/cdc_msc/src/msc_disk.c']
        src += ['../../tinyusb/examples/device/cdc_msc/src/usb_descriptors.c']
        path += [cwd + '/../../tinyusb/examples/device/cdc_msc/src'] # tusb_config.h
    elif GetDepend(['TUSBD_USING_CDC_MSC_FREERTOS']):
        src += ['../../tinyusb/examples/device/cdc_msc_freertos/src/freertos_hook.c']
        src += ['../../tinyusb/examples/device/cdc_msc_freertos/src/main.c']
        src += ['../../tinyusb/examples/device/cdc_msc_freertos/src/msc_disk.c']
        path += [cwd + '/../../tinyusb/examples/device/cdc_msc_freertos/src'] # tusb_config.h
        path += [cwd + '/../../tinyusb/examples/device/cdc_msc_freertos/src/FreeRTOSConfig']

# dfu src and inc
if GetDepend(['TUSBD_USING_DFU']):
    # dfu class
    path += [cwd + '/../../tinyusb/src/class/dfu']
    # dfu example
    if GetDepend(['TUSBD_USING_DFU_TEST']):
        src += ['../../tinyusb/src/class/dfu/dfu_device.c']
        src += ['../../tinyusb/examples/device/dfu/src/main.c']
        src += ['../../tinyusb/examples/device/dfu/src/usb_descriptors.c']
        path += [cwd + '/../../tinyusb/examples/device/dfu/src'] # tusb_config.h
    elif GetDepend(['TUSBD_USING_DFU_RUNTIME']):
        src += ['../../tinyusb/src/class/dfu/dfu_rt_device.c'] # dfu runtime class
        src += ['../../tinyusb/examples/device/dfu_runtime/src/main.c']
        src += ['../../tinyusb/examples/device/dfu_runtime/src/usb_descriptors.c']
        path += [cwd + '/../../tinyusb/examples/device/dfu_runtime/src'] # tusb_config.h
    elif GetDepend(['TUSBD_USING_DFU_ON_CHIP_FLASH']):
        src += ['../../tinyusb/src/class/dfu/dfu_device.c']

# dynamic src and inc
if GetDepend(['TUSBD_USING_DYNAMIC_CONFIGURATION']):
    # dynamic configuration example
    src += ['../../tinyusb/examples/device/dynamic_configuration/src/main.c']
    src += ['../../tinyusb/examples/device/dynamic_configuration/src/msc_disk.c']
    src += ['../../tinyusb/examples/device/dynamic_configuration/src/usb_descriptors.c']
    path += [cwd + '/../../tinyusb/examples/device/dynamic_configuration/src'] # tusb_config.h

# hid src and inc
if GetDepend(['TUSBD_USING_HID']):
    # hid class
    src += ['../../tinyusb/src/class/hid/hid_device.c']
    path += [cwd + '/../../tinyusb/src/class/hid']
    # hid example
    if GetDepend(['TUSBD_USING_HID_BOOT_INTERFACE']):
        src += ['../../tinyusb/examples/device/hid_boot_interface/src/main.c']
        src += ['../../tinyusb/examples/device/hid_boot_interface/src/usb_descriptors.c']
        path += [cwd + '/../../tinyusb/examples/device/hid_boot_interface/src'] # tusb_config.h
    elif GetDepend(['TUSBD_USING_HID_COMPOSITE']):
        src += ['../../tinyusb/examples/device/hid_composite/src/main.c']
        src += ['../../tinyusb/examples/device/hid_composite/src/usb_descriptors.c']
        path += [cwd + '/../../tinyusb/examples/device/hid_composite/src'] # tusb_config.h
    elif GetDepend(['TUSBD_USING_HID_COMPOSITE_FREERTOS']):
        src += ['../../tinyusb/examples/device/hid_composite_freertos/src/freertos_hook.c']
        src += ['../../tinyusb/examples/device/hid_composite_freertos/src/main.c']
        src += ['../../tinyusb/examples/device/hid_composite_freertos/src/usb_descriptors.c']
        path += [cwd + '/../../tinyusb/examples/device/hid_composite_freertos/src'] # tusb_config.h
        path += [cwd + '/../../tinyusb/examples/device/hid_composite_freertos/src/FreeRTOSConfig']
    elif GetDepend(['TUSBD_USING_HID_GENERIC_INOUT']):
        src += ['../../tinyusb/examples/device/hid_generic_inout/src/main.c']
        src += ['../../tinyusb/examples/device/hid_generic_inout/src/usb_descriptors.c']
        path += [cwd + '/../../tinyusb/examples/device/hid_generic_inout/src'] # tusb_config.h
    elif GetDepend(['TUSBD_USING_HID_MULTIPLE_INTERFACE']):
        src += ['../../tinyusb/examples/device/hid_multiple_interface/src/main.c']
        src += ['../../tinyusb/examples/device/hid_multiple_interface/src/usb_descriptors.c']
        path += [cwd + '/../../tinyusb/examples/device/hid_multiple_interface/src'] # tusb_config.h

# midi src and inc
if GetDepend(['TUSBD_USING_MIDI']):
    # midi class
    src += ['../../tinyusb/src/class/midi/midi_device.c']
    path += [cwd + '/../../tinyusb/src/class/midi']
    # midi example
    if GetDepend(['TUSBD_USING_MIDI_TEST']):
        src += ['../../tinyusb/examples/device/midi_test/src/main.c']
        src += ['../../tinyusb/examples/device/midi_test/src/usb_descriptors.c']
        path += [cwd + '/../../tinyusb/examples/device/midi_test/src'] # tusb_config.h

# msc src and inc
if GetDepend(['TUSBD_USING_MSC']):
    # msc class
    src += ['../../tinyusb/src/class/msc/msc_device.c']
    path += [cwd + '/../../tinyusb/src/class/msc']
    # msc example
    if GetDepend(['TUSBD_USING_MSC_DUAL_LUN']):
        src += ['../../tinyusb/examples/device/msc_dual_lun/src/main.c']
        src += ['../../tinyusb/examples/device/msc_dual_lun/src/msc_disk_dual.c']
        src += ['../../tinyusb/examples/device/msc_dual_lun/src/usb_descriptors.c']
        path += [cwd + '/../../tinyusb/examples/device/msc_dual_lun/src'] # tusb_config.h

# net src and inc
if GetDepend(['TUSBD_USING_NET']):
    # net class
    src += ['../../tinyusb/src/class/net/ecm_rndis_device.c']
    src += ['../../tinyusb/src/class/net/ncm_device.c']
    path += [cwd + '/../../tinyusb/src/class/net']
    # net example
    if GetDepend(['TUSBD_USING_NET_LWIP_WEBSERVER']):
        src += ['../../tinyusb/examples/device/net_lwip_webserver/src/main.c']
        src += ['../../tinyusb/examples/device/net_lwip_webserver/src/usb_descriptors.c']
        src += ['../../tinyusb/lib/networking/dhserver.c']
        src += ['../../tinyusb/lib/networking/dnserver.c']
        src += ['../../tinyusb/lib/networking/rndis_reports.c']
        path += [cwd + '/../../tinyusb/examples/device/net_lwip_webserver/src'] # tusb_config.h
        path += [cwd + '/../../tinyusb/examples/device/net_lwip_webserver/src/arch']
        path += [cwd + '/../../tinyusb/lib/networking']

# usbtmc src and inc
if GetDepend(['TUSBD_USING_USBTMC']):
    # usbtmc class
    src += ['../../tinyusb/src/class/usbtmc/usbtmc_device.c']
    path += [cwd + '/../../tinyusb/src/class/usbtmc']
    # usbtmc example
    if GetDepend(['TUSBD_USING_USBTMC_EXAMPLE']):
        src += ['../../tinyusb/examples/device/usbtmc/src/main.c']
        src += ['../../tinyusb/examples/device/usbtmc/src/usb_descriptors.c']
        src += ['../../tinyusb/examples/device/usbtmc/src/usbtmc_app.c']
        path += [cwd + '/../../tinyusb/examples/device/usbtmc/src'] # tusb_config.h

# vendor src and inc
if GetDepend(['TUSBD_USING_VENDOR']):
    # vendor class
    src += ['../../tinyusb/src/class/vendor/vendor_device.c']
    path += [cwd + '/../../tinyusb/src/class/vendor']
    # vendor example
    if GetDepend(['TUSBD_USING_VENDOR_WEBUSB_SERIAL']):
        src += ['../../tinyusb/examples/device/webusb_serial/src/main.c']
        src += ['../../tinyusb/examples/device/webusb_serial/src/usb_descriptors.c']
        path += [cwd + '/../../tinyusb/examples/device/webusb_serial/src'] # tusb_config.h

# video src and inc
if GetDepend(['TUSBD_USING_VIDEO']):
    # video class
    src += ['../../tinyusb/src/class/video/video_device.c']
    path += [cwd + '/../../tinyusb/src/class/video']
    # video example
    if GetDepend(['TUSBD_USING_VIDEO_CAPTURE']):
        src += ['../../tinyusb/examples/device/video_capture/src/main.c']
        src += ['../../tinyusb/examples/device/video_capture/src/usb_descriptors.c']
        path += [cwd + '/../../tinyusb/examples/device/video_capture/src'] # tusb_config.h

# tusb config define
if GetDepend(['SOC_STM32F429IG']):
    src += ['../../tinyusb/src/portable/st/synopsys/dcd_synopsys.c']
    path += [cwd + '/../../tinyusb/src/portable/st/synopsys']
    CPPDEFINES += [
        'BOARD_TUD_RHPORT = 1',
        'BOARD_TUD_MAX_SPEED = OPT_MODE_FULL_SPEED',
        'CFG_TUSB_MCU = OPT_MCU_STM32F4',
        'CFG_TUD_ENDPOINT0_SIZE = 64',
        'CFG_TUSB_DEBUG = 0',
        'CFG_TUSB_RHPORT1_MODE = 0x0201', # OPT_MODE_DEVICE | OPT_MODE_FULL_SPEED = 0x0201 to avoid build error
        ]

group = DefineGroup('tinyusb', src, depend = [''], CPPPATH = path, CPPDEFINES = CPPDEFINES)

list = os.listdir(cwd)
for item in list:
    if os.path.isfile(os.path.join(cwd, item, 'SConscript')):
        group = group + SConscript(os.path.join(item, 'SConscript'))

Return('group')
