Skip to content

Commit

Permalink
simplify begin() (#18)
Browse files Browse the repository at this point in the history
- simplify begin()
- added I2Ckeypad_Wire1_ESP32.ino
- update readme.md
- minor edits
  • Loading branch information
RobTillaart authored Nov 11, 2023
1 parent e139ca4 commit 999ec79
Show file tree
Hide file tree
Showing 20 changed files with 273 additions and 115 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 10 additions & 24 deletions I2CKeyPad .cpp
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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();
Expand Down Expand Up @@ -145,9 +131,9 @@ uint8_t I2CKeyPad::_getKey4x4()
else if (rows == 0x70) key = 3;
else return I2C_KEYPAD_FAIL;

// 4 columns as input pull up, 4 rows as output
// 4 columns as input pull up, 4 rows as output
uint8_t cols = _read(0x0F);
// check if single line has gone low.
// check if single line has gone low.
if (cols == 0x0F) return I2C_KEYPAD_NOKEY;
else if (cols == 0x0E) key += 0;
else if (cols == 0x0D) key += 4;
Expand All @@ -157,7 +143,7 @@ uint8_t I2CKeyPad::_getKey4x4()

_lastKey = key;

return key; // 0..15
return key; // 0..15
}


Expand Down Expand Up @@ -189,7 +175,7 @@ uint8_t I2CKeyPad::_getKey5x3()

_lastKey = key;

return key; // 0..14
return key; // 0..14
}


Expand All @@ -211,17 +197,17 @@ uint8_t I2CKeyPad::_getKey6x2()
else if (rows == 0x7C) key = 5;
else return I2C_KEYPAD_FAIL;

// 2 columns as input pull up, 6 rows as output
// 2 columns as input pull up, 6 rows as output
uint8_t cols = _read(0x03);
// check if single line has gone low.
// check if single line has gone low.
if (cols == 0x03) return I2C_KEYPAD_NOKEY;
else if (cols == 0x02) key += 0;
else if (cols == 0x01) key += 6;
else return I2C_KEYPAD_FAIL;

_lastKey = key;

return key; // 0..11
return key; // 0..11
}


Expand All @@ -247,9 +233,9 @@ uint8_t I2CKeyPad::_getKey8x1()

_lastKey = key;

return key; // 0..7
return key; // 0..7
}


// -- END OF FILE --
// -- END OF FILE --

10 changes: 4 additions & 6 deletions I2CKeyPad.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -69,5 +67,5 @@ class I2CKeyPad
};


// -- END OF FILE --
// -- END OF FILE --

41 changes: 37 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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,

20 changes: 9 additions & 11 deletions examples/I2CKeypad_interrupts_1/I2CKeypad_interrupts_1.ino
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// IRQ pin 2
// SDA A4
// SCL A5
// 4x4 or smaller keypad.
// 4x4 or smaller keypad.


// notes
Expand All @@ -36,7 +36,7 @@
// however it is more efficient to reset the flag only after the
// keypress is handled.
//
// Note: multiple keypresses are not queued.
// Note: multiple keypresses are not queued.


////////////////////////////////////////////////////////////////////////
Expand All @@ -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;
Expand All @@ -67,22 +67,20 @@ void setup()
Serial.println(__FILE__);


// NOTE: PCF8574 will generate an interrupt on key press and release.
// NOTE: PCF8574 will generate an interrupt on key press and release.
pinMode(3, INPUT_PULLUP);
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");
while (1);
}

measurePolling();

Wire.setClock(100000);
}


Expand All @@ -91,7 +89,7 @@ void loop()
if (keyChange)
{
uint8_t index = keyPad.getKey();
// only after keyChange is handled it is time reset the flag
// only after keyChange is handled it is time reset the flag
keyChange = false;
if (index != 16)
{
Expand All @@ -109,7 +107,7 @@ void loop()

void measurePolling()
{
// measure time to check isPressed() by polling.
// measure time to check isPressed() by polling.

// CLOCK TIME (us)
// ---------------------
Expand All @@ -126,7 +124,7 @@ void measurePolling()
Wire.setClock(clock);
for (int i = 0; i < 1; i++)
{
// reference time for keyPressed check UNO ~
// reference time for keyPressed check UNO ~
uint32_t start = micros();
uint8_t index = keyPad.isPressed();
uint32_t stop = micros();
Expand All @@ -144,5 +142,5 @@ void measurePolling()
}


// -- END OF FILE --
// -- END OF FILE --

4 changes: 2 additions & 2 deletions examples/I2Ckeypad_5x3/I2Ckeypad_5x3.ino
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void loop()
{
uint32_t now = millis();
// adjust keymap if needed
char keys[] = "1234567890ABCDE NF"; // N = NoKey, F = Fail
char keys[] = "1234567890ABCDE NF"; // N = NoKey, F = Fail

if (now - lastKeyPressed >= 100)
{
Expand All @@ -62,4 +62,4 @@ void loop()
}


// -- END OF FILE --
// -- END OF FILE --
4 changes: 2 additions & 2 deletions examples/I2Ckeypad_6x2/I2Ckeypad_6x2.ino
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void loop()
{
uint32_t now = millis();
// adjust keymap if needed
char keys[] = "1234567890ABC NF"; // N = NoKey, F = Fail
char keys[] = "1234567890ABC NF"; // N = NoKey, F = Fail

if (now - lastKeyPressed >= 100)
{
Expand All @@ -62,4 +62,4 @@ void loop()
}


// -- END OF FILE --
// -- END OF FILE --
5 changes: 2 additions & 3 deletions examples/I2Ckeypad_8x1/I2Ckeypad_8x1.ino
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//
// PCF8574
// pin p0-p7 rows
//


#include "Wire.h"
Expand Down Expand Up @@ -41,7 +40,7 @@ void loop()
{
uint32_t now = millis();
// adjust keymap if needed
char keys[] = "12345678 NF"; // N = NoKey, F = Fail
char keys[] = "12345678 NF"; // N = NoKey, F = Fail

if (now - lastKeyPressed >= 100)
{
Expand All @@ -62,4 +61,4 @@ void loop()
}


// -- END OF FILE --
// -- END OF FILE --
28 changes: 28 additions & 0 deletions examples/I2Ckeypad_Wire1_ESP32/.arduino-ci.yml
Original file line number Diff line number Diff line change
@@ -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

Loading

0 comments on commit 999ec79

Please sign in to comment.