pastebin - collaborative debugging tool
eckelmann.kpaste.net RSS


Quantron beeper-weeper demo1
Posted by Anonymous on Wed 11th Dec 2019 15:37
raw | new post
view followups (newest first): Quantron beeper-weeper demo1 by Anonymous

  1. /*
  2.  * qaweep1.c - Quantron beeper-weeper demo V0.0.1
  3.  *
  4.  * Written by Roland Mainz <r.mainz@eckelmann.de>
  5.  */
  6. #include <errno.h>
  7. #include <fcntl.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <sys/stat.h>
  11. #include <sys/types.h>
  12. #include <unistd.h>
  13.  
  14. #define NUM_ELEMENTS_IN_ARRAY(ar) (sizeof(ar) / sizeof (ar[0]))
  15. const double freqs[] = {
  16.         440.0000,
  17.         466.1638,
  18.         493.8833,
  19.         523.2511,
  20.         554.3653,
  21.         587.3295,
  22.         622.2540,
  23.         659.2551,
  24.         698.4565,
  25.         739.9888,
  26.         783.9909,
  27.         830.6094,
  28.         880.0000,
  29.         932.3275,
  30.         987.7666,
  31.         1046.502,
  32.         1108.731,
  33.         1174.659,
  34.         1244.508
  35. };
  36.  
  37.  
  38. int main(int ac, char *av[])
  39. {
  40.         int     retval = EXIT_FAILURE;
  41.         int     fd,
  42.                 gpiodirfd = -1;
  43.  
  44.         fd = open("/sys/class/gpio/export", O_WRONLY);
  45.         if (fd == -1) {
  46.                 perror("Unable to open /sys/class/gpio/export");
  47.                 goto exit_ioerr;
  48.         }
  49.  
  50.         if (write(fd, "11", 2) != 2) {
  51.                 perror("Error writing to /sys/class/gpio/export");
  52.                 goto exit_ioerr;
  53.         }
  54.  
  55.         (void)close(fd);
  56.    
  57.         gpiodirfd = open("/sys/class/gpio/gpio11/", O_DIRECTORY);
  58.         if (gpiodirfd == -1) {
  59.                 perror("Unable to open /sys/class/gpio/gpio11/");
  60.                 goto exit_ioerr;
  61.         }
  62.  
  63.         fd = openat(gpiodirfd, "direction", O_WRONLY);
  64.         if (fd == -1) {
  65.                 perror("Unable to open direction");
  66.                 goto exit_ioerr;
  67.         }
  68.  
  69.         if (write(fd, "out", 3) != 3) {
  70.                 perror("Error writing to /sys/class/gpio/gpio11/direction");
  71.                 goto exit_ioerr;
  72.         }
  73.  
  74.         (void)close(fd);
  75.  
  76.         fd = openat(gpiodirfd, "value", O_WRONLY);
  77.         if (fd == -1) {
  78.                 perror("Unable to open value");
  79.                 goto exit_ioerr;
  80.         }
  81.  
  82.         int i, j;
  83.         for (j=0 ; j < NUM_ELEMENTS_IN_ARRAY(freqs) ; j++)
  84.         {
  85.                 double f = freqs[j];
  86.                 long delay = 999999./f;
  87.                 long cycles = (100. * f) / 440.;
  88.                
  89.                 for (i = 0; i < cycles; i++) {
  90.                         if (write(fd, "1", 1) != 1) {
  91.                                 perror("Error writing 0 to value");
  92.                                 goto exit_ioerr;
  93.                         }
  94.  
  95.                         (void)usleep(delay/2);
  96.  
  97.                         if (write(fd, "0", 1) != 1) {
  98.                                 perror("Error writing 1 to value");
  99.                                 goto exit_ioerr;
  100.                         }
  101.  
  102.                         (void)usleep(delay/2);
  103.                 }
  104.                
  105.                 (void)usleep(100);
  106.         }
  107.  
  108.         (void)close(fd);
  109.  
  110.         fd = open("/sys/class/gpio/unexport", O_WRONLY);
  111.         if (fd == -1) {
  112.                 perror("Unable to open /sys/class/gpio/unexport");
  113.                 goto exit_ioerr;
  114.         }
  115.  
  116.         if (write(fd, "11", 2) != 2) {
  117.                 perror("Error writing to /sys/class/gpio/unexport");
  118.                 goto exit_ioerr;
  119.         }
  120.    
  121.         retval = EXIT_SUCCESS;
  122. exit_ioerr:
  123.         if (fd >=0)
  124.                 (void)close(fd);
  125.         if (gpiodirfd >=0)
  126.                 (void)close(gpiodirfd);
  127.  
  128.         return retval;
  129. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at