; PlatformIO Project Configuration File for EMS-ESP
;
; override any settings with your own local ones in pio_local.ini (see example pio_local.ini_example)
; The firmware is renamed following this convention, and placed in the build/firmware folder:
;   EMS-ESP32-<version>-<chip_type>-<flash_mem>[+ if PSRAM onboard].bin
;   For example: EMS-ESP-3_7_0-dev_31-esp32-16MB+.bin

[platformio]
; build all the CI targets as default. Use pio_local.ini to override.
default_envs = ci_s_4M, ci_s_16M, ci_s_16M_P, ci_s3_16M_P

extra_configs =
  factory_settings.ini
  pio_local.ini

[common]
core_build_flags = -std=gnu++2a -Isrc -Wno-type-limits -Wall -Wextra -Wno-unused-parameter -Wno-unused-variable -Wno-format
core_unbuild_flags = -std=gnu++11

my_build_flags =

; explanation on these custom settings:
;  CONFIG_ASYNC_TCP_QUEUE - see https://github.com/emsesp/EMS-ESP32/issues/177 
;  CONFIG_ASYNC_TCP_STACK_SIZE - stack usage measured: ESP32: ~2.3K, ESP32S3: ~3.5k
;  CONFIG_ASYNC_TCP_TASK_PRIORITY - default is 10, was 5 for EMS-ESP
build_flags =
  ${common.core_build_flags}
  ${factory_settings.build_flags}
  ${common.my_build_flags}
  -D ONEWIRE_CRC16=0
  -D CONFIG_ETH_ENABLED
  -D CONFIG_UART_ISR_IN_IRAM
  -D CONFIG_ASYNC_TCP_STACK_SIZE=6144
  -D CONFIG_ASYNC_TCP_QUEUE=32
  -D CONFIG_ASYNC_TCP_TASK_PRIORITY=10
  -D CORE_DEBUG_LEVEL=0

unbuild_flags =
  ${common.core_unbuild_flags}

[espressif32_base]
platform = espressif32@6.9.0
framework = arduino
board_build.filesystem = littlefs
build_flags = 
  ${common.build_flags}
build_unflags = 
  ${common.unbuild_flags}
extra_scripts =
  pre:scripts/build_interface.py
  scripts/rename_fw.py

[espressif32_base_tasmota]
;  use Tasmota's library for 4MB variants
;  it removes some unused libs (like mbedtsl, so no WiFi_secure.h) and increases available heap
;  Tasmota Arduino Core 2.0.18 with IPv6 support, based on IDF 4.4.8
platform = https://github.com/tasmota/platform-espressif32/releases/download/2024.06.00/platform-espressif32.zip
framework = arduino
board_build.filesystem = littlefs
build_flags =
  ${common.build_flags}
  -DTASMOTA_SDK
build_unflags = 
  ${common.unbuild_flags}
extra_scripts =
  pre:scripts/build_interface.py
  scripts/rename_fw.py

[env]
monitor_speed = 115200
monitor_filters = direct
; on MacBook default upload speed is 921600, which is too fast for USB. Lower for Mac.
; see https://docs.platformio.org/en/latest/projectconf/sections/env/options/upload/upload_speed.html
; upload_speed = 460800
upload_speed = 921600
build_type = release
check_tool = cppcheck, clangtidy
check_severity = high, medium
check_flags =
  cppcheck: --std=c++11 -v
  clangtidy: --checks=-*,clang-analyzer-*,performance-*
lib_ldf_mode = chain+
lib_deps = 
  https://github.com/emsesp/EMS-ESP-Modules.git@1.0.3

;
; builds for GitHub Actions CI
; the Web interface is built separately during the GH Action script, so skipped on these targets
;
[env:ci_s_4M]
; 4MB ESP32 - using Tasmota - no SSL, no PSRAM - like the BBQKees older S32 and E32 models
extends = espressif32_base_tasmota
extra_scripts = scripts/rename_fw.py
board = esp32dev
board_upload.flash_size = 4MB
board_build.partitions = esp32_partition_4M.csv

