/* * qaweep1.c - Quantron beeper-weeper demo V0.0.1 * * Written by Roland Mainz */ /* * 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 #include #include #include #include #include #include #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) { 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 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); if (!cycle_loop(fd, cycles, delay/2, delay/2)) { perror("Error writing 0 to value"); 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) { 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; }