Skip to content

Commit e3222e3

Browse files
Update SoftPathElectronics.cpp
1 parent 43369c0 commit e3222e3

File tree

1 file changed

+91
-11
lines changed

1 file changed

+91
-11
lines changed

src/SoftPathElectronics.cpp

Lines changed: 91 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* er sie vor der Verwendung ausreichend getestet hat.
77
* Durch die Nutzung dieser Software erklären Sie, dass Sie die Sicherheitswarnungen und Gebrauchsanweisungen gelesen und verstanden haben.
88
*
9-
* DISLAIMER:
9+
* DISCLAIMER:
1010
* This software is provided "as-is" without any express or implied warranty.
1111
* In no event shall the author or company be held liable for any damages arising from the use of this software.
1212
* It is the user's responsibility to ensure that the software is suitable for their needs and that they have tested it
@@ -25,7 +25,7 @@
2525
#include "SoftPathElectronics.h"
2626

2727
CustomKeyboard::CustomKeyboard() {
28-
_debug = true;
28+
_debug = false;
2929
_lastKey = -1;
3030
_resetRequired = false;
3131
_pressed = false;
@@ -59,7 +59,7 @@ void CustomKeyboard::setupKeyboard() {
5959
while (true) {
6060
calibrateKey(i);
6161
Serial.print("Möchtest du zu Taste ");
62-
Serial.print(i + 2);
62+
Serial.print(i + 1);
6363
Serial.println(" fortfahren oder 'redo' eingeben? (Enter für fortfahren / redo eingeben):");
6464
while (Serial.available() == 0) {}
6565
String input = Serial.readStringUntil('\n');
@@ -196,23 +196,103 @@ void CustomKeyboard::promptUser(String message) {
196196
}
197197

198198
void CustomKeyboard::setupKey(const String& key) {
199-
int spaceIndex = key.indexOf(' ');
200-
int nextIndex = 0;
199+
int values[20] = {0}; // Initialize all to zero
200+
int index = 0;
201+
int startIndex = 0;
202+
int spaceIndex = key.indexOf(' ', startIndex);
201203

202-
// Werte extrahieren und in _keyValues speichern
203-
for (int i = 0; i < 16 && spaceIndex != -1; i++) {
204-
_keyValues[i] = key.substring(nextIndex, spaceIndex).toInt();
205-
nextIndex = spaceIndex + 1;
206-
spaceIndex = key.indexOf(' ', nextIndex);
204+
// Parse the key string and fill the values array
205+
while (spaceIndex != -1 && index < 20) {
206+
String token = key.substring(startIndex, spaceIndex);
207+
values[index++] = token.toInt();
208+
startIndex = spaceIndex + 1;
209+
spaceIndex = key.indexOf(' ', startIndex);
210+
}
211+
// Capture the last value if there's no trailing space
212+
if (startIndex < key.length() && index < 20) {
213+
String token = key.substring(startIndex);
214+
values[index++] = token.toInt();
215+
}
216+
217+
// Check if we have all the required values
218+
if (index < 20) {
219+
Serial.println("Error: Key string does not have enough values.");
220+
return;
221+
}
222+
223+
// Assign the values to the appropriate variables
224+
_pin = values[0];
225+
_numKeys = values[1];
226+
_tolerance = values[2];
227+
_debounceMode = (values[3] != 0);
228+
for (int i = 0; i < 16; i++) {
229+
_keyValues[i] = values[4 + i];
230+
}
231+
232+
// Initialize the pin
233+
pinMode(_pin, INPUT);
234+
235+
if (_debug) {
236+
Serial.println("Keyboard Configuration:");
237+
Serial.print("Pin: ");
238+
Serial.println(_pin);
239+
Serial.print("Number of Keys: ");
240+
Serial.println(_numKeys);
241+
Serial.print("Tolerance: ");
242+
Serial.println(_tolerance);
243+
Serial.print("Debounce Mode: ");
244+
Serial.println(_debounceMode ? "Enabled" : "Disabled");
245+
Serial.print("Key Values: ");
246+
for (int i = 0; i < _numKeys; i++) {
247+
Serial.print(_keyValues[i]);
248+
if (i < _numKeys - 1) Serial.print(", ");
249+
}
250+
Serial.println();
207251
}
208252
}
209253

210254
int CustomKeyboard::getKeyPressed() {
211255
int value = readAnalogValue();
256+
if (_debug) {
257+
Serial.print("Analog Value Read: ");
258+
Serial.println(value);
259+
}
212260
for (int i = 0; i < _numKeys; i++) {
213261
if (abs(value - _keyValues[i]) < _tolerance) {
214-
return i + 1; // Rückgabe der Tastennummer, beginnend mit 1
262+
if (_lastKey != i + 1 || _resetRequired) {
263+
_lastKey = i + 1;
264+
_resetRequired = false;
265+
if (_debounceMode && value == 0) {
266+
_resetRequired = true;
267+
continue;
268+
}
269+
return i + 1; // Rückgabe der Tastennummer, beginnend mit 1
270+
}
215271
}
216272
}
273+
_lastKey = -1;
217274
return -1; // Keine Taste erkannt
218275
}
276+
277+
int CustomKeyboard::getKeyValue(int index) {
278+
if (index >= 0 && index < 16) {
279+
return _keyValues[index];
280+
}
281+
return -1;
282+
}
283+
284+
int CustomKeyboard::getPin() {
285+
return _pin;
286+
}
287+
288+
int CustomKeyboard::getNumKeys() {
289+
return _numKeys;
290+
}
291+
292+
int CustomKeyboard::getTolerance() {
293+
return _tolerance;
294+
}
295+
296+
bool CustomKeyboard::getDebounceMode() {
297+
return _debounceMode;
298+
}

0 commit comments

Comments
 (0)