Skip to content

Instantly share code, notes, and snippets.

@elecnix
Created January 10, 2009 03:57
Show Gist options
  • Select an option

  • Save elecnix/45373 to your computer and use it in GitHub Desktop.

Select an option

Save elecnix/45373 to your computer and use it in GitHub Desktop.
Control XMMS with parallel port
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/kd.h>
#include "parapin.h"
void xmms_command(int command);
void gom_volume(int delta);
void run_command(char* commandLine, char** argList);
#define XMMS_FWD 1
#define XMMS_REW 2
#define XMMS_PLAY 3
#define XMMS_PAUSE 4
#define XMMS_STOP 5
#define XMMS_VOL_P 6
#define XMMS_VOL_M 7
#define VOLUME_DELTA 5
int main(int argc, char *argv[])
{
struct timespec t_check;
struct timespec t_refrac;
int value;
int r_pin = LP_PIN02;
int min_nrefrac = 200000000;
int max_nrefrac = 500000000;
int refrac_delta = 50000000;
int fd = open("/dev/tty0", O_RDONLY);
t_check.tv_sec = 0;
t_check.tv_nsec = 100000000;
t_refrac.tv_sec = 0;
t_refrac.tv_nsec = max_nrefrac;
if (pin_init_user(LPT1) < 0) {
exit(0);
}
pin_output_mode(LP_DATA_PINS);
pin_input_mode(LP_PIN10 | LP_PIN11 | LP_PIN12 | LP_PIN13 | LP_PIN15);
printf("\nstarting\n");
clear_pin(r_pin);
set_pin(r_pin);
while (1) {
value = pin_is_set(LP_PIN10 | LP_PIN11 | LP_PIN12 | LP_PIN13 | LP_PIN15);
//printf("Pin 10: %s Pin 11: %s\n", value_10 ? "HIGH":"LOW", value_11 ? "HIGH":"LOW");
printf("Value: %i\n", value);
if (value) {
ioctl(fd,KDMKTONE,(10<<16)+(1193180/1000));
if (value & LP_PIN10) {
printf("Pin 10 is high\n");
xmms_command(XMMS_REW);
}
if (value & LP_PIN11) {
printf("Pin 11 is high\n");
gom_volume(VOLUME_DELTA);
}
if (value & LP_PIN12) {
printf("Pin 12 is high\n");
xmms_command(XMMS_PLAY);
}
if (value & LP_PIN13) {
printf("Pin 13 is high\n");
xmms_command(XMMS_FWD);
}
if (value & LP_PIN15) {
printf("Pin 15 is high\n");
gom_volume(-VOLUME_DELTA);
}
clear_pin(r_pin);
nanosleep(&t_refrac, NULL);
t_refrac.tv_nsec -= refrac_delta;
if (t_refrac.tv_nsec < min_nrefrac) t_refrac.tv_nsec = min_nrefrac;
} else {
t_refrac.tv_nsec = max_nrefrac;
nanosleep(&t_check, NULL);
}
set_pin(r_pin);
}
printf("\n\n");
return 0;
}
void xmms_command(int command) {
int cmd_length = 30;
char commandLine[cmd_length];
char* argList[6];
snprintf(commandLine, cmd_length, "./xmms-cmd.sh");
argList[0] = "./xmms-cmd.sh";
if (command == XMMS_FWD) {
argList[1] = "--fwd";
} else if (command == XMMS_REW) {
argList[1] = "--rew";
} else if (command == XMMS_PLAY) {
argList[1] = "--play-pause";
} else if (command == XMMS_PAUSE) {
argList[1] = "--pause";
} else if (command == XMMS_STOP) {
argList[1] = "--stop";
} else if (command == XMMS_VOL_P) {
argList[1] = "-v"; // not yet supported
} else if (command == XMMS_VOL_M) {
argList[1] = "-v"; // not yet supported
} else {
printf("xmms_command: Unknown command: %i\n", command);
exit;
}
argList[2] = NULL;
run_command(commandLine, argList);
}
void gom_volume(int delta) {
int cmd_length = 30;
char commandLine[cmd_length];
char* argList[6];
fprintf(stderr, "[gom_volume] Delta: %i\n", delta);
snprintf(commandLine, cmd_length, "gom");
argList[0] = "gom";
argList[1] = "--volume";
if (delta > 0) {
snprintf(argList[2], 10, "+%i", delta);
} else {
snprintf(argList[2], 10, "%i", delta);
}
argList[3] = NULL;
run_command(commandLine, argList);
}
void run_command(char* commandLine, char** argList) {
int returnValue = fork();
switch (returnValue) {
case 0:
returnValue = execvp(commandLine, argList); // should never return
perror("[child] Can't execute\n");
exit(1); // Causes the child process to become zombie until the parent process exits
break;
case -1:
fprintf(stderr, "Can't fork!");
break;
default:
//fprintf(stderr, "Child process ID: %i\n", returnValue);
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment