-
Notifications
You must be signed in to change notification settings - Fork 1
/
MuxSwitch.cpp
executable file
·126 lines (108 loc) · 4.2 KB
/
MuxSwitch.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA *
* *
***************************************************************************
* *
* (c) Copyright, 1997-2012, ANSR *
* *
***************************************************************************
* *
* Filename: MuxSwitch.cpp *
* *
***************************************************************************/
#include "armlib.h"
/**
* Constructor.
*
* @param rowCount number of rows in the switch matrix
* @param columnCount number of columns in the switch matrix
*/
MuxSwitch::MuxSwitch (uint32_t rowCount, uint32_t columnCount)
{
this->rowCount = rowCount;
this->columnCount = columnCount;
}
/**
* Enable the multiplex switch driver.
*/
void MuxSwitch::Enable()
{
uint32_t row, col;
this->keys.Clear();
this->tickCount = SystemControl::GetTick();
for (row = 0; row < this->rowCount; ++row)
for (col = 0; col < this->columnCount; ++col)
{
this->lastState[row][col] = false;
this->debounceCount[row][col] = 0;
} // END for
}
/**
* Read and process the multiplex switch array.
*/
void MuxSwitch::Update()
{
uint32_t columns, row, col;
// Process the switch at a regular rate.
if (this->tickCount > SystemControl::GetTick())
return;
// Time of the next scan.
this->tickCount += ScanRate;
// Scan the
for (row = 0; row < this->rowCount; ++row)
{
// Set the matrix row selection.
SetRow(row);
// Get the selected columns.
columns = GetColumn();
for (col = 0; col < this->columnCount; ++col)
{
if ((columns & 0x01) == 0x01)
{
if (!this->lastState[row][col])
{
this->lastState[row][col] = true;
if (this->debounceCount[row][col] == 0)
this->keys.Write((row << 4) | col);
} // END if
this->debounceCount[row][col] = DebounceCount;
} else {
this->lastState[row][col] = false;
if (this->debounceCount[row][col] != 0)
--this->debounceCount[row][col];
} // END if-else
columns = columns >> 1;
} // END for col
} // END for row
}
/**
* Indicate if the FIFO has keys.
*
* @return true if key has been pushed; otherwise false
*/
bool_t MuxSwitch::HasKey()
{
return this->keys.HasData();
}
/**
* Return the oldest entry in the FIFO.
*
* @return key pressed
*/
uint32_t MuxSwitch::GetKey()
{
return this->keys.Read();
}