Skip to content

Commit 02ea7a6

Browse files
committed
Fix Orange Pi build
1 parent 558ca04 commit 02ea7a6

File tree

4 files changed

+71
-25
lines changed

4 files changed

+71
-25
lines changed

Makefile

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
CC = cc
2-
CFLAGS = -Wall -Wpedantic -Wextra -Werror -std=c99 -O2 -lfcgi
2+
override CFLAGS += -Wall -Wpedantic -Wextra -Werror -std=c99 -O2 -lfcgi
33
TARGET = relay-control
44
PREFIX = /usr/local
55
WWW_PREFIX = /var/www/html
66

77
all: $(TARGET)
88

99
$(TARGET): src/$(TARGET).c include/$(TARGET).h
10-
$(CC) $(CFLAGS) -Iinclude -o $(TARGET) src/$(TARGET).c
11-
strip $(TARGET)
10+
$(CC) $(CFLAGS) -Iinclude -o $(TARGET) src/$(TARGET).c
11+
strip $(TARGET)
1212

1313
clean:
14-
$(RM) $(TARGET)
14+
$(RM) $(TARGET)
1515

1616
install: $(TARGET) www/index.html www/favicon.ico
17-
install -d $(PREFIX)/bin
18-
install -d $(WWW_PREFIX)
19-
install -m 755 relay-control $(PREFIX)/bin/relay-control
20-
install -m 644 www/index.html $(WWW_PREFIX)/index.html
17+
install -d $(PREFIX)/bin
18+
install -d $(WWW_PREFIX)
19+
install -m 755 relay-control $(PREFIX)/bin/relay-control
20+
install -m 644 www/index.html $(WWW_PREFIX)/index.html
2121
install -m 644 www/favicon.ico $(WWW_PREFIX)/favicon.ico

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Simple program for switching relays via GPIO on Linux with FastCGI HTTP-based AP
55

66
#### Tested boards
77
- `Raspberry Pi 3 Model B` running Debian 10
8-
- `Orange Pi Lite` (Allwinner H3) running Armbian 21.02.2 Buster
8+
- `Orange Pi Lite` (Allwinner H3) running Armbian 21.02.2 Buster (`make CFLAGS="-DSET_DIRECTION"`)
99
- `Orange Pi i96` (RDA8810) running Raspbian 8 on kernel 3.10.62
1010

1111
#### Installation

src/relay-control.c

+56-13
Original file line numberDiff line numberDiff line change
@@ -35,42 +35,60 @@ void gpio_unexport(__attribute__((unused)) const int signal) {
3535
exit(0);
3636
}
3737

