commit 8fa1bd146365b82620c6876c8034f07ea73b7c58
parent e530427d6687484cc82608bb4ede28be24808087
Author: MikoĊaj Lenczewski <mblenczewski@gmail.com>
Date: Sat, 15 Nov 2025 15:49:39 +0000
Updated to libgpiod-2.x
Diffstat:
| M | fand.c | | | 72 | +++++++++++++++++++++++++++++++++++++++++++----------------------------- |
1 file changed, 43 insertions(+), 29 deletions(-)
diff --git a/fand.c b/fand.c
@@ -1,6 +1,7 @@
/*
* mcbinfand - fan daemon for the MACCHIATObin board
* Copyright (C) 2020 Matteo Croce <mcroce@microsoft.com>
+ * Copyright (C) 2025 Mikolaj Lenczewski <mikolaj@lenczewski.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -16,14 +17,19 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
+#include <assert.h>
#include <stdio.h>
-#include <unistd.h>
+#include <stdlib.h>
#include <signal.h>
#include <setjmp.h>
#include <time.h>
+#include <unistd.h>
+
#include <gpiod.h>
#define GPIO_DEV "/dev/gpiochip1"
+#define GPIO_DEV_OFFSET 16
+
#define THERM_PATH "/sys/class/thermal/thermal_zone0/temp"
#define MIN_TEMP 40000
@@ -36,16 +42,18 @@
static jmp_buf loopjmp;
-static void sighandler(int _unused)
+static void
+sighandler(int _unused)
{
longjmp(loopjmp, 1);
}
-static int read_temp()
+static long
+read_temp()
{
char buf[16] = { 0 };
static FILE *ftemp;
- int temp;
+ long temp;
if (!ftemp)
ftemp = fopen(THERM_PATH, "r");
@@ -64,12 +72,14 @@ static int read_temp()
ftemp = NULL;
return MAX_TEMP;
}
- temp = atoi(buf);
+
+ temp = strtol(buf, NULL, 10);
return temp;
}
-static int duty_cycle(int temp)
+static int
+duty_cycle(int temp)
{
if (temp < MIN_TEMP)
return 0;
@@ -81,34 +91,36 @@ static int duty_cycle(int temp)
return (temp - MIN_TEMP) * PWM_USECS / (MAX_TEMP - MIN_TEMP);
}
-int main(int argc, char *argv[])
+int
+main(int argc, char *argv[])
{
struct gpiod_chip *chip;
- struct gpiod_line *line;
+ struct gpiod_line_config *cfg;
+ struct gpiod_line_settings *settings;
+ struct gpiod_line_request *line;
struct timespec oldtp;
int on = PWM_USECS;
int off = 0;
- int req;
chip = gpiod_chip_open(GPIO_DEV);
- if (!chip) {
- perror("gpiod_chip_open");
- return -1;
- }
+ assert(chip);
- line = gpiod_chip_get_line(chip, 16);
- if (!line) {
- perror("gpiod_chip_get_line");
- gpiod_chip_close(chip);
- return -1;
- }
+ cfg = gpiod_line_config_new();
+ assert(cfg);
- req = gpiod_line_request_output(line, "fand", 0);
- if (req) {
- perror("gpiod_line_request_output");
- gpiod_chip_close(chip);
- return -1;
- }
+ settings = gpiod_line_settings_new();
+ assert(settings);
+
+ gpiod_line_settings_set_direction(settings, GPIOD_LINE_DIRECTION_OUTPUT);
+ gpiod_line_settings_set_output_value(settings, GPIOD_LINE_VALUE_ACTIVE);
+
+ unsigned offset = GPIO_DEV_OFFSET;
+ gpiod_line_config_add_line_settings(cfg, &offset, 1, settings);
+
+ line = gpiod_chip_request_lines(chip, NULL, cfg);
+ assert(line);
+
+ gpiod_line_request_set_value(line, offset, GPIOD_LINE_VALUE_INACTIVE);
signal(SIGINT, sighandler);
signal(SIGQUIT, sighandler);
@@ -129,16 +141,17 @@ int main(int argc, char *argv[])
#ifdef DEBUG
printf("temp: %0.1f, duty cycle: %d%%: (%d on, %d off)\n",
- temp / 1000.0, on * 100 / PWM_USECS, on, off);
+ temp / 1000.0, on * 100 / PWM_USECS, on, off);
#endif
}
if (on) {
- gpiod_line_set_value(line, 1);
+ gpiod_line_request_set_value(line, offset, GPIOD_LINE_VALUE_ACTIVE);
usleep(on);
}
+
if (off) {
- gpiod_line_set_value(line, 0);
+ gpiod_line_request_set_value(line, offset, GPIOD_LINE_VALUE_INACTIVE);
usleep(off);
}
}
@@ -147,7 +160,8 @@ int main(int argc, char *argv[])
/* Don't leave the fan off and burn the CPU */
usleep(100000);
- gpiod_line_set_value(line, 1);
+ gpiod_line_request_set_value(line, offset, GPIOD_LINE_VALUE_ACTIVE);
+ gpiod_line_request_release(line);
gpiod_chip_close(chip);
return 0;