Skip to content

Commit

Permalink
0.1.0 I2CKeyPad8x8
Browse files Browse the repository at this point in the history
  • Loading branch information
RobTillaart committed Sep 29, 2022
1 parent 6c77f95 commit 3f6ab4d
Show file tree
Hide file tree
Showing 16 changed files with 621 additions and 0 deletions.
11 changes: 11 additions & 0 deletions libraries/I2CKeyPad8x8/.arduino-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
compile:
# Choosing to run compilation tests on 2 different Arduino platforms
platforms:
- uno
# - due
# - zero
# - leonardo
- m4
- esp32
# - esp8266
# - mega2560
4 changes: 4 additions & 0 deletions libraries/I2CKeyPad8x8/.github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# These are supported funding model platforms

github: RobTillaart

13 changes: 13 additions & 0 deletions libraries/I2CKeyPad8x8/.github/workflows/arduino-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

name: Arduino-lint

on: [push, pull_request]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: arduino/arduino-lint-action@v1
with:
library-manager: update
compliance: strict
17 changes: 17 additions & 0 deletions libraries/I2CKeyPad8x8/.github/workflows/arduino_test_runner.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
name: Arduino CI

on: [push, pull_request]

jobs:
runTest:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: ruby/setup-ruby@v1
with:
ruby-version: 2.6
- run: |
gem install arduino_ci
arduino_ci.rb
18 changes: 18 additions & 0 deletions libraries/I2CKeyPad8x8/.github/workflows/jsoncheck.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: JSON check

on:
push:
paths:
- '**.json'
pull_request:

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: json-syntax-check
uses: limitusus/json-syntax-check@v1
with:
pattern: "\\.json$"

149 changes: 149 additions & 0 deletions libraries/I2CKeyPad8x8/I2CKeyPad8x8.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
//
// FILE: I2CKeyPad8x8.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: Arduino library for 8x8 or smaller KeyPad connected to an I2C PCF8575.
// URL: https://github.com/RobTillaart/I2CKeyPad8x8
//
// HISTORY:
// 0.1.0 2022-09-29 initial version



#include "I2CKeyPad8x8.h"


I2CKeyPad8x8::I2CKeyPad8x8(const uint8_t deviceAddress, TwoWire *wire)
{
_lastKey = I2C_KEYPAD8x8_NOKEY;
_address = deviceAddress;
_wire = wire;
}


#if defined(ESP8266) || defined(ESP32)
bool I2CKeyPad8x8::begin(uint8_t sda, uint8_t scl)
{
_wire->begin(sda, scl);
// enable interrupts
_read(0xFF00);
return isConnected();
}
#endif


bool I2CKeyPad8x8::begin()
{
_wire->begin();
// enable interrupts
_read(0xFF00);
return isConnected();
}


uint8_t I2CKeyPad8x8::getKey()
{
// key = row + 8 x col
uint8_t key = 0;

// mask = 8 rows as input pull up, 8 columns as output
uint16_t rows = _read(0xFF00);

// check if single line has gone low.
if (rows == 0xFF00) return I2C_KEYPAD8x8_NOKEY;
else if (rows == 0xFE00) key = 0;
else if (rows == 0xFD00) key = 1;
else if (rows == 0xFB00) key = 2;
else if (rows == 0xF700) key = 3;
else if (rows == 0xEF00) key = 4;
else if (rows == 0xDF00) key = 5;
else if (rows == 0xBF00) key = 6;
else if (rows == 0x7F00) key = 7;
else return I2C_KEYPAD8x8_FAIL;

// 8 columns as input pull up, 8 rows as output
uint16_t cols = _read(0x00FF);
// check if single line has gone low.
if (cols == 0x00FF) return I2C_KEYPAD8x8_NOKEY;
else if (cols == 0x00FE) key += 0;
else if (cols == 0x00FD) key += 8;
else if (cols == 0x00FB) key += 16;
else if (cols == 0x00F7) key += 24;
else if (cols == 0x00EF) key += 32;
else if (cols == 0x00DF) key += 40;
else if (cols == 0x00BF) key += 48;
else if (cols == 0x007F) key += 56;
else return I2C_KEYPAD8x8_FAIL;

_lastKey = key;

return key; // 0..65
}


uint8_t I2CKeyPad8x8::getLastKey()
{
return _lastKey;
};


// to check "press any key"
bool I2CKeyPad8x8::isPressed()
{
uint16_t a = _read(0xFF00);
if (a == 0xFF00) return false;
return (a != 0xFF00);
}


bool I2CKeyPad8x8::isConnected()
{
_wire->beginTransmission(_address);
return (_wire->endTransmission() == 0);
}


uint8_t I2CKeyPad8x8::getChar()
{
return _keyMap[getKey()];
};


uint8_t I2CKeyPad8x8::getLastChar()
{
return _keyMap[_lastKey];
};


void I2CKeyPad8x8::loadKeyMap(char * keyMap)
{
_keyMap = keyMap;
}


//////////////////////////////////////////////////////
//
// PROTECTED
//
uint16_t I2CKeyPad8x8::_read(uint16_t mask)
{
// improve the odds that IO will not interrupted.
yield();

_wire->beginTransmission(_address);
_wire->write(mask >> 8);
_wire->write(mask & 0xFF);
if (_wire->endTransmission() != 0)
{
// set communication error
return 0xFFFF;
}
_wire->requestFrom(_address, (uint8_t)2);
uint16_t value = _wire->read() << 8;
value += _wire->read();
return value;
}


// -- END OF FILE --

56 changes: 56 additions & 0 deletions libraries/I2CKeyPad8x8/I2CKeyPad8x8.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#pragma once
//
// FILE: I2CKeyPad8x8.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: Arduino library for 8x8 or smaller KeyPad connected to an I2C PCF8575.
// URL: https://github.com/RobTillaart/I2CKeyPad


#include "Arduino.h"
#include "Wire.h"


#define I2C_KEYPAD8x8_LIB_VERSION (F("0.1.0"))

#define I2C_KEYPAD8x8_NOKEY 64
#define I2C_KEYPAD8x8_FAIL 65


class I2CKeyPad8x8
{
public:
I2CKeyPad8x8(const uint8_t deviceAddress, TwoWire *wire = &Wire);

#if defined(ESP8266) || defined(ESP32)
bool begin(uint8_t sda, uint8_t scl);
#endif
bool begin();

// get raw key's 0..65
uint8_t getKey();
uint8_t getLastKey();

bool isPressed();
bool isConnected();

// get 'translated' keys
// user must load KeyMap, there is no check.
uint8_t getChar();
uint8_t getLastChar();
void loadKeyMap(char * keyMap); // char[65]


protected:
uint8_t _address;
uint8_t _lastKey;
uint16_t _read(uint16_t mask);

TwoWire* _wire;

char * _keyMap = NULL;
};


// -- END OF FILE --

21 changes: 21 additions & 0 deletions libraries/I2CKeyPad8x8/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 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
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Loading

0 comments on commit 3f6ab4d

Please sign in to comment.