Skip to content

Commit 3f6ab4d

Browse files
committed
0.1.0 I2CKeyPad8x8
1 parent 6c77f95 commit 3f6ab4d

File tree

16 files changed

+621
-0
lines changed

16 files changed

+621
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
compile:
2+
# Choosing to run compilation tests on 2 different Arduino platforms
3+
platforms:
4+
- uno
5+
# - due
6+
# - zero
7+
# - leonardo
8+
- m4
9+
- esp32
10+
# - esp8266
11+
# - mega2560
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# These are supported funding model platforms
2+
3+
github: RobTillaart
4+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
name: Arduino-lint
3+
4+
on: [push, pull_request]
5+
jobs:
6+
lint:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v2
10+
- uses: arduino/arduino-lint-action@v1
11+
with:
12+
library-manager: update
13+
compliance: strict
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
name: Arduino CI
3+
4+
on: [push, pull_request]
5+
6+
jobs:
7+
runTest:
8+
runs-on: ubuntu-latest
9+
10+
steps:
11+
- uses: actions/checkout@v2
12+
- uses: ruby/setup-ruby@v1
13+
with:
14+
ruby-version: 2.6
15+
- run: |
16+
gem install arduino_ci
17+
arduino_ci.rb
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: JSON check
2+
3+
on:
4+
push:
5+
paths:
6+
- '**.json'
7+
pull_request:
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v2
14+
- name: json-syntax-check
15+
uses: limitusus/json-syntax-check@v1
16+
with:
17+
pattern: "\\.json$"
18+
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
//
2+
// FILE: I2CKeyPad8x8.cpp
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.1.0
5+
// PURPOSE: Arduino library for 8x8 or smaller KeyPad connected to an I2C PCF8575.
6+
// URL: https://github.com/RobTillaart/I2CKeyPad8x8
7+
//
8+
// HISTORY:
9+
// 0.1.0 2022-09-29 initial version
10+
11+
12+
13+
#include "I2CKeyPad8x8.h"
14+
15+
16+
I2CKeyPad8x8::I2CKeyPad8x8(const uint8_t deviceAddress, TwoWire *wire)
17+
{
18+
_lastKey = I2C_KEYPAD8x8_NOKEY;
19+
_address = deviceAddress;
20+
_wire = wire;
21+
}
22+
23+
24+
#if defined(ESP8266) || defined(ESP32)
25+
bool I2CKeyPad8x8::begin(uint8_t sda, uint8_t scl)
26+
{
27+
_wire->begin(sda, scl);
28+
// enable interrupts
29+
_read(0xFF00);
30+
return isConnected();
31+
}
32+
#endif
33+
34+
35+
bool I2CKeyPad8x8::begin()
36+
{
37+
_wire->begin();
38+
// enable interrupts
39+
_read(0xFF00);
40+
return isConnected();
41+
}
42+
43+
44+
uint8_t I2CKeyPad8x8::getKey()
45+
{
46+
// key = row + 8 x col
47+
uint8_t key = 0;
48+
49+
// mask = 8 rows as input pull up, 8 columns as output
50+
uint16_t rows = _read(0xFF00);
51+
52+
// check if single line has gone low.
53+
if (rows == 0xFF00) return I2C_KEYPAD8x8_NOKEY;
54+
else if (rows == 0xFE00) key = 0;
55+
else if (rows == 0xFD00) key = 1;
56+
else if (rows == 0xFB00) key = 2;
57+
else if (rows == 0xF700) key = 3;
58+
else if (rows == 0xEF00) key = 4;
59+
else if (rows == 0xDF00) key = 5;
60+
else if (rows == 0xBF00) key = 6;
61+
else if (rows == 0x7F00) key = 7;
62+
else return I2C_KEYPAD8x8_FAIL;
63+
64+
// 8 columns as input pull up, 8 rows as output
65+
uint16_t cols = _read(0x00FF);
66+
// check if single line has gone low.
67+
if (cols == 0x00FF) return I2C_KEYPAD8x8_NOKEY;
68+
else if (cols == 0x00FE) key += 0;
69+
else if (cols == 0x00FD) key += 8;
70+
else if (cols == 0x00FB) key += 16;
71+
else if (cols == 0x00F7) key += 24;
72+
else if (cols == 0x00EF) key += 32;
73+
else if (cols == 0x00DF) key += 40;
74+
else if (cols == 0x00BF) key += 48;
75+
else if (cols == 0x007F) key += 56;
76+
else return I2C_KEYPAD8x8_FAIL;
77+
78+
_lastKey = key;
79+
80+
return key; // 0..65
81+
}
82+
83+
84+
uint8_t I2CKeyPad8x8::getLastKey()
85+
{
86+
return _lastKey;
87+
};
88+
89+
90+
// to check "press any key"
91+
bool I2CKeyPad8x8::isPressed()
92+
{
93+
uint16_t a = _read(0xFF00);
94+
if (a == 0xFF00) return false;
95+
return (a != 0xFF00);
96+
}
97+
98+
99+
bool I2CKeyPad8x8::isConnected()
100+
{
101+
_wire->beginTransmission(_address);
102+
return (_wire->endTransmission() == 0);
103+
}
104+
105+
106+
uint8_t I2CKeyPad8x8::getChar()
107+
{
108+
return _keyMap[getKey()];
109+
};
110+
111+
112+
uint8_t I2CKeyPad8x8::getLastChar()
113+
{
114+
return _keyMap[_lastKey];
115+
};
116+
117+
118+
void I2CKeyPad8x8::loadKeyMap(char * keyMap)
119+
{
120+
_keyMap = keyMap;
121+
}
122+
123+
124+
//////////////////////////////////////////////////////
125+
//
126+
// PROTECTED
127+
//
128+
uint16_t I2CKeyPad8x8::_read(uint16_t mask)
129+
{
130+
// improve the odds that IO will not interrupted.
131+
yield();
132+
133+
_wire->beginTransmission(_address);
134+
_wire->write(mask >> 8);
135+
_wire->write(mask & 0xFF);
136+
if (_wire->endTransmission() != 0)
137+
{
138+
// set communication error
139+
return 0xFFFF;
140+
}
141+
_wire->requestFrom(_address, (uint8_t)2);
142+
uint16_t value = _wire->read() << 8;
143+
value += _wire->read();
144+
return value;
145+
}
146+
147+
148+
// -- END OF FILE --
149+