[env:ci_s_16M]
; 16MB ESP32 - using Tasmota - no SSL, no PSRAM - like the BBQKees some later S32 models
extends = espressif32_base_tasmota
extra_scripts = scripts/rename_fw.py
board = esp32dev
board_upload.flash_size = 16MB
board_build.partitions = esp32_partition_16M.csv

[env:ci_s_16M_P]
; 16MB ESP32 - with PSRAM - like BBQKees E32V2
extends = espressif32_base
extra_scripts = scripts/rename_fw.py
board = esp32dev
board_upload.flash_size = 16MB
board_build.partitions = esp32_partition_16M.csv
board_build.extra_flags = -DBOARD_HAS_PSRAM

[env:ci_s3_16M_P]
; 16MB ESP32-S3 - with PSRAM - like BBQKees S3
extends = espressif32_base
extra_scripts = scripts/rename_fw.py
board = lolin_s3
board_build.f_cpu = 240000000L
board_upload.flash_size = 16MB
board_build.partitions = esp32_partition_16M.csv

;
; Direct builds
;
; For board params see json files in https://github.com/platformio/platform-espressif32/tree/master/boards
;

[env:s_4M]
extends = espressif32_base_tasmota
board = esp32dev
board_upload.flash_size = 4MB
board_build.partitions = esp32_partition_4M.csv

[env:s_asym_4M]
extends = espressif32_base_tasmota
board = esp32dev
board_upload.flash_size = 4MB
board_build.partitions = esp32_asym_partition_4M.csv

[env:s_16M_P]
extends = espressif32_base
board = esp32dev
board_upload.flash_size = 16MB
board_build.partitions = esp32_partition_16M.csv
board_build.extra_flags = -DBOARD_HAS_PSRAM

[env:c3_mini_4M]
extends = espressif32_base_tasmota
board = lolin_c3_mini
board_upload.flash_size = 4MB
board_build.partitions = esp32_partition_4M.csv

; lolin C3 mini v1 needs special wifi init.
; https://www.wemos.cc/en/latest/c3/c3_mini_1_0_0.html#about-wifi
[env:c3_miniv1_4M]
extends = espressif32_base_tasmota
board = lolin_c3_mini
board_upload.flash_size = 4MB
board_build.partitions = esp32_partition_4M.csv
build_flags =
  ${common.build_flags}
  -DTASMOTA_SDK
  -DBOARD_C3_MINI_V1

[env:s2_4M]
extends = espressif32_base_tasmota
board = lolin_s2_mini
board_upload.flash_size = 4MB
board_build.partitions = esp32_partition_4M.csv

; https://github.com/platformio/platform-espressif32/blob/master/boards/lolin_s3.json
[env:s3_16M_P]
extends = espressif32_base
board = lolin_s3
board_upload.use_1200bps_touch = false
board_upload.wait_for_upload_port = false
board_upload.flash_size = 16MB
board_build.partitions = esp32_partition_16M.csv

[env:s3_32M_P]
extends = espressif32_base
board = lolin_s3
board_build.arduino.memory_type: opi_opi
board_build.flash_mode = opi
board_upload.flash_size = 32MB
board_build.partitions = esp32_partition_32M.csv

