An interrupt-driven interface to 4x4 keypad.

Dependents:   FYPFinalProgram FYPFinalizeProgram KEYS Proyect_Patric_electronic_door_MSC_Ok_ESP ... more

Committer:
yoonghm
Date:
Wed Jan 01 17:45:53 2014 +0000
Revision:
10:da060f8c03e8
Update Keypad library to be used with RTOS.; Support any combination of keypad size below 4x4.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yoonghm 10:da060f8c03e8 1 #include "Keypad.h"
yoonghm 10:da060f8c03e8 2
yoonghm 10:da060f8c03e8 3 extern Serial PC;
yoonghm 10:da060f8c03e8 4
yoonghm 10:da060f8c03e8 5 void Keypad::_cbRow0Rise()
yoonghm 10:da060f8c03e8 6 {
yoonghm 10:da060f8c03e8 7 _checkIndex(0, _rows[0]);
yoonghm 10:da060f8c03e8 8 }
yoonghm 10:da060f8c03e8 9
yoonghm 10:da060f8c03e8 10 void Keypad::_cbRow1Rise()
yoonghm 10:da060f8c03e8 11 {
yoonghm 10:da060f8c03e8 12 _checkIndex(1, _rows[1]);
yoonghm 10:da060f8c03e8 13 }
yoonghm 10:da060f8c03e8 14
yoonghm 10:da060f8c03e8 15 void Keypad::_cbRow2Rise()
yoonghm 10:da060f8c03e8 16 {
yoonghm 10:da060f8c03e8 17 _checkIndex(2, _rows[2]);
yoonghm 10:da060f8c03e8 18 }
yoonghm 10:da060f8c03e8 19
yoonghm 10:da060f8c03e8 20 void Keypad::_cbRow3Rise()
yoonghm 10:da060f8c03e8 21 {
yoonghm 10:da060f8c03e8 22 _checkIndex(3, _rows[3]);
yoonghm 10:da060f8c03e8 23 }
yoonghm 10:da060f8c03e8 24
yoonghm 10:da060f8c03e8 25 void
yoonghm 10:da060f8c03e8 26 Keypad::_setupRiseTrigger
yoonghm 10:da060f8c03e8 27 (
yoonghm 10:da060f8c03e8 28 )
yoonghm 10:da060f8c03e8 29 {
yoonghm 10:da060f8c03e8 30 if (_rows[0]) {
yoonghm 10:da060f8c03e8 31 _rows[0]->rise(this, &Keypad::_cbRow0Rise);
yoonghm 10:da060f8c03e8 32 }
yoonghm 10:da060f8c03e8 33
yoonghm 10:da060f8c03e8 34 if (_rows[1]) {
yoonghm 10:da060f8c03e8 35 _rows[1]->rise(this, &Keypad::_cbRow1Rise);
yoonghm 10:da060f8c03e8 36 }
yoonghm 10:da060f8c03e8 37
yoonghm 10:da060f8c03e8 38 if (_rows[2]) {
yoonghm 10:da060f8c03e8 39 _rows[2]->rise(this, &Keypad::_cbRow2Rise);
yoonghm 10:da060f8c03e8 40 }
yoonghm 10:da060f8c03e8 41
yoonghm 10:da060f8c03e8 42 if (_rows[3]) {
yoonghm 10:da060f8c03e8 43 _rows[3]->rise(this, &Keypad::_cbRow3Rise);
yoonghm 10:da060f8c03e8 44 }
yoonghm 10:da060f8c03e8 45 }
yoonghm 10:da060f8c03e8 46
yoonghm 10:da060f8c03e8 47
yoonghm 10:da060f8c03e8 48 Keypad::Keypad
yoonghm 10:da060f8c03e8 49 (PinName r0
yoonghm 10:da060f8c03e8 50 ,PinName r1
yoonghm 10:da060f8c03e8 51 ,PinName r2
yoonghm 10:da060f8c03e8 52 ,PinName r3
yoonghm 10:da060f8c03e8 53 ,PinName c0
yoonghm 10:da060f8c03e8 54 ,PinName c1
yoonghm 10:da060f8c03e8 55 ,PinName c2
yoonghm 10:da060f8c03e8 56 ,PinName c3
yoonghm 10:da060f8c03e8 57 ,int debounce_ms
yoonghm 10:da060f8c03e8 58 )
yoonghm 10:da060f8c03e8 59 {
yoonghm 10:da060f8c03e8 60 PinName rPins[4] = {r0, r1, r2, r3};
yoonghm 10:da060f8c03e8 61 PinName cPins[4] = {c0, c1, c2, c3};
yoonghm 10:da060f8c03e8 62
yoonghm 10:da060f8c03e8 63 for (int i = 0; i < 4; i++) {
yoonghm 10:da060f8c03e8 64 _rows[i] = NULL;
yoonghm 10:da060f8c03e8 65 _cols[i] = NULL;
yoonghm 10:da060f8c03e8 66 }
yoonghm 10:da060f8c03e8 67
yoonghm 10:da060f8c03e8 68 _nRow = 0;
yoonghm 10:da060f8c03e8 69 for (int i = 0; i < 4; i++) {
yoonghm 10:da060f8c03e8 70 if (rPins[i] != NC) {
yoonghm 10:da060f8c03e8 71 _rows[i] = new InterruptIn(rPins[i]);
yoonghm 10:da060f8c03e8 72 _nRow++;
yoonghm 10:da060f8c03e8 73 } else
yoonghm 10:da060f8c03e8 74 break;
yoonghm 10:da060f8c03e8 75 }
yoonghm 10:da060f8c03e8 76 _setupRiseTrigger();
yoonghm 10:da060f8c03e8 77
yoonghm 10:da060f8c03e8 78 _nCol = 0;
yoonghm 10:da060f8c03e8 79 for (int i = 0; i < 4; i++) {
yoonghm 10:da060f8c03e8 80 if (cPins[i] != NC) {
yoonghm 10:da060f8c03e8 81 _cols[i] = new DigitalOut(cPins[i]);
yoonghm 10:da060f8c03e8 82 _nCol++;
yoonghm 10:da060f8c03e8 83 } else
yoonghm 10:da060f8c03e8 84 break;
yoonghm 10:da060f8c03e8 85 }
yoonghm 10:da060f8c03e8 86
yoonghm 10:da060f8c03e8 87 _debounce = debounce_ms;
yoonghm 10:da060f8c03e8 88 }
yoonghm 10:da060f8c03e8 89
yoonghm 10:da060f8c03e8 90 Keypad::~Keypad
yoonghm 10:da060f8c03e8 91 ()
yoonghm 10:da060f8c03e8 92 {
yoonghm 10:da060f8c03e8 93 for (int i = 0; i < 4; i++) {
yoonghm 10:da060f8c03e8 94 if (_rows[i] != 0)
yoonghm 10:da060f8c03e8 95 delete _rows[i];
yoonghm 10:da060f8c03e8 96 }
yoonghm 10:da060f8c03e8 97
yoonghm 10:da060f8c03e8 98 for (int i = 0; i < 4; i++) {
yoonghm 10:da060f8c03e8 99 if (_cols[i] != 0)
yoonghm 10:da060f8c03e8 100 delete _cols[i];
yoonghm 10:da060f8c03e8 101 }
yoonghm 10:da060f8c03e8 102 }
yoonghm 10:da060f8c03e8 103
yoonghm 10:da060f8c03e8 104 void
yoonghm 10:da060f8c03e8 105 Keypad::start
yoonghm 10:da060f8c03e8 106 (
yoonghm 10:da060f8c03e8 107 )
yoonghm 10:da060f8c03e8 108 {
yoonghm 10:da060f8c03e8 109 for (int i = 0; i < _nCol; i++)
yoonghm 10:da060f8c03e8 110 _cols[i]->write(1);
yoonghm 10:da060f8c03e8 111 }
yoonghm 10:da060f8c03e8 112
yoonghm 10:da060f8c03e8 113 void
yoonghm 10:da060f8c03e8 114 Keypad::stop
yoonghm 10:da060f8c03e8 115 (
yoonghm 10:da060f8c03e8 116 )
yoonghm 10:da060f8c03e8 117 {
yoonghm 10:da060f8c03e8 118 for (int i = 0; i < _nCol; i++)
yoonghm 10:da060f8c03e8 119 _cols[i++]->write(0);
yoonghm 10:da060f8c03e8 120 }
yoonghm 10:da060f8c03e8 121
yoonghm 10:da060f8c03e8 122 void
yoonghm 10:da060f8c03e8 123 Keypad::attach
yoonghm 10:da060f8c03e8 124 (uint32_t (*fptr)(uint32_t index)
yoonghm 10:da060f8c03e8 125 )
yoonghm 10:da060f8c03e8 126 {
yoonghm 10:da060f8c03e8 127 _callback.attach(fptr);
yoonghm 10:da060f8c03e8 128 }
yoonghm 10:da060f8c03e8 129
yoonghm 10:da060f8c03e8 130 void
yoonghm 10:da060f8c03e8 131 Keypad::_checkIndex
yoonghm 10:da060f8c03e8 132 (int row
yoonghm 10:da060f8c03e8 133 ,InterruptIn *therow
yoonghm 10:da060f8c03e8 134 )
yoonghm 10:da060f8c03e8 135 {
yoonghm 10:da060f8c03e8 136 #ifdef THREAD_H
yoonghm 10:da060f8c03e8 137 Thread::wait(_debounce);
yoonghm 10:da060f8c03e8 138 #else
yoonghm 10:da060f8c03e8 139 wait_ms(_debounce);
yoonghm 10:da060f8c03e8 140 #endif
yoonghm 10:da060f8c03e8 141
yoonghm 10:da060f8c03e8 142 if (therow->read() == 0)
yoonghm 10:da060f8c03e8 143 return;
yoonghm 10:da060f8c03e8 144
yoonghm 10:da060f8c03e8 145 int c;
yoonghm 10:da060f8c03e8 146 for (c = 0; c < _nCol; c++) {
yoonghm 10:da060f8c03e8 147 _cols[c]->write(0); // de-energize the column
yoonghm 10:da060f8c03e8 148 if (therow->read() == 0) {
yoonghm 10:da060f8c03e8 149 break;
yoonghm 10:da060f8c03e8 150 }
yoonghm 10:da060f8c03e8 151 }
yoonghm 10:da060f8c03e8 152
yoonghm 10:da060f8c03e8 153 int index = row * _nCol + c;
yoonghm 10:da060f8c03e8 154 _callback.call(index);
yoonghm 10:da060f8c03e8 155 start(); // Re-energize all columns
yoonghm 10:da060f8c03e8 156 }
yoonghm 10:da060f8c03e8 157