Difference between
modified post 8c4e by Anonymous on Wed 11th Dec 2019 15:56
original post 6675d by Anonymous on Wed 11th Dec 2019 15:37
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 | 11 | #include <errno.h> | |
| 7 | 12 | #include <fcntl.h> | |
| 8 | 13 | #include <stdio.h> | |
| 9 | 14 | #include <stdlib.h> | |
| 10 | 15 | #include <sys/stat.h> | |
| 11 | 16 | #include <sys/types.h> | |
| 12 | 17 | #include <unistd.h> | |
| 13 | 18 | ||
| 14 | 19 | #define NUM_ELEMENTS_IN_ARRAY(ar) (sizeof(ar) / sizeof (ar[0])) | |
| 15 | 20 | const double freqs[] = { | |
| 16 | 21 | 440.0000, | |
| 17 | 22 | 466.1638, | |
| 18 | 23 | 493.8833, | |
| 19 | 24 | 523.2511, | |
| 20 | 25 | 554.3653, | |
| 21 | 26 | 587.3295, | |
| 22 | 27 | 622.2540, | |
| 23 | 28 | 659.2551, | |
| 24 | 29 | 698.4565, | |
| 25 | 30 | 739.9888, | |
| 26 | 31 | 783.9909, | |
| 27 | 32 | 830.6094, | |
| 28 | 33 | 880.0000, | |
| 29 | 34 | 932.3275, | |
| 30 | 35 | 987.7666, | |
| 31 | 36 | 1046.502, | |
| 32 | 37 | 1108.731, | |
| 33 | 38 | 1174.659, | |
| 34 | 39 | 1244.508 | |
| 35 | 40 | }; | |
| 36 | 41 | ||
| 42 | + | #define MICROSECONDS_PER_SEC (1000000L) | |
| 37 | 43 | ||
| 38 | 44 | int main(int ac, char *av[]) | |
| 39 | 45 | { | |
| 40 | 46 | int retval = EXIT_FAILURE; | |
| 41 | 47 | int fd, | |
| 42 | 48 | gpiodirfd = -1; | |
| 43 | 49 | ||
| 44 | 50 | fd = open("/sys/class/gpio/export", O_WRONLY); | |
| 45 | 51 | if (fd == -1) { | |
| 46 | 52 | perror("Unable to open /sys/class/gpio/export"); | |
| 47 | 53 | goto exit_ioerr; | |
| 48 | 54 | } | |
| 49 | 55 | ||
| 50 | 56 | if (write(fd, "11", 2) != 2) { | |
| 51 | 57 | perror("Error writing to /sys/class/gpio/export"); | |
| 52 | 58 | goto exit_ioerr; | |
| 53 | 59 | } | |
| 54 | 60 | ||
| 55 | 61 | (void)close(fd); | |
| 56 | 62 | ||
| 57 | 63 | gpiodirfd = open("/sys/class/gpio/gpio11/", O_DIRECTORY); | |
| 58 | 64 | if (gpiodirfd == -1) { | |
| 59 | 65 | perror("Unable to open /sys/class/gpio/gpio11/"); | |
| 60 | 66 | goto exit_ioerr; | |
| 61 | 67 | } | |
| 62 | 68 | ||
| 63 | 69 | fd = openat(gpiodirfd, "direction", O_WRONLY); | |
| 64 | 70 | if (fd == -1) { | |
| 65 | 71 | perror("Unable to open direction"); | |
| 66 | 72 | goto exit_ioerr; | |
| 67 | 73 | } | |
| 68 | 74 | ||
| 69 | 75 | if (write(fd, "out", 3) != 3) { | |
| 70 | 76 | perror("Error writing to /sys/class/gpio/gpio11/direction"); | |
| 71 | 77 | goto exit_ioerr; | |
| 72 | 78 | } | |
| 73 | 79 | ||
| 74 | 80 | (void)close(fd); | |
| 75 | 81 | ||
| 76 | 82 | fd = openat(gpiodirfd, "value", O_WRONLY); | |
| 77 | 83 | if (fd == -1) { | |
| 78 | 84 | perror("Unable to open value"); | |
| 79 | 85 | goto exit_ioerr; | |
| 80 | 86 | } | |
| 81 | 87 | ||
| 82 | 88 | int i, j; | |
| 83 | 89 | for (j=0 ; j < NUM_ELEMENTS_IN_ARRAY(freqs) ; j++) | |
| 84 | 90 | { | |
| 85 | 91 | double f = freqs[j]; | |
| 86 | - | long delay = 999999./f; | |
| 92 | + | long delay = ((double)(MICROSECONDS_PER_SEC))/f; | |
| 87 | 93 | long cycles = (100. * f) / 440.; | |
| 88 | 94 | ||
| 95 | + | (void)printf("freq=%4.4f\tdelay=%ld\tcycles=%ld\n", | |
| 89 | 100 | for (i = 0; i < cycles; i++) { | |
| 90 | 101 | if (write(fd, "1", 1) != 1) { | |
| 91 | 102 | perror("Error writing 0 to value"); | |
| 92 | 103 | goto exit_ioerr; | |
| 93 | 104 | } | |
| 94 | 105 | ||
| 95 | 106 | (void)usleep(delay/2); | |
| 96 | 107 | ||
| 97 | 108 | if (write(fd, "0", 1) != 1) { | |
| 98 | 109 | perror("Error writing 1 to value"); | |
| 99 | 110 | goto exit_ioerr; | |
| 100 | 111 | } | |
| 101 | 112 | ||
| 102 | 113 | (void)usleep(delay/2); | |
| 103 | 114 | } | |
| 104 | 115 | ||
| 105 | - | (void)usleep(100); | |
| 116 | + | /* Wait 1/10 second */ | |
| 117 | + | (void)usleep(MICROSECONDS_PER_SEC/10); | |
| 106 | 118 | } | |
| 107 | 119 | ||
| 108 | 120 | (void)close(fd); | |
| 109 | 121 | ||
| 110 | 122 | fd = open("/sys/class/gpio/unexport", O_WRONLY); | |
| 111 | 123 | if (fd == -1) { | |
| 112 | 124 | perror("Unable to open /sys/class/gpio/unexport"); | |
| 113 | 125 | goto exit_ioerr; | |
| 114 | 126 | } | |
| 115 | 127 | ||
| 116 | 128 | if (write(fd, "11", 2) != 2) { | |
| 117 | 129 | perror("Error writing to /sys/class/gpio/unexport"); | |
| 118 | 130 | goto exit_ioerr; | |
| 119 | 131 | } | |
| 120 | 132 | ||
| 121 | 133 | retval = EXIT_SUCCESS; | |
| 122 | 134 | exit_ioerr: | |
| 123 | 135 | if (fd >=0) | |
| 124 | 136 | (void)close(fd); | |
| 125 | 137 | if (gpiodirfd >=0) | |
| 126 | 138 | (void)close(gpiodirfd); | |
| 127 | 139 | ||
| 128 | 140 | return retval; | |
| 129 | 141 | } | |
| 130 | 142 |
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