- /*
- * 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)
- static
- int cycle_loop(int fd, long cycles, long low_delay, long high_delay)
- {
- int retval = 0;
- int i;
- for (i = 0; i < cycles; i++) {
- if (write(fd, "1", 1) != 1) {
- goto exit_ioerr;
- }
- (void)usleep(low_delay);
- if (write(fd, "0", 1) != 1) {
- goto exit_ioerr;
- }
- (void)usleep(high_delay);
- }
- retval = 1;
- exit_ioerr:
- return retval;
- }
- 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) {
- goto exit_ioerr;
- }
- if (write(fd, "11", 2) != 2) {
- goto exit_ioerr;
- }
- (void)close(fd);
- gpiodirfd = open("/sys/class/gpio/gpio11/", O_DIRECTORY);
- if (gpiodirfd == -1) {
- goto exit_ioerr;
- }
- fd = openat(gpiodirfd, "direction", O_WRONLY);
- if (fd == -1) {
- goto exit_ioerr;
- }
- if (write(fd, "out", 3) != 3) {
- goto exit_ioerr;
- }
- (void)close(fd);
- fd = openat(gpiodirfd, "value", O_WRONLY);
- if (fd == -1) {
- goto exit_ioerr;
- }
- int 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.;
- f,
- delay,
- cycles);
- if (!cycle_loop(fd, cycles, delay/2, delay/2))
- {
- goto exit_ioerr;
- }
- /* Wait 1/4 second */
- (void)usleep(MICROSECONDS_PER_SEC/4);
- }
- (void)close(fd);
- fd = open("/sys/class/gpio/unexport", O_WRONLY);
- if (fd == -1) {
- goto exit_ioerr;
- }
- if (write(fd, "11", 2) != 2) {
- 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 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)
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.