libraries/I2CKeyPad8x8/I2CKeyPad8x8.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#pragma once
2+
//
3+
// FILE: I2CKeyPad8x8.h
4+
// AUTHOR: Rob Tillaart
5+
// VERSION: 0.1.0
6+
// PURPOSE: Arduino library for 8x8 or smaller KeyPad connected to an I2C PCF8575.
7+
// URL: https://github.com/RobTillaart/I2CKeyPad
8+
9+
10+
#include "Arduino.h"
11+
#include "Wire.h"
12+
13+
14+
#define I2C_KEYPAD8x8_LIB_VERSION (F("0.1.0"))
15+
16+
#define I2C_KEYPAD8x8_NOKEY 64
17+
#define I2C_KEYPAD8x8_FAIL 65
18+
19+
20+
class I2CKeyPad8x8
21+
{
22+
public:
23+
I2CKeyPad8x8(const uint8_t deviceAddress, TwoWire *wire = &Wire);
24+
25+
#if defined(ESP8266) || defined(ESP32)
26+
bool begin(uint8_t sda, uint8_t scl);
27+
#endif
28+
bool begin();
29+
30+
// get raw key's 0..65
31+
uint8_t getKey();
32+
uint8_t getLastKey();
33+
34+
bool isPressed();
35+
bool isConnected();
36+
37+
// get 'translated' keys
38+
// user must load KeyMap, there is no check.
39+
uint8_t getChar();
40+
uint8_t getLastChar();
41+
void loadKeyMap(char * keyMap); // char[65]
42+
43+
44+
protected:
45+
uint8_t _address;
46+
uint8_t _lastKey;
47+
uint16_t _read(uint16_t mask);
48+
49+
TwoWire* _wire;
50+
51+
char * _keyMap = NULL;
52+
};
53+
54+
55+
// -- END OF FILE --
56+

libraries/I2CKeyPad8x8/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Rob Tillaart
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

0 commit comments

Comments
 (0)