GY-63_MS5611/libraries/I2CKeyPad/README.md

116 lines
4.8 KiB
Markdown
Raw Normal View History

2021-01-29 06:31:58 -05:00
[![Arduino CI](https://github.com/RobTillaart/I2CKeyPad/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
2021-11-08 07:13:29 -05:00
[![Arduino-lint](https://github.com/RobTillaart/I2CKeyPad/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/I2CKeyPad/actions/workflows/arduino-lint.yml)
[![JSON check](https://github.com/RobTillaart/I2CKeyPad/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/I2CKeyPad/actions/workflows/jsoncheck.yml)
2021-01-29 06:31:58 -05:00
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/I2CKeyPad/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/I2CKeyPad.svg?maxAge=3600)](https://github.com/RobTillaart/I2CKeyPad/releases)
2021-11-08 07:13:29 -05:00
2021-01-29 06:31:58 -05:00
# I2CKeyPad
2021-11-08 07:13:29 -05:00
Arduino library for 4x4 KeyPad connected to an I2C PCF8574
2021-01-29 06:31:58 -05:00
## Description
The I2CKeyPad library implements the reading of a 4x4 keypad by means of a PCF8574.
Smaller keypads, meaning less columns or rows (4x3) can be read with it too.
2021-11-08 07:13:29 -05:00
A 5x3 keypad would require modification (issue pending to support this).
2021-01-29 06:31:58 -05:00
## Connection
The PCF8574 is connected between the processor and the 4x4 keypad, see the conceptual
below. It might take some trying to get the correct pins connected.
```
PROC PCF8574 KEYPAD
+--------+ +--------+ +--------+
| | | 0|----------|R |
| SDA |--------| 1|----------|O |
| SCL |--------| 2|----------|W |
| | | 3|----------|S |
| | | | | |
| | | 4|----------|C |
| | | 5|----------|O |
| | | 6|----------|L |
| | | 7|----------|S |
+--------+ +--------+ +--------+
```
## Interface
2021-11-08 07:13:29 -05:00
- **I2CKEYPAD keypad(const uint8_t deviceAddress, TwoWire \*wire = &Wire)**
2021-05-08 03:52:18 -04:00
The constructor sets the device address and optionally
allows to selects the I2C bus to use.
2021-11-08 07:13:29 -05:00
- **bool keyPad.begin()** The return value shows if the PCF8574 with the given address is connected properly.
- **bool begin(uint8_t sda, uint8_t scl)** for ESP32.
2021-01-29 06:31:58 -05:00
The return value shows if the PCF8574 with the given address is connected properly.
2021-11-08 07:13:29 -05:00
- **keyPad.isConnected()** returns false if the PCF8574 cannot be connected to.
- **uint8_t keyPad.getKey()** Returns 0..15 for regular keys, 16 if no key is pressed
and 17 in case of an error.
- **keyPad.getLastKey()** Returns the last **valid** key pressed 0..15. Initially it will return 16 (noKey).
- **keyPad.isPressed()** Returns true if one or more keys of the keyPad is pressed,
however it is not checked if multiple keys are pressed.
2021-01-29 06:31:58 -05:00
2021-11-08 07:13:29 -05:00
#### KeyMap functions
2021-01-29 06:31:58 -05:00
2021-11-08 07:13:29 -05:00
**loadKeyMap()** must be called first!
2021-01-29 06:31:58 -05:00
2021-11-08 07:13:29 -05:00
- **char getChar()** returns the char corresponding to mapped key pressed.
- **char getLastChar()** returns the last char pressed.
- **bool loadKeyMap(char \* keyMap)** keyMap should point to a (global) char array of length 19.
This array maps index 0..15 on a char and index \[16\] maps to **I2CKEYPAD_NOKEY** (typical 'N')
and index \[17\] maps **I2CKEYPAD_FAIL** (typical 'F'). index 18 is the null char.
2021-01-29 06:31:58 -05:00
2021-11-08 07:13:29 -05:00
**WARNING**
If there is no key map loaded the user should **NOT** call **getChar()** or
**getLastChar()** as these would return meaningless bytes.
2021-01-29 06:31:58 -05:00
2021-11-08 07:13:29 -05:00
```cpp
char normal_keymap[19] = "123A456B789C*0#DNF"; // typical normal key map (phone layout)
char repeat_keymap[19] = "1234123412341234NF"; // effectively 4 identical columns
char partial_keymap[19] = "1234 NF"; // top row
char diag_keymap[19] = "1 2 3 4NF"; // diagonal keys only
```
2021-01-29 06:31:58 -05:00
2021-11-08 07:13:29 -05:00
In the examples above a 'space' key might be just meant to ignore.
However functionality there is no limit how one wants to use the key mapping.
It is even possible to change the mapping runtime.
2021-01-29 06:31:58 -05:00
2021-11-08 07:13:29 -05:00
Note: a keyMap char array may be longer than 18 characters, but only the first 18 are used.
The length is **NOT** checked upon loading.
2021-01-29 06:31:58 -05:00
2021-11-08 07:13:29 -05:00
#### Basic working
2021-05-08 03:52:18 -04:00
2021-11-08 07:13:29 -05:00
After the **keypad.begin()** the sketch calls the **keyPad.getKey()** to read values from the keypad.
- If no key is pressed **I2CKEYPAD_NOKEY** code (16) is returned.
- If the read value is not valid, e.g. two keys pressed, **I2CKEYPAD_FAIL** code (17) is returned.
- Otherwise a number 0..15 is returned.
2021-05-08 03:52:18 -04:00
2021-11-08 07:13:29 -05:00
Only if a key map is loaded, the user can call **getChar()** and **getLastChar()** to get mapped keys.
2021-05-08 03:52:18 -04:00
2021-11-08 07:13:29 -05:00
## Interrupts
2021-01-29 06:31:58 -05:00
2021-11-08 07:13:29 -05:00
Since version 0.2.1 the library enables the PCF8574 to generate interrupts
on the PCF8574 when a key is pressed.
This makes checking the keypad far more efficient as one does not need to poll over I2C.
See examples.
2021-01-29 06:31:58 -05:00
## Operation
See examples
2021-11-08 07:13:29 -05:00
## Future
- update documentation
- investigate 5x3 keypad and other 'formats'
- test key mapping functions.