/*
 * pins.h  —  MaTouch AI ESP32-S3 2.8" TFT ST7789V  (Makerfabs)
 *
 * Every pin below was read directly off schematic V1.1, not guessed and not
 * copied from the vendor examples. Where the vendor examples disagree with the
 * schematic, the disagreement is called out in a comment.
 *
 * Robojax.com  —  https://robojax.com/RTJ852
 *
 * ---------------------------------------------------------------------------
 * BOARD SETTINGS (Arduino IDE 2.3.4)
 *   Board            : ESP32S3 Dev Module
 *   ESP32 core       : 2.0.17     <-- NOT 3.x. esp-dl face detection is gone in 3.x.
 *   PSRAM            : OPI PSRAM  <-- nothing works without this
 *   Flash Size       : 16MB (128Mb)
 *   Partition Scheme : 16M Flash (3MB APP/9.9MB FATFS)
 *   USB CDC On Boot  : Disabled   <-- see the IO19/IO20 warning below
 *   Upload port      : the CH340K USB-C port (the one nearer the RESET button)
 * ---------------------------------------------------------------------------
 */

#pragma once

/* ===========================================================================
 * RGB565 COLOUR NAMES
 * Different versions of "GFX Library for Arduino" disagree about these: older
 * releases define GREEN/RED/etc directly, newer ones only define RGB565_GREEN
 * and friends. The guards below make the sketches compile on ANY version -
 * if the library already provides a name, ours steps aside.
 * =========================================================================== */
#ifndef BLACK
#define BLACK    0x0000
#endif
#ifndef WHITE
#define WHITE    0xFFFF
#endif
#ifndef RED
#define RED      0xF800
#endif
#ifndef GREEN
#define GREEN    0x07E0
#endif
#ifndef BLUE
#define BLUE     0x001F
#endif
#ifndef CYAN
#define CYAN     0x07FF
#endif
#ifndef MAGENTA
#define MAGENTA  0xF81F
#endif
#ifndef YELLOW
#define YELLOW   0xFFE0
#endif

/* ===========================================================================
 * LCD  —  ST7789V, 320x240, SPI
 * The reset line is tied to the board RESET net, so there is no GPIO for it:
 * TFT_RES must be -1.  Backlight is switched by an S8050 transistor on IO45.
 * =========================================================================== */
#define TFT_CS      40    // schematic: IO40 / HNSS0
#define TFT_DC      21    // schematic: IO21 / LCD_RS
#define TFT_MOSI    13    // schematic: IO13 / HMOSI
#define TFT_MISO    12    // schematic: IO12 / HMISO
#define TFT_SCLK    48    // schematic: IO48 / SCLK
#define TFT_BLK     45    // backlight, active HIGH (IO45 is a strapping pin, 10K pulldown)
#define TFT_RES     -1    // no GPIO: LCD reset is on the board RESET net

/* ===========================================================================
 * TOUCH  —  GT911 capacitive, 5 point, on the shared I2C bus
 * WARNING: TOUCH_RST is IO18, which the schematic labels IO18/LCD_RST. Pulsing
 * the touch reset also resets the display. That is why TFT_RES is -1 and why
 * the init order below is fixed.
 * =========================================================================== */
#define TOUCH_SDA   39
#define TOUCH_SCL   38
#define TOUCH_INT   14
#define TOUCH_RST   18    // SHARED with LCD_RST

/* ===========================================================================
 * I2C  —  FOUR devices share IO38/IO39. Confirmed on schematic V1.1.
 * Run 01_Board_Check to see all four answer a bus scan.
 * =========================================================================== */
#define I2C_SDA     39
#define I2C_SCL     38

#define ADDR_GT911_A   0x5D   // GT911 touch (address depends on INT level at reset)
#define ADDR_GT911_B   0x14   // ...the other possible GT911 address
#define ADDR_OV3660    0x3C   // camera SCCB
#define ADDR_PCF8563   0x51   // RTC
#define ADDR_MAX17048  0x36   // battery fuel gauge - NOT in the published spec sheet

/* ===========================================================================
 * CAMERA  —  OV3660, 3MP (2048x1536 max)
 * PWDN and RESET have no GPIO: CSI_RST is tied to the board RESET net through
 * a 0R link, so the camera CANNOT be power-cycled in software. It comes up at
 * boot or not at all - if init fails, press the RESET button.
 * SCCB (SIOD/SIOC) is the same I2C bus as the touch panel and RTC.
 * =========================================================================== */
#define CAM_PIN_PWDN    -1
#define CAM_PIN_RESET   -1
#define CAM_PIN_XCLK     9    // CSI_MCLK
#define CAM_PIN_SIOD    39    // shared I2C SDA
#define CAM_PIN_SIOC    38    // shared I2C SCL
#define CAM_PIN_D7      46    // Y9
#define CAM_PIN_D6       3    // Y8
#define CAM_PIN_D5       8    // Y7
#define CAM_PIN_D4      16    // Y6
#define CAM_PIN_D3       6    // Y5
#define CAM_PIN_D2       4    // Y4
#define CAM_PIN_D1       5    // Y3
#define CAM_PIN_D0       7    // Y2
#define CAM_PIN_VSYNC   11
#define CAM_PIN_HREF    10    // CSI_HSYNC
#define CAM_PIN_PCLK    17

