aboutsummaryrefslogtreecommitdiff
path: root/recipes-demo/de10-nano-gpio-apps/files/toggle_fpga_leds.c
blob: 87493b75e38f5be7c0e40f948185ee261202535f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <error.h>
#include <errno.h>
#include <limits.h>
#include <unistd.h>

int main(void) {

	int i;
	const char *leds_array[] = {
		"fpga_led0",
		"fpga_led1",
		"fpga_led2",
		"fpga_led3",
		"fpga_led4",
		"fpga_led5",
		"fpga_led6",
		"fpga_led7"
	};
	int leds_array_count = (sizeof leds_array) / (sizeof *leds_array);
	int led_fd;
	char path[PATH_MAX];
	int path_length;
	int result;
	char brightness;

	// turn off all leds
	for (i = 0 ; i < leds_array_count ; i++ ) {
		// set trigger to none
		path_length = snprintf(path, PATH_MAX,
				"/sys/class/leds/%s/trigger",
				leds_array[i]);
		if(path_length < 0)
			error(1, 0, "path output error");
		if(path_length >= PATH_MAX)
			error(1, 0, "path length overflow");

		led_fd = open(path, O_WRONLY | O_SYNC);
		if(led_fd < 0)
			error(1, errno, "could not open file '%s'", path);

		result = write(led_fd, "none", 4);
		if(result < 0)
			error(1, errno, "writing 'none' to '%s'", path);
		if(result != 4)
			error(1, 0, "did not write 4 bytes to '%s'", path);

		result = close(led_fd);
		if(result < 0)
			error(1, errno, "could not close file '%s'", path);

		// set brightness to 0
		path_length = snprintf(path, PATH_MAX,
				"/sys/class/leds/%s/brightness",
				leds_array[i]);
		if(path_length < 0)
			error(1, 0, "path output error");
		if(path_length >= PATH_MAX)
			error(1, 0, "path length overflow");

		led_fd = open(path, O_WRONLY | O_SYNC);
		if(led_fd < 0)
			error(1, errno, "could not open file '%s'", path);

		result = write(led_fd, "0", 1);
		if(result < 0)
			error(1, errno, "writing 'none' to '%s'", path);
		if(result != 1)
	
		led_fd = open(path, O_WRONLY | O_SYNC);
		if(led_fd < 0)
			error(1, errno, "could not open file '%s'", path);

		result = write(led_fd, "0", 1);
		if(result < 0)
			error(1, errno, "writing '0' to '%s'", path);
		if(result != 1)
			error(1, 0, "did not write 1 byte to '%s'", path);

		result = close(led_fd);
		if(result < 0)
			error(1, errno, "could not close file '%s'", path);
	}

	// toggle the leds individually
	for (i = 0 ; i < (leds_array_count * 2) ; i++ ) {
		// toggle the brightness
		path_length = snprintf(path, PATH_MAX,
				"/sys/class/leds/%s/brightness",
				leds_array[i % leds_array_count]);
		if(path_length < 0)
			error(1, 0, "path output error");
		if(path_length >= PATH_MAX)
			error(1, 0, "path length overflow");

		led_fd = open(path, O_RDWR | O_SYNC);
		if(led_fd < 0)
			error(1, errno, "could not open file '%s'", path);

		result = read(led_fd, &brightness, 1);
		if(result < 0)
			error(1, errno, "reading 1 byte from '%s'", path);
		if(result != 1)
			error(1, 0, "did not read 1 byte from '%s'", path);

		if(brightness == '0')
			brightness = '1';
		else if(brightness == '1')
			brightness = '0';
		else
			error(1, 0, "unexpected value for brightness");

		result = write(led_fd, &brightness, 1);
		if(result < 0)
			error(1, errno, "writing brightness to '%s'", path);
		if(result != 1)
			error(1, 0, "did not write 1 byte to '%s'", path);

		result = close(led_fd);
		if(result < 0)
			error(1, errno, "could not close file '%s'", path);

		result = usleep(125000);
		if(result < 0)
			error(1, errno, "usleep error");
	}

	return 0;
}