; Pins, radio, OLED — shared by all TLora envs. Does not include sensor_base (saves flash for MQTT observer).
[LilyGo_TLora_V2_1_1_6_core]
extends = esp32_base
board = ttgo-lora32-v1  ; LILYGO T-LoRa V2.1-1.6 ESP32 with SX1276
build_unflags = -Os
build_type = release  ; Set build type to release
board_build.partitions = min_spiffs.csv ; get around 4mb flash limit
build_flags =
  ${esp32_base.build_flags}
  -I variants/lilygo_tlora_v2_1
  -Os -ffunction-sections -fdata-sections  ; Optimize for size
  -D LILYGO_TLORA  ; LILYGO T-LoRa V2.1-1.6 ESP32 with SX1276
  -D P_LORA_DIO_0=26    ; SX1276 DIO0 interrupt pin
  -D P_LORA_DIO_1=33    ; SX1276 DIO1 interrupt pin
  -D P_LORA_NSS=18      ; Chip select - SS pin
  -D P_LORA_RESET=14    ; Reset pin
  -D P_LORA_SCLK=5      ; SPI clock
  -D P_LORA_MISO=19     ; SPI MISO
  -D P_LORA_MOSI=27     ; SPI MOSI
  -D P_LORA_TX_LED=25    ; LED pin for TX indication
  -D PIN_BOARD_SDA=21
  -D PIN_BOARD_SCL=22
  -D PIN_VBAT_READ=35   ; Battery voltage reading (analog pin)
  -D PIN_USER_BTN=0
  -D ARDUINO_LOOP_STACK_SIZE=16384
  -D DISPLAY_CLASS=SSD1306Display
  -D RADIO_CLASS=CustomSX1276
  -D WRAPPER_CLASS=CustomSX1276Wrapper
  -D SX127X_CURRENT_LIMIT=120
  -D LORA_TX_POWER=20
build_src_filter = ${esp32_base.build_src_filter}
  +<helpers/ui/SSD1306Display.cpp>
  +<../variants/lilygo_tlora_v2_1>
  +<helpers/sensors>
lib_deps =
  ${esp32_base.lib_deps}
  adafruit/Adafruit SSD1306 @ ^2.5.13

[LilyGo_TLora_V2_1_1_6]
extends = LilyGo_TLora_V2_1_1_6_core
build_flags =
  ${LilyGo_TLora_V2_1_1_6_core.build_flags}
  ${sensor_base.build_flags}
  -UENV_INCLUDE_GPS