; 
; Building and testing natively, standalone without an ESP32.
; See https://docs.platformio.org/en/latest/platforms/native.html
;
; It will generate an executable which when run will show the EMS-ESP Console where you can run tests using the `test` command.
;
; See https://docs.platformio.org/en/latest/core/installation/shell-commands.html#piocore-install-shell-commands
;
; to build and run directly on linux: pio run -e native -t exec
;
; to build and run on Windows, it needs winsock for the console input so:
;  - For the first time, install Msys2 (https://www.msys2.org/) and the GCC compiler with `run pacman -S mingw-w64-ucrt-x86_64-gcc`
;  - Then, build with `pio run -e native` to create the program.exe file
;  - run by calling the executable from the Mysys shell e.g. `C:/msys64/msys2_shell.cmd -defterm -here -no-start -ucrt64 -c <location>/.pio/build/native/program.exe`
;  - or use with Windows Terminal https://www.msys2.org/docs/terminals/
;
[env:native]
platform = native
build_flags =
  -DARDUINOJSON_ENABLE_ARDUINO_STRING=1
  -DEMSESP_STANDALONE -DEMSESP_TEST
  -DEMSESP_DEFAULT_LOCALE=\"en\" -DEMSESP_DEFAULT_TX_MODE=8 -DEMSESP_DEFAULT_VERSION=\"3.7.1-dev.0\" -DEMSESP_DEFAULT_BOARD_PROFILE=\"S32\"
  -std=gnu++14 -Og -ggdb
build_type = debug
build_src_flags =
  -Wall -Wextra
  -Wno-unused-parameter -Wno-sign-compare -Wno-missing-braces
  -I./lib_standalone
  -I./lib/uuid-common/src
  -I./lib/uuid-console/src
  -I./lib/uuid-log/src
  -I./lib/semver
  -I./lib/PButton
  -I./lib/ArduinoJson
  -I./lib/espMqttClient/src
  -I./lib/espMqttClient/src/Transport
build_src_filter =
  +<*>
  +<../lib_standalone>
  +<../lib/uuid-common>
  +<../lib/uuid-console>
  +<../lib/uuid-log>
  +<../lib/semver>
  +<../lib/PButton>
  +<../lib/espMqttClient/src>
  +<../lib/espMqttClient/src/Transport>
lib_compat_mode = off
lib_ldf_mode = off
lib_ignore = Module EMS-ESP-Modules

; unit tests
;  The code is in ./test/test_api.*
; to run use `pio run -e native-test -t exec`. All tests should PASS.
; to update the test results, compile with -DEMSESP_UNITY_CREATE by uncommenting the line below
; then run and capture the output between "START - CUT HERE" and "END - CUT HERE" into the test_api.h file
[env:native-test]
platform = native
test_build_src = true
build_flags =
  -DARDUINOJSON_ENABLE_ARDUINO_STRING=1
  -DEMSESP_STANDALONE -DEMSESP_TEST
  -DEMSESP_UNITY
  ; -DEMSESP_UNITY_CREATE
  -DEMSESP_DEFAULT_LOCALE=\"en\" -DEMSESP_DEFAULT_TX_MODE=8 -DEMSESP_DEFAULT_VERSION=\"3.7.1-dev.0\" -DEMSESP_DEFAULT_BOARD_PROFILE=\"S32\"
  -std=gnu++14 -Os -ggdb -Wall -Wextra
  -lgcov --coverage -fprofile-arcs -ftest-coverage
build_type = debug
build_src_flags =
  -Wno-unused-parameter -Wno-sign-compare -Wno-missing-braces
  -I./lib_standalone
  -I./lib/uuid-common/src
  -I./lib/uuid-console/src
  -I./lib/uuid-log/src
  -I./lib/semver
  -I./lib/PButton
  -I./lib/ArduinoJson
  -I./lib/espMqttClient/src
  -I./lib/espMqttClient/src/Transport
  -I./test/api
  -I./test
build_src_filter =
  +<*>
  -<src/main.cpp>
  +<../test>
  +<../lib_standalone>
  +<../lib/uuid-common>
  +<../lib/uuid-console>
  +<../lib/uuid-log>
  +<../lib/semver>
  +<../lib/PButton>
  +<../lib/espMqttClient/src>
  +<../lib/espMqttClient/src/Transport>
lib_ldf_mode = off
lib_deps = Unity
lib_ignore = Module EMS-ESP-Modules
test_testing_command =
  ${platformio.build_dir}/${this.__env__}/program
