- /*
- * qaweep1.c - Quantron beeper-weeper demo V0.0.1
- *
- * Written by Roland Mainz <r.mainz@eckelmann.de>
- */
- /*
- * Compile with
- * $ /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
- */
- #include <errno.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <unistd.h>
- #define NUM_ELEMENTS_IN_ARRAY(ar) (sizeof(ar) / sizeof (ar[0]))
- const double freqs[] = {
- 440.0000,
- 466.1638,
- 493.8833,
- 523.2511,
- 554.3653,
- 587.3295,
- 622.2540,
- 659.2551,
- 698.4565,
- 739.9888,
- 783.9909,
- 830.6094,
- 880.0000,
- 932.3275,
- 987.7666,
- 1046.502,
- 1108.731,
- 1174.659,
- 1244.508
- };
- #define MICROSECONDS_PER_SEC (1000000L)
- int main(int ac, char *av[])
- {
- int retval = EXIT_FAILURE;
- int fd,
- gpiodirfd = -1;
- fd = open("/sys/class/gpio/export", O_WRONLY);
- if (fd == -1) {
- perror("Unable to open /sys/class/gpio/export");
- goto exit_ioerr;
- }
- if (write(fd, "11", 2) != 2) {
- perror("Error writing to /sys/class/gpio/export");
- goto exit_ioerr;
- }
- (void)close(fd);
- gpiodirfd = open("/sys/class/gpio/gpio11/", O_DIRECTORY);
- if (gpiodirfd == -1) {
- perror("Unable to open /sys/class/gpio/gpio11/");
- goto exit_ioerr;
- }
- fd = openat(gpiodirfd, "direction", O_WRONLY);
- if (fd == -1) {
- perror("Unable to open direction");
- goto exit_ioerr;
- }
- if (write(fd, "out", 3) != 3) {
- perror("Error writing to /sys/class/gpio/gpio11/direction");
- goto exit_ioerr;
- }
- (void)close(fd);
- fd = openat(gpiodirfd, "value", O_WRONLY);
- if (fd == -1) {
- perror("Unable to open value");
- goto exit_ioerr;
- }
- int i, j;
- for (j=0 ; j < NUM_ELEMENTS_IN_ARRAY(freqs) ; j++)
- {
- double f = freqs[j];
- long delay = ((double)(MICROSECONDS_PER_SEC))/f;
- long cycles = (100. * f) / 440.;
- (void)printf("freq=%4.4f\tdelay=%ld\tcycles=%ld\n",
- f,
- delay,
- cycles);
- for (i = 0; i < cycles; i++) {
- if (write(fd, "1", 1) != 1) {
- perror("Error writing 0 to value");
- goto exit_ioerr;
- }
- (void)usleep(delay/2);
- if (write(fd, "0", 1) != 1) {
- perror("Error writing 1 to value");
- goto exit_ioerr;
- }
- (void)usleep(delay/2);
- }
- /* Wait 1/10 second */
- (void)usleep(MICROSECONDS_PER_SEC/10);
- }
- (void)close(fd);
- fd = open("/sys/class/gpio/unexport", O_WRONLY);
- if (fd == -1) {
- perror("Unable to open /sys/class/gpio/unexport");
- goto exit_ioerr;
- }
- if (write(fd, "11", 2) != 2) {
- perror("Error writing to /sys/class/gpio/unexport");
- goto exit_ioerr;
- }
- retval = EXIT_SUCCESS;
- exit_ioerr:
- if (fd >=0)
- (void)close(fd);
- if (gpiodirfd >=0)
- (void)close(gpiodirfd);
- return retval;
- }
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)
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.