forked from horchi/linux-p4d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathw1.c
108 lines (83 loc) · 2.43 KB
/
w1.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
//***************************************************************************
// p4d / Linux - Heizungs Manager
// File w1.c
// This code is distributed under the terms and conditions of the
// GNU GENERAL PUBLIC LICENSE. See the file LICENSE for details.
// Date 04.11.2010 - 07.02.2014 Jörg Wendel
//***************************************************************************
#include <dirent.h>
#include "w1.h"
//***************************************************************************
// Show W1 Sensors
//***************************************************************************
int W1::show()
{
for (SensorList::iterator it = sensors.begin(); it != sensors.end(); ++it)
tell(0, "%s: %2.3f", it->first.c_str(), it->second);
return done;
}
int W1::update()
{
for (SensorList::iterator it = sensors.begin(); it != sensors.end(); ++it)
{
char line[100+TB];
FILE* in;
char* path;
asprintf(&path, "%s/%s/w1_slave", w1Path, it->first.c_str());
if (!(in = fopen(path, "r")))
{
tell(eloAlways, "Error: Opening '%s' failed, %s", path, strerror(errno));
free(path);
continue;
}
while (fgets(line, 100, in))
{
char* p;
line[strlen(line)-1] = 0;
if ((p = strstr(line, " t=")))
{
double temp = atoi(p+3) / 1000.0;
sensors[it->first] = temp;
}
}
fclose(in);
free(path);
}
return done;
}
//***************************************************************************
// Scan
//***************************************************************************
int W1::scan()
{
DIR* dir;
dirent* dp;
if (!(dir = opendir(w1Path)))
{
tell(0, "Info: No One-Wire sensors found, path '%s' not exist (%s)", w1Path, strerror(errno));
return fail;
}
while ((dp = readdir(dir)))
{
if (strncmp(dp->d_name, "28-", 3) == 0 || strncasecmp(dp->d_name, "3b-", 3) == 0)
sensors[dp->d_name] = 0;
}
closedir(dir);
return done;
}
//***************************************************************************
// To ID
//***************************************************************************
unsigned int W1::toId(const char* name)
{
const char* p;
int len = strlen(name);
// use 4 minor bytes as id
if (len <= 2)
return na;
if (len <= 8)
p = name;
else
p = name + (len - 8);
return strtoull(p, 0, 16);
}