Difference between
modified post 226d8e2 by Anonymous on Fri 13th Dec 2019 14:55
original post 8c4e by Anonymous on Wed 11th Dec 2019 15:56
Download diff
Show
old version |
new version |
both versions
| 1 | 1 | /* | |
| 2 | 2 | * qaweep1.c - Quantron beeper-weeper demo V0.0.1 | |
| 3 | 3 | * | |
| 4 | 4 | * Written by Roland Mainz <r.mainz@eckelmann.de> | |
| 5 | 5 | */ | |
| 6 | 6 | ||
| 7 | 7 | /* | |
| 8 | 8 | * Compile with | |
| 9 | 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 | 10 | */ | |
| 11 | 11 | #include <errno.h> | |
| 12 | 12 | #include <fcntl.h> | |
| 13 | 13 | #include <stdio.h> | |
| 14 | 14 | #include <stdlib.h> | |
| 15 | 15 | #include <sys/stat.h> | |
| 16 | 16 | #include <sys/types.h> | |
| 17 | 17 | #include <unistd.h> | |
| 18 | 18 | ||
| 19 | 19 | #define NUM_ELEMENTS_IN_ARRAY(ar) (sizeof(ar) / sizeof (ar[0])) | |
| 20 | 20 | const double freqs[] = { | |
| 21 | 21 | 440.0000, | |
| 22 | 22 | 466.1638, | |
| 23 | 23 | 493.8833, | |
| 24 | 24 | 523.2511, | |
| 25 | 25 | 554.3653, | |
| 26 | 26 | 587.3295, | |
| 27 | 27 | 622.2540, | |
| 28 | 28 | 659.2551, | |
| 29 | 29 | 698.4565, | |
| 30 | 30 | 739.9888, | |
| 31 | 31 | 783.9909, | |
| 32 | 32 | 830.6094, | |
| 33 | 33 | 880.0000, | |
| 34 | 34 | 932.3275, | |
| 35 | 35 | 987.7666, | |
| 36 | 36 | 1046.502, | |
| 37 | 37 | 1108.731, | |
| 38 | 38 | 1174.659, | |
| 39 | 39 | 1244.508 | |
| 40 | 40 | }; | |
| 41 | 41 | ||
| 42 | 42 | #define MICROSECONDS_PER_SEC (1000000L) | |
| 43 | 43 | ||
| 44 | - | int main(int ac, char *av[]) | |
| 44 | + | static | |
| 45 | + | int cycle_loop(int fd, long cycles, long low_delay, long high_delay) | |
| 45 | 46 | { | |
| 46 | - | int retval = EXIT_FAILURE; | |
| 47 | + | int retval = 0; | |
| 47 | - | int fd, | |
| 48 | + | int i; | |
| 49 | 49 | ||
| 50 | - | fd = open("/sys/class/gpio/export", O_WRONLY); | |
| 50 | + | for (i = 0; i < cycles; i++) { | |
| 51 | - | if (fd == -1) { | |
| 51 | + | if (write(fd, "1", 1) != 1) { | |
| 53 | 52 | goto exit_ioerr; | |
| 54 | 53 | } | |
| 55 | 54 | ||
| 56 | - | if (write(fd, "11", 2) != 2) { | |
| 55 | + | (void)usleep(low_delay); | |
| 60 | 56 | ||
| 61 | - | (void)close(fd); | |
| 57 | + | if (write(fd, "0", 1) != 1) { | |
| 58 | + | goto exit_ioerr; | |
| 62 | 60 | ||
| 63 | - | gpiodirfd = open("/sys/class/gpio/gpio11/", O_DIRECTORY); | |
| 61 | + | (void)usleep(high_delay); | |
| 67 | 62 | } | |
| 68 | 63 | ||
| 69 | - | fd = openat(gpiodirfd, "direction", O_WRONLY); | |
| 64 | + | retval = 1; | |
| 70 | - | if (fd == -1) { | |
| 65 | + | exit_ioerr: | |
| 74 | 66 | ||
| 75 | - | if (write(fd, "out", 3) != 3) { | |
| 67 | + | return retval; | |
| 78 | 68 | } | |
| 79 | 69 | ||
| 80 | - | (void)close(fd); | |
| 70 | + | int main(int ac, char *av[]) | |
| 71 | + | { | |
| 81 | 75 | ||
| 82 | - | fd = openat(gpiodirfd, "value", O_WRONLY); | |
| 76 | + | fd = open("/sys/class/gpio/export", O_WRONLY); | |
| 83 | 77 | if (fd == -1) { | |
| 84 | - | perror("Unable to open value"); | |
| 78 | + | perror("Unable to open /sys/class/gpio/export"); | |
| 85 | 79 | goto exit_ioerr; | |
| 86 | 80 | } | |
| 87 | 81 | ||
| 88 | - | int i, j; | |
| 82 | + | if (write(fd, "11", 2) != 2) { | |
| 89 | - | for (j=0 ; j < NUM_ELEMENTS_IN_ARRAY(freqs) ; j++) | |
| 83 | + | perror("Error writing to /sys/class/gpio/export"); | |
| 90 | - | { | |
| 84 | + | goto exit_ioerr; | |
| 91 | - | double f = freqs[j]; | |
| 85 | + | } | |
| 94 | 86 | ||
| 95 | - | (void)printf("freq=%4.4f\tdelay=%ld\tcycles=%ld\n", | |
| 87 | + | (void)close(fd); | |
| 99 | 88 | ||
| 100 | - | for (i = 0; i < cycles; i++) { | |
| 89 | + | gpiodirfd = open("/sys/class/gpio/gpio11/", O_DIRECTORY); | |
| 101 | - | if (write(fd, "1", 1) != 1) { | |
| 90 | + | if (gpiodirfd == -1) { | |
| 102 | - | perror("Error writing 0 to value"); | |
| 91 | + | perror("Unable to open /sys/class/gpio/gpio11/"); | |
| 103 | 92 | goto exit_ioerr; | |
| 104 | 93 | } | |
| 105 | 94 | ||
| 106 | - | (void)usleep(delay/2); | |
| 95 | + | fd = openat(gpiodirfd, "direction", O_WRONLY); | |
| 96 | + | if (fd == -1) { | |
| 107 | 100 | ||
| 108 | - | if (write(fd, "0", 1) != 1) { | |
| 101 | + | if (write(fd, "out", 3) != 3) { | |
| 109 | - | perror("Error writing 1 to value"); | |
| 102 | + | perror("Error writing to /sys/class/gpio/gpio11/direction"); | |
| 110 | 103 | goto exit_ioerr; | |
| 111 | 104 | } | |
| 112 | 105 | ||
| 119 | - | ||
| 106 | + | (void)close(fd); | |
| 115 | 107 | ||
| 116 | - | /* Wait 1/10 second */ | |
| 108 | + | fd = openat(gpiodirfd, "value", O_WRONLY); | |
| 109 | + | if (fd == -1) { | |
| 118 | 112 | } | |
| 119 | 113 | ||
| 120 | - | (void)close(fd); | |
| 114 | + | int j; | |
| 115 | + | for (j=0 ; j < NUM_ELEMENTS_IN_ARRAY(freqs) ; j++) | |
| 121 | 120 | ||
| 122 | - | fd = open("/sys/class/gpio/unexport", O_WRONLY); | |
| 121 | + | (void)printf("freq=%4.4f\tdelay=%ld\tcycles=%ld\n", | |
| 123 | - | if (fd == -1) { | |
| 122 | + | f, | |
| 124 | - | perror("Unable to open /sys/class/gpio/unexport"); | |
| 123 | + | delay, | |
| 124 | + | cycles); | |
| 127 | 125 | ||
| 128 | - | if (write(fd, "11", 2) != 2) { | |
| 126 | + | if (!cycle_loop(fd, cycles, delay/2, delay/2)) | |
| 129 | - | perror("Error writing to /sys/class/gpio/unexport"); | |
| 127 | + | { | |
| 128 | + | perror("Error writing 0 to value"); | |
| 130 | 129 | goto exit_ioerr; | |
| 131 | 130 | } | |
| 132 | 131 | ||
| 133 | - | retval = EXIT_SUCCESS; | |
| 132 | + | /* Wait 1/4 second */ | |
| 134 | - | exit_ioerr: | |
| 133 | + | (void)usleep(MICROSECONDS_PER_SEC/4); | |
| 134 | + | } | |
| 136 | 136 | (void)close(fd); | |
| 139 | 137 | ||
| 140 | - | return retval; | |
| 138 | + | fd = open("/sys/class/gpio/unexport", O_WRONLY); | |
| 141 | - | } | |
| 139 | + | if (fd == -1) { | |
| 142 | - | ||
| 140 | + | perror("Unable to open /sys/class/gpio/unexport"); | |
| 143 | - | ||
| 141 | + | goto exit_ioerr; | |
| 144 | - | ||
| 142 | + | } | |
| 145 | 143 | ||
| 146 | - | ||
| 144 | + | if (write(fd, "11", 2) != 2) { | |
| 147 | - | ||
| 145 | + | perror("Error writing to /sys/class/gpio/unexport"); | |
| 148 | - | ||
| 146 | + | goto exit_ioerr; | |
| 149 | - | ||
| 147 | + | } | |
| 150 | 148 | ||
| 151 | - | ||
| 149 | + | retval = EXIT_SUCCESS; | |
| 152 | - | ||
| 150 | + | exit_ioerr: | |
| 153 | - | ||
| 151 | + | if (fd >=0) | |
| 154 | - | ||
| 152 | + | (void)close(fd); | |
| 155 | - | ||
| 153 | + | if (gpiodirfd >=0) | |
| 156 | - | ||
| 154 | + | (void)close(gpiodirfd); | |
| 157 | 155 | ||
| 158 | - | ||
| 156 | + | return retval; | |
| 159 | - | ||
| 157 | + | } | |
| 160 | 158 |
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.
eckelmann.kpaste.net RSS