Skip to content

Commit

Permalink
feat: use vue battery icon
Browse files Browse the repository at this point in the history
  • Loading branch information
chmelevskij committed May 8, 2022
1 parent 6319421 commit c5811f8
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 96 deletions.
2 changes: 2 additions & 0 deletions src/components/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import vueI18n from "./vueI18n.js";
import BatteryLegend from "./quad-status/BatteryLegend.vue";
import BetaflightLogo from "./betaflight-logo/BetaflightLogo.vue";
import StatusBar from "./status-bar/StatusBar.vue";
import BatteryIcon from "./quad-status/BatteryIcon.vue";

// Most of the global objects can go here at first.
// It's a bit of overkill for simple components,
Expand Down Expand Up @@ -38,6 +39,7 @@ i18next.on('initialized', function() {
BatteryLegend,
BetaflightLogo,
StatusBar,
BatteryIcon,
},
data: betaflightModel,
});
Expand Down
16 changes: 16 additions & 0 deletions src/components/quad-status/BatteryIcon.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import BatteryIcon from "./BatteryIcon.vue";

// More on default export: https://storybook.js.org/docs/vue/writing-stories/introduction#default-export
export default {
title: "Battery Icon",
component: BatteryIcon,
};

// More on component templates: https://storybook.js.org/docs/vue/writing-stories/introduction#using-args
const Template = (_args, { argTypes }) => ({
props: Object.keys(argTypes),
components: { BatteryIcon },
template: '<battery-icon v-bind="$props" />',
});

export const Primary = Template.bind({});
127 changes: 127 additions & 0 deletions src/components/quad-status/BatteryIcon.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<template>
<div class="battery-icon">
<div class="quad-status-contents">
<div
class="battery-status"
:class="classes"
:style="{ width: batteryWidth + '%' }"
/>
</div>
</div>
</template>
<script>
const NO_BATTERY_VOLTAGE_MAXIMUM = 1.8; // Maybe is better to add a call to MSP_BATTERY_STATE but is not available for all versions
export default {
props: {
voltage: {
type: Number,
default: 0,
},
vbatmincellvoltage: {
type: Number,
default: 1,
},
vbatmaxcellvoltage: {
type: Number,
default: 1,
},
vbatwarningcellvoltage: {
type: Number,
default: 1,
},
},
computed: {
nbCells() {
let nbCells = Math.floor(this.voltage / this.vbatmaxcellvoltage) + 1;
if (this.voltage === 0) {
nbCells = 1;
}
return nbCells;
},
min() {
return this.vbatmincellvoltage * this.nbCells;
},
max() {
return this.vbatmaxcellvoltage * this.nbCells;
},
warn() {
return this.vbatwarningcellvoltage * this.nbCells;
},
isEmpty() {
return (
this.voltage < this.min && this.voltage > NO_BATTERY_VOLTAGE_MAXIMUM
);
},
classes() {
const isWarning = this.voltage < this.warn;
return {
"state-empty": this.isEmpty,
"state-warning": isWarning,
"state-ok": !this.isEmpty && !isWarning,
};
},
batteryWidth() {
return this.isEmpty
? 100
: ((this.voltage - this.min) / (this.max - this.min)) * 100;
},
},
};
</script>

<style>
.quad-status-contents {
display: inline-block;
margin-top: 10px;
margin-left: 14px;
height: 10px;
width: 31px;
}
.quad-status-contents progress::-webkit-progress-bar {
height: 12px;
background-color: #eee;
}
.quad-status-contents progress::-webkit-progress-value {
background-color: #bcf;
}
.battery-icon {
background-image: url(../../images/icons/cf_icon_bat_grey.svg);
background-size: contain;
background-position: center;
display: inline-block;
height: 30px;
width: 60px;
transition: none;
margin-top: 4px;
margin-left: -4px;
background-repeat: no-repeat;
}
.battery-status {
height: 11px;
}
@keyframes error-blinker {
0% {
background-color: transparent;
}
50% {
background-color: var(--error);
}
}
.battery-status.state-ok {
background-color: #59aa29;
}
.battery-status.state-warning {
background-color: var(--error);
}
.battery-status.state-empty {
animation: error-blinker 1s linear infinite;
}
</style>
55 changes: 0 additions & 55 deletions src/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -648,61 +648,6 @@ input[type="number"]::-webkit-inner-spin-button {
}
}

