Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 46 additions & 9 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ Compatible with all arduino platforms (used arduino functions)

<a id="init"> </a>
## initialization
`` `CPP
```CPP
Analogkey <a0, 16> keys;// Specify the PIN and the number of buttons
Analogkey <a0, 16, Signals> keys;// Specify PIN, number of buttons and external array of signals
`` `
```

<a id="usage"> </a>
## 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:
Expand All @@ -61,7 +61,7 @@ Intsed ();// displays the number of the pressed button or -1, if there are no bu
<a id="EXAMPLE"> </a>
## Example
### External array
`` `CPP
```CPP
#include "AnaLogkey.h"
// Create an array of signal values from buttons
int16_t sigs [16] = {{
Expand Down Expand Up @@ -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 <a0, 16> keys;
Expand Down Expand Up @@ -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 <A0, 16, sigs> 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);
}
```

<a id="versions"> </a>
## versions
- V1.0
- v1.1 - optimization, the logic of the window size has been changed
- v1.2 - adding a window as a percentage

<a id="feedback"> </a>
## bugs and feedback
Expand All @@ -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
- Ideally, attach the minimum code in which the bug is observed.Not a canvas of a thousand lines, but a minimum code
73 changes: 54 additions & 19 deletions src/AnalogKey.h
Original file line number Diff line number Diff line change
@@ -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 <Arduino.h>

#define _AKEY_PERIOD 40 // период опроса в мс

#define _AKEY_PERIOD 40 // polling period in ms
template <uint8_t PIN, uint8_t AMOUNT, int16_t* S_PTR = nullptr>
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;
}
Expand All @@ -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
#endif