Skip to content

Commit 9e5e030

Browse files
committed
Adding keypad (resistor ladder) support
1 parent 601b781 commit 9e5e030

File tree

4 files changed

+109
-13
lines changed

4 files changed

+109
-13
lines changed

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,24 @@ instruction and execute in 1-2 clock cycles. The library functions are
55
more than 10 times faster than the Arduino digital pin functions. In
66
some cases as much as 100 times faster.
77

8-
Additional support classes are available for Debounced Button, Shift
9-
Register Input/Output, and Software Serial. These also demonstrate how
10-
the GPIO template class may be used to construct additional libraries.
8+
Additional support classes are available for Debounced Button/Keypad,
9+
Shift Register Input/Output, and Software Serial. These also
10+
demonstrate how the GPIO template class may be used to construct
11+
additional libraries.
1112

1213
This library supports boards based on SAM3X8E, ATmega168, ATmega328P,
1314
ATmega32U4, ATmega1280, ATmega2560, ATtinyX4 and ATtinyX5.
1415

15-
Version: 1.10
16+
Version: 1.11
1617

1718
## Classes
1819

1920
* [AVR Board Configuration, BOARD](./src/Hardware/AVR/Board.h)
2021
* [AVR General Purpose Input/Output, GPIO](./src/Hardware/AVR/GPIO.h)
2122
* [SAM Board Configuration, BOARD](./src/Hardware/SAM/Board.h)
2223
* [SAM General Purpose Input/Output, GPIO](./src/Hardware/SAM/GPIO.h)
23-
* [Debounced Input Pin, Button](./src/Button.h)
24+
* [Debounced Digital Pin, Button](./src/Button.h)
25+
* [Debounced Analog Pin, Keypad](./src/Keypad.h)
2426
* [Shift Register Parallel Input, SRPI](./src/SRPI.h)
2527
* [Shift Register Parallel Input/Output, SRPIO](./src/SRPIO.h)
2628
* [Shift Register Parallel Output, SRPO](./src/SRPO.h)
@@ -48,8 +50,9 @@ shiftOut | 103/160 | srpo << val | 8 | 13/20
4850

4951
## Usage
5052

51-
* [Arduino-OWI](https://github.com/mikaelpatel/Arduino-OWI)
5253
* [Arduino-DHT](https://github.com/mikaelpatel/Arduino-DHT)
54+
* [Arduino-LCD](https://github.com/mikaelpatel/Arduino-LCD)
55+
* [Arduino-OWI](https://github.com/mikaelpatel/Arduino-OWI)
5356
* [Arduino-RTC](https://github.com/mikaelpatel/Arduino-RTC)
5457
* [Arduino-SPI](https://github.com/mikaelpatel/Arduino-SPI)
5558
* [Arduino-Storage](https://github.com/mikaelpatel/Arduino-Storage)

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name=Arduino-GPIO
2-
version=1.10
2+
version=1.11
33
author=Mikael Patel
44
maintainer=Mikael Patel <[email protected]>
55
sentence=General Purpose Input/Output (GPIO) library for Arduino.
6-
paragraph=The GPIO library has been developed to allow high performance digital pin access. Most access functions are compiled to a single instruction and execute in 1-2 clock cycles. The library functions are more than 10 times faster than the Arduino digital pin functions. In some cases as much as 100 times faster. Additional support classes are available for Debounced Button, Shift Register Parallel Input/Output and Software Serial. These also demonstrate how the GPIO template class may be used to construct additional libraries.
6+
paragraph=The GPIO library has been developed to allow high performance digital pin access. Most access functions are compiled to a single instruction and execute in 1-2 clock cycles. The library functions are more than 10 times faster than the Arduino digital pin functions. In some cases as much as 100 times faster. Additional support classes are available for Debounced Button/Keypad, Shift Register Parallel Input/Output and Software Serial. These also demonstrate how the GPIO template class may be used to construct additional libraries.
77
category=Communication
88
url=https://github.com/mikaelpatel/Arduino-GPIO
99
architectures=avr,sam

mainpage.dox

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ instruction and execute in 1-2 clock cycles. The library functions are
66
more than 10 times faster than the Arduino digital pin functions. In
77
some cases as much as 100 times faster.
88

9-
Additional support classes are available for Debounced Button, Shift
10-
Register Parallel Input and Output (SRPI, SRPO and SRPIO), and Serial
11-
(Software::Serial). These also demonstrate how the GPIO template class
12-
may be used to construct additional classes and libraries.
9+
Additional support classes are available for Debounced Button/Keypad,
10+
Shift Register Parallel Input and Output (SRPI, SRPO and SRPIO), and
11+
Serial (Software::Serial). These also demonstrate how the GPIO
12+
template class may be used to construct additional classes and
13+
libraries.
1314

1415
This library supports boards based on SAM3X8E, ATmega168, ATmega328P,
1516
ATmega32U4, ATmega1280, ATmega2560, ATtinyX4 and ATtinyX5.
1617

17-
Version: 1.10
18+
Version: 1.11
1819
*/
1920

2021
/** @page License

src/Keypad.h

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* @file Keypad.h
3+
* @version 1.0
4+
*
5+
* @section License
6+
* Copyright (C) 2017, Mikael Patel
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; either
11+
* version 2.1 of the License, or (at your option) any later version.
12+
*
13+
* This library is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+
* Lesser General Public License for more details.
17+
*/
18+
19+
#ifndef KEYPAD_H
20+
#define KEYPAD_H
21+
22+
/**
23+
* Resistor ladder keypad read and debounce template class.
24+
* @param[in] PIN analog pin.
25+
* @param[in] DEBOUNCE time limit (default 50 ms).
26+
*/
27+
template<int PIN, uint16_t DEBOUNCE = 50>
28+
class Keypad {
29+
public:
30+
/**
31+
* Construct keypad with given template parameters.
32+
*/
33+
Keypad(const uint16_t* map = NULL) :
34+
m_timestamp(0),
35+
m_key(0),
36+
m_map(map)
37+
{
38+
}
39+
40+
/**
41+
* Check if it is time to sample the keypad and check for
42+
* key pressed/release.
43+
* @return true(1) if a change was detected, otherwise false(0).
44+
*/
45+
bool ischanged()
46+
{
47+
// Check if debounce time limit has elapsed
48+
if (millis() - m_timestamp < DEBOUNCE) return (false);
49+
m_timestamp = millis();
50+
51+
// Read the analog pin and map
52+
uint16_t value = analogRead(PIN);
53+
uint8_t key = 0;
54+
if (m_map == NULL) return (false);
55+
while (value < (uint16_t) pgm_read_word(&m_map[key])) key++;
56+
if (key == m_key) return (false);
57+
m_key = key;
58+
return (true);
59+
}
60+
61+
/**
62+
* Return latest key.
63+
* @return key number.
64+
*/
65+
uint8_t key()
66+
__attribute__((always_inline))
67+
{
68+
return (m_key);
69+
}
70+
71+
/**
72+
* Return debounce timestamp.
73+
* @return timestamp.
74+
*/
75+
uint16_t timestamp()
76+
__attribute__((always_inline))
77+
{
78+
return (m_timestamp);
79+
}
80+
81+
protected:
82+
/** Timestamp for latest pin read. */
83+
uint16_t m_timestamp;
84+
85+
/** Latest debounced key. */
86+
uint8_t m_key;
87+
88+
/** Key map; program memory with values in descent order. */
89+
const uint16_t* m_map;
90+
};
91+
92+
#endif

0 commit comments

Comments
 (0)