-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsensor.c
More file actions
59 lines (46 loc) · 1.13 KB
/
Copy pathsensor.c
File metadata and controls
59 lines (46 loc) · 1.13 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "common.h"
#include "hwmon.h"
#include "sensor.h"
float sensor_poll (struct sensor *s)
{
int val;
FILE *fd;
fd = fopen(s->temp_path, "r");
if (fd == NULL) {
DBG("sensor: failed to open %s\n", s->temp_path);
return NAN;
}
if (fscanf(fd, "%d", &val) != 1) {
DBG("sensor: failed to read value from %s\n", s->temp_path);
fclose(fd);
return NAN;
}
fclose(fd);
return ((float)val / 1000) + s->offset;
}
struct sensor *sensor_create (const char *hwmon_path, int index, int offset)
{
struct sensor *s = malloc(sizeof(struct sensor));
if (!s)
return NULL;
s->hwmon_path = hwmon_resolve_path(hwmon_path);
if (!s->hwmon_path) {
free(s);
return NULL;
}
s->index = index;
s->offset = offset;
s->temp_path = malloc(MAX_PATH * sizeof(char));
snprintf(s->temp_path, MAX_PATH, "%s/temp%d_input", s->hwmon_path, index);
return s;
}
void sensor_destroy (struct sensor *s)
{
free(s->hwmon_path);
free(s->temp_path);
free(s);
}