; Without this exclusion, standard builds compile MQTTMessageBuilder.cpp but lack Timezone (MQTT observer envs extend _core and still pick it up via helpers/*.cpp).
build_src_filter = ${LilyGo_TLora_V2_1_1_6_core.build_src_filter}
  -<helpers/MQTTMessageBuilder.cpp>
lib_deps =
  ${LilyGo_TLora_V2_1_1_6_core.lib_deps}
  ${sensor_base.lib_deps}

; === LILYGO T-LoRa V2.1-1.6 with SX1276 environments ===
[env:LilyGo_TLora_V2_1_1_6_repeater]
extends = LilyGo_TLora_V2_1_1_6
build_src_filter = ${LilyGo_TLora_V2_1_1_6.build_src_filter}
  +<../examples/simple_repeater>
build_flags =
  ${LilyGo_TLora_V2_1_1_6.build_flags}
  -D ADVERT_NAME='"TLora-V2.1-1.6 Repeater"'
  -D ADVERT_LAT=0.0
  -D ADVERT_LON=0.0
  -D ADMIN_PASSWORD='"password"'
  -D MAX_NEIGHBOURS=50
;  -D MESH_PACKET_LOGGING=1
;  -D MESH_DEBUG=1
;  -D CORE_DEBUG_LEVEL=3
lib_deps =
  ${LilyGo_TLora_V2_1_1_6.lib_deps}
  ${esp32_ota.lib_deps}

[env:LilyGo_TLora_V2_1_1_6_terminal_chat]
extends = LilyGo_TLora_V2_1_1_6
build_flags =
  ${LilyGo_TLora_V2_1_1_6.build_flags}
  -D MAX_CONTACTS=160
  -D MAX_GROUP_CHANNELS=8
;  -D MESH_PACKET_LOGGING=1
;  -D MESH_DEBUG=1
build_src_filter = ${LilyGo_TLora_V2_1_1_6.build_src_filter}
  +<../examples/simple_secure_chat/main.cpp>
lib_deps =
  ${LilyGo_TLora_V2_1_1_6.lib_deps}
  densaugeo/base64 @ ~1.4.0

[env:LilyGo_TLora_V2_1_1_6_companion_radio_usb]
extends = LilyGo_TLora_V2_1_1_6
build_flags =
  ${LilyGo_TLora_V2_1_1_6.build_flags}
  -I examples/companion_radio/ui-new
  -D MAX_CONTACTS=160
  -D MAX_GROUP_CHANNELS=8
  -D ENABLE_USB_INTERFACE
; NOTE: DO NOT ENABLE -->  -D MESH_PACKET_LOGGING=1
; NOTE: DO NOT ENABLE -->  -D MESH_DEBUG=1
build_src_filter = ${LilyGo_TLora_V2_1_1_6.build_src_filter}
  +<helpers/ui/MomentaryButton.cpp>
  +<../examples/companion_radio/*.cpp>
  +<../examples/companion_radio/ui-new/*.cpp>
lib_deps =
  ${LilyGo_TLora_V2_1_1_6.lib_deps}
  densaugeo/base64 @ ~1.4.0

[env:LilyGo_TLora_V2_1_1_6_companion_radio_ble]
extends = LilyGo_TLora_V2_1_1_6
build_flags =
  ${LilyGo_TLora_V2_1_1_6.build_flags}
  -I examples/companion_radio/ui-new
  -D MAX_CONTACTS=160
  -D MAX_GROUP_CHANNELS=8
  -D BLE_PIN_CODE=123456
  -D OFFLINE_QUEUE_SIZE=128
;  -D BLE_DEBUG_LOGGING=1
;  -D MESH_PACKET_LOGGING=1
;  -D MESH_DEBUG=1
build_src_filter = ${LilyGo_TLora_V2_1_1_6.build_src_filter}
  +<helpers/esp32/*.cpp>
  +<helpers/ui/MomentaryButton.cpp>
  +<../examples/companion_radio/*.cpp>
  +<../examples/companion_radio/ui-new/*.cpp>
lib_deps =
  ${LilyGo_TLora_V2_1_1_6.lib_deps}
  densaugeo/base64 @ ~1.4.0

[env:LilyGo_TLora_V2_1_1_6_room_server]
extends = LilyGo_TLora_V2_1_1_6
build_src_filter = ${LilyGo_TLora_V2_1_1_6.build_src_filter}
  +<helpers/ui/SSD1306Display.cpp>
  +<../examples/simple_room_server>
build_flags =
  ${LilyGo_TLora_V2_1_1_6.build_flags}
  -D ADVERT_NAME='"TLora-V2.1-1.6 Room"'
  -D ADVERT_LAT=0.0
  -D ADVERT_LON=0.0
  -D ADMIN_PASSWORD='"password"'
  -D ROOM_PASSWORD='"hello"'
;  -D MESH_PACKET_LOGGING=1
;  -D MESH_DEBUG=1
lib_deps =
  ${LilyGo_TLora_V2_1_1_6.lib_deps}
  ${esp32_ota.lib_deps}

; LilyGo T-LoRa V2.1-1.6 MQTT observer targets (ttgo-lora32-v1, classic ESP32, no PSRAM):
; Use ONE active WSS broker preset at a time. Two concurrent TLS sessions usually exhaust
; contiguous internal heap; the second slot fails (mbedtls_ssl_setup / esp-tls 0x8017).
; Set extra slots to "none" (e.g. set mqtt2.preset none). See MQTT_IMPLEMENTATION.md.
; TEMPORARILY EXCLUDED FROM AUTOMATIC BUILDS (trailing underscore, same
; convention as the nibble_screen_connect envs). This board does not fit:
;   flex baseline (no webconfig, no upstream merge): 2,069,397 / 1,966,080 = 105.3%
; It has been silently failing on production for some time — the observer release
; ships 30 envs, not 32, and build.sh swallowing pio's exit code hid it. Dropping
; webconfig would recover only ~46 KB of a ~101 KB (flex) / ~242 KB (dev) deficit,
; so a NO_WEBCONFIG flag cannot rescue it. ESP32 (not S3), 4 MB flash, already on
; min_spiffs.csv. Real options: single-app partition (fits, but loses OTA), or
; trim ~250 KB for this board. See .scratch/tlora-v2-oversize.md.
[env:LilyGo_TLora_V2_1_1_6_repeater_observer_mqtt_]
extends = LilyGo_TLora_V2_1_1_6_core
extra_scripts =
  ${esp32_base.extra_scripts}
  pre:scripts/generate_cert_bundle.py
board_ssl_cert_source = adafruit-full
board_build.embed_files = src/certs/x509_crt_bundle.bin
build_flags =
  ${LilyGo_TLora_V2_1_1_6_core.build_flags}
  -D ADVERT_NAME='"TLora-V2.1-1.6 MQTT Observer"'
  -D ADVERT_LAT=0.0
  -D ADVERT_LON=0.0
  -D ADMIN_PASSWORD='"password"'
  -D MAX_NEIGHBOURS=50
  -D WITH_MQTT_BRIDGE=1
  -D MQTT_MAX_PACKET_SIZE=1024
  -D MQTT_TASK_STACK_SIZE=16384
  -D ESP32_CPU_FREQ=240
;  -D MQTT_DEBUG=1
;  -D MESH_PACKET_LOGGING=1
;  -D MESH_DEBUG=1
  -D CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=y
#  -D WIFI_SSID='"ssid"'
#  -D WIFI_PWD='"password"'
#  -D MQTT_SERVER='"your-mqtt-broker.com"'
#  -D MQTT_PORT=1883
#  -D MQTT_USERNAME='"your-username"'
#  -D MQTT_PASSWORD='"your-password"'
build_src_filter = ${LilyGo_TLora_V2_1_1_6_core.build_src_filter}
  +<helpers/bridges/MQTTBridge.cpp>
  +<helpers/MQTTMessageBuilder.cpp>
  +<helpers/JWTHelper.cpp>
  +<helpers/ui/SSD1306Display.cpp>
  +<../examples/simple_repeater>
lib_deps =
  ${LilyGo_TLora_V2_1_1_6_core.lib_deps}
  ${esp32_ota.lib_deps}
  elims/PsychicMqttClient@^0.2.4
  bblanchon/ArduinoJson @ 7.4.3
  arduino-libraries/NTPClient
  JChristensen/Timezone
  paulstoffregen/Time@1.6.1

; Same single-broker guidance as LilyGo_TLora_V2_1_1_6_repeater_observer_mqtt (see comment block above).
; TEMPORARILY EXCLUDED — see the repeater env above.
[env:LilyGo_TLora_V2_1_1_6_room_server_observer_mqtt_]
extends = LilyGo_TLora_V2_1_1_6_core
extra_scripts =
  ${esp32_base.extra_scripts}
  pre:scripts/generate_cert_bundle.py
board_ssl_cert_source = adafruit-full
board_build.embed_files = src/certs/x509_crt_bundle.bin
build_flags =
  ${LilyGo_TLora_V2_1_1_6_core.build_flags}
  -D ADVERT_NAME='"TLora-V2.1-1.6 Room Observer"'
  -D ADVERT_LAT=0.0
  -D ADVERT_LON=0.0
  -D ADMIN_PASSWORD='"password"'
  -D ROOM_PASSWORD='"hello"'
  -D WITH_MQTT_BRIDGE=1
  -D MQTT_MAX_PACKET_SIZE=1024
  -D MQTT_TASK_STACK_SIZE=16384
  -D ESP32_CPU_FREQ=240
; MQTT_DEBUG must stay off here: with it on, this min_spiffs build overflows the
; app partition by ~357 bytes and produces no binary (the repeater observer env
; keeps it commented for the same reason).
;  -D MQTT_DEBUG=1
;  -D MESH_PACKET_LOGGING=1
;  -D MESH_DEBUG=1
  -D CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=y
build_src_filter = ${LilyGo_TLora_V2_1_1_6_core.build_src_filter}
  +<helpers/ui/SSD1306Display.cpp>
  +<helpers/bridges/MQTTBridge.cpp>
  +<helpers/MQTTMessageBuilder.cpp>
  +<helpers/JWTHelper.cpp>
  +<../examples/simple_room_server>
lib_deps =
  ${LilyGo_TLora_V2_1_1_6_core.lib_deps}
  ${esp32_ota.lib_deps}
  elims/PsychicMqttClient@^0.2.4
  bblanchon/ArduinoJson @ 7.4.3
  arduino-libraries/NTPClient
  JChristensen/Timezone
  paulstoffregen/Time@1.6.1

[env:LilyGo_TLora_V2_1_1_6_companion_radio_wifi]
extends = LilyGo_TLora_V2_1_1_6
build_flags =
  ${LilyGo_TLora_V2_1_1_6.build_flags}
  -I examples/companion_radio/ui-new
  -D MAX_CONTACTS=160
  -D MAX_GROUP_CHANNELS=8
  -D WIFI_SSID='"ssid"'
  -D WIFI_PWD='"password"'
  -D WIFI_DEBUG_LOGGING=1
  -D OFFLINE_QUEUE_SIZE=128
build_src_filter = ${LilyGo_TLora_V2_1_1_6.build_src_filter}
  +<helpers/esp32/*.cpp>
  +<helpers/ui/MomentaryButton.cpp>
  +<../examples/companion_radio/*.cpp>
  +<../examples/companion_radio/ui-new/*.cpp>
lib_deps =
  ${LilyGo_TLora_V2_1_1_6.lib_deps}
  densaugeo/base64 @ ~1.4.0

;
; Repeater Bridges
;
[env:LilyGo_TLora_V2_1_1_6_repeater_bridge_rs232]
extends = LilyGo_TLora_V2_1_1_6
build_src_filter = ${LilyGo_TLora_V2_1_1_6.build_src_filter}
  +<helpers/bridges/RS232Bridge.cpp>
  +<../examples/simple_repeater>
build_flags =
  ${LilyGo_TLora_V2_1_1_6.build_flags}
  -D ADVERT_NAME='"RS232 Bridge"'
  -D ADVERT_LAT=0.0
  -D ADVERT_LON=0.0
  -D ADMIN_PASSWORD='"password"'
  -D MAX_NEIGHBOURS=50
  -D WITH_RS232_BRIDGE=Serial2
  -D WITH_RS232_BRIDGE_RX=34
  -D WITH_RS232_BRIDGE_TX=25
;  -D BRIDGE_DEBUG=1
;  -D MESH_PACKET_LOGGING=1
;  -D MESH_DEBUG=1
;  -D CORE_DEBUG_LEVEL=3
lib_deps =
  ${LilyGo_TLora_V2_1_1_6.lib_deps}
  ${esp32_ota.lib_deps}

[env:LilyGo_TLora_V2_1_1_6_repeater_bridge_espnow]
extends = LilyGo_TLora_V2_1_1_6
build_src_filter = ${LilyGo_TLora_V2_1_1_6.build_src_filter}
  +<helpers/bridges/ESPNowBridge.cpp>
  +<../examples/simple_repeater>
build_flags =
  ${LilyGo_TLora_V2_1_1_6.build_flags}
  -D ADVERT_NAME='"ESPNow Bridge"'
  -D ADVERT_LAT=0.0
  -D ADVERT_LON=0.0
  -D ADMIN_PASSWORD='"password"'
  -D MAX_NEIGHBOURS=50
  -D WITH_ESPNOW_BRIDGE=1
;  -D BRIDGE_DEBUG=1
;  -D MESH_PACKET_LOGGING=1
;  -D MESH_DEBUG=1
;  -D CORE_DEBUG_LEVEL=3
lib_deps =
  ${LilyGo_TLora_V2_1_1_6.lib_deps}
  ${esp32_ota.lib_deps}

[env:LilyGo_TLora_V2_1_1_6_kiss_modem]
extends = LilyGo_TLora_V2_1_1_6
build_src_filter = ${LilyGo_TLora_V2_1_1_6.build_src_filter}
  +<../examples/kiss_modem/>
