- /*
- * qaweep1.c - Quantron beeper-weeper demo V0.0.1
- *
- * Written by Roland Mainz <r.mainz@eckelmann.de>
- */
- #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
- };
- 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 = 999999./f;
- long cycles = (100. * f) / 440.;
- 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);
- }
- (void)usleep(100);
- }
- (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:37
raw | new post
view followups (newest first): Quantron beeper-weeper demo1 by Anonymous
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.