Basic multiplexing (use equalizer.py when music is playing, rainbow.py otherwise)

This commit is contained in:
mrkva 2012-11-29 19:51:45 +01:00
parent 35822a08c9
commit 12fe9e8283
2 changed files with 119 additions and 0 deletions

26
host_python/init.sh Executable file
View file

@ -0,0 +1,26 @@
#!/bin/bash
mkdir -p /tmp/ledbar
rm -f /tmp/ledbar/rainbow /tmp/ledbar/equalizer /tmp/ledbar/serial
mkfifo /tmp/ledbar/equalizer
mkfifo /tmp/ledbar/rainbow
mkfifo /tmp/ledbar/serial
while true; do
echo "Starting rainbow"
./rainbow.py -s > /tmp/ledbar/rainbow
done &
while true; do
echo "Starting equalizer"
./equalizer.py -s > /tmp/ledbar/equalizer
done &
while true; do
echo "Starting serial writer"
./send_to_serial.py /dev/ttyUSB0 < /tmp/ledbar/serial
done &
echo "Starting multiplexer"
../multiplexer/mux &
wait

93
mux/mux.c Normal file
View file

@ -0,0 +1,93 @@
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#define READS_THR 20
#define RAINBOW_DIVISOR 30
int main() {
int rainbow, equalizer, serial;
fd_set readers;
struct timeval tv;
int r,s,w;
int reads_eq=0, reads_ra=0;
int source;
char buf_eq[65], buf_ra[65];
char *srcbuf=buf_eq;
rainbow=open("/tmp/ledbar/rainbow", O_RDWR);
equalizer=open("/tmp/ledbar/equalizer", O_RDWR);
serial=open("/tmp/ledbar/serial", O_RDWR);
if(rainbow==-1 || equalizer==-1 || serial==-1) {
perror("Open failed");
return 1;
}
while(1) {
tv.tv_sec=3;
tv.tv_usec=0;
FD_ZERO(&readers);
FD_SET(rainbow, &readers);
FD_SET(equalizer, &readers);
s=select(equalizer+1, &readers, NULL, NULL, &tv);
if(s==-1) {
perror("select failed");
return 2;
} else if(s) {
if(FD_ISSET(equalizer, &readers)) {
r=read(equalizer, buf_eq, sizeof(buf_eq)-1);
if(r==-1) {
perror("eq read");
return 3;
}
int i;
for(i=0; i<r; i++) {
if(buf_eq[i]) {
reads_eq++;
break;
}
}
}
if(FD_ISSET(rainbow, &readers)) {
r=read(rainbow, buf_ra, sizeof(buf_ra)-1);
if(r==-1) {
perror("ra read");
return 3;
}
reads_ra++;
}
// printf("eq: %i ra: %i r: %i\n", reads_eq, reads_ra, r);
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;
}
if(write(serial, srcbuf, r)==-1) {
perror("write failed");
return 4;
}
// usleep(500);
}
}
close(serial);
close(rainbow);
close(equalizer);
return 0;
}