pastebin - collaborative debugging tool
eckelmann.kpaste.net RSS


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

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

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