name: Build ESP

on:
  workflow_dispatch:
  push:
    paths:
      - 'src/**'
      - 'examples/**'
      - 'lib/**'
      - 'hw/**'
      - 'test/hil/**'
      - '.github/workflows/build_esp.yml'
  pull_request:
    branches: [ master ]
    paths:
      - 'src/**'
      - 'examples/**'
      - 'lib/**'
      - 'hw/**'
      - 'test/hil/**'
      - '.github/workflows/build_esp.yml'

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

jobs:
  build-esp:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        board:
        # ESP32-S2
        - 'espressif_kaluga_1'
        # ESP32-S3
        - 'espressif_s3_devkitc'
    steps:
    - name: Setup Python
      uses: actions/setup-python@v5
      with:
        python-version: '3.x'

    - name: Pull ESP-IDF docker
      run: docker pull espressif/idf:latest

    - name: Checkout TinyUSB
      uses: actions/checkout@v4

    - name: Build
      run: docker run --rm -v $PWD:/project -w /project espressif/idf:v5.1.1 python3 tools/build_esp32.py ${{ matrix.board }}

    - name: Upload Artifacts for Hardware Testing
      if: matrix.board == 'espressif_s3_devkitc' && github.repository_owner == 'hathach'
      uses: actions/upload-artifact@v4
      with:
        name: ${{ matrix.board }}
        path: |
          cmake-build/cmake-build-${{ matrix.board }}/*/*/bootloader/bootloader.bin
          cmake-build/cmake-build-${{ matrix.board }}/*/*/*.bin
          cmake-build/cmake-build-${{ matrix.board }}/*/*/partition_table/partition-table.bin
          cmake-build/cmake-build-${{ matrix.board }}/*/*/config.env
          cmake-build/cmake-build-${{ matrix.board }}/*/*/flash_args

  # ---------------------------------------
  # Hardware in the loop (HIL)
  # Current self-hosted instance is running on an RPI4. For attached hardware checkout hil_pi4.json
  # ---------------------------------------
  hil-test:
    # run only with hathach's commit due to limited resource on RPI4
    if: github.repository_owner == 'hathach'
    needs: build-esp
    runs-on: [self-hosted, esp32s3, hardware-in-the-loop]
    strategy:
      fail-fast: false
      matrix:
        board:
          - 'espressif_s3_devkitc'
    steps:
      - name: Clean workspace
        run: |
          echo "Cleaning up previous run"
          rm -rf "${{ github.workspace }}"
          mkdir -p "${{ github.workspace }}"

        # USB bus on rpi4 is not stable, reset it before testing
      - name: Reset USB bus
        run: |
          lsusb
          lsusb -t
          # reset VIA Labs 2.0 hub
          sudo usbreset 001/002

          # legacy code
          #for port in $(lspci | grep USB | cut -d' ' -f1); do
          #    echo -n "0000:${port}"| sudo tee /sys/bus/pci/drivers/xhci_hcd/unbind;
          #    sleep 0.1;
          #    echo -n "0000:${port}" | sudo tee /sys/bus/pci/drivers/xhci_hcd/bind;
          #done

      - name: Checkout test/hil
        uses: actions/checkout@v4
        with:
          sparse-checkout: test/hil

      - name: Download Artifacts
        uses: actions/download-artifact@v4
        with:
          name: ${{ matrix.board }}
          path: cmake-build/cmake-build-${{ matrix.board }}

      - name: Test on actual hardware
        run: |
          python3 test/hil/hil_test.py --board ${{ matrix.board }} hil_pi4.json
