opi-fancontrol/fancontrol.c
2025-07-20 11:42:05 +00:00

52 lines
1 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <wiringPi.h>
int main(int argc, char *argv[]) {
if(argc < 5) {
goto err;
}
int pin = atoi(argv[1]);
int period = atoi(argv[2]);
int thres_on = atoi(argv[3]);
int thres_off = atoi(argv[4]);
if(pin == 0 || period == 0 || thres_on == 0 || thres_off == 0) {
goto err;
}
wiringPiSetup();
pinMode(pin, OUTPUT);
digitalWrite(pin, HIGH);
char buf[8];
FILE *ftemp;
int temp;
size_t len;
int state = 1;
while(1) {
ftemp = fopen("/sys/class/hwmon/hwmon0/temp1_input", "r");
if(ftemp == NULL) {
temp = 0;
} else {
len = fread(buf, 1, 7, ftemp);
fclose(ftemp);
ftemp = NULL;
buf[len] = 0;
temp = atoi(buf);
}
if(temp > thres_on) {
digitalWrite(pin, HIGH);
state = 1;
} else if(state && temp < thres_off) {
digitalWrite(pin, LOW);
state = 0;
}
sleep(period);
}
err:
printf("Usage: fancontrol <pin wiringPi> <period in seconds> <power on threshold in millicelsius> <power off threshold in millicelsius>\n");
return 1;
}