Interrupt-driven keypad interface library. Compatible with RTOS. Able to handle various keypad size below 4x4.
Revision 1:1ae4a77af85b, committed 2014-01-01
- Comitter:
- yoonghm
- Date:
- Wed Jan 01 17:47:52 2014 +0000
- Parent:
- 0:8209bcf62e0a
- Commit message:
- Interrupt-driven keypad interface library.; Can be used safely with RTOS.; Able to handle keypad size 4x4 and below.
Changed in this revision
diff -r 8209bcf62e0a -r 1ae4a77af85b FPointer.lib --- a/FPointer.lib Mon Jan 30 09:40:01 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/AjK/libraries/FPointer/ll7nhv \ No newline at end of file
diff -r 8209bcf62e0a -r 1ae4a77af85b keypad.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/keypad.lib Wed Jan 01 17:47:52 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/yoonghm/code/keypad/#da060f8c03e8
diff -r 8209bcf62e0a -r 1ae4a77af85b keypad/.lib --- a/keypad/.lib Mon Jan 30 09:40:01 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ - \ No newline at end of file
diff -r 8209bcf62e0a -r 1ae4a77af85b keypad/keypad.cpp --- a/keypad/keypad.cpp Mon Jan 30 09:40:01 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,67 +0,0 @@ -#include "keypad.h" - -Keypad::Keypad(PinName row3, PinName row2, PinName row1, PinName row0, - PinName col3, PinName col2, PinName col1, PinName col0, - int debounce_ms): - _row0(row0), _row1(row1), _row2(row2), _row3(row3), - _cols(col0, col1, col2, col3) { - _debounce = debounce_ms; - _setupRiseTrigger(); -} - -void Keypad::Start(void) { - _cols = 0x0F; -} - -void Keypad::Stop(void) { - _cols = 0x00; -} - -void Keypad::CallAfterInput(uint32_t (*fptr)(uint32_t index)) { - _input.attach(fptr); -} - -void Keypad::_callback(int row, InterruptIn &therow) { - wait_ms(_debounce); - if (therow != 1) - return; - - int c = -1; - _cols = _cols & 0x0E; - if (therow == 0) - c = 0; - else { - _cols = _cols & 0x0D; - if (therow == 0) - c = 1; - else { - _cols = _cols & 0x0B; - if (therow == 0) - c = 2; - else - c = 3; - } - } - _input.call(row * 4 + c); - Start(); // Re-energize all columns -} - -void Keypad::_cbRow0Rise(void) { - _callback(0, _row0); -} -void Keypad::_cbRow1Rise(void) { - _callback(1, _row1); -} -void Keypad::_cbRow2Rise(void) { - _callback(2, _row2); -} -void Keypad::_cbRow3Rise(void) { - _callback(3, _row3); -} - -void Keypad::_setupRiseTrigger(void) { - _row0.rise(this, &Keypad::_cbRow0Rise); - _row1.rise(this, &Keypad::_cbRow1Rise); - _row2.rise(this, &Keypad::_cbRow2Rise); - _row3.rise(this, &Keypad::_cbRow3Rise); -} \ No newline at end of file
diff -r 8209bcf62e0a -r 1ae4a77af85b keypad/keypad.h --- a/keypad/keypad.h Mon Jan 30 09:40:01 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,109 +0,0 @@ -/* mbed Keypad library, using user-defined interrupt callback - * Copyright (c) 2012 Yoong Hor Meng - * - * 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 - */ - -#ifndef KEYPAD_H -#define KEYPAD_H - -#include "mbed.h" -#include "FPointer.h" - -/** - * A interrupt-based interface to 4x4 keypad. - * - * On each key pressed on a keypad, the index of the key is passed to a - * user-defined function. User is free to define what to be done with the - * input. - * - * - * @code - * -* #include "mbed.h" -* #include "keypad.h" -* -* // Define your own keypad values -* char Keytable[] = { '1', '2', '3', 'A', -* '4', '5', '6', 'B', -* '7', '8', '9', 'C', -* '*', '0', '#', 'D' -* }; -* -* uint32_t cbAfterInput(uint32_t index) { -* printf("Index:%d => Key:%c\n", key, Keytable[index]); -* return 0; -* } -* -* int main() { -* Keypad keypad(p25, p26, p27, p28, p21, p22, p23, p24); -* keypad.CallAfterInput(&cbAfterInput); -* keypad.Start(); -* -* while (1) { -* wait_ms(100); -* } -* } -* @endcode -*/ -class Keypad { -public: - /** Create a Keypad interface - * - * @param row<3..0> Row data lines - * @param col<3..0> Column data lines - * @param debounce_ms Debounce in ms (Default to 20ms) - */ - Keypad(PinName row3, PinName row2, PinName row1, PinName row0, - PinName col3, PinName col2, PinName col1, PinName col0, - int debounce_ms = 20); - - /** Start the keypad interrupt routines - */ - void Start(void); - - /** Stop the keypad interrupt routines - */ - void Stop(void); - - /** User-defined function that to be called when a key is pressed - * @param fptr A function pointer takes a uint32_t and - * returns uint32_t - */ - void CallAfterInput(uint32_t (*fptr)(uint32_t)); - -protected: - InterruptIn _row0; - InterruptIn _row1; - InterruptIn _row2; - InterruptIn _row3; - BusOut _cols; - int _debounce; - FPointer _input; // Called after each input - - void _callback(int row, InterruptIn &therow); - void _cbRow0Rise(void); - void _cbRow1Rise(void); - void _cbRow2Rise(void); - void _cbRow3Rise(void); - void _setupRiseTrigger(void); - void _dummy(void) { }; -}; - -#endif // KEYPAD_H \ No newline at end of file
diff -r 8209bcf62e0a -r 1ae4a77af85b main.cpp --- a/main.cpp Mon Jan 30 09:40:01 2012 +0000 +++ b/main.cpp Wed Jan 01 17:47:52 2014 +0000 @@ -1,53 +1,40 @@ #include "mbed.h" -#include "keypad.h" -#define KEYLEN 4 -#define ENDKEY 15 -char Buffer[KEYLEN]; -int Index = 0; +#include "Keypad.h" + +Serial PC(USBTX, USBRX); // Define your own keypad values -char Keytable[] = { '1', '2', '3', 'A', - '4', '5', '6', 'B', - '7', '8', '9', 'C', - '*', '0', '#', 'D' +char Keytable[] = { '1', '2', '3', // r0 + '4', '5', '6', // r1 + '7', '8', '9', // r2 + // c0 c1 c2 }; -uint32_t cbAfterInput(uint32_t key) { - bool finish = false; - - printf("Index:%d => Key:%c\n", Index, Keytable[key]); +int32_t Index = -1; +int State; - if (Index < KEYLEN - 1) - { - if (key != ENDKEY) // Terminating key - Buffer[Index] = Keytable[key]; - else // Terminating key is entered - finish = true; - Index++; - } - - if (finish || (Index == KEYLEN - 1)) { - printf("Complete string = %s\n", Buffer); - memset(&Buffer, 0, KEYLEN); - Index = 0; - } - +uint32_t cbAfterInput(uint32_t index) +{ + Index = index; return 0; } -void Sleep(void) { - __WFI(); -} +int main() +{ + PC.printf("I am Demo Keypad\r\n"); + + // r0 r1 r2 r3 c0 c1 c2 c3 + Keypad keypad(p21, p22, NC, NC, p23, p24, NC, NC); + keypad.attach(&cbAfterInput); + keypad.start(); // energize the columns c0-c3 of the keypad -int main() { - memset(&Buffer, 0, KEYLEN); - Index = 0; - Keypad keypad(p25, p26, p27, p28, p21, p22, p23, p24); - keypad.CallAfterInput(&cbAfterInput); - - keypad.Start(); while (1) { - Sleep(); + __wfi(); + if (Index > -1) { + PC.printf("Interrupted"); + PC.printf("Index:%d => Key:%c\r\n", Index, Keytable[Index]); + Index = -1; + } } -} \ No newline at end of file +}
diff -r 8209bcf62e0a -r 1ae4a77af85b mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Jan 01 17:47:52 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/b4b9f287a47e \ No newline at end of file
diff -r 8209bcf62e0a -r 1ae4a77af85b mbed.lib --- a/mbed.lib Mon Jan 30 09:40:01 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/projects/libraries/svn/mbed/trunk@38 \ No newline at end of file