pastebin - collaborative debugging tool
eckelmann.kpaste.net RSS

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

    
11
/*
22
 * qaweep1.c - Quantron beeper-weeper demo V0.0.1
33
 *
44
 * Written by Roland Mainz <r.mainz@eckelmann.de>
55
 */
6+
611
#include <errno.h>
712
#include <fcntl.h>
813
#include <stdio.h>
914
#include <stdlib.h>
1015
#include <sys/stat.h>
1116
#include <sys/types.h>
1217
#include <unistd.h>
1318
1419
#define NUM_ELEMENTS_IN_ARRAY(ar) (sizeof(ar) / sizeof (ar[0]))
1520
const double freqs[] = {
1621
	440.0000,
1722
	466.1638,
1823
	493.8833,
1924
	523.2511,
2025
	554.3653,
2126
	587.3295,
2227
	622.2540,
2328
	659.2551,
2429
	698.4565,
2530
	739.9888,
2631
	783.9909,
2732
	830.6094,
2833
	880.0000,
2934
	932.3275,
3035
	987.7666,
3136
	1046.502,
3237
	1108.731,
3338
	1174.659,
3439
	1244.508
3540
};
3641
42+
#define MICROSECONDS_PER_SEC (1000000L)
3743
3844
int main(int ac, char *av[])
3945
{
4046
	int	retval = EXIT_FAILURE;
4147
	int	fd,
4248
		gpiodirfd = -1;
4349
4450
	fd = open("/sys/class/gpio/export", O_WRONLY);
4551
	if (fd == -1) {
4652
		perror("Unable to open /sys/class/gpio/export");
4753
		goto exit_ioerr;
4854
	}
4955
5056
	if (write(fd, "11", 2) != 2) {
5157
		perror("Error writing to /sys/class/gpio/export");
5258
		goto exit_ioerr;
5359
	}
5460
5561
	(void)close(fd);
5662
5763
	gpiodirfd = open("/sys/class/gpio/gpio11/", O_DIRECTORY);
5864
	if (gpiodirfd == -1) {
5965
		perror("Unable to open /sys/class/gpio/gpio11/");
6066
		goto exit_ioerr;
6167
	}
6268
6369
	fd = openat(gpiodirfd, "direction", O_WRONLY);
6470
	if (fd == -1) {
6571
		perror("Unable to open direction");
6672
		goto exit_ioerr;
6773
	}
6874
6975
	if (write(fd, "out", 3) != 3) {
7076
		perror("Error writing to /sys/class/gpio/gpio11/direction");
7177
		goto exit_ioerr;
7278
	}
7379
7480
	(void)close(fd);
7581
7682
	fd = openat(gpiodirfd, "value", O_WRONLY);
7783
	if (fd == -1) {
7884
		perror("Unable to open value");
7985
		goto exit_ioerr;
8086
	}
8187
8288
	int i, j;
8389
	for (j=0 ; j < NUM_ELEMENTS_IN_ARRAY(freqs) ; j++)
8490
	{
8591
		double f = freqs[j];
86-
		long delay = 999999./f;
92+
		long delay = ((double)(MICROSECONDS_PER_SEC))/f;
8793
		long cycles = (100. * f) / 440.;
8894
95+
		(void)printf("freq=%4.4f\tdelay=%ld\tcycles=%ld\n",
89100
		for (i = 0; i < cycles; i++) {
90101
			if (write(fd, "1", 1) != 1) {
91102
				perror("Error writing 0 to value");
92103
				goto exit_ioerr;
93104
			}
94105
95106
			(void)usleep(delay/2);
96107
97108
			if (write(fd, "0", 1) != 1) {
98109
				perror("Error writing 1 to value");
99110
				goto exit_ioerr;
100111
			}
101112
102113
			(void)usleep(delay/2);
103114
		}
104115
105-
		(void)usleep(100);
116+
		/* Wait 1/10 second */
117+
		(void)usleep(MICROSECONDS_PER_SEC/10);
106118
	}
107119
108120
	(void)close(fd);
109121
110122
	fd = open("/sys/class/gpio/unexport", O_WRONLY);
111123
	if (fd == -1) {
112124
		perror("Unable to open /sys/class/gpio/unexport");
113125
		goto exit_ioerr;
114126
	}
115127
116128
	if (write(fd, "11", 2) != 2) {
117129
		perror("Error writing to /sys/class/gpio/unexport");
118130
		goto exit_ioerr;
119131
	}
120132
121133
	retval = EXIT_SUCCESS;
122134
exit_ioerr:
123135
	if (fd >=0)
124136
		(void)close(fd);
125137
	if (gpiodirfd >=0)
126138
		(void)close(gpiodirfd);
127139
128140
	return retval;
129141
}
130142

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.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at