Skip to content

Commit df5dbac

Browse files
added arduino style pins, and baseline functionality
1 parent 3ab992a commit df5dbac

File tree

3 files changed

+64
-25
lines changed

3 files changed

+64
-25
lines changed

examples/LoveButton/LoveButton.ino

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@ CapacitiveTouch touchButton = CapacitiveTouch(LOVE_BUTTON);
44

55
void setup() {
66
Serial.begin(9600);
7-
touchButton.begin();
8-
touchButton.setThreshold(9000);
7+
8+
if(touchButton.begin()){
9+
Serial.println("Capacitive touch sensor initialized.");
10+
} else {
11+
Serial.println("Failed to initialize capacitive touch sensor. Please use a supported pin.");
12+
while(true);
13+
}
14+
15+
touchButton.setThreshold(2000);
916
}
1017

1118
void loop() {
@@ -16,7 +23,7 @@ void loop() {
1623

1724
// Check if the sensor is touched (raw value exceeds the threshold).
1825
if (touchButton.isTouched()) {
19-
Serial.println("D1 touched!");
26+
Serial.println("Button touched!");
2027
}
2128

2229
delay(100);

src/CapacitiveTouch.cpp

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -177,17 +177,17 @@ void initDTC() {
177177
// Non-Template Class Member Functions
178178
// ------------------------------
179179

180-
CapacitiveTouch::CapacitiveTouch(uint8_t pin) : _pin(pin), _threshold(500), _sensorIndex(0) {
181-
CapTouchPinMapping mapping;
182-
if (lookupMapping(pin, mapping)) {
183-
_ts_num = mapping.ts_num;
184-
_chac_idx = mapping.chac_idx;
185-
_chac_val = mapping.chac_val;
186-
} else {
187-
// If unsupported, set values to 0 (error handling; you may choose to report an error)
188-
_ts_num = 0;
189-
_chac_idx = 0;
190-
_chac_val = 0;
180+
/**
181+
* @brief Construct with the Arduino-side pin number (e.g. D4, A1, LOVE_BUTTON).
182+
* @param pin Arduino pin (0..20 on Minima, 0..27 on WiFi).
183+
*/
184+
CapacitiveTouch::CapacitiveTouch(uint8_t pin)
185+
: _pin(pin), _mappingIndex(0xFF), _threshold(500), _baseline(0), _sensorIndex(0)
186+
{
187+
_mappingIndex = findMappingIndex(pin);
188+
if (_mappingIndex != 0xFF) {
189+
auto &m = capTouchMappings[_mappingIndex];
190+
// stash ts_num/chac_idx/chac_val if you need them—but you can also defer that until begin()
191191
}
192192
}
193193

@@ -202,27 +202,44 @@ bool CapacitiveTouch::lookupMapping(uint8_t pin, CapTouchPinMapping &mapping) {
202202
return false;
203203
}
204204

205-
void CapacitiveTouch::begin() {
205+
bool CapacitiveTouch::begin() {
206+
if (_mappingIndex == 0xFF) {
207+
return false;
208+
}
206209
pinMode(_pin, INPUT);
207210
initCTSU();
208211
if (!setTouchMode(_pin)) {
209-
Serial.println("Error: Failed to enable touch mode on this pin.");
210-
return;
212+
return false;
211213
}
212214
_sensorIndex = pinToDataIndex[_pin];
213-
Serial.println("CapacitiveTouch sensor initialized.");
215+
216+
long sum = 0;
217+
for (int i = 0; i < CALIBRATION_SAMPLES; i++) {
218+
sum += readRaw();
219+
delay(5);
220+
}
221+
_baseline = sum / CALIBRATION_SAMPLES;
222+
return true;
223+
}
224+
225+
int CapacitiveTouch::readRaw() {
226+
startTouchMeasurement(false);
227+
while (!touchMeasurementReady()) {
228+
delay(1);
229+
}
230+
return results[_sensorIndex][0];
214231
}
215232

216233
int CapacitiveTouch::read() {
217-
startTouchMeasurement(false);
218-
while (!touchMeasurementReady()) {
219-
delay(1);
220-
}
221-
return results[_sensorIndex][0];
234+
int raw = readRaw();
235+
int delta = raw - _baseline;
236+
return (delta > 0) ? delta : 0;
222237
}
223238

224239
bool CapacitiveTouch::isTouched() {
225-
return read() > _threshold;
240+
int delta = read();
241+
return (delta > _threshold) ||
242+
(delta < -_threshold);
226243
}
227244

228245
void CapacitiveTouch::setThreshold(int threshold) {

src/CapacitiveTouch.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
// Define a symbolic constant for the LOVE button (special pin).
1616
#define LOVE_BUTTON 20
1717

18+
#define CALIBRATION_SAMPLES 16 ///< Number of samples for calibration.
19+
1820
/**
1921
* @struct CapTouchPinMapping
2022
* @brief Defines the mapping between an Arduino pin and its capacitive touch hardware settings.
@@ -106,7 +108,7 @@ class CapacitiveTouch {
106108
*
107109
* Configures the pin and initializes the CTSU/DTC hardware.
108110
*/
109-
void begin();
111+
bool begin();
110112

111113
/**
112114
* @brief Reads the raw sensor value.
@@ -143,18 +145,31 @@ class CapacitiveTouch {
143145
uint8_t _chac_idx; ///< Channel control index (from mapping).
144146
uint8_t _chac_val; ///< Bit mask (from mapping).
145147
uint8_t _sensorIndex; ///< Index assigned after enabling the channel.
148+
uint8_t _mappingIndex; ///< Index in the mapping array.
149+
int _baseline;
150+
146151
/**
147152
* @brief Looks up the mapping for a given pin.
148153
* @param pin The Arduino pin.
149154
* @param mapping Reference to a mapping structure to fill.
150155
* @return true if mapping found and supported, false otherwise.
151156
*/
152157
bool lookupMapping(uint8_t pin, CapTouchPinMapping &mapping);
158+
int readRaw();
153159

154160
// Low-level helper functions as static members
155161
static bool setTouchMode(uint8_t pin);
156162
static void startTouchMeasurement(bool fr);
157163
static bool touchMeasurementReady();
164+
165+
static uint8_t findMappingIndex(uint8_t pin) {
166+
for (uint8_t i = 0; i < (sizeof(capTouchMappings)/sizeof(capTouchMappings[0])); i++) {
167+
if (capTouchMappings[i].arduinoPin == pin) {
168+
return i;
169+
}
170+
}
171+
return 0xFF;
172+
}
158173
};
159174

160175
#endif // CAPACITIVE_TOUCH_H

0 commit comments

Comments
 (0)