-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemperatureReader.cpp
More file actions
90 lines (71 loc) · 2.22 KB
/
TemperatureReader.cpp
File metadata and controls
90 lines (71 loc) · 2.22 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* TemperatureReader.cpp CS530 SDSU
main source File
Madison Bennett
id: 824800113
Alyssa Rivera
id: 825469587
Linh Tran
id: 828187432
*/
#include "TemperatureReader.h"
TemperatureReader::TemperatureReader()
{
//Constructor to initialize temperature sensor
//para: n/a
//returns: n/a
//connect data wire
const std::string baseDir = "/sys/bus/w1/devices/";
std::string deviceFolder;
//find device folder
for (const auto& entry : std::filesystem::directory_iterator(baseDir)) {
if (entry.is_directory() && entry.path().string().find("28-") != std::string::npos) {
deviceFolder = entry.path().string();
break;
}
}
if (deviceFolder.empty()) {
throw std::runtime_error("Unable to find temperature sensor");
}
deviceFile = deviceFolder + "/w1_slave";
}
// constructor to initialize device file path
TemperatureReader::TemperatureReader(const std::string& deviceFilePath) : deviceFile(deviceFilePath) {}
std::string TemperatureReader::getRawTemp() const
{
//Function to read the tempature sensors data with out processing it
//para: n/a
//returns: n/a
//get device file
std::ifstream file(deviceFile);
//check if file opened successfully
if (!file.is_open()) {
throw std::runtime_error("Unable to open device file " + deviceFile);
}
std::string lines;
std::string line;
//read file line by line
while (std::getline(file, line)) {
lines += line + "\n";
}
//close file and return lines
file.close();
return lines;
}
double TemperatureReader::getTemp() const
{
//Function to process the tempature sensors raw data
//para: n/a
//returns: n/a
std::string lines;
do {
lines = getRawTemp();
} while (lines.find("YES") == std::string::npos);
size_t equals_pos = lines.find("t=");
if (equals_pos != std::string::npos) {
std::string temp_string = lines.substr(equals_pos + 2);
double temp_c = std::stod(temp_string) / 1000.0; //get temp in celsius
double temp_f = temp_c * 9.0 / 5.0 + 32.0; //convert to fahrenheit
return temp_f; //return temperature
}
throw std::runtime_error("Temp data not found in file");
}