-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtempSensor.cpp
More file actions
76 lines (57 loc) · 1.19 KB
/
tempSensor.cpp
File metadata and controls
76 lines (57 loc) · 1.19 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* tempSensor.cpp
*
* Created on: Nov 7, 2012
* Author: tuxmux28
*/
#include "tempSensor.h"
using namespace std;
tempSensor::tempSensor() {
this->sensId = "";
this->retTemp = 0.0;
this->lastTemp = 0.0;
}
tempSensor::tempSensor(string id) {
this->setId(id);
this->lastTemp = 0.0;
this->retTemp = 0.0;
}
void tempSensor::setId(string id) {
this->sensId = id;
}
float tempSensor::getTemp() {
char* path = new char[45];
strcpy(path,"/sys/bus/w1/devices/");
strcat(path,this->sensId.c_str());
strcat(path,"/w1_slave");
this->lastTemp = this->retTemp;
ifstream fp;
char line[60];
char *tmpstring;
int findYes = 0;
int temp = 0;
fp.open(path,ios::in);
assert(!fp.fail());
while (!fp.eof()) {
fp >> line;
if (strstr(line, "YES")) findYes++;
}
if (findYes == 0) {
this->retTemp = this->lastTemp;
}
else {
tmpstring = strtok(line, "=");
tmpstring = strtok(NULL, "=");
temp = atoi(tmpstring);
this->retTemp = temp / 1000.000;
}
return this->retTemp;
}
float tempSensor::getLastTemp() {
return this->lastTemp;
}
void tempSensor::setLastTemp(float lastTempVal) {
this->lastTemp = lastTempVal;
}
tempSensor::~tempSensor() {
}