-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
176 lines (144 loc) · 3.97 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* Ferit Yiğit BALABAN, [email protected]
* rpi-fan-control 2023
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <pigpio.h>
#include <time.h>
#define TEMP_PATH "/sys/class/thermal/thermal_zone0/temp"
#define FAN_PIN 17
#define LOG 1
#define LOG_FILE "/var/log/fan_control.log"
#define STATUS "/var/log/fanstatus"
#define HIGH 1
#define LOW 0
int logMessage(const char *, ...);
int getFanStatus();
int writeFanStatus(int);
int getCoreTemperature();
int main(int argc, char **argv) {
int fanRunning;
double temperature;
int threshold;
int variance;
int function;
if (argc == 2) {
if (!strcmp(*(argv + 1), "on")) {
logMessage("Unexpected token: %s\n", *(argv + 1));
return 1;
} else
function = 1;
} else if (argc == 3) {
threshold = atoi(*(argv + 1));
variance = atoi(*(argv + 2));
if (!threshold) {
logMessage("Expected integer for threshold temperature: %s\n", *(argv + 1));
return 2;
} else if (!variance) {
logMessage("Expected integer for variance: %s\n", *(argv + 2));
return 3;
} else {
function = 0;
}
} else {
logMessage("Expected threshold temperature and variance\n");
return 4;
}
if (gpioInitialise() < 0) {
logMessage("Failed to initialize GPIO.\n");
return 5;
}
fanRunning = getFanStatus();
logMessage("Fan status: %d\n", fanRunning);
gpioSetMode(FAN_PIN, PI_OUTPUT);
temperature = getCoreTemperature();
logMessage("CPU Temperature: %.2f°C\n", temperature);
if (!fanRunning && temperature >= threshold + variance) {
fanRunning = HIGH;
logMessage("Setting pin GPIO17 to HIGH\n");
} else {
if (temperature <= threshold - variance) {
fanRunning = LOW;
logMessage("Setting pin GPIO17 to LOW\n");
} else
logMessage("Pin GPIO17 is already HIGH\n");
}
gpioWrite(FAN_PIN, fanRunning);
writeFanStatus(fanRunning);
gpioTerminate();
return 0;
}
int logMessage(const char *format, ...) {
if (!LOG) {
#pragma clang diagnostic push
#pragma ide diagnostic ignored "UnreachableCode"
return 0;
#pragma clang diagnostic pop
}
FILE *logFile = fopen(LOG_FILE, "a");
if (!logFile) {
printf("Error opening log file");
return 1;
}
time_t currentTime;
time(¤tTime);
struct tm *timeInfo = localtime(¤tTime);
fprintf(logFile, "[%04d-%02d-%02d %02d:%02d:%02d] ", timeInfo->tm_year + 1900, timeInfo->tm_mon + 1,
timeInfo->tm_mday, timeInfo->tm_hour, timeInfo->tm_min, timeInfo->tm_sec);
va_list args;
va_start(args, format);
vfprintf(logFile, format, args);
va_end(args);
fclose(logFile);
return 0;
}
int getFanStatus() {
int status;
FILE *file = fopen(STATUS, "r");
if (!file) {
logMessage("Error opening status file\n");
return -1;
}
if (fscanf(file, "%d", &status) != 1) {
logMessage("Error reading status file\n");
fclose(file);
return -2;
}
fclose(file);
return status;
}
int writeFanStatus(int status) {
if (status != LOW && status != HIGH) {
logMessage("Invalid status: %d\n", status);
return 1;
}
FILE *f = fopen(STATUS, "w");
if (!f) {
logMessage("Error opening status file.\n");
return 2;
}
fprintf(f, "%d", status);
fclose(f);
return 0;
}
int getCoreTemperature() {
int fd;
char buffer[6];
fd = open(TEMP_PATH, O_RDONLY);
if (fd < 0) {
logMessage("Error opening temperature file\n");
return -1;
}
if (read(fd, buffer, sizeof(buffer)) < 0) {
logMessage("Error reading temperature\n");
close(fd);
return -2;
}
close(fd);
return (atof(buffer) / 1000.0);
}