|
6 | 6 | * er sie vor der Verwendung ausreichend getestet hat. |
7 | 7 | * Durch die Nutzung dieser Software erklären Sie, dass Sie die Sicherheitswarnungen und Gebrauchsanweisungen gelesen und verstanden haben. |
8 | 8 | * |
9 | | - * DISLAIMER: |
| 9 | + * DISCLAIMER: |
10 | 10 | * This software is provided "as-is" without any express or implied warranty. |
11 | 11 | * In no event shall the author or company be held liable for any damages arising from the use of this software. |
12 | 12 | * It is the user's responsibility to ensure that the software is suitable for their needs and that they have tested it |
|
25 | 25 | #include "SoftPathElectronics.h" |
26 | 26 |
|
27 | 27 | CustomKeyboard::CustomKeyboard() { |
28 | | - _debug = true; |
| 28 | + _debug = false; |
29 | 29 | _lastKey = -1; |
30 | 30 | _resetRequired = false; |
31 | 31 | _pressed = false; |
@@ -59,7 +59,7 @@ void CustomKeyboard::setupKeyboard() { |
59 | 59 | while (true) { |
60 | 60 | calibrateKey(i); |
61 | 61 | Serial.print("Möchtest du zu Taste "); |
62 | | - Serial.print(i + 2); |
| 62 | + Serial.print(i + 1); |
63 | 63 | Serial.println(" fortfahren oder 'redo' eingeben? (Enter für fortfahren / redo eingeben):"); |
64 | 64 | while (Serial.available() == 0) {} |
65 | 65 | String input = Serial.readStringUntil('\n'); |
@@ -196,23 +196,103 @@ void CustomKeyboard::promptUser(String message) { |
196 | 196 | } |
197 | 197 |
|
198 | 198 | 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); |
201 | 203 |
|
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(); |
207 | 251 | } |
208 | 252 | } |
209 | 253 |
|
210 | 254 | int CustomKeyboard::getKeyPressed() { |
211 | 255 | int value = readAnalogValue(); |
| 256 | + if (_debug) { |
| 257 | + Serial.print("Analog Value Read: "); |
| 258 | + Serial.println(value); |
| 259 | + } |
212 | 260 | for (int i = 0; i < _numKeys; i++) { |
213 | 261 | 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 | + } |
215 | 271 | } |
216 | 272 | } |
| 273 | + _lastKey = -1; |
217 | 274 | return -1; // Keine Taste erkannt |
218 | 275 | } |
| 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