-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensor.c
47 lines (34 loc) · 835 Bytes
/
sensor.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "sensor.h"
float sensor_poll (struct sensor *s)
{
int val;
FILE *fd;
fd = fopen(s->temp_path, "r");
if (fd == NULL)
return -1;
fscanf (fd, "%d", &val);
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 = strdup(hwmon_path);
s->index = index;
s->offset = offset;
s->temp_path = malloc(MAX_PATH * sizeof(char));
sprintf (s->temp_path, "%s/temp%d_input", hwmon_path, index);
return s;
}
void sensor_destroy (struct sensor *s)
{
free(s->hwmon_path);
free(s->temp_path);
free(s);
}