Code reformatting and simple TUI

This commit is contained in:
mrkva 2012-11-30 21:09:27 +01:00
parent 12fe9e8283
commit 6a241cc06b
2 changed files with 75 additions and 55 deletions

View file

@ -16,7 +16,8 @@ int main() {
int source;
char buf_eq[65], buf_ra[65];
char *srcbuf = buf_eq;
int c = 'a';
int tmp;
rainbow = open("/tmp/ledbar/rainbow", O_RDWR);
equalizer = open("/tmp/ledbar/equalizer", O_RDWR);
@ -27,17 +28,38 @@ int main() {
return 1;
}
printf( "****************************************\n"
"* Usage: press [r] for rainbow mode *\n"
"* press [e] for equalizer mode *\n"
"* press [a] for automatic mode *\n"
"****************************************\n");
while(1) {
tv.tv_sec = 3;
tv.tv_usec = 0;
FD_ZERO(&readers);
FD_SET(rainbow, &readers);
FD_SET(equalizer, &readers);
FD_SET(fileno(stdin), &readers);
s = select(equalizer+1, &readers, NULL, NULL, &tv);
if(s == -1) {
perror("select failed");
return 2;
} else if(s) {
}
if(!s)
continue;
if(FD_ISSET(fileno(stdin), &readers)) {
tmp = getc(stdin);
if (tmp != '\r' && tmp != '\n') {
if(tmp != 'a' && tmp != 'r' && tmp != 'e')
printf("Invalid command!\n");
else
c = tmp;
}
}
if(FD_ISSET(equalizer, &readers)) {
r=read(equalizer, buf_eq, sizeof(buf_eq) - 1);
if(r == -1) {
@ -61,33 +83,31 @@ int main() {
reads_ra++;
}
// printf("eq: %i ra: %i r: %i\n", reads_eq, reads_ra, r);
if (c == 'a') {
if(reads_eq > READS_THR) {
reads_eq = 0;
reads_ra = 0;
if(srcbuf!=buf_eq)
printf("switching to equalizer\n");
srcbuf = buf_eq;
} else if(reads_ra > READS_THR*RAINBOW_DIVISOR) {
reads_eq = 0;
reads_ra = 0;
if(srcbuf!=buf_ra)
printf("switching to rainbow\n");
srcbuf = buf_ra;
}
} else if (c == 'r') {
srcbuf = buf_ra;
} else if (c == 'e') {
srcbuf = buf_eq;
}
if(write(serial, srcbuf, r) == -1) {
perror("write failed");
return 4;
}
// usleep(500);
}
}
close(serial);
close(rainbow);
close(equalizer);
return 0;
}