### Initialize XPT2046 Touchscreen with Bitbang SPI Source: https://github.com/ddxfish/xpt2046_bitbang_arduino_library/blob/main/README.md Defines the pins for MOSI, MISO, CLK, and CS, then includes the library and instantiates the touchscreen object. This setup is for manual bit-banging SPI communication. ```cpp #define MOSI_PIN 32 #define MISO_PIN 39 #define CLK_PIN 25 #define CS_PIN 33 #include XPT2046_Bitbang touchscreen(MOSI_PIN, MISO_PIN, CLK_PIN, CS_PIN); ``` -------------------------------- ### Perform Interactive Serial Calibration Source: https://context7.com/ddxfish/xpt2046_bitbang_arduino_library/llms.txt Guides the user through a two-point calibration process via the Serial monitor, capturing raw coordinates for the top-left and bottom-right corners. Call saveCalibration() afterwards to persist the data. ```cpp void setup() { Serial.begin(115200); SPIFFS.begin(true); touchscreen.begin(); // Force a fresh calibration regardless of existing data Serial.println("Starting manual calibration..."); touchscreen.calibrate(); // Serial output during calibration: // "Calibration starting..." // "Touch the top-left corner, hold it down until the next message..." // "Touch the bottom-right corner, hold it down until the next message..." // "Calibration done!" touchscreen.saveCalibration(); Serial.println("Calibration saved to SPIFFS."); } ``` -------------------------------- ### begin() Source: https://context7.com/ddxfish/xpt2046_bitbang_arduino_library/llms.txt Initializes the touchscreen. This method configures all SPI GPIO pins, mounts SPIFFS, and loads calibration data. If no calibration data is found or if RERUN_CALIBRATE is true, it initiates an interactive calibration routine. ```APIDOC ## begin() ### Description Initializes the touchscreen driver. This function configures the assigned GPIO pins for SPI communication, mounts the SPIFFS filesystem, and attempts to load existing calibration data. If calibration data is not found or if a re-calibration is forced, it automatically starts the interactive calibration process and saves the new calibration data. ### Method `void begin();` ### Parameters None ### Request Example ```cpp void setup() { Serial.begin(115200); // SPIFFS must be mounted before begin() if (!SPIFFS.begin(true)) { Serial.println("SPIFFS mount failed"); return; } // Initializes pins, loads calibration from SPIFFS, // or runs calibrate() + saveCalibration() if none found touchscreen.begin(); Serial.println("Touchscreen ready."); } ``` ``` -------------------------------- ### Initialize Touchscreen and Load Calibration Source: https://context7.com/ddxfish/xpt2046_bitbang_arduino_library/llms.txt Initializes SPIFFS, configures GPIO pins, and loads calibration data. If no calibration data is found, it automatically initiates the interactive calibration routine and saves the results. ```cpp void setup() { Serial.begin(115200); // SPIFFS must be mounted before begin() if (!SPIFFS.begin(true)) { Serial.println("SPIFFS mount failed"); return; } // Initializes pins, loads calibration from SPIFFS, // or runs calibrate() + saveCalibration() if none found touchscreen.begin(); Serial.println("Touchscreen ready."); } ``` -------------------------------- ### Instantiate XPT2046_Bitbang with Custom Pins Source: https://context7.com/ddxfish/xpt2046_bitbang_arduino_library/llms.txt Instantiates the touchscreen driver by assigning custom GPIO pins for SPI communication. Ensure to call begin() after instantiation. ```cpp #include // Pin definitions for ESP32-2432S028R (Cheap Yellow Display) #define MOSI_PIN 32 #define MISO_PIN 39 #define CLK_PIN 25 #define CS_PIN 33 XPT2046_Bitbang touchscreen(MOSI_PIN, MISO_PIN, CLK_PIN, CS_PIN); ``` -------------------------------- ### Load Calibration from SPIFFS Source: https://context7.com/ddxfish/xpt2046_bitbang_arduino_library/llms.txt Loads calibration values from '/calxpt2040.txt' on SPIFFS. Returns true on success, false if the file is not found or cannot be opened. `begin()` calls this internally, but it can be called manually. ```cpp void setup() { Serial.begin(115200); SPIFFS.begin(true); touchscreen.begin(); // begin() calls loadCalibration() internally, // but it can also be called manually: if (touchscreen.loadCalibration()) { Serial.println("Calibration loaded successfully."); } else { Serial.println("No calibration file found — running calibration."); touchscreen.calibrate(); touchscreen.saveCalibration(); } } ``` -------------------------------- ### Set Calibration Programmatically Source: https://context7.com/ddxfish/xpt2046_bitbang_arduino_library/llms.txt Directly sets calibration boundary values at runtime without using SPIFFS or interactive routines. Useful for hardcoding known-good values or skipping the interactive flow. `begin()` is still needed for pin initialization. ```cpp void setup() { Serial.begin(115200); SPIFFS.begin(true); // Hardcode calibration values determined from a prior calibration session // xMin, yMin = raw ADC at top-left; xMax, yMax = raw ADC at bottom-right touchscreen.setCalibration(245, 310, 3820, 3750); // begin() is still needed for pin initialization, but skip auto-calibration // by ensuring SPIFFS has a valid file or calling setCalibration before begin() touchscreen.begin(); } void loop() { Point p = touchscreen.getTouch(); if (p.x != 0 || p.y != 0) { Serial.printf("X=%d Y=%d\n", p.x, p.y); } delay(50); } ``` -------------------------------- ### setCalibration(xMin, yMin, xMax, yMax) Source: https://context7.com/ddxfish/xpt2046_bitbang_arduino_library/llms.txt Directly sets the four calibration boundary values at runtime without using SPIFFS or the interactive routine. Useful for hardcoding known-good calibration values. ```APIDOC ## setCalibration(xMin, yMin, xMax, yMax) ### Description Directly sets the four calibration boundary values at runtime without using SPIFFS or the interactive routine. Useful for hardcoding known-good calibration values for a specific hardware revision or skipping the interactive flow entirely. ### Parameters - **xMin** (int) - The minimum X-axis raw ADC value. - **yMin** (int) - The minimum Y-axis raw ADC value. - **xMax** (int) - The maximum X-axis raw ADC value. - **yMax** (int) - The maximum Y-axis raw ADC value. ### Usage Example ```cpp // Hardcode calibration values determined from a prior calibration session // xMin, yMin = raw ADC at top-left; xMax, yMax = raw ADC at bottom-right touchscreen.setCalibration(245, 310, 3820, 3750); // begin() is still needed for pin initialization, but skip auto-calibration // by ensuring SPIFFS has a valid file or calling setCalibration before begin() touchscreen.begin(); ``` ``` -------------------------------- ### XPT2046_Bitbang(mosiPin, misoPin, clkPin, csPin) Source: https://context7.com/ddxfish/xpt2046_bitbang_arduino_library/llms.txt Constructor for the XPT2046_Bitbang class. It instantiates the touchscreen driver by assigning the four SPI GPIO pins. Hardware initialization is deferred until the begin() method is called. ```APIDOC ## XPT2046_Bitbang(mosiPin, misoPin, clkPin, csPin) - Constructor ### Description Instantiates the touchscreen driver by assigning the four SPI GPIO pins. No hardware initialization occurs at this stage; call `begin()` before use. ### Parameters * **mosiPin** (int) - The GPIO pin for MOSI. * **misoPin** (int) - The GPIO pin for MISO. * **clkPin** (int) - The GPIO pin for CLK. * **csPin** (int) - The GPIO pin for CS. ### Request Example ```cpp #include // Pin definitions for ESP32-2432S028R (Cheap Yellow Display) #define MOSI_PIN 32 #define MISO_PIN 39 #define CLK_PIN 25 #define CS_PIN 33 XPT2046_Bitbang touchscreen(MOSI_PIN, MISO_PIN, CLK_PIN, CS_PIN); ``` ``` -------------------------------- ### calibrate() Source: https://context7.com/ddxfish/xpt2046_bitbang_arduino_library/llms.txt Initiates an interactive calibration routine via the Serial monitor. The user is prompted to touch and hold specific screen corners to capture calibration points. After completion, `saveCalibration()` should be called to persist the data. ```APIDOC ## calibrate() ### Description Starts an interactive touchscreen calibration process using the Serial Monitor. The user will be prompted to touch and hold the top-left and bottom-right corners of the screen sequentially. This function captures the raw touch data at these points to determine the calibration parameters. ### Method `void calibrate();` ### Parameters None ### Request Example ```cpp void setup() { Serial.begin(115200); SPIFFS.begin(true); touchscreen.begin(); // Force a fresh calibration regardless of existing data Serial.println("Starting manual calibration..."); touchscreen.calibrate(); // Serial output during calibration: // "Calibration starting..." // "Touch the top-left corner, hold it down until the next message..." // "Touch the bottom-right corner, hold it down until the next message..." // "Calibration done!" touchscreen.saveCalibration(); Serial.println("Calibration saved to SPIFFS."); } ``` ``` -------------------------------- ### loadCalibration() Source: https://context7.com/ddxfish/xpt2046_bitbang_arduino_library/llms.txt Loads calibration values from SPIFFS. Returns true on success, false if the file does not exist or cannot be opened. This is often called internally by `begin()` but can be invoked manually. ```APIDOC ## loadCalibration() ### Description Opens `/calxpt2040.txt` on SPIFFS and reads the four calibration values. Returns `true` on success and `false` if the file does not exist or cannot be opened. ### Usage Example ```cpp if (touchscreen.loadCalibration()) { Serial.println("Calibration loaded successfully."); } else { Serial.println("No calibration file found — running calibration."); touchscreen.calibrate(); touchscreen.saveCalibration(); } ``` ``` -------------------------------- ### saveCalibration() Source: https://context7.com/ddxfish/xpt2046_bitbang_arduino_library/llms.txt Persists the current calibration data (xMin, yMin, xMax, yMax) to a file named '/calxpt2040.txt' on the SPIFFS filesystem. If the file cannot be opened for writing, the operation fails silently. ```APIDOC ## saveCalibration() ### Description Saves the current touchscreen calibration parameters (xMin, yMin, xMax, yMax) to a file named `/calxpt2040.txt` on the SPIFFS filesystem. This allows the calibration settings to persist across device reboots. If the file cannot be accessed or written to, this function will not report an error but will simply not save the data. ### Method `void saveCalibration();` ### Parameters None ### Request Example ```cpp // After a successful calibrate() call, persist the data: touchscreen.saveCalibration(); // File written: /calxpt2040.txt // Contents (example): // 245 // 310 // 3820 // 3750 ``` ``` -------------------------------- ### Persist Calibration Data to SPIFFS Source: https://context7.com/ddxfish/xpt2046_bitbang_arduino_library/llms.txt Writes the captured calibration values (xMin, yMin, xMax, yMax) to the '/calxpt2040.txt' file on SPIFFS. The operation is silent if the file cannot be opened. ```cpp // After a successful calibrate() call, persist the data: touchscreen.saveCalibration(); // File written: /calxpt2040.txt // Contents (example): // 245 // 310 // 3820 // 3750 ``` -------------------------------- ### Read Touch Coordinates and Map to Screen Pixels Source: https://context7.com/ddxfish/xpt2046_bitbang_arduino_library/llms.txt Reads raw ADC values from the touchscreen, maps them to screen pixel coordinates using stored calibration data, and clamps the result to display bounds. Returns {0, 0} if no touch is detected. ```cpp void loop() { Point touch = touchscreen.getTouch(); // Filter out idle (0,0) readings if (touch.x != 0 || touch.y != 0) { Serial.print("Touch at X: "); Serial.print(touch.x); // 0 – TFT_WIDTH (default 0–240) Serial.print(", Y: "); Serial.println(touch.y); // 0 – TFT_HEIGHT (default 0–320) } delay(100); // Poll at ~10 Hz } ``` -------------------------------- ### Custom Display Resolution Source: https://context7.com/ddxfish/xpt2046_bitbang_arduino_library/llms.txt Define `TFT_WIDTH` and `TFT_HEIGHT` macros before including the header to match your display dimensions. This maps raw ADC readings to the correct pixel coordinates. ```cpp // Override display resolution for a 320x480 panel #define TFT_WIDTH 320 #define TFT_HEIGHT 480 #include #define MOSI_PIN 32 #define MISO_PIN 39 #define CLK_PIN 25 #define CS_PIN 33 XPT2046_Bitbang touchscreen(MOSI_PIN, MISO_PIN, CLK_PIN, CS_PIN); void setup() { Serial.begin(115200); SPIFFS.begin(true); touchscreen.begin(); } void loop() { Point p = touchscreen.getTouch(); // Coordinates now mapped to 0–320 (X) and 0–480 (Y) if (p.x || p.y) { Serial.printf("Touch: %d, %d\n", p.x, p.y); } delay(100); } ``` -------------------------------- ### getTouch() Source: https://context7.com/ddxfish/xpt2046_bitbang_arduino_library/llms.txt Reads the current touch coordinates from the touchscreen. It sends read commands over SPI, maps raw ADC values to screen pixel coordinates using calibration data, and clamps the results to the display bounds. Returns a Point struct with x and y coordinates, or {0, 0} if no touch is detected. ```APIDOC ## getTouch() ### Description Reads the current touch coordinates from the XPT2046 touchscreen. This function sends the necessary commands over the bit-banged SPI interface to retrieve raw X and Y ADC values. These raw values are then converted into screen pixel coordinates using the stored calibration data and clamped to the display's resolution. If no touch is detected, it returns a Point with coordinates {0, 0}. ### Method `Point getTouch();` ### Parameters None ### Response #### Success Response - **Point** (struct) - A structure containing the `x` and `y` pixel coordinates of the touch. - **x** (int) - The X-coordinate (0 to TFT_WIDTH, default 240). - **y** (int) - The Y-coordinate (0 to TFT_HEIGHT, default 320). ### Response Example ```cpp { "x": 123, "y": 234 } ``` ### Request Example ```cpp void loop() { Point touch = touchscreen.getTouch(); // Filter out idle (0,0) readings if (touch.x != 0 || touch.y != 0) { Serial.print("Touch at X: "); Serial.print(touch.x); // 0 – TFT_WIDTH (default 0–240) Serial.print(", Y: "); Serial.println(touch.y); // 0 – TFT_HEIGHT (default 0–320) } delay(100); // Poll at ~10 Hz } ``` ``` -------------------------------- ### Custom Display Resolution via TFT_WIDTH / TFT_HEIGHT Source: https://context7.com/ddxfish/xpt2046_bitbang_arduino_library/llms.txt Defines custom display resolution by setting `TFT_WIDTH` and `TFT_HEIGHT` macros before including the library header to match your display dimensions. ```APIDOC ## Custom Display Resolution via `TFT_WIDTH` / `TFT_HEIGHT` ### Description The library maps raw ADC readings to pixel coordinates using `TFT_WIDTH` (default 240) and `TFT_HEIGHT` (default 320). Define these macros before including the header to match your display dimensions. ### Usage Example ```cpp // Override display resolution for a 320x480 panel #define TFT_WIDTH 320 #define TFT_HEIGHT 480 #include #define MOSI_PIN 32 #define MISO_PIN 39 #define CLK_PIN 25 #define CS_PIN 33 XPT2046_Bitbang touchscreen(MOSI_PIN, MISO_PIN, CLK_PIN, CS_PIN); void setup() { Serial.begin(115200); SPIFFS.begin(true); touchscreen.begin(); } void loop() { Point p = touchscreen.getTouch(); // Coordinates now mapped to 0–320 (X) and 0–480 (Y) if (p.x || p.y) { Serial.printf("Touch: %d, %d\n", p.x, p.y); } delay(100); } ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.