Skip to content

Commit 830067c

Browse files
committed
Replace wiringpi with lgpio
1 parent 1f7ff4d commit 830067c

File tree

3 files changed

+75
-23
lines changed

3 files changed

+75
-23
lines changed

Dockerfile

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM alpine:3.20.3 as builder
1+
FROM alpine:3.20.3 AS builder
22

33
#ARG ALSAEQUAL_VERSION=master
44
# use older commit to be compatible with version from raspberry pi OS
@@ -22,7 +22,7 @@ RUN cd /usr/local/src \
2222
&& make install
2323

2424
RUN apk update \
25-
&& apk add --no-cache wget unzip autoconf automake wiringpi wiringpi-dev flac-dev faad2-dev mpg123-dev libvorbis-dev libmad-dev soxr-dev openssl-dev opusfile-dev opus-dev libogg-dev libtool
25+
&& apk add --no-cache wget unzip autoconf automake flac-dev faad2-dev mpg123-dev libvorbis-dev libmad-dev soxr-dev openssl-dev opusfile-dev opus-dev libogg-dev libtool
2626

2727
RUN cd /usr/local/src \
2828
&& mkdir dest \
@@ -55,15 +55,22 @@ RUN cd /usr/local/src \
5555
&& cd .. \
5656
&& cd squeezelite-$SQUEEZELITE_VERSION \
5757
&& patch -p1 -i alpine/load-libtremor-first.patch \
58-
&& make OPTS="-DRESAMPLE -DDSD -DGPIO -DVISEXPORT -DUSE_SSL -DNO_SSLSYM -DOPUS -DALAC -DLINE_IN -I/usr/include/opus -I/usr/include/alac" \
58+
&& make OPTS="-DRESAMPLE -DDSD -DGPIO -DVISEXPORT -DUSE_SSL -DOPUS -DALAC -DLINE_IN -I/usr/include/opus -I/usr/include/alac" \
5959
&& gcc -Os -fomit-frame-pointer -fcommon -s -o find_servers tools/find_servers.c \
6060
&& gcc -Os -fomit-frame-pointer -fcommon -s -o alsacap tools/alsacap.c -lasound
6161

62+
RUN cd /usr/local/src \
63+
&& wget https://github.com/joan2937/lg/archive/master.zip -O lgpio.zip \
64+
&& unzip lgpio.zip \
65+
&& cd lg-master \
66+
&& make \
67+
&& make DESTDIR="/usr/local/src/dest" install
68+
6269
COPY gpio.c /usr/local/src/gpio.c
6370

6471
RUN cd /usr/local/src \
65-
&& gcc -o gpio gpio.c -Wall -Wextra -Winline -I/usr/include -L/usr/lib -pipe -lwiringPi
66-
72+
&& gcc -O3 -Wall -pthread -o gpio gpio.c -I/usr/local/src/lg-master -L/usr/local/src/lg-master -llgpio
73+
6774
FROM alpine:3.20.3
6875

6976
ENV LANG C.UTF-8
@@ -89,7 +96,6 @@ COPY --from=builder /usr/local/src/dest/usr/lib/* /usr/lib/
8996
COPY --from=builder /usr/local/src/squeezelite-*/squeezelite /usr/local/bin/squeezelite
9097
#COPY --from=builder /usr/local/src/squeezelite-*/alsacap /usr/local/bin/alsacap
9198
#COPY --from=builder /usr/local/src/squeezelite-*/find_servers /usr/local/bin/find_servers
92-
COPY --from=builder /usr/lib/libwiringPi.so* /usr/lib/
9399
COPY --from=builder /usr/local/src/gpio /usr/local/bin/gpio
94100
COPY --from=builder /usr/lib/alsa-lib/libasound_module_pcm_equal.so /usr/lib/alsa-lib/libasound_module_pcm_equal.so
95101
COPY --from=builder /usr/lib/alsa-lib/libasound_module_ctl_equal.so /usr/lib/alsa-lib/libasound_module_ctl_equal.so

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ The speaker switcher GPIO is used to trigger an external 5V relais changing spea
5656
Additionally an external switch can be controlled via home assistant.
5757

