-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp-sysutil.c
274 lines (247 loc) · 7.78 KB
/
app-sysutil.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* NXCTRL BeagleBone Black Control Library
*
* System Control Utilities App Program
*
* Copyright (C) 2014 Sungjin Chun <chunsj@gmail.com>
*
* 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 3 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#define _GNU_SOURCE
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/reboot.h>
#include <NXCTRL_appEx.h>
#define LOGO_WIDTH 128
#define LOGO_HEIGHT 64
#define FONT_WIDTH 6
#define FONT_HEIGHT 8
#define MENU_SEL_CHAR ((unsigned char)16)
#define DPY_IDLE_COUNT_MAX 300
#define MIN_ACTION_DURATION 200
#define MENU_IDX_COUNT 8
#define MENU_IDX_GO_MAIN 0
#define MENU_IDX_GO_CONNINFO 1
#define MENU_IDX_GO_SYSINFO 2
#define MENU_IDX_GO_PERI 3
#define MENU_IDX_GO_SPARK 4
#define MENU_IDX_MENU_OFF 5
#define MENU_IDX_TURN_OFF 6
#define MENU_IDX_EXIT_MENU 7
static NXCTRL_BOOL MENU_U_BUTTON_STATE = NXCTRL_LOW;
static NXCTRL_BOOL MENU_D_BUTTON_STATE = NXCTRL_LOW;
static NXCTRL_BOOL EXEC_BUTTON_STATE = NXCTRL_LOW;
static unsigned char DPY_IDLE_COUNT = 0;
static unsigned char MENU_IDX = MENU_IDX_GO_MAIN;
static NXCTRL_BOOL IN_MENU = NXCTRL_FALSE;
static unsigned long long LAST_ACTION_TIME = 0;
static NXCTRL_BOOL
canAction (NXCTRL_VOID) {
struct timespec tm;
unsigned long long timeInMillis;
extern int clock_gettime(int, struct timespec *);
clock_gettime(_POSIX_CPUTIME, &tm);
timeInMillis = tm.tv_sec * 1000 + tm.tv_nsec/1000000;
if ((timeInMillis - LAST_ACTION_TIME) > MIN_ACTION_DURATION) {
LAST_ACTION_TIME = timeInMillis;
return NXCTRL_TRUE;
} else
return NXCTRL_FALSE;
}
static NXCTRL_VOID
updateMenuButtonState (LPNXCTRLAPP pApp) {
if (MENU_U_BUTTON_STATE == NXCTRL_LOW) {
if (pApp->digitalRead(MENU_U_BUTTON_BANK, MENU_U_BUTTON_PIN) == NXCTRL_HIGH) {
MENU_U_BUTTON_STATE = NXCTRL_HIGH;
DPY_IDLE_COUNT = 0;
}
} else {
if (pApp->digitalRead(MENU_U_BUTTON_BANK, MENU_U_BUTTON_PIN) == NXCTRL_LOW) {
MENU_U_BUTTON_STATE = NXCTRL_LOW;
DPY_IDLE_COUNT = 0;
}
}
if (MENU_D_BUTTON_STATE == NXCTRL_LOW) {
if (pApp->digitalRead(MENU_D_BUTTON_BANK, MENU_D_BUTTON_PIN) == NXCTRL_HIGH) {
MENU_D_BUTTON_STATE = NXCTRL_HIGH;
DPY_IDLE_COUNT = 0;
}
} else {
if (pApp->digitalRead(MENU_D_BUTTON_BANK, MENU_D_BUTTON_PIN) == NXCTRL_LOW) {
MENU_D_BUTTON_STATE = NXCTRL_LOW;
DPY_IDLE_COUNT = 0;
}
}
}
static NXCTRL_VOID
updateExecButtonState (LPNXCTRLAPP pApp) {
if (EXEC_BUTTON_STATE == NXCTRL_LOW) {
if (pApp->digitalRead(EXEC_BUTTON_BANK, EXEC_BUTTON_PIN) == NXCTRL_HIGH) {
EXEC_BUTTON_STATE = NXCTRL_HIGH;
DPY_IDLE_COUNT = 0;
}
} else {
if (pApp->digitalRead(EXEC_BUTTON_BANK, EXEC_BUTTON_PIN) == NXCTRL_LOW) {
EXEC_BUTTON_STATE = NXCTRL_LOW;
DPY_IDLE_COUNT = 0;
}
}
}
static char *
mkMenuSTR (char *rch, const char *pszName, int nMenu) {
sprintf(rch, "%c %s\n",
(MENU_IDX == nMenu ? MENU_SEL_CHAR : ' '),
pszName);
return rch;
}
static char *
mkAppMenuSTR (char *rch, const char *pszName, int nMenu) {
sprintf(rch, "%c %c%s\n",
(MENU_IDX == nMenu ? MENU_SEL_CHAR : ' '),
MENU_SEL_CHAR-9,
pszName);
return rch;
}
static NXCTRL_VOID
displayMenu (LPNXCTRLAPP pApp) {
char rch[21];
pApp->clearDisplay();
pApp->setCursor(0, 0);
pApp->writeSTR("TC");
pApp->drawLine(13, 6, 127, 6, NXCTRL_ON);
pApp->setCursor(0, 16);
if (MENU_IDX < 5)
pApp->writeSTR(mkAppMenuSTR(rch, "MAIN", MENU_IDX_GO_MAIN));
if (MENU_IDX < 6)
pApp->writeSTR(mkAppMenuSTR(rch, "CONNECTION", MENU_IDX_GO_CONNINFO));
if (MENU_IDX < 7)
pApp->writeSTR(mkAppMenuSTR(rch, "SYSTEM", MENU_IDX_GO_SYSINFO));
pApp->writeSTR(mkAppMenuSTR(rch, "PERIPHERAL", MENU_IDX_GO_PERI));
pApp->writeSTR(mkAppMenuSTR(rch, "SPARK CORE", MENU_IDX_GO_SPARK));
if (MENU_IDX >= 5)
pApp->writeSTR(mkMenuSTR(rch, "SCREEN OFF", MENU_IDX_MENU_OFF));
if (MENU_IDX >= 6)
pApp->writeSTR(mkMenuSTR(rch, "POWER OFF", MENU_IDX_TURN_OFF));
if (MENU_IDX >= 7)
pApp->writeSTR(mkMenuSTR(rch, "EXIT MENU", MENU_IDX_EXIT_MENU));
pApp->updateDisplay();
}
NXCTRL_VOID
NXCTRLAPP_init (LPNXCTRLAPP pApp) {
MENU_U_BUTTON_STATE = pApp->digitalRead(MENU_U_BUTTON_BANK, MENU_U_BUTTON_PIN);
MENU_D_BUTTON_STATE = pApp->digitalRead(MENU_D_BUTTON_BANK, MENU_D_BUTTON_PIN);
EXEC_BUTTON_STATE = pApp->digitalRead(EXEC_BUTTON_BANK, EXEC_BUTTON_PIN);
DPY_IDLE_COUNT = 0;
MENU_IDX = MENU_IDX_GO_MAIN;
IN_MENU = NXCTRL_FALSE;
LAST_ACTION_TIME = 0;
IN_MENU = NXCTRL_TRUE;
while (MENU_U_BUTTON_STATE == NXCTRL_HIGH) {
pApp->sleep(100, 0);
MENU_U_BUTTON_STATE = pApp->digitalRead(MENU_U_BUTTON_BANK, MENU_U_BUTTON_PIN);
}
while (MENU_D_BUTTON_STATE == NXCTRL_HIGH) {
pApp->sleep(100, 0);
MENU_D_BUTTON_STATE = pApp->digitalRead(MENU_D_BUTTON_BANK, MENU_D_BUTTON_PIN);
}
while (EXEC_BUTTON_STATE == NXCTRL_HIGH) {
pApp->sleep(100, 0);
EXEC_BUTTON_STATE = pApp->digitalRead(EXEC_BUTTON_BANK, EXEC_BUTTON_PIN);
}
pApp->clearDisplay();
pApp->updateDisplay();
}
NXCTRL_VOID
NXCTRLAPP_clean (LPNXCTRLAPP pApp) {
}
NXCTRL_VOID
NXCTRLAPP_run (LPNXCTRLAPP pApp) {
updateMenuButtonState(pApp);
updateExecButtonState(pApp);
displayMenu(pApp);
if (MENU_U_BUTTON_STATE == NXCTRL_ON || MENU_D_BUTTON_STATE == NXCTRL_ON) {
if (IN_MENU) {
if (canAction()) {
if (MENU_D_BUTTON_STATE == NXCTRL_ON) {
if (MENU_IDX < MENU_IDX_COUNT - 1)
MENU_IDX++;
else
MENU_IDX = 0;
} else {
if (MENU_IDX > 0)
MENU_IDX--;
else
MENU_IDX = MENU_IDX_COUNT - 1;
}
displayMenu(pApp);
}
} else {
IN_MENU = NXCTRL_TRUE;
displayMenu(pApp);
canAction();
}
}
if (EXEC_BUTTON_STATE == NXCTRL_ON) {
if (IN_MENU) {
if (canAction()) {
switch (MENU_IDX) {
case MENU_IDX_GO_MAIN:
pApp->nCmd = 1234;
return;
case MENU_IDX_GO_CONNINFO:
pApp->nCmd = 1234+3;
return;
case MENU_IDX_GO_SYSINFO:
pApp->nCmd = 1234+4;
return;
case MENU_IDX_GO_PERI:
pApp->nCmd = 1234+5;
return;
case MENU_IDX_GO_SPARK:
pApp->nCmd = 1234+6;
return;
case MENU_IDX_EXIT_MENU:
pApp->nCmd = 1;
return;
case MENU_IDX_TURN_OFF:
pApp->clearDisplay();
pApp->setCursor(3*FONT_WIDTH, 3*FONT_HEIGHT);
pApp->writeSTR("TURNING OFF...");
pApp->updateDisplay();
sync();
sync();
sync();
pApp->sleep(500, 0);
//pApp->clearDisplay();
//pApp->updateDisplay();
sync();
sync();
sync();
//reboot(RB_POWER_OFF);
system("poweroff");
break;
case MENU_IDX_MENU_OFF:
pApp->nCmd = 2;
return;
default:
break;
}
}
}
}
}