38+
bool set_direction(const char* direction_path, const char* direction) {
39+
int direction_data = open(direction_path, O_WRONLY);
40+
if (direction_data == -1 || write(direction_data, direction, 3) != 3) {
41+
fprintf(stderr, "Error writing to %s\n", direction_path);
42+
close(direction_data);
43+
return 0;
44+
}
45+
close(direction_data);
46+
return 1;
47+
}
48+
3849
bool gpio_set_active(const char* gpio, bool active) {
3950
// Checking existance of provided relay
4051
for (unsigned short i = 0; i <= RELAYS_COUNT; i++) {
4152
if (i == RELAYS_COUNT) {
4253
fprintf(stderr, "GPIO pin %d isn't configured.\n", i);
4354
return 0;
4455
} else if (strcmp(RELAYS[i].id, gpio) == 0) {
56+
#ifdef SET_DIRECTION
4557
// Setting direction "out" or "in" for this GPIO pin
46-
int direction_data = open(RELAYS[i].direction_path, O_WRONLY);
47-
char* direction = active ? "out" : "in";
48-
if (direction_data == -1 || write(direction_data, direction, 3) != 3) {
49-
fprintf(stderr, "Error writing to %s\n", RELAYS[i].direction_path);
50-
close(direction_data);
58+
char direction[4] = {0};
59+
if (active) {
60+
strncpy(direction, "out", 4);
61+
} else {
62+
strncpy(direction, "in", 4);
63+
}
64+
65+
if (!set_direction(RELAYS[i].direction_path, direction)) {
5166
return 0;
5267
}
53-
close(direction_data);
5468
if (active) {
69+
#endif
5570
int value_data = open(RELAYS[i].value_path, O_WRONLY);
56-
if (value_data == -1 || write(value_data, "1", 1) != 1) {
71+
if (value_data == -1 || write(value_data, active ? "1" : "0", 1) != 1) {
5772
fprintf(stderr, "Error writing to %s\n", RELAYS[i].value_path);
5873
close(value_data);
5974
return 0;
6075
}
6176
close(value_data);
77+
#ifdef SET_DIRECTION
6278
}
79+
#endif
6380
break;
6481
}
6582
}
6683
return 1;
6784
}
6885

69-
bool gpio_get_active(const char* direction_path) {
86+
bool gpio_get_active(const char* path) {
87+
#ifdef SET_DIRECTION
7088
char direction[4] = {0};
71-
int direction_data = open(direction_path, O_RDONLY);
89+
int direction_data = open(path, O_RDONLY);
7290
if (direction_data == -1 || read(direction_data, direction, 3) != 3) {
73-
fprintf(stderr, "Error while reading %s\n", direction_path);
91+
fprintf(stderr, "Error while reading %s\n", path);
7492
close(direction_data);
7593
return 0;
7694
}
@@ -79,6 +97,20 @@ bool gpio_get_active(const char* direction_path) {
7997
return 1;
8098
else
8199
return 0;
100+
#else
101+
char value[2] = {0};
102+
int value_data = open(path, O_RDONLY);
103+
if (value_data == -1 || read(value_data, value, 1) != 1) {
104+
fprintf(stderr, "Error while reading %s\n", path);
105+
close(value_data);
106+
return 0;
107+
}
108+
close(value_data);
109+
if (strcmp(value, "1") == 0)
110+
return 1;
111+
else
112+
return 0;
113+
#endif
82114
}
83115

84116
bool starts_with(const char* a, const char* b) {
@@ -111,11 +143,16 @@ int main() {
111143
if (export_data == -1)
112144
fail("Unable to open /sys/class/gpio/export\n");
113145

114-
for (unsigned short i = 0; i < RELAYS_COUNT; i++)
146+
for (unsigned short i = 0; i < RELAYS_COUNT; i++) {
115147
if (write(export_data, RELAYS[i].id, 4) != 4) {
116148
close(export_data);
117149
fail("Error writing to /sys/class/gpio/export\n");
118150
}
151+
#ifndef SET_DIRECTION
152+
if (!set_direction(RELAYS[i].direction_path, "out"))
153+
fail("Unable to set direction\n");
154+
#endif
155+
}
119156
close(export_data);
120157
// Unexport GPIO on Ctrl + C
121158
signal(SIGINT, gpio_unexport);
@@ -139,15 +176,21 @@ int main() {
139176
while (FCGX_Accept_r(&request) >= 0) {
140177
char* query_string = FCGX_GetParam("QUERY_STRING", request.envp);
141178
char* request_method = FCGX_GetParam("REQUEST_METHOD", request.envp);
142-
if (strcmp(query_string, "status=csv") == 0 && strcmp(request_method, "GET") == 0) {
179+
if (strcmp(query_string, "status=csv") == 0 &&
180+
strcmp(request_method, "GET") == 0) {
143181
FCGX_PutS(
144182
"Status: 200 OK\r\n"
145183
"Content-type: text/csv\r\n\r\n"
146184
"relay,active,name\n",
147185
request.out);
148186
for (unsigned short i = 0; i < RELAYS_COUNT; i++)
149187
FCGX_FPrintF(request.out, "%s,%d,%s\n", RELAYS[i].id,
150-
gpio_get_active(RELAYS[i].direction_path), RELAYS[i].name);
188+
#ifdef SET_DIRECTION
189+
gpio_get_active(RELAYS[i].direction_path),
190+
#else
191+
gpio_get_active(RELAYS[i].value_path),
192+
#endif
193+
RELAYS[i].name);
151194
continue;
152195
} else if (strcmp(request_method, "POST") == 0) {
153196
// Get POST body data

www/index.html

+6-3
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
width: 500px;
1212
max-width: 90vw;
1313
margin: 0 auto;
14-
line-height: 2em
14+
line-height: 3em
1515
}
1616
button {
17-
float: right
17+
float: right;
18+
height: 2em;
19+
margin-top: .5em
1820
}
1921
h1 {
2022
margin: 0 auto;
@@ -29,6 +31,7 @@ <h1>Relay control</h1>
2931
</body>
3032
<footer>
3133
<script>
34+
'use strict'
3235
let fetch_relays
3336
const switch_relay = async (relay, mode) => {
3437
const buttons = document.querySelectorAll('button')
@@ -81,4 +84,4 @@ <h1>Relay control</h1>
8184
})()
8285
</script>
8386
</footer>
84-
</html>
87+
</html>

0 commit comments

Comments
 (0)