Stefan Schuermans commited on 2013-12-28 00:23:04
Showing 3 changed files, with 48 additions and 7 deletions.
... | ... |
@@ -196,6 +196,7 @@ static int serial_settings_set(int fd, int settings, int speed) |
196 | 196 |
// returns error code (not for device-errors, 0 for success) |
197 | 197 |
static int recv_and_out(SOCKET udpSocket, int dev_fd, |
198 | 198 |
int *p_device_output_active, |
199 |
+ unsigned int min_interval_ms, |
|
199 | 200 |
unsigned int format_change, |
200 | 201 |
unsigned int format_height, unsigned int format_width, |
201 | 202 |
unsigned int format_channels, |
... | ... |
@@ -205,10 +206,12 @@ static int recv_and_out(SOCKET udpSocket, int dev_fd, |
205 | 206 |
fd_set readFds, errFds; |
206 | 207 |
stBlinkenFrame *pFrame; |
207 | 208 |
char buffer[65536]; // 64kB is more than maximum UDP size |
208 |
- int maxFd, len, dev_eof; |
|
209 |
+ int maxFd, len, dev_eof, out_ok; |
|
209 | 210 |
struct timeval start, timeout, *p_timeout, end; |
211 |
+ struct timeval out_last, out_now; |
|
210 | 212 |
|
211 | 213 |
dev_eof = 0; |
214 |
+ gettimeofday(&out_last, NULL); |
|
212 | 215 |
for (;;) { |
213 | 216 |
// wait for next frame |
214 | 217 |
FD_ZERO(&readFds); |
... | ... |
@@ -272,8 +275,19 @@ static int recv_and_out(SOCKET udpSocket, int dev_fd, |
272 | 275 |
// free frame |
273 | 276 |
BlinkenFrameFree(pFrame); |
274 | 277 |
|
278 |
+ // check if minimum interval between output frames is met |
|
279 |
+ gettimeofday(&out_now, NULL); |
|
280 |
+ if (out_now.tv_sec > out_last.tv_sec - min_interval_ms / 1000) |
|
281 |
+ out_ok = 1; |
|
282 |
+ else if ((out_now.tv_sec - out_last.tv_sec) * 1000 + |
|
283 |
+ ((long)out_now.tv_usec - (long)out_last.tv_usec) / 1000 > |
|
284 |
+ min_interval_ms) |
|
285 |
+ out_ok = 1; |
|
286 |
+ else |
|
287 |
+ out_ok = 0; |
|
288 |
+ |
|
275 | 289 |
// output data to device |
276 |
- if (dev_fd != -1 && len > 0) { |
|
290 |
+ if (dev_fd != -1 && len > 0 && out_ok) { |
|
277 | 291 |
if (write(dev_fd, buffer, len) != len) { |
278 | 292 |
if (*p_device_output_active) |
279 | 293 |
printf("error writing to device\n"); |
... | ... |
@@ -283,6 +297,8 @@ static int recv_and_out(SOCKET udpSocket, int dev_fd, |
283 | 297 |
if (!*p_device_output_active) |
284 | 298 |
printf("restarted output to device...\n"); |
285 | 299 |
*p_device_output_active = 1; |
300 |
+ // remember last time of output |
|
301 |
+ out_last = out_now; |
|
286 | 302 |
} |
287 | 303 |
} |
288 | 304 |
} |
... | ... |
@@ -331,7 +347,9 @@ static int recv_and_out(SOCKET udpSocket, int dev_fd, |
331 | 347 |
static int open_and_output(SOCKET udpSocket, char *device, |
332 | 348 |
int *p_device_output_active, |
333 | 349 |
int serial_settings_change, int serial_settings, |
334 |
- int serial_speed, unsigned int format_change, |
|
350 |
+ int serial_speed, |
|
351 |
+ unsigned int min_interval_ms, |
|
352 |
+ unsigned int format_change, |
|
335 | 353 |
unsigned int format_height, |
336 | 354 |
unsigned int format_width, |
337 | 355 |
unsigned int format_channels, |
... | ... |
@@ -360,6 +378,7 @@ static int open_and_output(SOCKET udpSocket, char *device, |
360 | 378 |
} |
361 | 379 |
// receive frames and output to device |
362 | 380 |
err = recv_and_out(udpSocket, dev_fd, p_device_output_active, |
381 |
+ min_interval_ms, |
|
363 | 382 |
format_change, format_height, format_width, |
364 | 383 |
format_channels, format_colors, proto, 0, 0); |
365 | 384 |
|
... | ... |
@@ -376,6 +395,7 @@ static int open_and_output_loop(SOCKET udpSocket, char *device, |
376 | 395 |
int serial_settings, int serial_speed, |
377 | 396 |
int reopen_device, |
378 | 397 |
unsigned int reopen_device_ms, |
398 |
+ unsigned int min_interval_ms, |
|
379 | 399 |
unsigned int format_change, |
380 | 400 |
unsigned int format_height, |
381 | 401 |
unsigned int format_width, |
... | ... |
@@ -394,7 +414,8 @@ static int open_and_output_loop(SOCKET udpSocket, char *device, |
394 | 414 |
err = open_and_output(udpSocket, device, |
395 | 415 |
&device_output_active, |
396 | 416 |
serial_settings_change, serial_settings, |
397 |
- serial_speed, format_change, format_height, |
|
417 |
+ serial_speed, min_interval_ms, |
|
418 |
+ format_change, format_height, |
|
398 | 419 |
format_width, format_channels, format_colors, |
399 | 420 |
proto); |
400 | 421 |
if (err != 0 || !reopen_device) |
... | ... |
@@ -407,6 +428,7 @@ static int open_and_output_loop(SOCKET udpSocket, char *device, |
407 | 428 |
|
408 | 429 |
// only fetch data from socket for a short time |
409 | 430 |
err = recv_and_out(udpSocket, -1, &device_output_active, |
431 |
+ min_interval_ms, |
|
410 | 432 |
format_change, format_height, format_width, |
411 | 433 |
format_channels, format_colors, proto, 1, |
412 | 434 |
reopen_device_ms); |
... | ... |
@@ -430,7 +452,8 @@ int main(int argCnt, char **args) |
430 | 452 |
etBlinkenProto proto; |
431 | 453 |
unsigned int format_change, format_height, format_width, format_channels, |
432 | 454 |
format_colors; |
433 |
- unsigned int height, width, channels, colors, reopen_device_ms; |
|
455 |
+ unsigned int height, width, channels, colors; |
|
456 |
+ unsigned int reopen_device_ms, min_interval_ms; |
|
434 | 457 |
int serial_settings_change, reopen_device; |
435 | 458 |
char txt[64]; |
436 | 459 |
unsigned short port; |
... | ... |
@@ -466,6 +489,8 @@ int main(int argCnt, char **args) |
466 | 489 |
" settings to use for serial devices (defaults to no change)\n" |
467 | 490 |
" -o <milliseconds>\n" |
468 | 491 |
" reopen device after short time on error (defaults to not reopen)\n" |
492 |
+ " -i <milliseconds>\n" |
|
493 |
+ " minimum interval between two frames on output (defaults to 0)\n" |
|
469 | 494 |
"\n", args[0]); |
470 | 495 |
return 0; |
471 | 496 |
} |
... | ... |
@@ -489,6 +514,7 @@ int main(int argCnt, char **args) |
489 | 514 |
serial_speed = 0; |
490 | 515 |
serial_settings_change = 0; |
491 | 516 |
reopen_device = 0; |
517 |
+ min_interval_ms = 0; |
|
492 | 518 |
for (i = 1; i < argCnt; i++) { |
493 | 519 |
|
494 | 520 |
// local address |
... | ... |
@@ -601,6 +627,17 @@ int main(int argCnt, char **args) |
601 | 627 |
} else |
602 | 628 |
printf("missing time for \"-o\"\n"); |
603 | 629 |
} |
630 |
+ // minimum interval between two frame on output |
|
631 |
+ else if (strcmp(args[i], "-i") == 0) { |
|
632 |
+ if (i + 1 < argCnt) { |
|
633 |
+ i++; |
|
634 |
+ if (sscanf(args[i], "%u", &min_interval_ms) != 1) { |
|
635 |
+ min_interval_ms = 0; |
|
636 |
+ printf("invalid number of milliseconds \"%s\"\n", args[i]); |
|
637 |
+ } |
|
638 |
+ } else |
|
639 |
+ printf("missing time for \"-i\"\n"); |
|
640 |
+ } |
|
604 | 641 |
// unknown parameter |
605 | 642 |
else |
606 | 643 |
printf |
... | ... |
@@ -627,7 +664,7 @@ int main(int argCnt, char **args) |
627 | 664 |
// open device and output frames in a loop |
628 | 665 |
open_and_output_loop(udpSocket, device, |
629 | 666 |
serial_settings_change, serial_settings, serial_speed, |
630 |
- reopen_device, reopen_device_ms, |
|
667 |
+ reopen_device, reopen_device_ms, min_interval_ms, |
|
631 | 668 |
format_change, format_height, format_width, |
632 | 669 |
format_channels, format_colors, proto); |
633 | 670 |
|