Skip to content

fixed lovebutton bug on UNO R4 #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions examples/LoveButton/LoveButton.ino
Original file line number Diff line number Diff line change
@@ -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!");
}

Expand Down
47 changes: 27 additions & 20 deletions src/CapacitiveTouch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)();
Expand Down Expand Up @@ -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;
Expand All @@ -102,7 +103,7 @@ void CTSUWR_handler() {
// Restart measurement cycle if in free-running mode.
startCTSUmeasure();
}
}
}

// ------------------------------
// Low-level Hardware Initialization
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand All @@ -307,6 +314,6 @@ bool CapacitiveTouch::setTouchMode(uint8_t pin) {
}
}

bool CapacitiveTouch::touchMeasurementReady() {
bool CapacitiveTouch::touchMeasurementReady() {
return (free_running || ctsu_done);
}
}
15 changes: 7 additions & 8 deletions src/CapacitiveTouch.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +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

/// @cond DEV
#define LOVE_BUTTON 20

/**
* @struct CapTouchPinMapping
Expand Down Expand Up @@ -84,8 +78,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

/**
Expand Down
Loading