-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpiods18b20.cpp
86 lines (61 loc) · 1.49 KB
/
gpiods18b20.cpp
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
#include "gpiods18b20.h"
#include <fstream>
#include <sstream>
#include <iostream>
using namespace std;
gpioDS18B20::gpioDS18B20(){
}
gpioDS18B20::~gpioDS18B20(){
}
void gpioDS18B20::run(){
//if (!stopped){ }
//stopped = false;
sensor1val = readDS18B20(1);
emit readOK();
}
void gpioDS18B20::stop(){
stopped = true;
}
float gpioDS18B20::readDS18B20(int sensor){
QString sensorSN = "";
switch (sensor){
case 1: sensorSN = ds18b20_SN1;
break;
}
QString cmd = DS18B20_PATH + sensorSN + DS18B20_READ;
//qDebug() << cmd;
stringstream ss;
ss << cmd.toUtf8().constData();
fstream fs;
fs.open(ss.str().c_str(), fstream::in);
char * buffer = new char [80];
fs.read(buffer, 80);
QString str(buffer);
//qDebug() << buffer;
fs.close();
QRegExp rxd("\\b(t)\\b");
int posd = rxd.indexIn(str);
//qDebug() << "t= " << posd;
// QRegExp rx("(\\d{5})");
// QRegExp rx("(\\d+)");
QRegExp rx("(\\-?\\d+)");
//QRegExp rx("^\\-?[1-9]\\d+$");
QStringList list;
int pos = 0;
while ((pos = rx.indexIn(str, pos)) != -1) {
list << rx.cap(1);
pos += rx.matchedLength();
if (pos > posd)
break;
//qDebug() << "pos; " << pos;
}
QString valstr = "";
float val = -1;
if (!list.isEmpty()){
//qDebug() << list.last();
valstr = list.last();
val = valstr.toInt() / 1000.0;
}
qDebug() << val;
return val;
}