This repository has been archived by the owner on Oct 11, 2018. It is now read-only.
forked from muehlbau/odroid-smartpower-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smartgauge.cpp
262 lines (226 loc) · 6.74 KB
/
smartgauge.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
* Copyright (c) 2013 Robert Seilbeck. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
#ifndef H_SMARTMETER
#define H_SMARTMETER
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <fstream>
#include <thread>
#include <chrono>
#include <stdexcept>
#include "smartgauge.hpp"
using namespace std;
#define REQUEST_DATA 0x37
#define REQUEST_STARTSTOP 0x80
#define REQUEST_STATUS 0x81
#define REQUEST_ONOFF 0x82
#define REQUEST_VERSION 0x83
SmartGauge::SmartGauge() :
device(NULL), meter(NULL), measure(
false) {
}
SmartGauge::~SmartGauge() {
buf[0] = 0x00;
memset((void*) &buf[2], 0x00, sizeof(buf) - 2);
buf[1] = REQUEST_STARTSTOP;
if (hid_write(device, buf, sizeof(buf)) == -1) {
cerr << "couldn't stop meter" << endl;
}
//Meter needs some time to reset
std::this_thread::sleep_for(std::chrono::milliseconds(50));
if (device != NULL)
hid_close(device);
if (meter != NULL)
delete meter;
}
void SmartGauge::requestStatus(){
buf[0] = 0x00;
memset((void*) &buf[2], 0x00, sizeof(buf) - 2);
buf[1] = REQUEST_STATUS;
if (hid_write(device, buf, sizeof(buf)) == -1) {
throw runtime_error("Request status write failed");
}
if (hid_read(device, buf, sizeof(buf)) == -1) {
throw runtime_error("Request status read failed");
}
}
void SmartGauge::requestData(){
buf[0] = 0x00;
memset((void*) &buf[2], 0x00, sizeof(buf) - 2);
buf[1] = REQUEST_DATA;
if (hid_write(device, buf, sizeof(buf)) == -1) {
throw runtime_error("Request data write failed");
}
if (hid_read(device, buf, sizeof(buf)) == -1) {
throw runtime_error("Request data read failed");
}
}
void SmartGauge::requestStartStop(bool started){
if (!started) {
buf[1] = REQUEST_STARTSTOP;
if (hid_write(device, buf, sizeof(buf)) == -1) {
throw runtime_error("Request start stop failed");
}
}
buf[1] = REQUEST_STARTSTOP;
if (hid_write(device, buf, sizeof(buf)) == -1) {
throw runtime_error("Request start stop failed");
}
//Meter needs some time to reset
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
bool SmartGauge::initDevice() {
buf[0] = 0x00;
memset((void*) &buf[2], 0x00, sizeof(buf) - 2);
device = hid_open(0x04d8, 0x003f, NULL);
if (device) {
hid_set_nonblocking(device, false);
} else {
cerr << "no device" << endl;
return false;
}
buf[1] = REQUEST_STATUS;
requestStatus();
bool started = (buf[1] == 0x01);
requestStartStop(started);
return true;
}
void SmartGauge::reset() {
buf[1] = REQUEST_STATUS;
requestStatus();
bool started = (buf[1] == 0x01);
requestStartStop(started);
}
double SmartGauge::getWattHour() {
//request status two times because the first time meter delivers sometimes wrong values
requestData();
requestData();
if (buf[0] == REQUEST_DATA) {
char volt[7] = { '\0' };
strncpy(volt, (char*) &buf[2], 5);
char ampere[7] = { '\0' };
strncpy(ampere, (char*) &buf[10], 5);
char watt[7] = { '\0' };
strncpy(watt, (char*) &buf[18], 5);
char wh[7] = { '\0' };
strncpy(wh, (char*) &buf[26], 5);
return atof(wh);
}
return -1;
}
double SmartGauge::getVolt() {
//request status two times because the first time meter delivers sometimes wrong values
requestData();
requestData();
if (buf[0] == REQUEST_DATA) {
char volt[7] = { '\0' };
strncpy(volt, (char*) &buf[2], 5);
return atof(volt);
}
return -1;
}
double SmartGauge::getAmpere() {
//request status two times because the first time meter delivers sometimes wrong values
requestData();
requestData();
if (buf[0] == REQUEST_DATA) {
char ampere[7] = { '\0' };
strncpy(ampere, (char*) &buf[10], 5);
return atof(ampere);
}
return -1;
}
double SmartGauge::getWatt() {
//request status two times because the first time meter delivers sometimes wrong values
requestData();
requestData();
if (buf[0] == REQUEST_DATA) {
char watt[7] = { '\0' };
strncpy(watt, (char*) &buf[18], 5);
return atof(watt);
}
return -1;
}
SmartGauge::Measurement SmartGauge::getMeasurement() {
//request status two times because the first time meter delivers sometimes wrong values
requestData();
requestData();
Measurement measurement;
if (buf[0] == 0x37) {
char volt[7] = { '\0' };
strncpy(volt, (char*) &buf[2], 5);
char ampere[7] = { '\0' };
strncpy(ampere, (char*) &buf[10], 5);
char watt[7] = { '\0' };
strncpy(watt, (char*) &buf[18], 5);
char wh[7] = { '\0' };
strncpy(wh, (char*) &buf[26], 5);
measurement.volt = atof(volt);
measurement.ampere = atof(ampere);
measurement.watt = atof(watt);
measurement.wattHour = atof(wh);
}
return measurement;
}
void SmartGauge::startSampling( uint intervall) {
measurements.clear();
if (measure == true) {
throw runtime_error("An old measurement still runs. End it first before starting a new one.");
return;
}
measure = true;
meter = new thread(&SmartGauge::collectSamples, this, intervall);
}
vector<SmartGauge::Measurement> SmartGauge::endSampling() {
measure = false;
meter->join();
return measurements;
}
void SmartGauge::collectSamples(uint intervall) {
//sometime first call to meter returns wrong values, there we ignore the results of the frist call
requestData();
while (measure) {
Measurement measurement;
requestData();
if (buf[0] == 0x37) {
char volt[7] = { '\0' };
strncpy(volt, (char*) &buf[2], 5);
char ampere[7] = { '\0' };
strncpy(ampere, (char*) &buf[10], 5);
char watt[7] = { '\0' };
strncpy(watt, (char*) &buf[18], 5);
char wh[7] = { '\0' };
strncpy(wh, (char*) &buf[26], 5);
measurement.volt = atof(volt);
measurement.ampere = atof(ampere);
measurement.watt = atof(watt);
measurement.wattHour = atof(wh);
}
measurements.push_back(measurement);
std::this_thread::sleep_for(std::chrono::milliseconds(intervall));
}
}
#endif