# ESP32-2432S028R Migration Summary ## Overview Successfully migrated the DCC-Bench project from WiFi/WebServer control to touchscreen-based control using the ESP32-2432S028R module (ESP32 with ILI9341 TFT touchscreen). ## Hardware Configuration ### ESP32-2432S028R Module - **Board**: ESP32-WROOM-32 - **Display**: ILI9341 TFT (320x240 pixels) - **Touch**: XPT2046 resistive touchscreen - **Pins Used**: - TFT MISO: GPIO 12 - TFT MOSI: GPIO 13 - TFT SCLK: GPIO 14 - TFT CS: GPIO 15 - TFT DC: GPIO 2 - TFT BL (Backlight): GPIO 21 - Touch CS: GPIO 22 - Relay Control: GPIO 4 - PWM/DCC_A: GPIO 18 (dual purpose) - DIR/DCC_B: GPIO 19 (dual purpose) - Motor BRAKE: GPIO 23 ### LM18200 H-Bridge Driver (Dual Purpose) The LM18200 serves as **BOTH** the DC motor controller AND DCC signal booster: - **DC Analog Mode**: GPIO 18 sends PWM for speed, GPIO 19 sets direction - **DCC Digital Mode**: GPIO 18 sends DCC signal A, GPIO 19 sends DCC signal B (inverted) - Same hardware, different signals depending on mode selected - LM18200 amplifies the 3.3V logic signals to track voltage (12-18V) ## Key Changes ### 1. PlatformIO Configuration (`platformio.ini`) - **Changed**: Board target from `esp32doit-devkit-v1` to `esp32dev` for ESP32-2432S028R - **Removed**: WiFi/WebServer libraries (ESPAsyncWebServer, AsyncTCP) - **Added**: - `bodmer/TFT_eSPI@^2.5.43` - Display driver - `paulstoffregen/XPT2046_Touchscreen@^1.4` - Touch controller - **Added**: TFT_eSPI build flags for ILI9341 configuration ### 2. New Components #### RelayController (`RelayController.h/cpp`) - Controls relay on GPIO 27 for 2-rail/3-rail track switching - Simple HIGH/LOW control - State tracking and persistence through Config #### TouchscreenUI (`TouchscreenUI.h/cpp`) - Full graphical user interface with touch controls - **Features**: - Power ON/OFF button (green/red indicator) - DCC/Analog mode toggle button (cyan/yellow) - 2-Rail/3-Rail selector button - Direction control (FWD/REV) - Horizontal speed slider (0-100%) - Status bar showing all current settings - **Behavior**: - Switching from DCC to Analog (or vice versa) automatically powers off the system - All settings are saved to NVS (persistent storage) - Touch events mapped to screen coordinates with calibration ### 3. Modified Components #### Config (`Config.h/cpp`) - **Removed**: All WiFi-related configuration (`WiFiConfig` struct) - **Added to SystemConfig**: - `bool is3Rail` - Track configuration (2-rail/3-rail) - `bool powerOn` - Power state tracking - **Updated**: Save/load methods to persist new settings #### Main (`main.cpp`) - **Removed**: WiFi, WebServer, LEDIndicator components - **Added**: TouchscreenUI, RelayController - **Updated**: Setup sequence and main loop - **Simplified**: Loop now only handles UI updates and motor/DCC control based on power state ### 4. Removed Files - `include/WiFiManager.h` - `src/WiFiManager.cpp` - `include/WebServer.h` - `src/WebServer.cpp` - `include/LEDIndicator.h` (was already commented out) ## User Interface Layout ``` ┌─────────────────────────────────────────────────┐ │ [POWER] [MODE ] [RAILS] [DIR ] │ │ ON/OFF DCC/DC 2/3Rail FWD/REV │ │ │ │ Speed: 45% │ │ │ │ ╔════════════════○═════════════╗ │ │ ║ ║ Speed Slider│ │ ╚═══════════════════════════════╝ │ │ │ │ ┌─────────────────────────────────────────────┐ │ │ │ PWR:ON | Mode:DCC | 3-Rail | Addr:3 │ │ │ │ Speed:45% FWD │ │ │ └─────────────────────────────────────────────┘ │ └─────────────────────────────────────────────────┘ ``` ## Features Implemented ### ✅ Power Control - Power ON/OFF button - **Safety**: Power automatically turns OFF when switching between DCC and Analog modes - Power state persisted in configuration ### ✅ Mode Switching - Toggle between DCC and DC Analog control - Visual indication (Cyan for DCC, Yellow for Analog) - Automatic power-off on mode change prevents unsafe transitions ### ✅ Rail Configuration - 2-Rail / 3-Rail selector - Physical relay control on GPIO 27 - Energized = 3-Rail, De-energized = 2-Rail ### ✅ Speed Control - Interactive horizontal slider - Range: 0-100% - Real-time speed updates to motor/DCC controller - Visual feedback with active/inactive portions ### ✅ Direction Control - Forward/Reverse toggle - Updates motor or DCC direction based on current mode ### ✅ Persistent Storage - All settings saved to ESP32 NVS (Non-Volatile Storage) - Settings persist across power cycles - Automatic save on every change ## Building and Uploading ```bash # Install dependencies and build pio run # Upload to ESP32-2432S028R pio run --target upload # Monitor serial output pio device monitor ``` ## Next Steps / Future Enhancements 1. **DCC Address Entry**: Add touchscreen numeric keypad for changing DCC address 2. **Function Buttons**: Add DCC function controls (F0-F12) with toggle buttons 3. **Speed Presets**: Add quick-access speed buttons (25%, 50%, 75%, 100%) 4. **Track Current Monitoring**: Display track current if current sensor is added 5. **Emergency Stop**: Large red emergency stop button 6. **Locomotive Profiles**: Save/load different locomotive configurations ## Testing Checklist - [ ] Display initializes correctly - [ ] Touch calibration is accurate - [ ] Power button toggles ON/OFF - [ ] Mode switch changes DCC/Analog - [ ] Mode switch automatically powers off - [ ] Rail selector controls relay - [ ] Speed slider adjusts output - [ ] Direction button changes FWD/REV - [ ] Settings persist after reboot - [ ] DCC signals generated correctly (when powered on) - [ ] DC motor control works (when powered on) - [ ] Relay switches correctly ## Pin Reference | Function | GPIO | Notes | |----------|------|-------| | PWM/DCC_A | 18 | DC: 20kHz PWM / DCC: Signal A | | DIR/DCC_B | 19 | DC: Direction / DCC: Signal B | | Motor Brake | 23 | Active LOW brake | | Relay Control | 4 | HIGH=3-Rail, LOW=2-Rail | | TFT MISO | 12 | SPI data in | | TFT MOSI | 13 | SPI data out | | TFT SCLK | 14 | SPI clock | | TFT CS | 15 | Chip select | | TFT DC | 2 | Data/Command | | TFT Backlight | 21 | Backlight control | | Touch CS | 22 | Touch chip select | ## Notes - Motor PWM frequency: 20kHz (silent operation) - Display orientation: Landscape (320x240) - Touch type: Resistive (XPT2046) - All configuration stored in NVS partition - Pin assignments avoid conflicts with ESP32-2432S028R built-in peripherals