Skip to content

Commit ebd1d72

Browse files
committed
Fix BM2 voltage readings and realtime graphs
1 parent 0913226 commit ebd1d72

7 files changed

Lines changed: 205 additions & 90 deletions

qml/DeviceBatteryMonitor.qml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Loader {
2424
if (currentDevice !== clickedDevice) currentDevice = clickedDevice
2525

2626
// connect device
27-
//currentDevice.actionConnect(true)
27+
currentDevice.actionConnect(true)
2828

2929
// load screen
3030
if (!deviceBM.active) deviceBM.active = true

qml/charts/ChartBatteryHistory.qml

Lines changed: 41 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,37 @@ Item {
1212
property bool showGraphDots: settingsManager.graphShowDots
1313
property color legendColor: Theme.colorSubText
1414

15-
property real limitMin: 0
16-
property real limitMax: 0
15+
property real valueMin_p: 0
16+
property real valueMax_p: 100
1717

18-
property real valueMin: 0
19-
property real valueMax: 20
18+
property real valueMin_v: 0
19+
property real valueMax_v: 20
20+
property real limitMin_v: 0
21+
property real limitMax_v: 0
2022

2123
////////////////////////////////////////////////////////////////////////////
2224

2325
function loadGraph() {
2426
if (typeof currentDevice === "undefined" || !currentDevice) return
2527
//console.log("chartBatteryHistory // loadGraph() >> " + currentDevice)
2628

27-
battery2Data.visible = true // currentDevice.hasBatteryVoltage
28-
battery2Data.clear()
29+
//// DATA
30+
batteryData_p.clear()
31+
batteryData_v.clear()
32+
33+
batteryData_p.visible = true
34+
batteryData_v.visible = false // currentDevice.hasBatteryVoltage
2935

36+
//// AXIS
3037
axisPercents.min = 0
3138
axisPercents.max = 100
32-
axisVolts.min = valueMin
33-
axisVolts.max = valueMax
3439

35-
chartBatteryHistory.limitMin = (currentPreset) ? currentPreset.voltageMin : 0
36-
chartBatteryHistory.limitMax = (currentPreset) ? currentPreset.voltageMax : 0
40+
axisVolts.min = valueMin_v
41+
axisVolts.max = valueMax_v
42+
43+
//// LEGEND
44+
chartBatteryHistory.limitMin_v = (currentPreset) ? currentPreset.voltageMin : 0
45+
chartBatteryHistory.limitMax_v = (currentPreset) ? currentPreset.voltageMax : 0
3746
chartBatteryHistory.legendColor = Qt.rgba(legendColor.r, legendColor.g, legendColor.b, 0.8)
3847
}
3948

@@ -45,16 +54,16 @@ Item {
4554
var count = 0 // currentDevice.countDataNamed("battery2", days)
4655

4756
//// DATA
48-
battery2Data.clear()
57+
batteryData_v.clear()
4958

50-
currentDevice.getChartData_batteryHistory(axisTime, battery2Data, false, days)
59+
currentDevice.getChartData_batteryHistory(axisTime, batteryData_v, false, days)
5160

5261
//// AXIS
5362
//axisVolts.min = currentDevice.voltMin*0.85
5463
//axisVolts.max = currentDevice.voltMax*1.15
5564

5665
/// Graph visibility
57-
count = battery2Data.count
66+
count = batteryData_v.count
5867
aioGraph.visible = (count > 1)
5968
noDataIndicator.visible = (count <= 0)
6069
showGraphDots = (settingsManager.graphShowDots && count < 16)
@@ -65,21 +74,23 @@ Item {
6574

6675
if (currentPreset) {
6776
//console.log("DeviceBatteryMonitor // onPresetUpdated() >> " + currentPreset.name)
68-
chartBatteryHistory.limitMin = currentPreset.voltageMin
69-
chartBatteryHistory.limitMax = currentPreset.voltageMax
77+
chartBatteryHistory.limitMin_v = currentPreset.voltageMin
78+
chartBatteryHistory.limitMax_v = currentPreset.voltageMax
7079
} else {
7180
//console.log("DeviceBatteryMonitor // onPresetUpdated() >> empty preset")
72-
chartBatteryHistory.limitMin = 0
73-
chartBatteryHistory.limitMax = 0
81+
chartBatteryHistory.limitMin_v = 0
82+
chartBatteryHistory.limitMax_v = 0
7483
}
7584
}
7685

77-
function qpoint_lerp(p0, p1, x) { return (p0.y + (x - p0.x) * ((p1.y - p0.y) / (p1.x - p0.x))) }
86+
function isIndicator() { return false }
87+
function resetIndicator() { }
7888

7989
////////////////////////////////////////////////////////////////////////////
8090

8191
ChartView {
8292
id: aioGraph
93+
8394
anchors.fill: parent
8495
anchors.topMargin: -28
8596
anchors.leftMargin: -24
@@ -101,10 +112,17 @@ Item {
101112
gridLineColor: Theme.colorSeparator; }
102113

103114
LineSeries {
104-
id: battery2Data
115+
id: batteryData_p
105116
useOpenGL: useOpenGL
106117
pointsVisible: showGraphDots
107118
color: Theme.colorBlue; width: 2;
119+
axisY: axisPercents; axisX: axisTime;
120+
}
121+
LineSeries {
122+
id: batteryData_v
123+
useOpenGL: useOpenGL
124+
pointsVisible: showGraphDots
125+
color: Theme.colorGreen; width: 2;
108126
axisY: axisVolts; axisX: axisTime;
109127
}
110128

@@ -126,47 +144,7 @@ Item {
126144
}
127145

128146
function moveIndicator(mouse, isMoving) {
129-
var mmm = Qt.point(mouse.x, mouse.y)
130-
/*
131-
// we adjust coordinates with graph area margins
132-
var ppp = Qt.point(mouse.x, mouse.y)
133-
ppp.x = ppp.x + aioGraph.anchors.rightMargin
134-
ppp.y = ppp.y - aioGraph.anchors.topMargin
135-
136-
// map mouse position to graph value // mpmp.x is the timestamp
137-
var mpmp = aioGraph.mapToValue(mmm, batteryData)
138-
139-
//console.log("clicked " + mouse.x + " " + mouse.y)
140-
//console.log("clicked adjusted " + ppp.x + " " + ppp.y)
141-
//console.log("clicked mapped " + mpmp.x + " " + mpmp.y)
142-
143-
if (isMoving) {
144-
// dragging outside the graph area?
145-
if (mpmp.x < batteryData.at(0).x){
146-
ppp.x = aioGraph.mapToPosition(batteryData.at(0), batteryData).x + aioGraph.anchors.rightMargin
147-
mpmp.x = batteryData.at(0).x
148-
}
149-
if (mpmp.x > batteryData.at(batteryData.count-1).x){
150-
ppp.x = aioGraph.mapToPosition(batteryData.at(batteryData.count-1), batteryData).x + aioGraph.anchors.rightMargin
151-
mpmp.x = batteryData.at(batteryData.count-1).x
152-
}
153-
} else {
154-
// did we clicked outside the graph area?
155-
if (mpmp.x < batteryData.at(0).x || mpmp.x > batteryData.at(batteryData.count-1).x) {
156-
resetIndicator()
157-
return
158-
}
159-
}
160-
161-
// indicators is now visible
162-
dateIndicator.visible = true
163-
verticalIndicator.visible = true
164-
verticalIndicator.x = ppp.x
165-
verticalIndicator.clickedCoordinates = mpmp
166-
167-
// update the indicator data
168-
updateIndicator()
169-
*/
147+
//
170148
}
171149
}
172150

@@ -187,10 +165,10 @@ Item {
187165
anchors.left: parent.left
188166
anchors.right: parent.right
189167

190-
y: UtilsNumber.mapNumber(Math.min(limitMax, valueMax), // value
191-
valueMin, valueMax, // from
168+
y: UtilsNumber.mapNumber(Math.min(limitMax_v, valueMax_v), // value
169+
valueMin_p, valueMax_v, // from
192170
aioGraph.plotArea.height, 0) // to
193-
height: ((Math.min(limitMax, valueMax) - limitMin) / (valueMax - valueMin)) * aioGraph.plotArea.height
171+
height: ((Math.min(limitMax_v, valueMax_v) - limitMin_v) / (valueMax_v - valueMin_v)) * aioGraph.plotArea.height
194172

195173
color: Theme.colorGreen
196174
opacity: 0.20

qml/charts/ChartBatteryRealTime.qml

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@ Item {
1212
property bool showGraphDots: false
1313
property color legendColor: Theme.colorSubText
1414

15-
property real valueMin: 0
16-
property real valueMax: 100
15+
property real valueMin_p: 0
16+
property real valueMax_p: 100
17+
18+
property real valueMin_v: 0
19+
property real valueMax_v: 20
20+
property real limitMin_v: 0
21+
property real limitMax_v: 0
1722

1823
////////////////////////////////////////////////////////////////////////////
1924

@@ -22,17 +27,25 @@ Item {
2227
//console.log("chartBatteryRealTime // loadGraph() >> " + currentDevice)
2328

2429
//// DATA
25-
battery1Data.clear()
30+
batteryData_p.clear()
31+
batteryData_v.clear()
32+
33+
//// AXIS
34+
//axisVolts.min = currentDevice.voltMin*0.85
35+
//axisVolts.max = currentDevice.voltMax*1.15
2636

27-
legendColor = Qt.rgba(legendColor.r, legendColor.g, legendColor.b, 0.8)
37+
//// LEGEND
38+
chartBatteryRealTime.limitMin_v = (currentPreset) ? currentPreset.voltageMin : 0
39+
chartBatteryRealTime.limitMax_v = (currentPreset) ? currentPreset.voltageMax : 0
40+
chartBatteryRealTime.legendColor = Qt.rgba(legendColor.r, legendColor.g, legendColor.b, 0.8)
2841
}
2942

3043
function reloadGraph() {
3144
if (typeof currentDevice === "undefined" || !currentDevice) return
3245
if (appContent.state !== "DeviceBatteryMonitor") return
3346
//console.log("chartBatteryRealTime // reloadGraph() >> " + currentDevice)
3447

35-
currentDevice.getChartData_batteryRT(axisTime, battery1Data, true)
48+
currentDevice.getChartData_batteryRT(axisTime, batteryData_p, batteryData_v, true)
3649
}
3750

3851
function updateGraph() {
@@ -41,7 +54,21 @@ Item {
4154
//console.log("chartBatteryRealTime // updateGraph() >> " + currentDevice)
4255

4356
// update
44-
currentDevice.getChartData_batteryRT(axisTime, battery1Data, false)
57+
currentDevice.getChartData_batteryRT(axisTime, batteryData_p, batteryData_v, false)
58+
}
59+
60+
function updatePreset() {
61+
if (typeof currentDevice === "undefined" || !currentDevice) return
62+
63+
if (currentPreset) {
64+
console.log("DeviceBatteryMonitor // onPresetUpdated() >> " + currentPreset.name)
65+
chartBatteryRealTime.limitMin_v = currentPreset.voltageMin
66+
chartBatteryRealTime.limitMax_v = currentPreset.voltageMax
67+
} else {
68+
console.log("DeviceBatteryMonitor // onPresetUpdated() >> empty preset")
69+
chartBatteryRealTime.limitMin_v = 0
70+
chartBatteryRealTime.limitMax_v = 0
71+
}
4572
}
4673

4774
function isIndicator() { return false }
@@ -66,6 +93,7 @@ Item {
6693

6794
ChartView {
6895
id: rtGraph
96+
6997
anchors.fill: parent
7098
anchors.topMargin: -40
7199
anchors.leftMargin: -32
@@ -85,18 +113,57 @@ Item {
85113
color: legendColor;
86114
gridLineColor: Theme.colorSeparator; }
87115

116+
ValueAxis { id: axisVolts; visible: true; gridVisible: false;
117+
labelFormat: "%i";
118+
labelsFont.pixelSize: Theme.fontSizeContentSmall-1; labelsColor: legendColor;
119+
min: 0; max: 20; }
120+
88121
DateTimeAxis { id: axisTime; visible: true; gridVisible: true;
89122
labelsFont.pixelSize: Theme.fontSizeContentSmall-1; labelsColor: legendColor;
90123
color: legendColor;
91124
gridLineColor: Theme.colorSeparator; }
92125

93126
LineSeries {
94-
id: battery1Data
127+
id: batteryData_p
95128
useOpenGL: useOpenGL
96129
pointsVisible: showGraphDots
97130
color: Theme.colorMaterialBlue; width: 2;
98131
axisY: axisPercent; axisX: axisTime;
99132
}
133+
LineSeries {
134+
id: batteryData_v
135+
useOpenGL: useOpenGL
136+
pointsVisible: showGraphDots
137+
color: Theme.colorGreen; width: 2;
138+
axisY: axisVolts; axisX: axisTime;
139+
}
140+
}
141+
142+
////////////////////////////////////////////////////////////////////////////
143+
144+
Item {
145+
id: legend_area
146+
147+
width: rtGraph.plotArea.width
148+
height: rtGraph.plotArea.height
149+
x: rtGraph.plotArea.x + rtGraph.anchors.leftMargin
150+
y: rtGraph.plotArea.y + rtGraph.anchors.topMargin
151+
152+
visible: rtGraph.visible
153+
clip: true
154+
155+
Rectangle {
156+
anchors.left: parent.left
157+
anchors.right: parent.right
158+
159+
y: UtilsNumber.mapNumber(Math.min(limitMax_v, valueMax_v), // value
160+
valueMin_p, valueMax_v, // from
161+
rtGraph.plotArea.height, 0) // to
162+
height: ((Math.min(limitMax_v, valueMax_v) - limitMin_v) / (valueMax_v - valueMin_v)) * rtGraph.plotArea.height
163+
164+
color: Theme.colorGreen
165+
opacity: 0.20
166+
}
100167
}
101168

102169
////////////////////////////////////////////////////////////////////////////

src/devices/device_bm26.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ void DeviceTheengsBM26::bleReadDone(const QLowEnergyCharacteristic &c, const QBy
208208

209209
void DeviceTheengsBM26::bleReadNotify(const QLowEnergyCharacteristic &c, const QByteArray &value)
210210
{
211-
qDebug() << "DeviceTheengsBM26::bleReadNotify(" << m_deviceAddress << ") on" << c.name() << " / uuid" << c.uuid() << value.size();
212-
qDebug() << "DATA: 0x" << value.toHex();
211+
//qDebug() << "DeviceTheengsBM26::bleReadNotify(" << m_deviceAddress << ") on" << c.name() << " / uuid" << c.uuid() << value.size();
212+
//qDebug() << "DATA: 0x" << value.toHex();
213213

214214
// Volt UUID // 16 bytes frames
215215
if (c.uuid() == uuid_volt_char_notify && value.size() == 16)
@@ -228,6 +228,7 @@ void DeviceTheengsBM26::bleReadNotify(const QLowEnergyCharacteristic &c, const Q
228228
mbedtls_aes_free(&aes);
229229

230230
float volt = ((output[2] | (output[1] << 8)) >> 4) / 100.0f;
231+
//qDebug() << "volt : " << volt;
231232

232233
if (areValuesValid_voltage(volt))
233234
{
@@ -240,10 +241,13 @@ void DeviceTheengsBM26::bleReadNotify(const QLowEnergyCharacteristic &c, const Q
240241
Q_EMIT dataUpdated();
241242
}
242243

243-
// save?
244+
// rt data
245+
addRealtimeRecord_voltage(QDateTime::currentDateTime(), volt);
246+
247+
// save in db?
244248
if (needsUpdateDb())
245249
{
246-
addDatabaseRecord_voltage(m_lastUpdate, m_batteryVoltage);
250+
addDatabaseRecord_voltage(m_lastUpdate, volt);
247251
}
248252
}
249253
else
@@ -292,7 +296,10 @@ void DeviceTheengsBM26::actionFakeVoltage()
292296
Q_EMIT dataUpdated();
293297
}
294298

295-
// save?
299+
// rt graph
300+
addRealtimeRecord_voltage(QDateTime::currentDateTime(), m_batteryVoltage);
301+
302+
// database
296303
if (needsUpdateDb())
297304
{
298305
addDatabaseRecord_voltage(m_lastUpdate, m_batteryVoltage);

src/devices/device_bm26.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class DeviceTheengsBM26: public DeviceTheengsBatteryMonitors
4343
Q_OBJECT
4444

4545
QBluetoothUuid uuid_volt_srv = QBluetoothUuid(QStringLiteral("0000fff0-0000-1000-8000-00805f9b34fb"));
46-
QBluetoothUuid uuid_volt_char_notify = QBluetoothUuid(QStringLiteral("0000ff04-0000-1000-8000-00805f9b34fb"));
46+
QBluetoothUuid uuid_volt_char_notify = QBluetoothUuid(QStringLiteral("0000fff4-0000-1000-8000-00805f9b34fb"));
4747

4848
QLowEnergyService *m_serviceVolt = nullptr;
4949
QLowEnergyCharacteristic m_charNotif;

0 commit comments

Comments
 (0)