forked from Damme/LandLord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keypad.c
executable file
·194 lines (169 loc) · 5.39 KB
/
keypad.c
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <stdio.h>
#include "keypad.h"
#include "common.h"
#include "FreeRTOS.h"
#include "timers.h"
#include "queue.h"
#include "powermgmt.h"
//#include "motorctrl.h"
#include "global.h"
const uint8_t tblKey[4][4] = {
{ KEY8, KEY9, KEY0, KEYSTART },
{ KEY6, KEYOK, KEYDOWN, KEY7 },
{ KEYBACK, KEYUP, KEY4, KEY5 },
{ KEYHOME, KEY1, KEY2, KEY3 },
};
uint8_t keyState, keyLastState, keyPessTime = 0;
uint8_t keypadRow, keypadCol, keypadPressedKey, keypadIsRow = 0;
void keypad_SetRow(bool row) {
if (row) {
keypadIsRow = 1;
GPIO_FNC_PULL(KEYPAD_COL0, PINMODE_PULLDOWN);
GPIO_FNC_PULL(KEYPAD_COL1, PINMODE_PULLDOWN);
GPIO_FNC_PULL(KEYPAD_COL2, PINMODE_PULLDOWN);
GPIO_FNC_PULL(KEYPAD_COL3, PINMODE_PULLDOWN);
GPIO_FNC_PULL(KEYPAD_ROW0, PINMODE_PULLUP);
GPIO_FNC_PULL(KEYPAD_ROW1, PINMODE_PULLUP);
GPIO_FNC_PULL(KEYPAD_ROW2, PINMODE_PULLUP);
GPIO_FNC_PULL(KEYPAD_ROW3, PINMODE_PULLUP);
} else {
keypadIsRow = 0;
GPIO_FNC_PULL(KEYPAD_COL0, PINMODE_PULLUP);
GPIO_FNC_PULL(KEYPAD_COL1, PINMODE_PULLUP);
GPIO_FNC_PULL(KEYPAD_COL2, PINMODE_PULLUP);
GPIO_FNC_PULL(KEYPAD_COL3, PINMODE_PULLUP);
GPIO_FNC_PULL(KEYPAD_ROW0, PINMODE_PULLDOWN);
GPIO_FNC_PULL(KEYPAD_ROW1, PINMODE_PULLDOWN);
GPIO_FNC_PULL(KEYPAD_ROW2, PINMODE_PULLDOWN);
GPIO_FNC_PULL(KEYPAD_ROW3, PINMODE_PULLDOWN);
}
vTaskDelay(xDelay25);
}
bool keypad_IsPressed() {
uint8_t ret = 0;
if (GPIO_CHK_PIN(KEYPAD_STOP1)) { // Stop
ret = 1;
goto ret;
}
if (GPIO_CHK_PIN(KEYPAD_POWER)) { // Power
ret = 1;
goto ret;
}
if (!keypadIsRow) {
ret = ((GPIO_CHK_PIN(KEYPAD_COL0) | GPIO_CHK_PIN(KEYPAD_COL1) |
GPIO_CHK_PIN(KEYPAD_COL2) | GPIO_CHK_PIN(KEYPAD_COL3)) > 0);
goto ret;
}
ret:
return ret;
}
uint8_t keypad_ProcessKey() {
uint8_t ret = 0;
if (GPIO_CHK_PIN(KEYPAD_STOP1)) { // stop
ret = KEYSTOP;
goto ret;
} else if (GPIO_CHK_PIN(KEYPAD_POWER)) { //Power
ret = KEYPWR;
goto ret;
} else if (GPIO_CHK_PIN(KEYPAD_COL0))
keypadCol = 0;
else if (GPIO_CHK_PIN(KEYPAD_COL1))
keypadCol = 1;
else if (GPIO_CHK_PIN(KEYPAD_COL2))
keypadCol = 2;
else if (GPIO_CHK_PIN(KEYPAD_COL3))
keypadCol = 3;
keypad_SetRow(1);
if (GPIO_CHK_PIN(KEYPAD_ROW0))
keypadRow = 0;
else if (GPIO_CHK_PIN(KEYPAD_ROW1))
keypadRow = 1;
else if (GPIO_CHK_PIN(KEYPAD_ROW2))
keypadRow = 2;
else if (GPIO_CHK_PIN(KEYPAD_ROW3))
keypadRow = 3;
keypad_SetRow(0);
ret = tblKey[keypadCol][keypadRow];
ret:
return ret;
}
uint8_t keypad_ProcessTask() {
if (keypad_IsPressed()) {
if (keyLastState == 0) {
keypadPressedKey = keypad_ProcessKey();
keyState = 1;
goto ret;
}
if (keyLastState == 1) { // down
keyState = 2;
keyPessTime = 0;
goto ret;
}
if (keyLastState == 2) { // hold
keyPessTime++;
goto ret;
}
} else if (keyLastState == 1 || keyLastState == 2) { // released
keyState = 3;
goto ret;
}
keyState = 0; //normal
ret:
keyLastState = keyState;
return keyState;
}
uint8_t keypad_GetState() {
return keyState;
}
uint8_t keypad_GetKey() {
if (keyState > 0) {
return keypadPressedKey;
}
return KEYNULL;
}
uint8_t keypad_GetTime() {
return keyPessTime;
}
void keypad_Task(void *pvParameters) {
// Init keypad
GPIO_DIR_IN(KEYPAD_COL0);
GPIO_DIR_IN(KEYPAD_COL1);
GPIO_DIR_IN(KEYPAD_COL2);
GPIO_DIR_IN(KEYPAD_COL3);
GPIO_DIR_IN(KEYPAD_ROW0);
GPIO_DIR_IN(KEYPAD_ROW1);
GPIO_DIR_IN(KEYPAD_ROW2);
GPIO_DIR_IN(KEYPAD_ROW3);
GPIO_FNC_INV(KEYPAD_COL0, PINMODE_INV);
GPIO_FNC_INV(KEYPAD_COL1, PINMODE_INV);
GPIO_FNC_INV(KEYPAD_COL2, PINMODE_INV);
GPIO_FNC_INV(KEYPAD_COL3, PINMODE_INV);
GPIO_FNC_INV(KEYPAD_ROW0, PINMODE_INV);
GPIO_FNC_INV(KEYPAD_ROW1, PINMODE_INV);
GPIO_FNC_INV(KEYPAD_ROW2, PINMODE_INV);
GPIO_FNC_INV(KEYPAD_ROW3, PINMODE_INV);
GPIO_FNC_INV(KEYPAD_POWER, PINMODE_INV);
keypad_SetRow(0);
for (;;) {
vTaskDelay(xDelay100);
keypad_ProcessTask();
if (keypad_GetKey() == KEYSTOP) {
xMotorMsgType MotorMsg;
MotorMsg.action = EMGSTOP;
xQueueSendFromISR(xMotorMsgQueue, &MotorMsg, NULL);
}
/*if (keypad_GetKey() == KEYPWR && keypad_GetTime() > 6) {
// TODO: Shut down motor! break etc, draws current from battery even in cpu-off!
// TODO: display shutdown counter! (doesn't shutdown until released (hardware feature))
// CPU in sleep??
xPowerMgmtMsg PowerMsg;
PowerMsg.xType = COMMAND_SHUTDOWN;
PowerMsg.shutdown.xDelay = xDelay100;
xQueueSend(xPowerMgmtMsgQueue, &PowerMsg, (TickType_t)0);
}*/
#if LOWSTACKWARNING
int stack = uxTaskGetStackHighWaterMark(NULL);
if (stack < 50) printf("Task task_Keypad has %u words left in stack.\r\n", stack);
#endif
}
}