Advertisement
milanmetal

[C] MSREAL primer

Oct 30th, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.12 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /*
  5. char readBtn1() {
  6. }*/
  7.  
  8.  
  9. int main ()
  10. {
  11.   int rval;
  12.   FILE *fp;
  13.   FILE *fp1;
  14.   FILE *fp2;
  15.   char *str;
  16.   int tval1,tval2;
  17.   int tval1_old, tval2_old;
  18.  
  19.   size_t num_of_bytes = 1;
  20.  
  21.   float percentage;
  22.   float pcnt_step = 0.2;
  23.     long int period = 20000L;
  24.  
  25.   while(1)
  26.     {
  27.  
  28.       //Citanje vrednosti prvog tastera
  29.       fp1 = fopen ("/sys/class/gpio/gpio956/value", "r");
  30.       if(fp1==NULL)
  31.         puts("Problem pri otvaranju 1. fajla");
  32.  
  33.       str = (char *)malloc(num_of_bytes+1);
  34.       getline(&str, &num_of_bytes, fp1);
  35.  
  36.       if(fclose(fp1))
  37.         puts("Problem pri zatvaranju 1. fajla");
  38.  
  39.       sscanf(str, "%d", &tval1);
  40.       free(str);
  41.  
  42.       if(tval1 && tval1 != tval1_old && percentage+pcnt_step < 1) {
  43.         percentage+=pcnt_step;
  44.         tval1_old = tval1;
  45.       }
  46.  
  47.  
  48.       // -----------------------------------------------
  49.  
  50.       //Citanje vrednosti drugog tastera
  51.       fp2 = fopen ("/sys/class/gpio/gpio957/value", "r");
  52.       if(fp2==NULL)
  53.         puts("Problem pri otvaranju 2. fajla");
  54.  
  55.       str = (char *)malloc(num_of_bytes+1);
  56.       getline(&str, &num_of_bytes, fp2);
  57.  
  58.       if(fclose(fp2))
  59.         puts("Problem pri zatvaranju 2. fajla");
  60.  
  61.       sscanf(str, "%d", &tval2);
  62.       free(str);
  63.  
  64.       //Ispis procitanih vrednosti
  65.       printf("\n:Taster1: %d        Taster2: %d", tval1, tval2);
  66.       usleep(100000);
  67.  
  68.       if(tval2 && tval2 != tval2_old && percentage-pcnt_step > 0) {
  69.         percentage-=pcnt_step;
  70.         tval2_old = tval2;
  71.       }
  72.  
  73.       // ------------------------------------------------
  74.       fp = fopen("/proc/myled", "w");
  75.       if(fp == NULL)
  76.       {
  77.         printf("Cannot open /proc/myled for write\n");
  78.         return -1;
  79.       }
  80.       fputs("0x0F\n", fp);
  81.       fclose(fp);
  82.       usleep(percentage*period);
  83.  
  84.       fp = fopen("/proc/myled", "w");
  85.       if(fp == NULL)
  86.         {
  87.           printf("Cannot open /proc/myled for write\n");
  88.           return -1;
  89.         }
  90.       fputs("0x00\n", fp);
  91.       fclose(fp);
  92.       usleep((1-percentage)*period);
  93.     }
  94.  
  95.  
  96.     }
  97.  
  98.  
  99.  
  100.   return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement