/
lcdscreenmain.cpp
executable file
·391 lines (347 loc) · 8.77 KB
/
lcdscreenmain.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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// Filename: lcdmainscreen.cpp
// Description: main screen and user input for record and play
//
// Open Source Licensing GPL 3
//
// Author: Dr. Valentin Illich, www.valentins-qtsolutions.de
//--------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include "lcdscreenmain.h"
#include "lcdscreentimer.h"
#include "lcdscreenselect.h"
#include "lcdscreencpu.h"
#include "lcdscreenmessages.h"
#include "screenids.h"
#ifdef QT_EMULATION
int spawn (const char* /*program*/, const char** /*arg_list*/)
{
return 1234;
}
void writepipe(const char */*str*/)
{
}
#else
int filedes[2];
int spawn (const char* program, const char** arg_list)
{
pipe(filedes);
pid_t child_pid;
/* Duplicate this process. */
child_pid = fork ();
if (child_pid != 0)
{
close(filedes[0]);
/* This is the parent process. */
return child_pid;
}
else
{
dup2(filedes[0], 0);
/* Now execute PROGRAM, searching for it in the path. */
execvp (program, (char**)arg_list);
/* The execvp function returns only if an error occurs. */
fprintf (stderr, "an error occurred in execvp\n");
_exit (-1);
}
}
void writepipe(const char *str)
{
// Write to child’s stdin
write(filedes[1], str, strlen(str));
}
#endif
static objectinfo strings[] = {
{ eText,true, 7, 0, 0, 0, 0,"31.01.2015 12:00:39" },
{ eText,true, 0,56, 0, 0, 0,"Power About" },
{ eText,true, 48,56, 0, 0, 0,"Timer" },
{ eText,true, 80, 9, 0, 0, 1,"Record" },
{ eText,true, 96,38, 0, 0, 1,"Play" },
{ eText,true, 17,24, 0, 0, 3,"--:--:--" },
{ eNone,false, 0,0,0,0, 0,NULL },
};
static lcdscreenmain mainscreen;
int outfd[2];
int infd[2];
lcdscreenmain::lcdscreenmain()
: lcdscreen(strings)
, m_recordId(-1)
, m_playId(-1)
, m_playSuspended(false)
, m_startTime(-1)
, m_lastScreen(eNoAction)
{
strcpy(m_recordfile,"");
setupScreen(RECORD_PLAY_SCREEN,this,"playScreen");
}
lcdscreenmain::~lcdscreenmain()
{
}
int proc_exists(pid_t pid)
{
#ifdef QT_EMULATION
return pid;
#else
// this is a better method...
if( kill(pid,0)==0 || errno==EPERM )
return 1;
else
return 0;
#endif
}
void lcdscreenmain::activatedHandler()
{
strings[2].visible=true;
strings[3].visible=true;
strings[4].visible=true;
updateRemaining();
}
keyType lcdscreenmain::secTimerHandler(struct tm *result)
{
static char buffer[128];
static int secs = 0;
if( m_recordId>=0 )
{
if( !proc_exists(m_recordId) )
{
myprintf("record no longer running: %d!\n",errno);
stopRecording();
}
}
if( m_playId>=0 )
{
if( m_playSuspended ) m_startTime++;
if( !proc_exists(m_playId) )
{
myprintf("play no longer running: %d!\n",errno);
stopPlay();
}
}
int actTime = lcdscreen::toTimeInSeconds(result);
int delta = actTime-m_startTime;
int dh = delta/3600;
delta = delta % 3600;
int dm = delta / 60;
delta = delta % 60;
int ds = delta;
if( lcdscreentimer::timerActive() )
{
if( (actTime%2)==0 )
strings[2].visible=true;
else
strings[2].visible=false;
}
if( (m_recordId>=0) || (m_playId>=0) )
{
static char buffer[128];
if( (actTime%2)==0 )
{
if( m_recordId>=0 ) strings[3].visible=true;
if( m_playId>=0 ) strings[4].visible=true;
if( m_playSuspended ) strings[5].visible = true;
}
else
{
if( m_recordId>=0 ) strings[3].visible=false;
if( m_playId>=0 ) strings[4].visible=false;
if( m_playSuspended ) strings[5].visible = false;
}
sprintf(buffer,"%02d:%02d:%02d",dh,dm,ds);
strings[5].text = buffer;
}
sprintf(buffer,"%02d.%02d.%04d %02d:%02d:%02d",result->tm_mday,result->tm_mon+1,result->tm_year+1900,result->tm_hour,result->tm_min,result->tm_sec);
strings[0].text = buffer;
repaint();
if( (secs%60)==0 )
{
FILE *fp = fopen("/home/pi/usbstick/cputemp.csv","a");
if( fp ) fprintf(fp,"%s;%s\n",buffer,lcdscreencpu::getTemp());
fclose(fp);
}
secs++;
return eKeyNone;
}
keyType lcdscreenmain::keyEventHandler( keyType key )
{
keyType ret = eKeyNone;
switch(key)
{
case eKeyUp:
if( m_recordId>=0 )
stopRecording();
else
startRecording();
break;
case eKeyDown:
if( m_playId>=0 )
{
stopPlay();
strcpy(m_recordfile,"");
}
else if( strlen(m_recordfile)>0 )
startPlay(m_recordfile);
else
{
m_lastScreen = eSelection;
lcdscreen::activateScreen(SELECT_SCREEN);
}
break;
case eKeyA:
if( lcdscreentimer::timerActive() || m_recordId>=0 )
{
m_lastScreen = eNoShutdown;
lcdscreen::activateScreen(POWER_DENIED);
}
else if( m_playId>=0 )
{
m_startTime += 10;
time_t clock = time(NULL);
struct tm *result = localtime(&clock);
int actTime = lcdscreen::toTimeInSeconds(result);
if( m_startTime>actTime ) m_startTime = actTime;
writepipe("<");
repaint();
}
else
{
m_lastScreen = eShutDown;
lcdscreenQuestion::setMessage(" Power Down ?");
lcdscreenQuestion::setButtons(" Yes Cancel");
lcdscreen::activateScreen(MESSAGE_SCREEN);
}
break;
case eKeyB:
if( m_playId>=0 )
{
if( m_playSuspended ) strings[5].visible = true;
m_playSuspended = !m_playSuspended;
writepipe(" ");
if( m_playSuspended )
//strings[1].text = "Cont About";
strings[1].text = "-10 s Cont +10 s";
else
//strings[1].text = "Pause About";
strings[1].text = "-10 s Pause +10 s";
}
else
lcdscreen::activateScreen(TIMER_SCREEN);
break;
case eKeyC:
if( m_playId>=0 )
{
m_startTime -= 10;
writepipe(">");
repaint();
}
else
lcdscreen::activateScreen(ABOUT_SCREEN);
break;
case eKeyCancel:
switch( m_lastScreen )
{
case eSelection:
if( strlen(lcdscreenselect::selectedFile())>0 )
{
startPlay(lcdscreenselect::selectedFile());
lcdscreenselect::resetSelectedFile();
}
break;
case eShutDown:
if( lcdscreenQuestion::YesClicked() )
{
lcdscreenQuestion::setMessage(" Shut Down...");
lcdscreenQuestion::setButtons("");
lcdscreen::activateScreen(MESSAGE_SCREEN);
system("sudo umount /dev/sda1");
system("sudo shutdown -h now");
}
break;
default:
break;
}
break;
default:
break;
}
if( ret!=eKeyNone )
myprintf("mainscreen returning %s\n",lcdscreen::keyToString(ret));
return ret;
}
void lcdscreenmain::updateRemaining()
{
int hours=0,mins=0,secs=0;
lcdscreenselect::getRemainig(hours,mins,secs);
sprintf(m_remaining,"%02d:%02d:%02d",hours,mins,secs);
strings[5].text = m_remaining;
}
void lcdscreenmain::startRecording()
{
if( m_recordId==-1 )
{
time_t clock = time(NULL);
struct tm *result = localtime(&clock);
sprintf(m_recordfile,"/home/pi/usbstick/%04d-%02d-%02dT%02d-%02d-%02d.wav",result->tm_year+1900,result->tm_mon+1,result->tm_mday,result->tm_hour,result->tm_min,result->tm_sec);
const char *args[] = { "/usr/bin/arecord","-f", "cd", "-Dhw:1,0", "-r", "44100", m_recordfile, NULL };
m_recordId = spawn("/usr/bin/arecord",args);
myprintf("record process id is %d\n",m_recordId);
m_startTime = lcdscreen::toTimeInSeconds(result);
strings[5].text = "00:00:00";
repaint();
}
}
void lcdscreenmain::stopRecording()
{
if( m_recordId>=0 )
{
#ifndef QT_EMULATION
int ret = kill(m_recordId,SIGTERM);
printf("record process killed: %d\n",ret);
#endif
m_recordId = -1;
strings[3].visible=true;
updateRemaining();
repaint();
}
}
void lcdscreenmain::startPlay(char *playfile)
{
if( m_playId==-1 )
{
time_t clock = time(NULL);
struct tm *result = localtime(&clock);
const char *args[] = { "/usr/bin/aplay","-f", "cd", "-Dhw:1,0", "-r", "44100", "-i", playfile, NULL };
m_playId = spawn("/usr/bin/aplay",args);
myprintf("play process id is %d\n",m_playId);
m_startTime = lcdscreen::toTimeInSeconds(result);
//strings[1].text = "Pause About";
strings[1].text = "-10 s Pause +10 s";
strings[2].visible=false;
strings[4].text = "Stop";
strings[5].text = "00:00:00";
repaint();
}
}
void lcdscreenmain::stopPlay()
{
if( m_playId>=0 )
{
#ifndef QT_EMULATION
int ret = kill(m_playId,SIGTERM);
printf("play process killed: %d\n",ret);
close(filedes[1]);
#endif
m_playId = -1;
m_playSuspended = false;
strings[1].text = "Power About";
strings[4].text = "Play";
strings[2].visible=true;
strings[4].visible=true;
strings[5].visible = true;
updateRemaining();
repaint();
}
}