pastebin - collaborative debugging tool
eckelmann.kpaste.net RSS


Quantron beeper-weeper demo1
Posted by Anonymous on Fri 13th Dec 2019 14:57
raw | new post
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. static
  45. int cycle_loop(int fd, long cycles, long low_delay, long high_delay)
  46. {
  47.         int i;
  48.  
  49.         for (i = 0; i < cycles; i++) {
  50.                 if (write(fd, "1", 1) != 1)
  51.                         return 0;
  52.  
  53.                 (void)usleep(low_delay);
  54.  
  55.                 if (write(fd, "0", 1) != 1)
  56.                         return 0;
  57.  
  58.                 (void)usleep(high_delay);
  59.         }
  60.  
  61.         return 1;
  62. }
  63.  
  64. int main(int ac, char *av[])
  65. {
  66.         int     retval = EXIT_FAILURE;
  67.         int     fd,
  68.                 gpiodirfd = -1;
  69.  
  70.         fd = open("/sys/class/gpio/export", O_WRONLY);
  71.         if (fd == -1) {
  72.                 perror("Unable to open /sys/class/gpio/export");
  73.                 goto exit_ioerr;
  74.         }
  75.  
  76.         if (write(fd, "11", 2) != 2) {
  77.                 perror("Error writing to /sys/class/gpio/export");
  78.                 goto exit_ioerr;
  79.         }
  80.  
  81.         (void)close(fd);
  82.    
  83.         gpiodirfd = open("/sys/class/gpio/gpio11/", O_DIRECTORY);
  84.         if (gpiodirfd == -1) {
  85.                 perror("Unable to open /sys/class/gpio/gpio11/");
  86.                 goto exit_ioerr;
  87.         }
  88.  
  89.         fd = openat(gpiodirfd, "direction", O_WRONLY);
  90.         if (fd == -1) {
  91.                 perror("Unable to open direction");
  92.                 goto exit_ioerr;
  93.         }
  94.  
  95.         if (write(fd, "out", 3) != 3) {
  96.                 perror("Error writing to /sys/class/gpio/gpio11/direction");
  97.                 goto exit_ioerr;
  98.         }
  99.  
  100.         (void)close(fd);
  101.  
  102.         fd = openat(gpiodirfd, "value", O_WRONLY);
  103.         if (fd == -1) {
  104.                 perror("Unable to open value");
  105.                 goto exit_ioerr;
  106.         }
  107.  
  108.         int j;
  109.         for (j=0 ; j < NUM_ELEMENTS_IN_ARRAY(freqs) ; j++)
  110.         {
  111.                 double f = freqs[j];
  112.                 long delay = ((double)(MICROSECONDS_PER_SEC))/f;
  113.                 long cycles = (100. * f) / 440.;
  114.                
  115.                 (void)printf("freq=%4.4f\tdelay=%ld\tcycles=%ld\n",
  116.                         f,
  117.                         delay,
  118.                         cycles);
  119.                
  120.                 if (!cycle_loop(fd, cycles, delay/2, delay/2))
  121.                 {
  122.                         perror("Error writing 0 to value");
  123.                         goto exit_ioerr;
  124.                 }
  125.  
  126.                 /* Wait 1/4 second */
  127.                 (void)usleep(MICROSECONDS_PER_SEC/4);
  128.         }
  129.  
  130.         (void)close(fd);
  131.  
  132.         fd = open("/sys/class/gpio/unexport", O_WRONLY);
  133.         if (fd == -1) {
  134.                 perror("Unable to open /sys/class/gpio/unexport");
  135.                 goto exit_ioerr;
  136.         }
  137.  
  138.         if (write(fd, "11", 2) != 2) {
  139.                 perror("Error writing to /sys/class/gpio/unexport");
  140.                 goto exit_ioerr;
  141.         }
  142.    
  143.         retval = EXIT_SUCCESS;
  144. exit_ioerr:
  145.         if (fd >=0)
  146.                 (void)close(fd);
  147.         if (gpiodirfd >=0)
  148.                 (void)close(gpiodirfd);
  149.  
  150.         return retval;
  151. }

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