Skip to content

Commit 3ab992a

Browse files
Merge pull request #1 from bcmi-labs/fixed_r4wifi
fixed lovebutton bug on UNO R4
2 parents b55b0d6 + 66a0d8e commit 3ab992a

File tree

3 files changed

+40
-40
lines changed

3 files changed

+40
-40
lines changed

examples/LoveButton/LoveButton.ino

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
11
#include "CapacitiveTouch.h"
22

3-
// Create a CapacitiveTouch object for the LOVE button using the defined LOVE_BUTTON macro.
4-
CapacitiveTouch loveButton = CapacitiveTouch(1);
3+
CapacitiveTouch touchButton = CapacitiveTouch(LOVE_BUTTON);
54

65
void setup() {
76
Serial.begin(9600);
8-
// Initialize the LOVE button sensor.
9-
loveButton.begin();
10-
// Optionally adjust the threshold if needed.
11-
loveButton.setThreshold(5000);
12-
Serial.println("LOVE button sensor test started.");
7+
touchButton.begin();
8+
touchButton.setThreshold(9000);
139
}
1410

1511
void loop() {
16-
// Retrieve the raw sensor reading.
17-
int sensorValue = loveButton.read();
18-
19-
// Print the raw value.
12+
// Read the raw value from the capacitive touch sensor.
13+
int sensorValue = touchButton.read();
2014
Serial.print("Raw value: ");
2115
Serial.println(sensorValue);
2216

2317
// Check if the sensor is touched (raw value exceeds the threshold).
24-
if (loveButton.isTouched()) {
18+
if (touchButton.isTouched()) {
2519
Serial.println("D1 touched!");
2620
}
2721

src/CapacitiveTouch.cpp

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@
1717
#define NOT_A_TOUCH_PIN 255
1818

1919
#if defined(ARDUINO_UNOR4_MINIMA)
20-
#define LOVE_PORT 2
21-
#define LOVE_PIN 4
20+
// On Minima, LOVE lives at P204
21+
#define LOVE_GPIO_PORT 2
22+
#define LOVE_GPIO_PIN 4
2223
#elif defined(ARDUINO_UNOR4_WIFI)
23-
#define LOVE_PORT 1
24-
#define LOVE_PIN 13
24+
// On WiFi, LOVE lives at P113
25+
#define LOVE_GPIO_PORT 1
26+
#define LOVE_GPIO_PIN 13
2527
#endif
26-
2728
// Forward declarations for internal functions used in ISRs:
2829

2930
typedef void (*fn_callback_ptr_t)();
@@ -85,13 +86,13 @@ void CTSUWR_handler() {
8586
// Write ISR: typically triggers a state change.
8687
}
8788

88-
void CTSURD_handler() {
89+
void CTSURD_handler() {
8990
IRQn_Type irq = R_FSP_CurrentIrqGet();
9091
R_BSP_IrqStatusClear(irq);
9192
// Read ISR: retrieve measurement counters into the results array.
92-
}
93+
}
9394

94-
void CTSUFN_handler() {
95+
void CTSUFN_handler() {
9596
IRQn_Type irq = R_FSP_CurrentIrqGet();
9697
R_BSP_IrqStatusClear(irq);
9798
ctsu_done = true;
@@ -102,7 +103,7 @@ void CTSUWR_handler() {
102103
// Restart measurement cycle if in free-running mode.
103104
startCTSUmeasure();
104105
}
105-
}
106+
}
106107

107108
// ------------------------------
108109
// Low-level Hardware Initialization
@@ -176,7 +177,6 @@ void initDTC() {
176177
// Non-Template Class Member Functions
177178
// ------------------------------
178179

179-
180180
CapacitiveTouch::CapacitiveTouch(uint8_t pin) : _pin(pin), _threshold(500), _sensorIndex(0) {
181181
CapTouchPinMapping mapping;
182182
if (lookupMapping(pin, mapping)) {
@@ -251,14 +251,21 @@ bool CapacitiveTouch::setTouchMode(uint8_t pin) {
251251
return false;
252252
}
253253

254-
// Configure the pin's peripheral function.
255-
if (pin == NUM_ARDUINO_PINS - 1) { // Special case for LOVE pin.
256-
R_PFS->PORT[LOVE_PORT].PIN[LOVE_PIN].PmnPFS =
257-
(1 << R_PFS_PORT_PIN_PmnPFS_PMR_Pos) | (12 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos);
258-
} else {
259-
R_IOPORT_PinCfg(&g_ioport_ctrl, g_pin_cfg[pin].pin,
260-
(uint32_t)(IOPORT_CFG_PERIPHERAL_PIN | IOPORT_PERIPHERAL_CTSU));
254+
255+
if (pin == LOVE_BUTTON) {
256+
// configure the physical PFS for LOVE_GPIO_[PORT|PIN]
257+
R_PFS->PORT[LOVE_GPIO_PORT]
258+
.PIN[LOVE_GPIO_PIN]
259+
.PmnPFS = (1 << R_PFS_PORT_PIN_PmnPFS_PMR_Pos)
260+
| (12 << R_PFS_PORT_PIN_PmnPFS_PSEL_Pos);
261261
}
262+
else {
263+
// all the other CTSU-capable pins
264+
R_IOPORT_PinCfg(&g_ioport_ctrl,
265+
g_pin_cfg[pin].pin,
266+
IOPORT_CFG_PERIPHERAL_PIN | IOPORT_PERIPHERAL_CTSU);
267+
}
268+
262269

263270
// Reinitialize CTSU hardware.
264271
initCTSU();
@@ -295,7 +302,7 @@ bool CapacitiveTouch::setTouchMode(uint8_t pin) {
295302
return true;
296303
}
297304

298-
void CapacitiveTouch::startTouchMeasurement(bool fr) {
305+
void CapacitiveTouch::startTouchMeasurement(bool fr) {
299306
free_running = fr;
300307
if (ctsu_done || ((R_CTSU->CTSUST & 7) == 0)) {
301308
ctsu_done = false;
@@ -307,6 +314,6 @@ bool CapacitiveTouch::setTouchMode(uint8_t pin) {
307314
}
308315
}
309316

310-
bool CapacitiveTouch::touchMeasurementReady() {
317+
bool CapacitiveTouch::touchMeasurementReady() {
311318
return (free_running || ctsu_done);
312-
}
319+
}

src/CapacitiveTouch.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,7 @@
1313
#endif
1414

1515
// Define a symbolic constant for the LOVE button (special pin).
16-
#if defined(ARDUINO_UNOR4_MINIMA)
17-
#define LOVE_BUTTON 20
18-
#elif defined(ARDUINO_UNOR4_WIFI)
19-
#define LOVE_BUTTON 27
20-
#endif
21-
22-
/// @cond DEV
16+
#define LOVE_BUTTON 20
2317

2418
/**
2519
* @struct CapTouchPinMapping
@@ -84,8 +78,13 @@ static const CapTouchPinMapping capTouchMappings[] = {
8478
{17, false, 0, 0, 0}, // A3 unsupported.
8579
{18, false, 0, 0, 0}, // A4 unsupported.
8680
{19, false, 0, 0, 0}, // A5 unsupported.
87-
{27, true, 27, 3, (1 << 3)} // LOVE pin on WiFi.
81+
{20, true, 27, 3, (1<<3)}
8882
};
83+
84+
#define CTSUMCH0_LOVE 0x1B //TS27
85+
#define LOVE_PORT 1
86+
#define LOVE_PIN 13 //Capacitive button connected to pin P113
87+
8988
#endif
9089

9190
/**

0 commit comments

Comments
 (0)