/* ===========================================================================
 * MICROPHONES  —  TWO INMP441 on ONE shared I2S bus (I2S port 0)
 * U9 has its L/R pin tied LOW  (R22 = 0R to GND)  -> LEFT  channel
 * U10 has its L/R pin tied HIGH (R24 = 0R to 3V3) -> RIGHT channel
 *
 * Every Makerfabs example configures I2S as LEFT-only and therefore throws one
 * of the two microphones away. Read both channels and average them.
 * =========================================================================== */
#define I2S_MIC_PORT    I2S_NUM_0
#define I2S_MIC_SCK     42    // bit clock
#define I2S_MIC_WS       2    // word select / LRCK
#define I2S_MIC_SD      41    // data in

/* ===========================================================================
 * SPEAKER  —  MAX98357A class-D amp (I2S port 1)
 *
 * *** READ THIS ***
 * IO19 and IO20 are the ESP32-S3's hardwired native-USB D-/D+ pins, and the
 * NATIVE USB-C connector reaches them through 33R resistors (R64/R65). The amp
 * sits on the same two pins through 0R links (R3/R4).
 *
 *   => Upload and power through the CH340K UART port, never the native USB port.
 *   => Set "USB CDC On Boot" to Disabled.
 *
 * The amp's SD_MODE pin sits on a 560K/100K divider = 0.5V, which selects the
 * (L+R)/2 mono downmix mode. Gain is fixed at 15dB. So just send mono.
 * =========================================================================== */
#define I2S_SPK_PORT    I2S_NUM_1
#define I2S_SPK_BCLK    20    // also native USB D+
#define I2S_SPK_LRC      1
#define I2S_SPK_DOUT    19    // also native USB D-

/* ===========================================================================
 * MICRO SD  —  SPI mode, sharing the LCD's pins
 * The schematic sheet is literally captioned "SPI MODE".
 *
 * NOTE: the vendor's Display_Touch_Camera.ino also defines
 *       PIN_SD_CMD 2 / PIN_SD_CLK 42 / PIN_SD_D0 41
 *       Those are stale copy-paste from the non-AI board. Pins 2/42/41 are the
 *       MICROPHONES. Do not use those defines. There is no SD/mic conflict.
 * =========================================================================== */
#define SD_CS       47    // schematic: IO47 / SPI_NCS2  -> card's CD/DAT3
#define SD_MOSI     13    // -> card CMD
#define SD_SCLK     48    // -> card CLK
#define SD_MISO     12    // <- card DAT0

/* ===========================================================================
 * RGB LED  —  one WS2812B
 * Data is IO0, level-shifted by a 2N7002, and the LED is powered from VPWR
 * (battery / USB rail), not 3V3 - so it is bright.
 *
 * IO0 does triple duty: BOOT button, CH340K auto-reset (RTS), and LED data.
 * Expect the LED to flicker during upload and reset. That is normal.
 * Do not hold the BOOT button while a sketch is driving the LED.
 * =========================================================================== */
#define RGB_LED_PIN  0
#define RGB_LED_NUM  1

/* ===========================================================================
 * RTC  —  PCF8563T, battery-backed from the VRTC rail (through D4/Q1),
 * so it keeps time across power cycles. Interrupt output lands on IO15.
 * =========================================================================== */
#define RTC_INT     15

/* ===========================================================================
 * MABEE EXPANSION CONNECTORS  —  a trap for the unwary
 * J1 = CSI_D0 (IO7) + CSI_D1 (IO5)
 * J3 = CSI_D2 (IO4) + CSI_D3 (IO6)
 * These are CAMERA DATA LINES. The Mabee ports cannot be used while the
 * camera is running, despite looking like general purpose I2C/Grove ports.
 * =========================================================================== */
#define MABEE_J1_A   7
#define MABEE_J1_B   5
#define MABEE_J3_A   4
#define MABEE_J3_B   6

/* ===========================================================================
 * PINS YOU CANNOT USE
 *   IO35, IO36, IO37 : wired to the octal PSRAM. Touching them crashes the board.
 *   IO43, IO44       : UART0 to the CH340K (upload / Serial monitor).
 *   IO0, IO3, IO45, IO46 : strapping pins. Safe as outputs at runtime, but do
 *                          not hold them at the wrong level during reset.
 * =========================================================================== */

/* ---------------------------------------------------------------------------
 * FIXED PERIPHERAL INIT ORDER
 * The touch reset and the LCD reset are the same wire, and the camera shares
 * the I2C bus. Bring things up in this order or the display and touch panel
 * will fight:
 *      1. backlight off
 *      2. SPI + SD
 *      3. gfx->begin()          (LCD)
 *      4. bbct.init()           (touch - this pulses the shared reset)
 *      5. Wire.begin()          (RTC + fuel gauge scan)
 *      6. esp_camera_init()     (camera last)
 *      7. backlight on
 * --------------------------------------------------------------------------- */

/* ---------------------------------------------------------------------------
 * TOUCH COORDINATE TRANSFORM (display rotation 1, i.e. 320x240 landscape)
 * The GT911 reports in the panel's native portrait frame. To get screen
 * coordinates, swap and flip - this is the transform the vendor example uses
 * and it is correct:
 *      screen_x = ti.y[0];
 *      screen_y = 240 - ti.x[0];
 * There is a ready-made helper for this in every sketch: getTouch(&x, &y)
 * --------------------------------------------------------------------------- */
