mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-09-19 16:46:11 -04:00
0.3.1 I2CKeyPad
This commit is contained in:
parent
0fe393948c
commit
193621bc6a
@ -1,7 +1,7 @@
|
||||
//
|
||||
// FILE: I2CKeyPad.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.3.0
|
||||
// VERSION: 0.3.1
|
||||
// PURPOSE: Arduino library for 4x4 KeyPad connected to an I2C PCF8574
|
||||
// URL: https://github.com/RobTillaart/I2CKeyPad
|
||||
//
|
||||
@ -15,6 +15,7 @@
|
||||
// 0.2.1 2021-05-06 add _read(0xF0) to begin() to enable PCF8574
|
||||
// interrupts. (#5 thanks to JohnMac1234)
|
||||
// 0.3.0 2021-11-04 add key mapping functions.
|
||||
// 0.3.1 2021-12-19 update library.json, license, minor edits
|
||||
|
||||
|
||||
#include "I2CKeyPad.h"
|
||||
@ -32,7 +33,8 @@ I2CKeyPad::I2CKeyPad(const uint8_t deviceAddress, TwoWire *wire)
|
||||
bool I2CKeyPad::begin(uint8_t sda, uint8_t scl)
|
||||
{
|
||||
_wire->begin(sda, scl);
|
||||
_read(0xF0); // enable interrupts
|
||||
// enable interrupts
|
||||
_read(0xF0);
|
||||
return isConnected();
|
||||
}
|
||||
#endif
|
||||
@ -41,7 +43,7 @@ bool I2CKeyPad::begin(uint8_t sda, uint8_t scl)
|
||||
bool I2CKeyPad::begin()
|
||||
{
|
||||
_wire->begin();
|
||||
// enable interrupts
|
||||
// enable interrupts
|
||||
_read(0xF0);
|
||||
return isConnected();
|
||||
}
|
||||
@ -53,7 +55,7 @@ uint8_t I2CKeyPad::getKey()
|
||||
}
|
||||
|
||||
|
||||
// to check "press any key"
|
||||
// to check "press any key"
|
||||
bool I2CKeyPad::isPressed()
|
||||
{
|
||||
uint8_t a = _read(0xF0);
|
||||
@ -81,13 +83,14 @@ void I2CKeyPad::loadKeyMap(char * keyMap)
|
||||
//
|
||||
uint8_t I2CKeyPad::_read(uint8_t mask)
|
||||
{
|
||||
yield(); // improve the odds that IO will not interrupted.
|
||||
// improve the odds that IO will not interrupted.
|
||||
yield();
|
||||
|
||||
_wire->beginTransmission(_address);
|
||||
_wire->write(mask);
|
||||
if (_wire->endTransmission() != 0)
|
||||
{
|
||||
// set communication error
|
||||
// set communication error
|
||||
return 0xFF;
|
||||
}
|
||||
_wire->requestFrom(_address, (uint8_t)1);
|
||||
@ -97,12 +100,12 @@ uint8_t I2CKeyPad::_read(uint8_t mask)
|
||||
|
||||
uint8_t I2CKeyPad::_getKey4x4()
|
||||
{
|
||||
// key = row + 4 x col
|
||||
// key = row + 4 x col
|
||||
uint8_t key = 0;
|
||||
|
||||
// mask = 4 rows as input pull up, 4 columns as output
|
||||
// mask = 4 rows as input pull up, 4 columns as output
|
||||
uint8_t rows = _read(0xF0);
|
||||
// check if single line has gone low.
|
||||
// check if single line has gone low.
|
||||
if (rows == 0xF0) return I2C_KEYPAD_NOKEY;
|
||||
else if (rows == 0xE0) key = 0;
|
||||
else if (rows == 0xD0) key = 1;
|
||||
|
@ -2,7 +2,7 @@
|
||||
//
|
||||
// FILE: I2CKeyPad.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.3.0
|
||||
// VERSION: 0.3.1
|
||||
// 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.0"))
|
||||
#define I2C_KEYPAD_LIB_VERSION (F("0.3.1"))
|
||||
|
||||
#define I2C_KEYPAD_NOKEY 16
|
||||
#define I2C_KEYPAD_FAIL 17
|
||||
@ -27,19 +27,20 @@ public:
|
||||
#endif
|
||||
bool begin();
|
||||
|
||||
// get raw key's 0..15
|
||||
// get raw key's 0..15
|
||||
uint8_t getKey();
|
||||
uint8_t getLastKey() { return _lastKey; };
|
||||
|
||||
bool isPressed();
|
||||
bool isConnected();
|
||||
|
||||
// get 'translated' keys
|
||||
// user must load KeyMap self, there is no check.
|
||||
// get 'translated' keys
|
||||
// user must load KeyMap self, there is no check.
|
||||
uint8_t getChar() { return _keyMap[getKey()]; };
|
||||
uint8_t getLastChar() { return _keyMap[_lastKey]; };
|
||||
void loadKeyMap(char * keyMap); // char[19]
|
||||
|
||||
|
||||
protected:
|
||||
uint8_t _address;
|
||||
uint8_t _lastKey;
|
||||
|
@ -1,6 +1,6 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2019-2021 Rob Tillaart
|
||||
Copyright (c) 2019-2022 Rob Tillaart
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
@ -8,7 +8,7 @@
|
||||
|
||||
# I2CKeyPad
|
||||
|
||||
Arduino library for 4x4 KeyPad connected to an I2C PCF8574
|
||||
Arduino library for 4x4 KeyPad connected to an I2C PCF8574.
|
||||
|
||||
|
||||
## Description
|
||||
|
@ -1,17 +1,12 @@
|
||||
//
|
||||
// FILE: I2CKeypad_interrupts_1.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// PURPOSE: demo interrupt checking keyPressed
|
||||
// URL: https://github.com/RobTillaart/I2CKeyPad
|
||||
//
|
||||
// HISTORY:
|
||||
// 2021-05-08 0.1.0 initial version
|
||||
|
||||
// interrupts are supported since version 0.2.1
|
||||
// this sketch show usage and some comparison with polling.
|
||||
|
||||
|
||||
//
|
||||
// KEYPAD PCF8574 UNO
|
||||
// rows p0-p3
|
||||
// columns p4-p7
|
||||
@ -38,7 +33,7 @@
|
||||
// Note: depending on keypad used some bouncing may occur
|
||||
// (saw it only during release)
|
||||
// can be solved by tracking the last interrupt in the ISR routine
|
||||
// however it is more efficient to reset the flag only after the
|
||||
// however it is more efficient to reset the flag only after the
|
||||
// keypress is handled.
|
||||
//
|
||||
// Note: multiple keypresses are not queued.
|
||||
|
@ -1,16 +1,9 @@
|
||||
//
|
||||
// FILE: I2Ckeypad_demo01.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.2.0
|
||||
// PURPOSE: demo
|
||||
// URL: https://github.com/RobTillaart/I2CKeyPad
|
||||
//
|
||||
// HISTORY:
|
||||
// 2019-10-01 0.0.1 initial version
|
||||
// 2020-06-26 0.1.0 updated with lib
|
||||
// 2021-05-06 0.2.0 updated with lib
|
||||
|
||||
|
||||
// PCF8574
|
||||
// pin p0-p3 rows
|
||||
// pin p4-p7 columns
|
||||
|
@ -1,16 +1,9 @@
|
||||
//
|
||||
// FILE: I2Ckeypad_demo02.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.2.0
|
||||
// PURPOSE: demo
|
||||
// URL: https://github.com/RobTillaart/I2CKeyPad
|
||||
//
|
||||
// HISTORY:
|
||||
// 2019-10-01 0.0.1 initial version
|
||||
// 2020-06-26 0.1.0 updated with lib
|
||||
// 2021-05-06 0.2.0 updated with lib
|
||||
|
||||
|
||||
// PCF8574
|
||||
// pin p0-p3 rows
|
||||
// pin p4-p7 columns
|
||||
@ -54,7 +47,7 @@ void loop()
|
||||
bool connected = keyPad.isConnected();
|
||||
bool pressed = keyPad.isPressed();
|
||||
uint8_t lastKey = keyPad.getLastKey();
|
||||
|
||||
|
||||
start = micros();
|
||||
uint8_t index = keyPad.getKey();
|
||||
stop = micros();
|
||||
@ -74,5 +67,6 @@ void loop()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -1,16 +1,9 @@
|
||||
//
|
||||
// FILE: I2CKeyPad_demo03.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.2.0
|
||||
// PURPOSE: demo reading a larger value from the keyPad
|
||||
// URL: https://github.com/RobTillaart/I2CKeyPad
|
||||
//
|
||||
// HISTORY:
|
||||
// 2019-10-01 0.0.1 initial version
|
||||
// 2020-06-26 0.1.0 updated with lib
|
||||
// 2021-05-06 0.2.0 updated with lib
|
||||
|
||||
|
||||
// PCF8574
|
||||
// pin p0-p3 rows
|
||||
// pin p4-p7 columns
|
||||
@ -28,6 +21,7 @@ uint32_t start, stop;
|
||||
uint32_t lastKeyPressed = 0;
|
||||
uint32_t value = 0;
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
@ -1,11 +1,9 @@
|
||||
//
|
||||
// FILE: I2Ckeypad_keymap.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// PURPOSE: demo key mapping
|
||||
// URL: https://github.com/RobTillaart/I2CKeyPad
|
||||
|
||||
|
||||
//
|
||||
// PCF8574
|
||||
// pin p0-p3 rows
|
||||
// pin p4-p7 columns
|
||||
@ -54,3 +52,4 @@ void loop()
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -1,17 +1,15 @@
|
||||
//
|
||||
// FILE: I2Ckeypad_keymap_calculator.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// PURPOSE: demo key mapping
|
||||
// URL: https://github.com/RobTillaart/I2CKeyPad
|
||||
|
||||
|
||||
//
|
||||
// PCF8574
|
||||
// pin p0-p3 rows
|
||||
// pin p4-p7 columns
|
||||
// 4x4 or smaller keypad.
|
||||
//
|
||||
// to elaborate
|
||||
|
||||
|
||||
#include "Wire.h"
|
||||
#include "I2CKeyPad.h"
|
||||
@ -76,3 +74,4 @@ void loop()
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -1,18 +1,15 @@
|
||||
//
|
||||
// FILE: I2Ckeypad_keymap_demo2.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// PURPOSE: demo key mapping
|
||||
// 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"
|
||||
|
||||
@ -64,3 +61,4 @@ void loop()
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -1,11 +1,9 @@
|
||||
//
|
||||
// FILE: I2Ckeypad_readKeyUntil.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// PURPOSE: demo reading until specific keyPress
|
||||
// URL: https://github.com/RobTillaart/I2CKeyPad
|
||||
//
|
||||
|
||||
// PCF8574
|
||||
// pin p0-p3 rows
|
||||
// pin p4-p7 columns
|
||||
@ -14,6 +12,7 @@
|
||||
// This demo doesn't use the build in key mapping.
|
||||
//
|
||||
|
||||
|
||||
#include "Wire.h"
|
||||
#include "I2CKeyPad.h"
|
||||
|
||||
@ -118,3 +117,4 @@ int readKeyPadUntil(char until, char * buffer, uint8_t length, uint16_t timeout)
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -1,11 +1,9 @@
|
||||
//
|
||||
// FILE: I2Ckeypad_readKeyUntil_KM.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// PURPOSE: demo reading until specific keyPress - using build in key mapping
|
||||
// URL: https://github.com/RobTillaart/I2CKeyPad
|
||||
//
|
||||
|
||||
// PCF8574
|
||||
// pin p0-p3 rows
|
||||
// pin p4-p7 columns
|
||||
@ -38,7 +36,6 @@ void setup()
|
||||
Serial.println("\nERROR: cannot communicate to keypad.\nPlease reboot.\n");
|
||||
while (1);
|
||||
}
|
||||
|
||||
keyPad.loadKeyMap(keymap);
|
||||
}
|
||||
|
||||
@ -113,3 +110,4 @@ int readKeyPadUntil(char until, char * buffer, uint8_t length, uint16_t timeout)
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -15,8 +15,9 @@
|
||||
"type": "git",
|
||||
"url": "https://github.com/RobTillaart/I2CKeyPad.git"
|
||||
},
|
||||
"version": "0.3.0",
|
||||
"version": "0.3.1",
|
||||
"license": "MIT",
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*"
|
||||
"platforms": "*",
|
||||
"headers": "I2CKeyPad.h"
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=I2CKeyPad
|
||||
version=0.3.0
|
||||
version=0.3.1
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=Arduino library for 4x4 KeyPad connected to a PCF8574
|
||||
|
@ -30,6 +30,7 @@
|
||||
|
||||
unittest_setup()
|
||||
{
|
||||
fprintf(stderr, "I2C_KEYPAD_LIB_VERSION: %s\n", (char *) I2C_KEYPAD_LIB_VERSION);
|
||||
}
|
||||
|
||||
|
||||
@ -38,12 +39,16 @@ unittest_teardown()
|
||||
}
|
||||
|
||||
|
||||
unittest(test_constants)
|
||||
{
|
||||
assertEqual(16, I2C_KEYPAD_NOKEY);
|
||||
assertEqual(17, I2C_KEYPAD_FAIL);
|
||||
}
|
||||
|
||||
|
||||
|
||||
unittest(test_constructor)
|
||||
{
|
||||
fprintf(stderr, "VERSION: %s\n", (char *) I2C_KEYPAD_LIB_VERSION);
|
||||
|
||||
const uint8_t KEYPAD_ADDRESS = 0x38;
|
||||
I2CKeyPad keyPad(KEYPAD_ADDRESS);
|
||||
|
||||
@ -58,8 +63,6 @@ unittest(test_constructor)
|
||||
//
|
||||
// unittest(test_read)
|
||||
// {
|
||||
// fprintf(stderr, "VERSION: %s\n", (char *) I2C_KEYPAD_LIB_VERSION);
|
||||
|
||||
// const uint8_t KEYPAD_ADDRESS = 0x38;
|
||||
// I2CKeyPad keyPad(KEYPAD_ADDRESS);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user