mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-09-19 16:46:11 -04:00
0.2.0 AnalogKeypad
This commit is contained in:
parent
5461be599c
commit
695c60b6d6
@ -2,6 +2,10 @@ compile:
|
||||
# Choosing to run compilation tests on 2 different Arduino platforms
|
||||
platforms:
|
||||
- uno
|
||||
- leonardo
|
||||
- due
|
||||
- zero
|
||||
# - due
|
||||
# - zero
|
||||
# - leonardo
|
||||
- m4
|
||||
- esp32
|
||||
# - esp8266
|
||||
# - mega2560
|
@ -4,10 +4,14 @@ name: Arduino CI
|
||||
on: [push, pull_request]
|
||||
|
||||
jobs:
|
||||
arduino_ci:
|
||||
runTest:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: Arduino-CI/action@master
|
||||
# Arduino-CI/action@v0.1.1
|
||||
- uses: ruby/setup-ruby@v1
|
||||
with:
|
||||
ruby-version: 2.6
|
||||
- run: |
|
||||
gem install arduino_ci
|
||||
arduino_ci.rb
|
@ -1,50 +1,49 @@
|
||||
//
|
||||
// FILE: AnalogKeypad.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.6
|
||||
// VERSION: 0.2.0
|
||||
// DATE: 2019-01-31
|
||||
// PURPOSE: Class for (Robotdyn) 4x4 and 4x3 analog keypad
|
||||
//
|
||||
// HISTORY:
|
||||
// 0.1.0 2019-01-31 initial version
|
||||
// 0.1.1 2019-02-01 add pressed() event() last()
|
||||
// 0.1.2 2019-02-01 refactored rawRead(), first stable version
|
||||
// 0.1.3 2020-03-25 minor refactoring
|
||||
// 0.1.4 2020-05-27 update library.json
|
||||
// 0.1.5 2020-12-09 add arduino-ci
|
||||
// 0.1.6 2021-05-27 fix arduino-lint
|
||||
// HISTORY:
|
||||
// 0.1.0 2019-01-31 initial version
|
||||
// 0.1.1 2019-02-01 add pressed() event() last()
|
||||
// 0.1.2 2019-02-01 refactored rawRead(), first stable version
|
||||
// 0.1.3 2020-03-25 minor refactoring
|
||||
// 0.1.4 2020-05-27 update library.json
|
||||
// 0.1.5 2020-12-09 add arduino-ci
|
||||
// 0.1.6 2021-05-27 fix arduino-lint
|
||||
// 0.2.0 2021-10-17 update build-ci, readme,
|
||||
// add bits as parameter in constructor.
|
||||
|
||||
|
||||
#include "AnalogKeypad.h"
|
||||
|
||||
|
||||
// NOTE the MAGIC NUMBERS in rawRead() are for 8 BIT ADC
|
||||
// (8 bit compares are fast)
|
||||
// as 8 bit compares are fast
|
||||
//
|
||||
// The AKP_SHIFT takes care if the ADC generates more
|
||||
// than e.g. 10 bits. Change AKP_BITS to match your
|
||||
// build in ADC.
|
||||
// The _analogShift takes care if the ADC has more
|
||||
// than e.g. 10 bits.
|
||||
//
|
||||
// Arduino UNO3 build in ==> 10 BITS
|
||||
// so AKP_SHIFT ==> 2
|
||||
//
|
||||
#define AKP_BITS 10
|
||||
#define AKP_SHIFT (AKP_BITS - 8)
|
||||
// Arduino UNO3 build in ==> 10 bits
|
||||
// Other may have 12 or even 16 bits.
|
||||
|
||||
|
||||
AnalogKeypad::AnalogKeypad(const uint8_t pin)
|
||||
AnalogKeypad::AnalogKeypad(const uint8_t pin, const uint8_t bits)
|
||||
{
|
||||
_pin = pin;
|
||||
_lastKey = NOKEY;
|
||||
_analogPin = pin;
|
||||
_analogShift = bits - 8;
|
||||
_lastKey = NOKEY;
|
||||
}
|
||||
|
||||
|
||||
uint8_t AnalogKeypad::event()
|
||||
{
|
||||
int rv = NOKEY;
|
||||
uint8_t _key = rawRead();
|
||||
uint8_t _key = _rawRead();
|
||||
|
||||
if (_key == 0 && _lastKey == 0) rv = NOKEY;
|
||||
if (_key == 0 && _lastKey == 0) rv = NOKEY;
|
||||
else if (_key != 0 && _lastKey == 0) rv = PRESSED;
|
||||
else if (_key == 0 && _lastKey != 0) rv = RELEASED;
|
||||
else if (_key != 0 && _lastKey != 0 && _key == _lastKey) rv = REPEATED;
|
||||
@ -60,7 +59,7 @@ uint8_t AnalogKeypad::pressed()
|
||||
{
|
||||
int rv = NOKEY;
|
||||
|
||||
uint8_t _key = rawRead();
|
||||
uint8_t _key = _rawRead();
|
||||
if (_key == _lastKey) // NOKEY OR REPEAT
|
||||
{
|
||||
rv = _lastKey;
|
||||
@ -86,16 +85,16 @@ uint8_t AnalogKeypad::pressed()
|
||||
|
||||
uint8_t AnalogKeypad::read()
|
||||
{
|
||||
_lastKey = rawRead();
|
||||
_lastKey = _rawRead();
|
||||
return _lastKey;
|
||||
}
|
||||
|
||||
|
||||
// Adjust numbers for other than 4x4 keypad
|
||||
uint8_t AnalogKeypad::rawRead()
|
||||
uint8_t AnalogKeypad::_rawRead()
|
||||
{
|
||||
// spends most time in analogRead (uno ~110 usec)
|
||||
uint8_t val = analogRead(_pin) >> AKP_SHIFT;
|
||||
// spends most time in analogRead (UNO ~110 microseconds)
|
||||
uint8_t val = (analogRead(_analogPin) >> _analogShift);
|
||||
|
||||
// handle NOKEY first
|
||||
if (val < 57) return 0;
|
||||
|
@ -2,9 +2,9 @@
|
||||
//
|
||||
// FILE: AnalogKeypad.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.6
|
||||
// VERSION: 0.2.0
|
||||
// DATE: 2019-01-31
|
||||
// PURPOSE: Class for (Robotdyn) 4x4 and 4x3 analog keypad
|
||||
// PURPOSE: Class for (Robotdyn) 4x4 and 4x3 analogue keypad
|
||||
// URL: https://github.com/RobTillaart/AnalogKeypad
|
||||
//
|
||||
|
||||
@ -12,7 +12,7 @@
|
||||
#include "Arduino.h"
|
||||
|
||||
|
||||
#define ANALOGKEYPAD_LIB_VERSION "0.1.6"
|
||||
#define ANALOGKEYPAD_LIB_VERSION (F("0.2.0"))
|
||||
|
||||
#define NOKEY 0x00
|
||||
#define PRESSED 0x80
|
||||
@ -24,7 +24,7 @@
|
||||
class AnalogKeypad
|
||||
{
|
||||
public:
|
||||
explicit AnalogKeypad(const uint8_t pin);
|
||||
explicit AnalogKeypad(const uint8_t pin, const uint8_t bits = 10);
|
||||
|
||||
// returns 0 if no key pressed
|
||||
// otherwise returns key pressed first => ignoring fluctuations
|
||||
@ -36,13 +36,14 @@ public:
|
||||
uint8_t read();
|
||||
|
||||
// event alike approach
|
||||
// switch(int e = event())
|
||||
// switch(int e = event()) see examples
|
||||
uint8_t event();
|
||||
uint8_t key() { return _lastKey; } ;
|
||||
|
||||
private:
|
||||
uint8_t rawRead();
|
||||
uint8_t _pin;
|
||||
uint8_t _rawRead();
|
||||
uint8_t _analogPin;
|
||||
uint8_t _analogShift;
|
||||
uint8_t _lastKey;
|
||||
};
|
||||
|
||||
|
@ -1,43 +1,51 @@
|
||||
|
||||
[![Arduino CI](https://github.com/RobTillaart/AnalogKeypad/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
|
||||
|
||||
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/AnalogKeypad/blob/master/LICENSE)
|
||||
[![GitHub release](https://img.shields.io/github/release/RobTillaart/AnalogKeypad.svg?maxAge=3600)](https://github.com/RobTillaart/AnalogKeypad/releases)
|
||||
|
||||
|
||||
# AnalogKeypad
|
||||
|
||||
Library for (Robotdyn) 4x4 and 4x3 analog keypad
|
||||
|
||||
|
||||
## Description
|
||||
|
||||
AnalogKeypad is a simple library to read the keys from a (robotdyn) 4x4 or 4x3 keypad.
|
||||
No other keypads are tested, but they should work with this library after adjusting
|
||||
the **MAGIC NUMBERS** in the function **rawRead()**.
|
||||
|
||||
|
||||
## Interface
|
||||
|
||||
|
||||
### Constructor
|
||||
|
||||
- **AnalogKeypad(pin)** constructor, pin is typical A0 etc
|
||||
- **AnalogKeypad(const uint8_t pin, const uint8_t bits = 10)** constructor, pin is typical A0 etc
|
||||
Bits has a default of 10, but need to be set to match the platform.
|
||||
|
||||
|
||||
### polling interface
|
||||
|
||||
- **pressed()** returns 0 if no key pressed, otherwise returns key pressed (may fluctuate)
|
||||
- **read()** read the key pressed returns 0..16
|
||||
- **uint8_t pressed()** returns 0 if no key pressed, otherwise returns key pressed (may fluctuate)
|
||||
- **uint8_t read()** read the key pressed returns 0..16
|
||||
|
||||
|
||||
### event interface
|
||||
|
||||
- **event()** checks if a change has occurred since last time.
|
||||
- **key()** returns the key involved with last event
|
||||
- **uint8_t event()** checks if a change has occurred since last time.
|
||||
- **uint8_t key()** returns the key involved with last event
|
||||
|
||||
Switch(int e = event())
|
||||
|
||||
| Event | value |
|
||||
|:----:|:----:|
|
||||
| PRESSED | 0x80 |
|
||||
| RELEASED | 0x40 |
|
||||
| REPEATED | 0x20 |
|
||||
| CHANGED | 0x10 |
|
||||
| NOKEY | 0x00 |
|
||||
| Event | value |
|
||||
|:--------:|:-----:|
|
||||
| PRESSED | 0x80 |
|
||||
| RELEASED | 0x40 |
|
||||
| REPEATED | 0x20 |
|
||||
| CHANGED | 0x10 |
|
||||
| NOKEY | 0x00 |
|
||||
|
||||
|
||||
## Operation
|
||||
@ -53,3 +61,9 @@ are less likely to disturbe your program.
|
||||
|
||||
See Examples
|
||||
|
||||
## Future
|
||||
|
||||
- more examples
|
||||
- self-learning example?
|
||||
|
||||
|
||||
|
@ -1,18 +1,18 @@
|
||||
//
|
||||
// FILE: analogKeypad.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.0.1
|
||||
// PURPOSE:
|
||||
// PURPOSE: demo 4x4 analogue keypad
|
||||
//
|
||||
// HISTORY:
|
||||
// https://www.tinytronics.nl/shop/nl/arduino/accessoires/robotdyn-keypad-4x4-matrix-analoog?search=matrix
|
||||
//
|
||||
|
||||
|
||||
#include "AnalogKeypad.h"
|
||||
|
||||
AnalogKeypad AKP(A0);
|
||||
uint32_t start, stop;
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
@ -20,6 +20,9 @@ void setup()
|
||||
Serial.print("ANALOGKEYPAD_LIB_VERSION:\t");
|
||||
Serial.println(ANALOGKEYPAD_LIB_VERSION);
|
||||
|
||||
Serial.println();
|
||||
test3();
|
||||
|
||||
for (int i = 0; i < 100; i++) test1();
|
||||
for (int i = 0; i < 100; i++) test2();
|
||||
}
|
||||
@ -28,6 +31,7 @@ void loop()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
void test1()
|
||||
{
|
||||
@ -40,6 +44,7 @@ void test1()
|
||||
delay(100);
|
||||
}
|
||||
|
||||
|
||||
// use the "event" interface
|
||||
void test2()
|
||||
{
|
||||
@ -79,3 +84,5 @@ void test3()
|
||||
Serial.println(button);
|
||||
delay(100);
|
||||
}
|
||||
|
||||
// -- END OF FILE --
|
||||
|
@ -1,6 +1,6 @@
|
||||
# Syntax Coloring Map For AnalogKeypad
|
||||
# Syntax Colouring Map For AnalogKeypad
|
||||
|
||||
# Datatypes (KEYWORD1)
|
||||
# Data types (KEYWORD1)
|
||||
AnalogKeypad KEYWORD1
|
||||
|
||||
|
||||
|
@ -15,7 +15,7 @@
|
||||
"type": "git",
|
||||
"url": "https://github.com/RobTillaart/AnalogKeypad"
|
||||
},
|
||||
"version": "0.1.6",
|
||||
"version": "0.2.0",
|
||||
"license": "MIT",
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*"
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=AnalogKeypad
|
||||
version=0.1.6
|
||||
version=0.2.0
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=Arduino Library for (Robotdyn) 4x4 and 4x3 AnalogKeypad
|
||||
|
Loading…
Reference in New Issue
Block a user