From 0c33dc374cd917b7593358f17b04f8fcf5b45f07 Mon Sep 17 00:00:00 2001 From: Cristian Dragomir Date: Thu, 8 May 2025 02:03:58 +0300 Subject: [PATCH] fixed lovebutton bug on UNO R4 --- examples/LoveButton/LoveButton.ino | 18 ++++-------- src/CapacitiveTouch.cpp | 47 +++++++++++++++++------------- src/CapacitiveTouch.h | 14 ++++----- 3 files changed, 40 insertions(+), 39 deletions(-) diff --git a/examples/LoveButton/LoveButton.ino b/examples/LoveButton/LoveButton.ino index d242256..e74e315 100644 --- a/examples/LoveButton/LoveButton.ino +++ b/examples/LoveButton/LoveButton.ino @@ -1,27 +1,21 @@ #include "CapacitiveTouch.h" -// Create a CapacitiveTouch object for the LOVE button using the defined LOVE_BUTTON macro. -CapacitiveTouch loveButton = CapacitiveTouch(1); +CapacitiveTouch touchButton = CapacitiveTouch(LOVE_BUTTON); void setup() { Serial.begin(9600); - // Initialize the LOVE button sensor. - loveButton.begin(); - // Optionally adjust the threshold if needed. - loveButton.setThreshold(5000); - Serial.println("LOVE button sensor test started."); + touchButton.begin(); + touchButton.setThreshold(9000); } void loop() { - // Retrieve the raw sensor reading. - int sensorValue = loveButton.read(); - - // Print the raw value. + // Read the raw value from the capacitive touch sensor. + int sensorValue = touchButton.read(); Serial.print("Raw value: "); Serial.println(sensorValue); // Check if the sensor is touched (raw value exceeds the threshold). - if (loveButton.isTouched()) { + if (touchButton.isTouched()) { Serial.println("D1 touched!"); } diff --git a/src/CapacitiveTouch.cpp b/src/CapacitiveTouch.cpp index b154996..99b964f 100644 --- a/src/CapacitiveTouch.cpp +++ b/src/CapacitiveTouch.cpp @@ -17,13 +17,14 @@ #define NOT_A_TOUCH_PIN 255 #if defined(ARDUINO_UNOR4_MINIMA) - #define LOVE_PORT 2 - #define LOVE_PIN 4 + // On Minima, LOVE lives at P204 + #define LOVE_GPIO_PORT 2 + #define LOVE_GPIO_PIN 4 #elif defined(ARDUINO_UNOR4_WIFI) - #define LOVE_PORT 1 - #define LOVE_PIN 13 + // On WiFi, LOVE lives at P113 + #define LOVE_GPIO_PORT 1 + #define LOVE_GPIO_PIN 13 #endif - // Forward declarations for internal functions used in ISRs: typedef void (*fn_callback_ptr_t)(); @@ -85,13 +86,13 @@ void CTSUWR_handler() { // Write ISR: typically triggers a state change. } - void CTSURD_handler() { +void CTSURD_handler() { IRQn_Type irq = R_FSP_CurrentIrqGet(); R_BSP_IrqStatusClear(irq); // Read ISR: retrieve measurement counters into the results array. - } +} - void CTSUFN_handler() { +void CTSUFN_handler() { IRQn_Type irq = R_FSP_CurrentIrqGet(); R_BSP_IrqStatusClear(irq); ctsu_done = true; @@ -102,7 +103,7 @@ void CTSUWR_handler() { // Restart measurement cycle if in free-running mode. startCTSUmeasure(); } - } +} // ------------------------------ // Low-level Hardware Initialization @@ -176,7 +177,6 @@ void initDTC() { // Non-Template Class Member Functions // ------------------------------ - CapacitiveTouch::CapacitiveTouch(uint8_t pin) : _pin(pin), _threshold(500), _sensorIndex(0) { CapTouchPinMapping mapping; if (lookupMapping(pin, mapping)) { @@ -251,14 +251,21 @@ bool CapacitiveTouch::setTouchMode(uint8_t pin) { return false; } - // Configure the pin's peripheral function. - if (pin == NUM_ARDUINO_PINS - 1) { // Special case for LOVE pin. - R_PFS->PORT[LOVE_PORT].PIN[LOVE_PIN].PmnPFS = - (1 << R_PFS_PORT_PIN_PmnPFS_PMR_Pos) | (12 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos); - } else { - R_IOPORT_PinCfg(&g_ioport_ctrl, g_pin_cfg[pin].pin, - (uint32_t)(IOPORT_CFG_PERIPHERAL_PIN | IOPORT_PERIPHERAL_CTSU)); + + if (pin == LOVE_BUTTON) { + // configure the physical PFS for LOVE_GPIO_[PORT|PIN] + R_PFS->PORT[LOVE_GPIO_PORT] + .PIN[LOVE_GPIO_PIN] + .PmnPFS = (1 << R_PFS_PORT_PIN_PmnPFS_PMR_Pos) + | (12 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos); } + else { + // all the other CTSU-capable pins + R_IOPORT_PinCfg(&g_ioport_ctrl, + g_pin_cfg[pin].pin, + IOPORT_CFG_PERIPHERAL_PIN | IOPORT_PERIPHERAL_CTSU); + } + // Reinitialize CTSU hardware. initCTSU(); @@ -295,7 +302,7 @@ bool CapacitiveTouch::setTouchMode(uint8_t pin) { return true; } - void CapacitiveTouch::startTouchMeasurement(bool fr) { +void CapacitiveTouch::startTouchMeasurement(bool fr) { free_running = fr; if (ctsu_done || ((R_CTSU->CTSUST & 7) == 0)) { ctsu_done = false; @@ -307,6 +314,6 @@ bool CapacitiveTouch::setTouchMode(uint8_t pin) { } } - bool CapacitiveTouch::touchMeasurementReady() { +bool CapacitiveTouch::touchMeasurementReady() { return (free_running || ctsu_done); - } \ No newline at end of file +} \ No newline at end of file diff --git a/src/CapacitiveTouch.h b/src/CapacitiveTouch.h index 58ff389..6b21f57 100644 --- a/src/CapacitiveTouch.h +++ b/src/CapacitiveTouch.h @@ -13,12 +13,7 @@ #endif // Define a symbolic constant for the LOVE button (special pin). -#if defined(ARDUINO_UNOR4_MINIMA) - #define LOVE_BUTTON 20 -#elif defined(ARDUINO_UNOR4_WIFI) - #define LOVE_BUTTON 27 -#endif - +#define LOVE_BUTTON 20 /** * @struct CapTouchPinMapping * @brief Defines the mapping between an Arduino pin and its capacitive touch hardware settings. @@ -80,8 +75,13 @@ static const CapTouchPinMapping capTouchMappings[] = { {17, false, 0, 0, 0}, // A3 unsupported. {18, false, 0, 0, 0}, // A4 unsupported. {19, false, 0, 0, 0}, // A5 unsupported. - {27, true, 27, 3, (1 << 3)} // LOVE pin on WiFi. + {20, true, 27, 3, (1<<3)} }; + +#define CTSUMCH0_LOVE 0x1B //TS27 +#define LOVE_PORT 1 +#define LOVE_PIN 13 //Capacitive button connected to pin P113 + #endif /**