mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
0.4.0 I2CKeyPad
This commit is contained in:
parent
ac95aef851
commit
8cf136ecad
@ -6,11 +6,18 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
||||
and this project adheres to [Semantic Versioning](http://semver.org/).
|
||||
|
||||
|
||||
## [0.4.0] - 2023-11-09
|
||||
- simplify begin()
|
||||
- added I2Ckeypad_Wire1_ESP32.ino
|
||||
- update readme.md
|
||||
- minor edits
|
||||
|
||||
----
|
||||
|
||||
## [0.3.3] - 2022-11-12
|
||||
- Add RP2040 support to build-CI.
|
||||
- Add CHANGELOG.md
|
||||
|
||||
|
||||
## [0.3.2] - 2022-09-19
|
||||
- experimental version
|
||||
- add 5x3, 6x2 and 8x1 support
|
||||
|
@ -1,11 +1,9 @@
|
||||
//
|
||||
// FILE: I2CKeyPad.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.3.3
|
||||
// VERSION: 0.4.0
|
||||
// PURPOSE: Arduino library for 4x4 KeyPad connected to an I2C PCF8574
|
||||
// URL: https://github.com/RobTillaart/I2CKeyPad
|
||||
//
|
||||
// HISTORY: see changelog.md
|
||||
|
||||
|
||||
#include "I2CKeyPad.h"
|
||||
@ -20,20 +18,8 @@ I2CKeyPad::I2CKeyPad(const uint8_t deviceAddress, TwoWire *wire)
|
||||
}
|
||||
|
||||
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
bool I2CKeyPad::begin(uint8_t sda, uint8_t scl)
|
||||
{
|
||||
_wire->begin(sda, scl);
|
||||
// enable interrupts
|
||||
_read(0xF0);
|
||||
return isConnected();
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
bool I2CKeyPad::begin()
|
||||
{
|
||||
_wire->begin();
|
||||
// enable interrupts
|
||||
_read(0xF0);
|
||||
return isConnected();
|
||||
|
@ -2,7 +2,7 @@
|
||||
//
|
||||
// FILE: I2CKeyPad.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.3.3
|
||||
// VERSION: 0.4.0
|
||||
// PURPOSE: Arduino library for 4x4 KeyPad connected to an I2C PCF8574
|
||||
// URL: https://github.com/RobTillaart/I2CKeyPad
|
||||
|
||||
@ -11,7 +11,7 @@
|
||||
#include "Wire.h"
|
||||
|
||||
|
||||
#define I2C_KEYPAD_LIB_VERSION (F("0.3.3"))
|
||||
#define I2C_KEYPAD_LIB_VERSION (F("0.4.0"))
|
||||
|
||||
#define I2C_KEYPAD_NOKEY 16
|
||||
#define I2C_KEYPAD_FAIL 17
|
||||
@ -28,9 +28,7 @@ class I2CKeyPad
|
||||
public:
|
||||
I2CKeyPad(const uint8_t deviceAddress, TwoWire *wire = &Wire);
|
||||
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
bool begin(uint8_t sda, uint8_t scl);
|
||||
#endif
|
||||
// call Wire.begin() first!
|
||||
bool begin();
|
||||
|
||||
// get raw key's 0..15
|
||||
|
@ -2,8 +2,11 @@
|
||||
[![Arduino CI](https://github.com/RobTillaart/I2CKeyPad/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
|
||||
[![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)
|
||||
[![GitHub issues](https://img.shields.io/github/issues/RobTillaart/I2CKeyPad.svg)](https://github.com/RobTillaart/I2CKeyPad/issues)
|
||||
|
||||
[![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)
|
||||
[![PlatformIO Registry](https://badges.registry.platformio.org/packages/robtillaart/library/I2CKeyPad.svg)](https://registry.platformio.org/libraries/robtillaart/I2CKeyPad)
|
||||
|
||||
|
||||
# I2CKeyPad
|
||||
@ -18,7 +21,13 @@ Smaller keypads, meaning less columns or rows (4x3) can be read with it too.
|
||||
|
||||
Since 0.3.2 the library allows a 5x3, 6x2 or 8x1 or smaller keypad to be connected too.
|
||||
|
||||
Relates to https://github.com/RobTillaart/I2CKeyPad8x8. which is an 8x8 version using PCF8575.
|
||||
#### Related
|
||||
|
||||
Relates strongly to https://github.com/RobTillaart/I2CKeyPad8x8. which is an 8x8 version using **PCF8575**.
|
||||
|
||||
- https://github.com/RobTillaart/AnalogKeypad
|
||||
- https://github.com/RobTillaart/I2CKeyPad8x8
|
||||
- https://github.com/WK-Software56/AdvKeyPad (derived work with keyboard alike interface)
|
||||
|
||||
|
||||
## Connection
|
||||
@ -45,12 +54,17 @@ It might take some trying to get the correct pins connected.
|
||||
|
||||
## Interface
|
||||
|
||||
- **I2CKEYPAD(const uint8_t deviceAddress, TwoWire \*wire = &Wire)**
|
||||
```cpp
|
||||
#include "I2CKeyPad.h"
|
||||
```
|
||||
|
||||
#### Base
|
||||
|
||||
- **I2CKeyPad(const uint8_t deviceAddress, TwoWire \*wire = &Wire)**
|
||||
The constructor sets the device address and optionally
|
||||
allows to selects the I2C bus to use.
|
||||
- **bool 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.
|
||||
The return value shows if the PCF8574 with the given address is connected properly.
|
||||
Call wire.begin() first!
|
||||
- **bool isConnected()** returns false if the PCF8574 cannot be connected to.
|
||||
- **uint8_t getKey()** Returns default 0..15 for regular keys,
|
||||
Returns 16 if no key is pressed and 17 in case of an error.
|
||||
@ -141,5 +155,24 @@ See examples
|
||||
|
||||
## Future
|
||||
|
||||
#### Must
|
||||
|
||||
- update documentation
|
||||
|
||||
#### Should
|
||||
|
||||
- test key mapping functions.
|
||||
|
||||
#### Could
|
||||
|
||||
#### Wont
|
||||
|
||||
|
||||
## Support
|
||||
|
||||
If you appreciate my libraries, you can support the development and maintenance.
|
||||
Improve the quality of the libraries by providing issues and Pull Requests, or
|
||||
donate through PayPal or GitHub sponsors.
|
||||
|
||||
Thank you,
|
||||
|
||||
|
@ -49,7 +49,7 @@
|
||||
|
||||
const uint8_t KEYPAD_ADDRESS = 0x20;
|
||||
I2CKeyPad keyPad(KEYPAD_ADDRESS);
|
||||
char keys[] = "123A456B789C*0#DNF"; // N = NoKey, F = Fail (e.g. >1 keys pressed)
|
||||
char keys[] = "123A456B789C*0#DNF"; // N = NoKey, F = Fail (e.g. > 1 keys pressed)
|
||||
|
||||
// volatile for IRQ var
|
||||
volatile bool keyChange = false;
|
||||
@ -72,8 +72,8 @@ void setup()
|
||||
attachInterrupt(1, keyChanged, FALLING);
|
||||
keyChange = false;
|
||||
|
||||
|
||||
Wire.begin();
|
||||
Wire.setClock(100000);
|
||||
if (keyPad.begin() == false)
|
||||
{
|
||||
Serial.println("\nERROR: cannot communicate to keypad.\nPlease reboot.\n");
|
||||
@ -81,8 +81,6 @@ void setup()
|
||||
}
|
||||
|
||||
measurePolling();
|
||||
|
||||
Wire.setClock(100000);
|
||||
}
|
||||
|
||||
|
||||
|
@ -6,7 +6,6 @@
|
||||
//
|
||||
// PCF8574
|
||||
// pin p0-p7 rows
|
||||
//
|
||||
|
||||
|
||||
#include "Wire.h"
|
||||
|
@ -0,0 +1,28 @@
|
||||
platforms:
|
||||
rpipico:
|
||||
board: rp2040:rp2040:rpipico
|
||||
package: rp2040:rp2040
|
||||
gcc:
|
||||
features:
|
||||
defines:
|
||||
- ARDUINO_ARCH_RP2040
|
||||
warnings:
|
||||
flags:
|
||||
|
||||
packages:
|
||||
rp2040:rp2040:
|
||||
url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
|
||||
|
||||
compile:
|
||||
# Choosing to run compilation tests on 2 different Arduino platforms
|
||||
platforms:
|
||||
# - uno
|
||||
# - due
|
||||
# - zero
|
||||
# - leonardo
|
||||
# - m4
|
||||
- esp32
|
||||
# - esp8266
|
||||
# - mega2560
|
||||
# - rpipico
|
||||
|
@ -0,0 +1,111 @@
|
||||
//
|
||||
// FILE: I2Ckeypad_Wire1_ESP32.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// PURPOSE: demo reading a larger value from the keyPad
|
||||
// URL: https://github.com/RobTillaart/I2CKeyPad
|
||||
//
|
||||
// PCF8574
|
||||
// pin p0-p3 rows
|
||||
// pin p4-p7 columns
|
||||
// 4x4 or smaller keypad.
|
||||
|
||||
|
||||
#include "Wire.h"
|
||||
#include "I2CKeyPad.h"
|
||||
|
||||
const uint8_t KEYPAD_ADDRESS = 0x38;
|
||||
|
||||
I2CKeyPad keyPad(KEYPAD_ADDRESS, &Wire1);
|
||||
|
||||
uint32_t start, stop;
|
||||
uint32_t lastKeyPressed = 0;
|
||||
uint32_t value = 0;
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println(__FILE__);
|
||||
|
||||
Wire1.begin(22, 23); // adjust pins if needed
|
||||
Wire1.setClock(400000);
|
||||
if (keyPad.begin() == false)
|
||||
{
|
||||
Serial.println("\nERROR: cannot communicate to keypad.\nPlease reboot.\n");
|
||||
while(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
uint32_t now = millis();
|
||||
|
||||
if (now - lastKeyPressed >= 100)
|
||||
{
|
||||
lastKeyPressed = now;
|
||||
|
||||
start = micros();
|
||||
char c = handleKeyPadValue(value);
|
||||
if (value > 125000) value = 125000; // some sample max.
|
||||
stop = micros();
|
||||
Serial.print(millis());
|
||||
Serial.print("\t");
|
||||
Serial.print(value);
|
||||
Serial.print("\t");
|
||||
Serial.print((char) c);
|
||||
Serial.print("\t");
|
||||
Serial.println(stop - start);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// handleKeyPadValue is used to read a uint32_t from the keypad
|
||||
// every digit must be pressed separately
|
||||
// the last key pressed is also returned.
|
||||
//
|
||||
// 0..9 adds to the number
|
||||
// * means remove last digit
|
||||
// # resets to zero
|
||||
// ABCD are not mapped.
|
||||
char handleKeyPadValue(uint32_t &value)
|
||||
{
|
||||
char v[19] = "123A456B789C*0#DNF"; // N = NoKey, F = Fail
|
||||
static uint8_t lastKey = 0;
|
||||
|
||||
uint8_t idx = keyPad.getKey();
|
||||
char c = v[idx];
|
||||
|
||||
if (lastKey != c)
|
||||
{
|
||||
lastKey = c;
|
||||
switch (c)
|
||||
{
|
||||
case '0' ... '9':
|
||||
value *= 10;
|
||||
value += c - '0';
|
||||
break;
|
||||
case '*':
|
||||
if (value > 0) value /= 10;
|
||||
break;
|
||||
case '#':
|
||||
value = 0;
|
||||
break;
|
||||
case 'A' ... 'D':
|
||||
// e.g. store value in EEPROM
|
||||
break;
|
||||
case 'F':
|
||||
Serial.println("FAIL");
|
||||
break;
|
||||
case 'N':
|
||||
Serial.println("NOKEY");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
@ -10,7 +10,6 @@
|
||||
// 4x4 or smaller keypad.
|
||||
//
|
||||
// This demo doesn't use the build in key mapping.
|
||||
//
|
||||
|
||||
|
||||
#include "Wire.h"
|
||||
@ -67,7 +66,6 @@ void loop()
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// until = end character
|
||||
// buffer = buffer to fill
|
||||
// length = length of buffer (including '\0'
|
||||
|
@ -15,9 +15,9 @@
|
||||
"type": "git",
|
||||
"url": "https://github.com/RobTillaart/I2CKeyPad.git"
|
||||
},
|
||||
"version": "0.3.3",
|
||||
"version": "0.4.0",
|
||||
"license": "MIT",
|
||||
"frameworks": "arduino",
|
||||
"frameworks": "*",
|
||||
"platforms": "*",
|
||||
"headers": "I2CKeyPad.h"
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=I2CKeyPad
|
||||
version=0.3.3
|
||||
version=0.4.0
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=Arduino library for a KeyPad connected to a PCF8574.
|
||||
|
Loading…
x
Reference in New Issue
Block a user