From 2cba8a0832d8400abab2fcaa60ad75489cb6b59d Mon Sep 17 00:00:00 2001 From: Thierry MARTIN Date: Sat, 2 Aug 2025 17:11:14 +0200 Subject: [PATCH] Adding a window as a percentage --- README_EN.md | 55 +++++++++++++++++++++++++++++++------ src/AnalogKey.h | 73 ++++++++++++++++++++++++++++++++++++------------- 2 files changed, 100 insertions(+), 28 deletions(-) diff --git a/README_EN.md b/README_EN.md index 27f0c4b..62a7c47 100644 --- a/README_EN.md +++ b/README_EN.md @@ -37,19 +37,19 @@ Compatible with all arduino platforms (used arduino functions) ## initialization -`` `CPP +```CPP Analogkey keys;// Specify the PIN and the number of buttons Analogkey keys;// Specify PIN, number of buttons and external array of signals -`` ` +``` ## Usage -`` `CPP +```CPP VOID attach (uint8_t num, int velue);// connects the button to the specified value VOID Setwindow (Intsdow);// sets the signal window (silence 40) Bool Status (uint8_t num);// returns the status of the specified button Intsed ();// displays the number of the pressed button or -1, if there are no buttons pressed -`` ` +``` ### How to work - measure and write signals from the keyboard (for example, display values to the port monitor) - Pass the values to the library: @@ -61,7 +61,7 @@ Intsed ();// displays the number of the pressed button or -1, if there are no bu ## Example ### External array -`` `CPP +```CPP #include "AnaLogkey.h" // Create an array of signal values from buttons int16_t sigs [16] = {{ @@ -96,10 +96,10 @@ VOID loop () { if (keys.pressed ()! = -1) serial.println (keys.pressed ()); Delay (10); } -`` ` +``` ### Inner array -`` `CPP +```CPP #include "AnaLogkey.h" // Specify the PIN and the number of buttons Analogkey keys; @@ -140,12 +140,49 @@ VOID loop () { if (keys.pressed ()! = -1) serial.println (keys.pressed ()); Delay (10); } -`` ` +``` +### Window as a percentage +```CPP +#include "AnaLogkey.h" +// Create an array of signal values from buttons +int16_t sigs [16] = {{ + 1023, 927, 856, 783, + 671, 632, 590, 560, + 504, 480, 455, 440, + 399, 319, 255, 230 +}; + +// Specify PIN, number of buttons and array of values +ANALOGKEY keys; + +VOID setup () { + Serial.Begin (9600); + + // The survey window is one-third of the gap with the previous or next button that has the closest value + keys.setwindow (0); +} + +VOID loop () { + // Check each button in manual mode + if (keys.status (0)) serial.println ("Press 0"); + if (keys.status (1)) serial.println ("Press 1"); + if (keys.status (2)) serial.println ("Press 2"); + if (keys.status (3)) serial.println ("Press 3"); + if (keys.status (4)) serial.println ("Press 4"); + if (keys.status (5)) serial.println ("Press 5"); + if (keys.status (6)) serial.println ("Press 6"); + + // or display the number of the current pressed (-1 means not one is pressed) + if (keys.pressed ()! = -1) serial.println (keys.pressed ()); + Delay (10); +} +``` ## versions - V1.0 - v1.1 - optimization, the logic of the window size has been changed +- v1.2 - adding a window as a percentage ## bugs and feedback @@ -160,4 +197,4 @@ When reporting about bugs or incorrect work of the library, it is necessary to i - version of Arduino ide - whether the built -in examples work correctly, in which the functions and designs are used, leading to a bug in your code - what code has been loaded, what work was expected from it and how it works in reality -- Ideally, attach the minimum code in which the bug is observed.Not a canvas of a thousand lines, but a minimum code \ No newline at end of file +- Ideally, attach the minimum code in which the bug is observed.Not a canvas of a thousand lines, but a minimum code diff --git a/src/AnalogKey.h b/src/AnalogKey.h index 411144a..51a7644 100644 --- a/src/AnalogKey.h +++ b/src/AnalogKey.h @@ -1,52 +1,86 @@ /* - Библиотека для работы с аналоговой клавиатурой + Library for working with an analog keyboard GitHub: https://github.com/GyverLibs/AnalogKey - - Встроенный простенький дебаунс - - Оптимальный опрос пина по таймеру - - Проверка конкретной кнопки или вывод номера нажатой - + - Built-in simple debounce + - Optimal pin polling by timer + - Check a specific button or output the number of the pressed button + AlexGyver, alex@alexgyver.ru https://alexgyver.ru/ MIT License - - v1.1 - оптимизация, изменена логика размера окна + + v1.1 - optimization, changed the logic of window size + v1.2 - adding a window as a percentage */ #ifndef _AnalogKey_h #define _AnalogKey_h #include -#define _AKEY_PERIOD 40 // период опроса в мс - +#define _AKEY_PERIOD 40 // polling period in ms template class AnalogKey { public: - // подключает кнопку на указанное значение + // connects the button to the specified value void attach(uint8_t num, int value) { if (num >= AMOUNT) return; if (S_PTR == nullptr) signals[num] = value; else signals_p[num] = value; } - // устанавливает окно сигнала (умолч. 40) + // sets the signal window (default is 20) void setWindow(int window) { + _autoWindow = !window ? true : false; _window = window / 2; } - - // возвращает статус указанной кнопки + + // returns the status of the specified button bool status(uint8_t num) { if (millis() - tmr > _AKEY_PERIOD) { tmr = millis(); int16_t thisRead = analogRead(PIN); + if (_autoWindow && thisRead != _lastRead) { + _window = 0; + for (uint8_t i = 0; i < AMOUNT; i++) { + int16_t currentSignal = (S_PTR == nullptr) ? signals[i] : signals_p[i]; + bool currentKey = true; + int16_t currentWindow = 0; + if (i > 0) { + int16_t previousSignal = (S_PTR == nullptr) ? signals[i - 1] : signals_p[i - 1]; + if (thisRead < (previousSignal + currentSignal) / 2) { + currentKey = false; + } + else { + currentWindow = (currentSignal - previousSignal) / 3; + } + } + if (i < AMOUNT - 1) { + int16_t nextSignal = (S_PTR == nullptr) ? signals[i + 1] : signals_p[i + 1]; + if (thisRead > (currentSignal + nextSignal) / 2) { + currentKey = false; + } + else { + int16_t secondWindow = (nextSignal - currentSignal) / 3; + if (!currentWindow || secondWindow < currentWindow) { + currentWindow = secondWindow; + } + } + } + if (currentKey) { + _window = currentWindow; + break; + } + } + } _ready = (abs(thisRead - _lastRead) < _window); - _lastRead = thisRead; + _lastRead = thisRead; } return check(num); } - - // выводит номер нажатой кнопки или -1, если нажатых кнопок нет + + // outputs the number of the pressed button or -1 if there are no pressed buttons int pressed() { - status(0); // вызываем опрос + status(0); // call polling for (uint8_t i = 0; i < AMOUNT; i++) { if (check(i)) return i; } @@ -58,12 +92,13 @@ class AnalogKey { int16_t sig = (S_PTR == nullptr) ? signals[i] : signals_p[i]; return (_ready && (_lastRead > sig - _window) && (_lastRead < sig + _window)); } - + int16_t signals[(S_PTR == nullptr) ? AMOUNT : 0]; int16_t* signals_p = S_PTR; int16_t _lastRead = 0; + bool _autoWindow = false; int16_t _window = 20; bool _ready = false; uint32_t tmr; }; -#endif \ No newline at end of file +#endif