Skip to content

Commit

Permalink
add custom battery type
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelscholle committed May 12, 2024
1 parent 469e359 commit 2d4faa7
Showing 1 changed file with 163 additions and 84 deletions.
247 changes: 163 additions & 84 deletions qml/ui/widgets/GroundPowerWidget.qml
Original file line number Diff line number Diff line change
Expand Up @@ -31,50 +31,56 @@ BaseWidget {

BaseWidgetDefaultUiControlElements {
id: idBaseWidgetDefaultUiControlElements
Item {
width: 230
height: 32
Text {
text: qsTr("Show single cell voltage")
color: "white"
height: parent.height
font.bold: true
font.pixelSize: detailPanelFontPixels
anchors.left: parent.left
verticalAlignment: Text.AlignVCenter
}
Switch {
width: 32
height: parent.height
anchors.rightMargin: 6
anchors.right: parent.right
checked: settings.ground_battery_show_single_cell
onCheckedChanged: settings.ground_battery_show_single_cell = checked
}
}
Item {
width: 230
height: 32

Text {
text: qsTr("Battery Type")
color: "white"
height: parent.height
font.bold: true
font.pixelSize: detailPanelFontPixels
anchors.left: parent.left
verticalAlignment: Text.AlignVCenter
ColumnLayout {
width: 230 // Set the width of the layout
spacing: 10 // Adjust spacing between elements if needed
Layout.alignment: Qt.AlignHCenter // Center the layout horizontally

Item {
width: 230
height: 32
Text {
text: qsTr("Show single cell voltage")
color: "white"
height: parent.height
font.bold: true
font.pixelSize: detailPanelFontPixels
anchors.left: parent.left
verticalAlignment: Text.AlignVCenter
}
Switch {
width: 32
height: parent.height
anchors.rightMargin: 6
anchors.right: parent.right
checked: settings.ground_battery_show_single_cell
onCheckedChanged: settings.ground_battery_show_single_cell = checked
}
}

ComboBox {
id: batteryComboBox
width: 100
height: parent.height
anchors.rightMargin: 6
anchors.right: parent.right
model: ["Lipo", "LiIon", "LiFe"]
onCurrentIndexChanged: {
switch (batteryComboBox.currentIndex) {
Item {
Layout.fillWidth: true // Expand to fill the width of the layout
height: 32

Text {
text: qsTr("Battery Type")
color: "white"
height: parent.height
font.bold: true
font.pixelSize: detailPanelFontPixels
Layout.alignment: Qt.AlignLeft
verticalAlignment: Text.AlignVCenter
}

ComboBox {
id: batteryComboBox
width: 100
height: parent.height
Layout.alignment: Qt.AlignRight | Qt.AlignVCenter // Align to the right and vertically center
model: ["Lipo", "LiIon", "LiFe", "Custom"]
onCurrentIndexChanged: {
switch (batteryComboBox.currentIndex) {
case 0:
settings.ground_battery_type = 0;
settings.ground_battery_low = 33;
Expand All @@ -93,19 +99,92 @@ BaseWidget {
settings.ground_battery_mid = 35;
settings.ground_battery_full = 37;
break;
case 3:
customType.visible=true;
break;
}
}
}
}
Text {
id:customType
text: qsTr("Battery Low")
visible: false
color: "white"
height: 32
font.bold: true
font.pixelSize: detailPanelFontPixels
Layout.topMargin: 25
Layout.alignment: Qt.AlignLeft
verticalAlignment: Text.AlignVCenter
}
TextField {
id: battery_low
visible: customType.visible
color: "white"
width: 200
height: 40
placeholderText: (settings.ground_battery_low/10)
}

Text {
text: qsTr("Battery Mid")
visible: customType.visible
color: "white"
height: 32
font.bold: true
font.pixelSize: detailPanelFontPixels
Layout.alignment: Qt.AlignLeft
verticalAlignment: Text.AlignVCenter
}
TextField {
id: battery_mid
visible: customType.visible
color: "white"
width: 200
height: 40
placeholderText: (settings.ground_battery_mid/10)
}

Text {
text: qsTr("Battery Full")
visible: customType.visible
color: "white"
height: 32
font.bold: true
font.pixelSize: detailPanelFontPixels
Layout.alignment: Qt.AlignLeft
verticalAlignment: Text.AlignVCenter
}
TextField {
id: battery_full
visible: customType.visible
color: "white"
width: 200
height: 40
placeholderText: (settings.ground_battery_full/10)
}
Button {
text: "Save"
visible: customType.visible
Layout.alignment: Qt.AlignHCenter // Center the button horizontally
onClicked: {
settings.ground_battery_type = 3;
settings.ground_battery_low = (battery_low.text*10);
settings.ground_battery_mid = (battery_mid.text*10);
settings.ground_battery_full = (battery_full.text*10);
}
}
}
}
}

function calculateBatteryPercentage(currentVoltage) {
var currentVoltage2 = ((currentVoltage/settings.ground_battery_cells) / 100).toFixed(0);
var fullVoltage = settings.ground_battery_full;
var midVoltage = settings.ground_battery_mid;
var emptyVoltage = settings.ground_battery_low;
var percentage;
var currentVoltage2 = ((currentVoltage/settings.ground_battery_cells) / 100).toFixed(0);
var fullVoltage = settings.ground_battery_full;
var midVoltage = settings.ground_battery_mid;
var emptyVoltage = settings.ground_battery_low;
var percentage;

if (currentVoltage2 >= fullVoltage) {
percentage = 100;
Expand All @@ -122,9 +201,9 @@ BaseWidget {
percentage = 0;
}
}
return percentage; // Ensure the percentage is within [0, 100]
return percentage; // Ensure the percentage is within [0, 100]

}
}


Item {
Expand Down Expand Up @@ -178,17 +257,17 @@ BaseWidget {
onTriggered: {
var currentVoltage = _ohdSystemGround.ina219_voltage_millivolt;
if (_ohdSystemGround.ina219_current_milliamps===1338) {
var percentage = _ohdSystemGround.ina219_voltage_millivolt;
battery_volt_text.visible= false;
battery_amp_text.visible=false;
var percentage = _ohdSystemGround.ina219_voltage_millivolt;
battery_volt_text.visible= false;
battery_amp_text.visible=false;
}
else if (_ohdSystemGround.ina219_current_milliamps===1337) {
var percentage = _ohdSystemGround.ina219_voltage_millivolt;
battery_volt_text.visible= false;
battery_amp_text.visible= false;
var percentage = _ohdSystemGround.ina219_voltage_millivolt;
battery_volt_text.visible= false;
battery_amp_text.visible= false;
}
else {
var percentage = calculateBatteryPercentage(currentVoltage);
var percentage = calculateBatteryPercentage(currentVoltage);
}
settings.ground_voltage_in_percent = percentage;
}
Expand Down Expand Up @@ -252,37 +331,37 @@ BaseWidget {
}
opacity: bw_current_opacity
text: {
var percent = settings.ground_voltage_in_percent;

// Define symbols based on battery level
var symbol;
if (_ohdSystemGround.ina219_current_milliamps===1337) {
symbol = "\uF084"; // Charging
}
else if (percent < 10) {
symbol = "\uf07a"; // Change the symbol for battery level below 10%
} else if (percent < 20) {
symbol = "\uf07b"; // Change the symbol for battery level below 20%
} else if (percent < 30) {
symbol = "\uf07c"; // Change the symbol for battery level below 30%
} else if (percent < 40) {
symbol = "\uf07d"; // Change the symbol for battery level below 40%
} else if (percent < 50) {
symbol = "\uf07e"; // Change the symbol for battery level below 50%
} else if (percent < 60) {
symbol = "\uf07f"; // Change the symbol for battery level below 60%
} else if (percent < 70) {
symbol = "\uf080"; // Change the symbol for battery level below 70%
} else if (percent < 80) {
symbol = "\uf081"; // Change the symbol for battery level below 80%
} else if (percent < 90) {
symbol = "\uf082"; // Change the symbol for battery level below 90%
} else {
symbol = "\uf079"; // Default symbol for battery level above or equal to 100%
}
var percent = settings.ground_voltage_in_percent;

return symbol;
// Define symbols based on battery level
var symbol;
if (_ohdSystemGround.ina219_current_milliamps===1337) {
symbol = "\uF084"; // Charging
}
else if (percent < 10) {
symbol = "\uf07a"; // Change the symbol for battery level below 10%
} else if (percent < 20) {
symbol = "\uf07b"; // Change the symbol for battery level below 20%
} else if (percent < 30) {
symbol = "\uf07c"; // Change the symbol for battery level below 30%
} else if (percent < 40) {
symbol = "\uf07d"; // Change the symbol for battery level below 40%
} else if (percent < 50) {
symbol = "\uf07e"; // Change the symbol for battery level below 50%
} else if (percent < 60) {
symbol = "\uf07f"; // Change the symbol for battery level below 60%
} else if (percent < 70) {
symbol = "\uf080"; // Change the symbol for battery level below 70%
} else if (percent < 80) {
symbol = "\uf081"; // Change the symbol for battery level below 80%
} else if (percent < 90) {
symbol = "\uf082"; // Change the symbol for battery level below 90%
} else {
symbol = "\uf079"; // Default symbol for battery level above or equal to 100%
}

return symbol;
}
anchors.left: parent.left
anchors.leftMargin: 12
fontSizeMode: Text.VerticalFit
Expand Down

0 comments on commit 2d4faa7

Please sign in to comment.