User | Revision | Line number | New contents of line |
grantphillips |
0:4bbd88022a6f
|
1
|
/* Keypad Library v1.0
|
grantphillips |
0:4bbd88022a6f
|
2
|
* Copyright (c) 2016 Grant Phillips
|
grantphillips |
0:4bbd88022a6f
|
3
|
* grant.phillips@nmmu.ac.za
|
grantphillips |
0:4bbd88022a6f
|
4
|
*
|
grantphillips |
0:4bbd88022a6f
|
5
|
* This is a modified version of Riaan Ehlers' library which was written for the
|
grantphillips |
0:4bbd88022a6f
|
6
|
* Microchip PIC18 series microcontrollers.
|
grantphillips |
0:4bbd88022a6f
|
7
|
*
|
grantphillips |
0:4bbd88022a6f
|
8
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
grantphillips |
0:4bbd88022a6f
|
9
|
* of this software and associated documentation files (the "Software"), to deal
|
grantphillips |
0:4bbd88022a6f
|
10
|
* in the Software without restriction, including without limitation the rights
|
grantphillips |
0:4bbd88022a6f
|
11
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
grantphillips |
0:4bbd88022a6f
|
12
|
* copies of the Software, and to permit persons to whom the Software is
|
grantphillips |
0:4bbd88022a6f
|
13
|
* furnished to do so, subject to the following conditions:
|
grantphillips |
0:4bbd88022a6f
|
14
|
*
|
grantphillips |
0:4bbd88022a6f
|
15
|
* The above copyright notice and this permission notice shall be included in
|
grantphillips |
0:4bbd88022a6f
|
16
|
* all copies or substantial portions of the Software.
|
grantphillips |
0:4bbd88022a6f
|
17
|
*
|
grantphillips |
0:4bbd88022a6f
|
18
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
grantphillips |
0:4bbd88022a6f
|
19
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
grantphillips |
0:4bbd88022a6f
|
20
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
grantphillips |
0:4bbd88022a6f
|
21
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
grantphillips |
0:4bbd88022a6f
|
22
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
grantphillips |
0:4bbd88022a6f
|
23
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
grantphillips |
0:4bbd88022a6f
|
24
|
* THE SOFTWARE.
|
grantphillips |
0:4bbd88022a6f
|
25
|
*/
|
grantphillips |
0:4bbd88022a6f
|
26
|
|
grantphillips |
0:4bbd88022a6f
|
27
|
#ifndef Keypad_H
|
grantphillips |
0:4bbd88022a6f
|
28
|
#define Keypad_H
|
grantphillips |
0:4bbd88022a6f
|
29
|
|
grantphillips |
0:4bbd88022a6f
|
30
|
#include "mbed.h"
|
grantphillips |
0:4bbd88022a6f
|
31
|
|
grantphillips |
0:4bbd88022a6f
|
32
|
/** Class library for a polling-based 4x4 keypad.
|
grantphillips |
0:4bbd88022a6f
|
33
|
*
|
grantphillips |
0:4bbd88022a6f
|
34
|
* Example:
|
grantphillips |
0:4bbd88022a6f
|
35
|
* @code
|
grantphillips |
0:4bbd88022a6f
|
36
|
* #include "mbed.h"
|
grantphillips |
0:4bbd88022a6f
|
37
|
* #include "Keypad.h"
|
grantphillips |
0:4bbd88022a6f
|
38
|
*
|
grantphillips |
0:4bbd88022a6f
|
39
|
* Keypad kpad(PE_15, PE_14, PE_13, PE_12, PE_11, PE_10, PE_9, PE_8);
|
grantphillips |
0:4bbd88022a6f
|
40
|
*
|
grantphillips |
0:4bbd88022a6f
|
41
|
* int main() {
|
grantphillips |
0:4bbd88022a6f
|
42
|
* char key;
|
grantphillips |
0:4bbd88022a6f
|
43
|
* int released = 1;
|
grantphillips |
0:4bbd88022a6f
|
44
|
*
|
grantphillips |
0:4bbd88022a6f
|
45
|
* while(1){
|
grantphillips |
0:4bbd88022a6f
|
46
|
* key = kpad.ReadKey(); //read the current key pressed
|
grantphillips |
0:4bbd88022a6f
|
47
|
*
|
grantphillips |
0:4bbd88022a6f
|
48
|
* if(key == '\0')
|
grantphillips |
0:4bbd88022a6f
|
49
|
* released = 1; //set the flag when all keys are released
|
grantphillips |
0:4bbd88022a6f
|
50
|
*
|
grantphillips |
0:4bbd88022a6f
|
51
|
* if((key != '\0') && (released == 1)) { //if a key is pressed AND previous key was released
|
grantphillips |
0:4bbd88022a6f
|
52
|
* printf("%c\n", key);
|
grantphillips |
0:4bbd88022a6f
|
53
|
* released = 0; //clear the flag to indicate that key is still pressed
|
grantphillips |
0:4bbd88022a6f
|
54
|
* }
|
grantphillips |
0:4bbd88022a6f
|
55
|
* }
|
grantphillips |
0:4bbd88022a6f
|
56
|
* }
|
grantphillips |
0:4bbd88022a6f
|
57
|
* @endcode
|
grantphillips |
0:4bbd88022a6f
|
58
|
*/
|
grantphillips |
0:4bbd88022a6f
|
59
|
|
grantphillips |
0:4bbd88022a6f
|
60
|
class Keypad {
|
grantphillips |
0:4bbd88022a6f
|
61
|
public:
|
grantphillips |
0:4bbd88022a6f
|
62
|
/** Create a Keypad object.
|
grantphillips |
0:4bbd88022a6f
|
63
|
* @param col1..4 DigitalOut (or BusOut) compatible pins for keypad Column data lines
|
grantphillips |
0:4bbd88022a6f
|
64
|
* @param row1..4 DigitalIn (or BusIn) compatible pins for keypad Row data lines
|
grantphillips |
0:4bbd88022a6f
|
65
|
*/
|
grantphillips |
0:4bbd88022a6f
|
66
|
Keypad(PinName col1, PinName col2, PinName col3, PinName col4, PinName row1, PinName row2, PinName row3, PinName row4);
|
grantphillips |
0:4bbd88022a6f
|
67
|
|
grantphillips |
0:4bbd88022a6f
|
68
|
/** Returns the letter of the key pressed.
|
grantphillips |
0:4bbd88022a6f
|
69
|
* @param
|
grantphillips |
0:4bbd88022a6f
|
70
|
* None
|
grantphillips |
0:4bbd88022a6f
|
71
|
* @return
|
grantphillips |
0:4bbd88022a6f
|
72
|
* The character of the key pressed. Returns '\0' if no key is pressed.
|
grantphillips |
0:4bbd88022a6f
|
73
|
*/
|
grantphillips |
0:4bbd88022a6f
|
74
|
char ReadKey();
|
grantphillips |
0:4bbd88022a6f
|
75
|
|
grantphillips |
0:4bbd88022a6f
|
76
|
|
grantphillips |
0:4bbd88022a6f
|
77
|
private:
|
grantphillips |
0:4bbd88022a6f
|
78
|
BusOut _cols;
|
grantphillips |
0:4bbd88022a6f
|
79
|
BusIn _rows;
|
grantphillips |
0:4bbd88022a6f
|
80
|
};
|
grantphillips |
0:4bbd88022a6f
|
81
|
|
grantphillips |
0:4bbd88022a6f
|
82
|
#endif |