5858
The [power_mute.sh](power_mute.sh) script is automatically configured via the `-S` squeezelite parameter when one of the relevant ENV vars is present in the docker run command.
59-
It uses a custom [gpio control binary](gpio.c) based on [WiringPi](https://github.com/WiringPi/WiringPi).
59+
It uses a custom [gpio control binary](gpio.c) based on [lgpio](https://github.com/joan2937/lg).
6060

6161
[flock](https://linux.die.net/man/1/flock) is used to prevent race conditions of parallel running scripts for different player instances.
6262

gpio.c

+62-16
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,106 @@
22
#include <stdlib.h>
33
#include <stdint.h>
44
#include <strings.h>
5-
#include <wiringPi.h>
5+
#include <lgpio.h>
6+
7+
#define LFLAGS 0
68

79
/*
8-
* gcc -o gpio gpio.c -Wall -Wextra -Winline -I/usr/include -L/usr/lib -pipe -lwiringPi
10+
* gcc -O3 -Wall -pthread -o gpio gpio.c -I/usr/local/src/lg-master -L/usr/local/src/lg-master -llgpio
911
*/
1012

1113
int main(int argc, char **argv)
1214
{
15+
int h;
16+
int pin, mode, state;
17+
18+
static int board_map[41] =
19+
{
20+
-1, // 0
21+
-1, -1, // 1, 2
22+
2, -1, // 3, 4
23+
3, -1, // ...
24+
4, 14,
25+
-1, 15,
26+
17, 18,
27+
27, -1,
28+
22, 23,
29+
-1, 24,
30+
10, -1,
31+
9, 25,
32+
11, 8,
33+
-1, 7,
34+
0, 1,
35+
5, -1,
36+
6, 12,
37+
13, -1,
38+
19, 16,
39+
26, 20,
40+
-1, 21,
41+
};
42+
43+
h = lgGpiochipOpen(0);
1344

14-
wiringPiSetupPhys ();
45+
if (h < 0)
46+
{
47+
printf("Could not open GPIO!\n");
48+
exit(1);
49+
}
1550

1651
if (argc > 1)
1752
{
1853
// INPUT 0, OUTPUT 1,
1954
if (strcasecmp (argv [1], "set_mode") == 0)
2055
{
21-
int pin = atoi(argv[2]);
22-
int mode = atoi(argv [3]);
23-
pinMode (pin, mode);
24-
pullUpDnControl (pin, PUD_OFF);
56+
pin = atoi(argv[2]);
57+
mode = atoi(argv[3]);
58+
if (mode == 1)
59+
{
60+
state = lgGpioRead(h, board_map[pin]);
61+
lgGpioClaimOutput(h, LFLAGS, board_map[pin], state);
62+
}
63+
else if (mode == 0)
64+
{
65+
lgGpioClaimInput(h, LFLAGS, board_map[pin]);
66+
}
2567
}
2668
// INPUT 0, OUTPUT 1,
2769
else if (strcasecmp(argv[1], "get_mode") == 0)
2870
{
29-
int pin = atoi(argv [2]);
30-
int val = getAlt(pin);
31-
printf("%s", val == 0 ? "0" : "1");
71+
pin = atoi(argv[2]);
72+
mode = lgGpioGetMode(h, board_map[pin]);
73+
printf("%s", mode & 0x2 ? "1" : "0");
3274
}
3375
// LOW 0, HIGH 1
3476
else if (strcasecmp(argv[1], "read") == 0)
3577
{
36-
int pin = atoi(argv[2]);
37-
int val = digitalRead(pin);
38-
printf("%s", val == 0 ? "0" : "1");
78+
pin = atoi(argv[2]);
79+
state = lgGpioRead(h, board_map[pin]);
80+
printf("%s", state == 0 ? "0" : "1");
3981
}
4082
// LOW 0, HIGH 1
4183
else if (strcasecmp(argv[1], "write") == 0)
4284
{
43-
int pin = atoi(argv[2]);
44-
int state = atoi(argv[3]);
45-
digitalWrite(pin, state);
85+
pin = atoi(argv[2]);
86+
state = atoi(argv[3]);
87+
lgGpioWrite(h, board_map[pin], state);
4688
}
4789
else
4890
{
4991
printf("Unknown command!\n");
92+
lgGpiochipClose(h);
5093
exit(1);
5194
}
5295
}
5396
else
5497
{
5598
printf("Unknown command!\n");
99+
lgGpiochipClose(h);
56100
exit(1);
57101
}
58102

103+
lgGpiochipClose(h);
104+
59105
return 0;
60106

61107
}

0 commit comments

Comments
 (0)