s

Fork of keypad by HM Yoong

Committer:
felipe_r_andreis
Date:
Fri Aug 05 13:20:41 2016 +0000
Revision:
11:1d35d1df6c60
Parent:
10:da060f8c03e8
No changes

Who changed what in which revision?

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