pastebin - collaborative debugging tool
eckelmann.kpaste.net RSS


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

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