; =============================================================================
; T-Deck Pro MAX V0.1 — Meck Build Environments
;
; Hardware: ESP32-S3 + XL9555 I/O expander + combined 4G (A7682E) + Audio (ES8311)
;
; Key differences from LilyGo_TDeck_Pro (V1.1):
;   - Peripheral power controlled via XL9555 (not direct GPIO)
;   - 4G modem and ES8311 audio coexist (no longer mutually exclusive)
;   - ES8311 I2C codec replaces PCM5102A (different I2S pins, needs I2C config)
;   - Several GPIO reassignments (see variant.h for full map)
;   - 1500 mAh battery (was 1400)
;   - Working e-ink front-light on IO41
;
; WHAT WORKS OUT OF THE BOX:
;   LoRa mesh, keyboard, e-ink display, GPS, touchscreen, battery management,
;   SD card, text reader, notes, contacts, channels, settings, discovery,
;   last heard, repeater admin, web reader (WiFi builds), OTA update.
;
; NEEDS ADAPTATION (future work):
;   - HAS_4G_MODEM: ModemManager uses direct GPIO for MODEM_POWER_EN/PWRKEY
;     which are XL9555-routed on MAX. Needs board.modemPowerOn() etc.
;   - MECK_AUDIO_VARIANT: ES8311 needs I2C codec init (PCM5102A didn't).
;     I2S pins are different. AudiobookPlayerScreen needs ES8311 driver.
;   - Combined 4G+audio: existing #ifdef guards treat them as mutually
;     exclusive. Needs restructuring for coexistence.
; =============================================================================

; ---------------------------------------------------------------------------
; Base environment for T-Deck Pro MAX
; ---------------------------------------------------------------------------
[LilyGo_TDeck_Pro_Max]
extends = esp32_base
extra_scripts = post:merge_firmware.py
board = t-deck_pro_max
board_build.flash_mode = qio
board_build.f_flash = 80000000L
board_build.arduino.memory_type = qio_qspi
board_upload.flash_size = 16MB
build_flags =
  ${esp32_base.build_flags}
  ${sensor_base.build_flags}
  ; Include MAX variant first (for variant.h, target.h, TDeckProMaxBoard.h)
  ; then V1.1 variant (for TDeckBoard.h, which TDeckProMaxBoard inherits from)
  -I variants/LilyGo_TDeck_Pro_Max
  -I variants/LilyGo_TDeck_Pro
  ; Both defines needed: LilyGo_TDeck_Pro for existing UI code guards,
  ; LilyGo_TDeck_Pro_Max for MAX-specific code paths
  -D LilyGo_TDeck_Pro
  -D LilyGo_TDeck_Pro_Max
  -D HAS_XL9555=1
  -D HAS_GPS=1
  -D BOARD_HAS_PSRAM=1
  -D CORE_DEBUG_LEVEL=1
  -D FORMAT_SPIFFS_IF_FAILED=1
  -D FORMAT_LITTLEFS_IF_FAILED=1
  -D ARDUINO_USB_CDC_ON_BOOT=1
  -D RADIO_CLASS=CustomSX1262
  -D WRAPPER_CLASS=CustomSX1262Wrapper
  -D LORA_TX_POWER=22
  -D SX126X_DIO2_AS_RF_SWITCH
  -D SX126X_CURRENT_LIMIT=140
  -D SX126X_RX_BOOSTED_GAIN=1
  -D SX126X_DIO3_TCXO_VOLTAGE=2.4f
  ; LoRa SPI pins (direct GPIO — unchanged from V1.1)
  -D P_LORA_DIO_1=5
  -D P_LORA_NSS=3
  -D P_LORA_RESET=4
  -D P_LORA_BUSY=6
  -D P_LORA_SCLK=36
  -D P_LORA_MISO=47
  -D P_LORA_MOSI=33
  ; P_LORA_EN deliberately NOT defined — LoRa power via XL9555 in board.begin()
  ; GPS pins (direct GPIO — changed from V1.1!)
  -D ENV_INCLUDE_GPS=1
  -D ENV_SKIP_GPS_DETECT=1
  -D PIN_GPS_RX=2
  -D PIN_GPS_TX=16
  -D GPS_BAUD_RATE=38400
  ; Sensor exclusions (same as V1.1)
  -D ENV_INCLUDE_AHTX0=0
  -D ENV_INCLUDE_BME280=0
  -D ENV_INCLUDE_BMP280=0
  -D ENV_INCLUDE_SHTC3=0
  -D ENV_INCLUDE_SHT4X=0
  -D ENV_INCLUDE_LPS22HB=0
  -D ENV_INCLUDE_INA3221=0
  -D ENV_INCLUDE_INA219=0
  -D ENV_INCLUDE_INA226=0
  -D ENV_INCLUDE_INA260=0
  -D ENV_INCLUDE_MLX90614=0
  -D ENV_INCLUDE_VL53L0X=0
  -D ENV_INCLUDE_BME680=0
  -D ENV_INCLUDE_BMP085=0
  ; E-ink display (pin changes from V1.1: RST=9, BL=41)
  -D USE_EINK
  -D DISPLAY_CLASS=GxEPDDisplay
  -D EINK_DISPLAY_MODEL=GxEPD2_310_GDEQ031T10
  -D EINK_WIDTH=240
  -D EINK_HEIGHT=320
  -D EINK_CS=34
  -D EINK_DC=35
  -D EINK_RST=9
  -D EINK_BUSY=37
  -D EINK_SCLK=36
  -D EINK_MOSI=33
  -D EINK_BL=41
  -D EINK_NOT_HIBERNATE=1
  ; Battery (1500 mAh on MAX, was 1400 on V1.1)
  -D HAS_BQ27220=1
  -D AUTO_SHUTDOWN_MILLIVOLTS=2800
  ; Display rendering parameters
  -D EINK_LIMIT_FASTREFRESH=10
  -D EINK_LIMIT_GHOSTING_PX=2000
  -D DISPLAY_ROTATION=0
  -D EINK_ROTATION=0
  -D EINK_SCALE_X=1.875f
  -D EINK_SCALE_Y=2.5f
  -D EINK_X_OFFSET=0
  -D EINK_Y_OFFSET=5
  ; Legacy display pin aliases (for GxEPDDisplay.cpp)
  -D PIN_DISPLAY_CS=34
  -D PIN_DISPLAY_DC=35
  -D PIN_DISPLAY_RST=9
  -D PIN_DISPLAY_BUSY=37
  -D PIN_DISPLAY_SCLK=36
  -D PIN_DISPLAY_MISO=-1
  -D PIN_DISPLAY_MOSI=33
  -D PIN_DISPLAY_BL=41
  -D PIN_USER_BTN=0
  ; Touch (INT is direct GPIO; RST is XL9555, handled by board class)
  -D HAS_TOUCHSCREEN=1
  -D CST328_PIN_INT=12
  -D CST328_PIN_RST=-1
  -D ARDUINO_LOOP_STACK_SIZE=32768
  -D HAS_MECK_FONTS
build_src_filter = ${esp32_base.build_src_filter}
  ; Include TDeckBoard.cpp from V1.1 (parent class with BQ27220 code)
  +<../variants/LilyGo_TDeck_Pro/TDeckBoard.cpp>
  ; Include MAX variant (target.cpp + TDeckProMaxBoard.cpp)
  +<../variants/LilyGo_TDeck_Pro_Max>
  +<helpers/sensors/*.cpp>
lib_deps =
  ${esp32_base.lib_deps}
  ${sensor_base.lib_deps}
  zinggjm/GxEPD2@^1.5.9
  adafruit/Adafruit GFX Library@^1.11.0
  bitbank2/PNGdec@^1.0.1
  WebServer
  Update


; ===========================================================================
; Meck MAX builds — LoRa mesh works out of the box on all variants.
; 4G modem and ES8311 audio need adaptation before they can be enabled.
; ===========================================================================

; MAX + BLE companion (standard BLE phone bridging)
; Both 4G + audio hardware present but not yet enabled in firmware.
; BLE_PIN_CODE limit: MAX_CONTACTS=500 (BLE protocol ceiling).
[env:meck_max_ble]
extends = LilyGo_TDeck_Pro_Max
build_flags =
  ${LilyGo_TDeck_Pro_Max.build_flags}
  -I examples/companion_radio/ui-new
  -D MAX_CONTACTS=500
  -D MAX_GROUP_CHANNELS=20
  -D BLE_PIN_CODE=123456
  -D OFFLINE_QUEUE_SIZE=256
  -D MECK_WEB_READER=1
  -D MECK_OTA_UPDATE=1
  -D FIRMWARE_VERSION='"Meck v1.3.MAX"'
build_src_filter = ${LilyGo_TDeck_Pro_Max.build_src_filter}
  +<helpers/esp32/*.cpp>
  +<helpers/ui/MomentaryButton.cpp>
  +<../examples/companion_radio/*.cpp>
  +<../examples/companion_radio/ui-new/*.cpp>
  +<helpers/ui/GxEPDDisplay.cpp>
lib_deps =
  ${LilyGo_TDeck_Pro_Max.lib_deps}
  densaugeo/base64 @ ~1.4.0
  https://github.com/schreibfaul1/ESP32-audioI2S.git#2.0.6
  bitbank2/JPEGDEC

; MAX + WiFi companion (WiFi app bridging — no BLE, higher contact limit)
; WiFi credentials loaded from SD card (/web/wifi.cfg).
; Connect via MeshCore web app, meshcore.js, or Python CLI.
[env:meck_max_wifi]
extends = LilyGo_TDeck_Pro_Max
build_flags =
  ${LilyGo_TDeck_Pro_Max.build_flags}
  -I examples/companion_radio/ui-new
  -D MAX_CONTACTS=1500
  -D MAX_GROUP_CHANNELS=20
  -D MECK_WIFI_COMPANION=1
  -D TCP_PORT=5000
  -D WIFI_DEBUG_LOGGING=1
  -D OFFLINE_QUEUE_SIZE=256
  -D MECK_WEB_READER=1
  -D MECK_OTA_UPDATE=1
  -D FIRMWARE_VERSION='"Meck v1.3.MAX.WiFi"'
build_src_filter = ${LilyGo_TDeck_Pro_Max.build_src_filter}
  +<helpers/esp32/*.cpp>
  +<helpers/ui/MomentaryButton.cpp>
  +<../examples/companion_radio/*.cpp>
  +<../examples/companion_radio/ui-new/*.cpp>
  +<helpers/ui/GxEPDDisplay.cpp>
lib_deps =
  ${LilyGo_TDeck_Pro_Max.lib_deps}
  densaugeo/base64 @ ~1.4.0
  https://github.com/schreibfaul1/ESP32-audioI2S.git#2.0.6
  bitbank2/JPEGDEC

; MAX standalone (no BLE/WiFi — maximum battery life, LoRa mesh only)
; Contacts in PSRAM (1500 capacity). OTA enabled (WiFi AP on demand).
[env:meck_max_standalone]
extends = LilyGo_TDeck_Pro_Max
build_flags =
  ${LilyGo_TDeck_Pro_Max.build_flags}
  -I examples/companion_radio/ui-new
  -D MAX_CONTACTS=1500
  -D MAX_GROUP_CHANNELS=20
  -D OFFLINE_QUEUE_SIZE=1
  -D MECK_OTA_UPDATE=1
  -D FIRMWARE_VERSION='"Meck v1.3.MAX.SA"'
build_src_filter = ${LilyGo_TDeck_Pro_Max.build_src_filter}
  +<helpers/esp32/*.cpp>
  +<helpers/ui/MomentaryButton.cpp>
  +<../examples/companion_radio/*.cpp>
  +<../examples/companion_radio/ui-new/*.cpp>
  +<helpers/ui/GxEPDDisplay.cpp>
lib_deps =
  ${LilyGo_TDeck_Pro_Max.lib_deps}
  densaugeo/base64 @ ~1.4.0
