Skip to content

Commit 0b280e0

Browse files
committed
Updated platform.cpp for linux to support kernel 6
1 parent 529c7ef commit 0b280e0

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

src/lcd_hal/linux/platform.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,20 @@
3333
#include <stdio.h>
3434
#include <stdlib.h>
3535
#include <unistd.h>
36+
3637
#include <sys/ioctl.h>
3738
#if defined(__linux__)
3839
#include <linux/i2c-dev.h>
3940
#include <linux/spi/spidev.h>
4041
#endif
4142

43+
#if __has_include(<gpiod.h>)
44+
#define LCDGFX_USE_LIBGPIOD 1
45+
#include <gpiod.h>
46+
#else
47+
#define LCDGFX_USE_LIBGPIOD 0
48+
#endif
49+
4250
//#include <cstdlib>
4351

4452
#include <map>
@@ -47,6 +55,7 @@
4755
#define LINUX_SPI_AVAILABLE
4856
#endif
4957

58+
5059
#define MAX_GPIO_COUNT 256
5160

5261
#ifdef IN
@@ -59,8 +68,28 @@
5968
#endif
6069
#define OUT 1
6170

71+
72+
#if LCDGFX_USE_LIBGPIOD
73+
// libgpiod context
74+
static struct gpiod_chip *lcdgfx_gpiod_chip = NULL;
75+
static int lcdgfx_gpiod_init_chip(void) {
76+
if (!lcdgfx_gpiod_chip) {
77+
lcdgfx_gpiod_chip = gpiod_chip_open_by_number(0); // default to gpiochip0
78+
if (!lcdgfx_gpiod_chip) {
79+
fprintf(stderr, "Failed to open gpiochip0 via libgpiod!\n");
80+
return -1;
81+
}
82+
}
83+
return 0;
84+
}
85+
#endif
86+
6287
int gpio_export(int pin)
6388
{
89+
#if LCDGFX_USE_LIBGPIOD
90+
// No export needed for libgpiod
91+
return lcdgfx_gpiod_init_chip();
92+
#else
6493
char buffer[4];
6594
ssize_t bytes_written;
6695
int fd;
@@ -91,10 +120,15 @@ int gpio_export(int pin)
91120
}
92121
close(fd);
93122
return (0);
123+
#endif
94124
}
95125

96126
int gpio_unexport(int pin)
97127
{
128+
#if LCDGFX_USE_LIBGPIOD
129+
// No unexport needed for libgpiod
130+
return 0;
131+
#else
98132
char buffer[4];
99133
ssize_t bytes_written;
100134
int fd;
@@ -113,10 +147,30 @@ int gpio_unexport(int pin)
113147
}
114148
close(fd);
115149
return (0);
150+
#endif
116151
}
117152

118153
int gpio_direction(int pin, int dir)
119154
{
155+
#if LCDGFX_USE_LIBGPIOD
156+
if (lcdgfx_gpiod_init_chip() < 0) return -1;
157+
struct gpiod_line *line = gpiod_chip_get_line(lcdgfx_gpiod_chip, pin);
158+
if (!line) {
159+
fprintf(stderr, "libgpiod: Failed to get line for pin %d\n", pin);
160+
return -1;
161+
}
162+
int ret = 0;
163+
if (dir == OUT) {
164+
ret = gpiod_line_request_output(line, "lcdgfx", 0);
165+
} else {
166+
ret = gpiod_line_request_input(line, "lcdgfx");
167+
}
168+
if (ret < 0) {
169+
fprintf(stderr, "libgpiod: Failed to set direction for pin %d\n", pin);
170+
return -1;
171+
}
172+
return 0;
173+
#else
120174
static const char s_directions_str[] = "in\0out";
121175

122176
char path[64];
@@ -138,10 +192,27 @@ int gpio_direction(int pin, int dir)
138192

139193
close(fd);
140194
return (0);
195+
#endif
141196
}
142197

143198
int gpio_read(int pin)
144199
{
200+
#if LCDGFX_USE_LIBGPIOD
201+
if (lcdgfx_gpiod_init_chip() < 0) return -1;
202+
struct gpiod_line *line = gpiod_chip_get_line(lcdgfx_gpiod_chip, pin);
203+
if (!line) {
204+
fprintf(stderr, "libgpiod: Failed to get line for pin %d\n", pin);
205+
return -1;
206+
}
207+
int ret = gpiod_line_request_input(line, "lcdgfx");
208+
if (ret < 0) {
209+
fprintf(stderr, "libgpiod: Failed to request input for pin %d\n", pin);
210+
return -1;
211+
}
212+
int value = gpiod_line_get_value(line);
213+
gpiod_line_release(line);
214+
return value;
215+
#else
145216
char path[32];
146217
char value_str[3];
147218
int fd;
@@ -163,10 +234,27 @@ int gpio_read(int pin)
163234
close(fd);
164235

165236
return (atoi(value_str));
237+
#endif
166238
}
167239

168240
int gpio_write(int pin, int value)
169241
{
242+
#if LCDGFX_USE_LIBGPIOD
243+
if (lcdgfx_gpiod_init_chip() < 0) return -1;
244+
struct gpiod_line *line = gpiod_chip_get_line(lcdgfx_gpiod_chip, pin);
245+
if (!line) {
246+
fprintf(stderr, "libgpiod: Failed to get line for pin %d\n", pin);
247+
return -1;
248+
}
249+
int ret = gpiod_line_request_output(line, "lcdgfx", value);
250+
if (ret < 0) {
251+
fprintf(stderr, "libgpiod: Failed to request output for pin %d\n", pin);
252+
return -1;
253+
}
254+
ret = gpiod_line_set_value(line, value);
255+
gpiod_line_release(line);
256+
return ret;
257+
#else
170258
static const char s_values_str[] = "01";
171259

172260
char path[64];
@@ -190,6 +278,7 @@ int gpio_write(int pin, int value)
190278

191279
close(fd);
192280
return (0);
281+
#endif
193282
}
194283

195284
#if defined(__KERNEL__) // ============== KERNEL

0 commit comments

Comments
 (0)