.quad-status-contents {
display: none;
margin-top: 10px;
margin-left: 14px;
height: 10px;
width: 31px;
}

.quad-status-contents progress::-webkit-progress-bar {
height: 12px;
background-color: #eee;
}

.quad-status-contents progress::-webkit-progress-value {
background-color: #bcf;
}

.battery-status {
height: 11px;
}

.battery-status.state-ok {
background-color: #59AA29;
}
.battery-status.state-warning {
background-color: var(--error);
}

.battery-status.state-empty {
animation: error-blinker 1s linear infinite;
}

@keyframes error-blinker {
0% {
background-color: transparent;
}
50% {
background-color: var(--error);
}
}

.battery-icon {
background-image: url(../images/icons/cf_icon_bat_grey.svg);
background-size: contain;
background-position: center;
display: inline-block;
height: 30px;
width: 60px;
transition: none;
margin-top: 4px;
margin-left: -4px;
background-repeat: no-repeat;
}


.armedicon,
.failsafeicon,
.linkicon {
Expand Down
36 changes: 0 additions & 36 deletions src/js/serial_backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -697,10 +697,6 @@ function update_live_status() {

const statuswrapper = $('#quad-status_wrapper');

$(".quad-status-contents").css({
display: 'inline-block',
});

if (GUI.active_tab !== 'cli' && GUI.active_tab !== 'presets') {
MSP.send_message(MSPCodes.MSP_BOXNAMES, false, false);
if (semver.gte(FC.CONFIG.apiVersion, API_VERSION_1_32)) {
Expand Down Expand Up @@ -730,38 +726,6 @@ function update_live_status() {
}
}

if (FC.ANALOG != undefined) {
let nbCells = Math.floor(FC.ANALOG.voltage / FC.BATTERY_CONFIG.vbatmaxcellvoltage) + 1;

if (FC.ANALOG.voltage == 0) {
nbCells = 1;
}

const min = FC.BATTERY_CONFIG.vbatmincellvoltage * nbCells;
const max = FC.BATTERY_CONFIG.vbatmaxcellvoltage * nbCells;
const warn = FC.BATTERY_CONFIG.vbatwarningcellvoltage * nbCells;

const NO_BATTERY_VOLTAGE_MAXIMUM = 1.8; // Maybe is better to add a call to MSP_BATTERY_STATE but is not available for all versions

if (FC.ANALOG.voltage < min && FC.ANALOG.voltage > NO_BATTERY_VOLTAGE_MAXIMUM) {
$(".battery-status").addClass('state-empty').removeClass('state-ok').removeClass('state-warning');
$(".battery-status").css({
width: "100%",
});
} else {
$(".battery-status").css({
width: `${((FC.ANALOG.voltage - min) / (max - min) * 100)}%`,
});

if (FC.ANALOG.voltage < warn) {
$(".battery-status").addClass('state-warning').removeClass('state-empty').removeClass('state-ok');
} else {
$(".battery-status").addClass('state-ok').removeClass('state-warning').removeClass('state-empty');
}
}

}

if (active) {
$(".linkicon").addClass('active');
} else {
Expand Down
12 changes: 7 additions & 5 deletions src/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,13 @@
</div>
<div class="header-wrapper">
<div id="quad-status_wrapper">
<div class="battery-icon">
<div class="quad-status-contents">
<div class="battery-status"></div>
</div>
</div>
<battery-icon
:voltage="FC.ANALOG.voltage"
:vbatmincellvoltage="FC.BATTERY_CONFIG.vbatmincellvoltage"
:vbatmaxcellvoltage="FC.BATTERY_CONFIG.vbatmaxcellvoltage"
:vbatwarningcellvoltage="FC.BATTERY_CONFIG.vbatwarningcellvoltage"
>
</battery-icon>
<battery-legend
:voltage="FC.ANALOG.voltage"
:vbatmaxcellvoltage="FC.BATTERY_CONFIG.vbatmaxcellvoltage"
Expand Down

0 comments on commit c5811f8

Please sign in to comment.