Skip to content
Snippets Groups Projects

elw3 vol

  • Clone with SSH
  • Clone with HTTP
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by aTc
    takt.c 2.91 KiB
    // gcc THISFILE.c -o volumed -l pthread  
    #include <unistd.h>
    #include <string.h>
    #include <limits.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <pthread.h>
    #include <linux/inotify.h>
    
    
    #define EVENT_BUF_LEN     (10 * (sizeof(struct inotify_event) + NAME_MAX + 1) )
    #define PCMPATH "/dev/snd/pcmC0D0p"
    #define PCMPATH1 "/dev/snd/pcmC1D0p"
    #define PCMPATH2 "/dev/snd/pcmC2D0p"
    #define PCMPATH3 "/dev/snd/pcmC3D0p"
    #define PCMPATH4 "/dev/snd/pcmC4D0p"
    #define POLLTIME 100000 //0.1s
    #define WHEELRANGE 2000
    
    
    unsigned int Delay = POLLTIME;
    int Hhandle;
    char Buffer[EVENT_BUF_LEN];
    pthread_t Moni;
    int Stat=1;
    int Value=0;
    int OldValue=-20;
    FILE *File;
    char Command[40];
    int Wheelrange=WHEELRANGE/100;
    int Pro;
    
    void *Tik(){
     while(Stat){
      fscanf(File,"%d", &Value);
      fseek(File, 0L, SEEK_SET);
      if (abs(Value-OldValue)>3){
       Pro= (Value+Wheelrange-1)/Wheelrange; //this should be Value^0.6-1 for an optimal scaling.
       snprintf(Command, sizeof(Command), "pactl set-sink-volume 0 %i%% ", Pro); //UUUUGLYYYY but still better than compiling against pulse.
       system(Command);
       OldValue=Value;
    //   printf( "changed to:%i\n",Pro );
      };
    //  printf( "%iRunning!%i\n",Stat,Value);
      usleep(Delay);
     };
    };
    void Openwheel(){
     if (fopen("/sys/bus/iio/devices/iio:device3/in_voltage2_input", "r")){ 
     File= fopen ("/sys/bus/iio/devices/iio:device3/in_voltage2_raw", "r");}
     else if(fopen("/sys/bus/iio/devices/iio:device4/in_voltage2_input", "r")){ 
     File= fopen ("/sys/bus/iio/devices/iio:device4/in_voltage2_raw", "r");}
     else if(fopen("/sys/bus/iio/devices/iio:device2/in_voltage2_input", "r")){ 
     File= fopen ("/sys/bus/iio/devices/iio:device2/in_voltage2_raw", "r");}
     else if(fopen("/sys/bus/iio/devices/iio:device1/in_voltage2_raw", "r")){ 
     File= fopen ("/sys/bus/iio/devices/iio:device1/in_voltage2_raw", "r");}
     else if(fopen("/sys/bus/iio/devices/iio:device0/in_voltage2_input", "r")){ 
     File= fopen ("/sys/bus/iio/devices/iio:device0/in_voltage2_raw", "r");}
     setvbuf(File, NULL, _IONBF, 0);
    }
    
    
    void main(int argc, char *argv[]){
     Openwheel();
     int jj;
     for (jj=1; jj < argc; jj++) {
      if (!strcmp(argv[jj], "Speed")){ 
       Delay = atoi(argv[jj+1]);
      }
      else if (!strcmp(argv[jj], "speed")){ //placeholder for addational flags
       Delay = atoi(argv[jj+1]);
      }  
      else if (!strcmp(argv[jj], "Range")){ //placeholder for addational flags
       Wheelrange = atoi(argv[jj+1])/100;
      }
     } 
     Hhandle = inotify_init();	
     inotify_add_watch( Hhandle,PCMPATH, IN_OPEN | IN_CLOSE_WRITE  ); 
     inotify_add_watch( Hhandle,PCMPATH1, IN_OPEN | IN_CLOSE_WRITE  ); 
     inotify_add_watch( Hhandle,PCMPATH2, IN_OPEN | IN_CLOSE_WRITE  ); 
     inotify_add_watch( Hhandle,PCMPATH3, IN_OPEN | IN_CLOSE_WRITE  ); 
     inotify_add_watch( Hhandle,PCMPATH4, IN_OPEN | IN_CLOSE_WRITE  ); 
     while (read( Hhandle, Buffer, EVENT_BUF_LEN )){
      if (Buffer[4]==32){
       Stat=1;
       pthread_create (&Moni, NULL, Tik, NULL);
      }
      else{  // printf( "going to sleep\n");
       Stat=0;
    //   OldValue=-20;
      };
     };
    };
    
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Please register or to comment