started on battery life calculator
This commit is contained in:
parent
0d9ecf1fb9
commit
ece1ed6bbe
8 changed files with 358 additions and 25 deletions
147
src/routes/tools/battery-life-calculator/+page.svelte
Normal file
147
src/routes/tools/battery-life-calculator/+page.svelte
Normal file
|
@ -0,0 +1,147 @@
|
|||
<script>
|
||||
import A4 from "../../zhen/notes/physics/sharedComps/A4.svelte";
|
||||
import { BatteryLifeCalculator } from "./comps/BatteryCalc";
|
||||
|
||||
let mathMachine = new BatteryLifeCalculator();
|
||||
|
||||
let useCustom = false;
|
||||
let selectedText = '';
|
||||
const options = ["Text One", "Text Two", "Text Three"];
|
||||
</script>
|
||||
|
||||
<div class="flex justify-center pt-10">
|
||||
<A4
|
||||
bottomBorder={false}
|
||||
bgColor={"rounded-lg bg-base-300"}
|
||||
class="cozette text-base-content h-full"
|
||||
>
|
||||
<div class="p-4 flex flex-col h-full">
|
||||
<h1 class="text-5xl font-bold">Battery life calculator</h1>
|
||||
<span class="w-full text-xl">
|
||||
Calculates the time a battery will last. Too lazy to explain
|
||||
more.
|
||||
</span>
|
||||
|
||||
<!-- Spacing -->
|
||||
<div class="pt-14"></div>
|
||||
|
||||
<div class="p-4 bg-base-200 rounded-lg">
|
||||
<h2 class="text-2xl font-bolc">Software</h2>
|
||||
<div class="grid grid-cols-2 gap-4">
|
||||
<div>
|
||||
<span class="text-sm text-slate-300 text-opacity-60"
|
||||
>Duration of code execution</span
|
||||
>
|
||||
<div class="flex">
|
||||
<input
|
||||
type="number"
|
||||
class="bg-slate-600 bg-opacity-60 rounded-l-lg appearance-none [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:appearance-none"
|
||||
/>
|
||||
<div
|
||||
class="bg-slate-500 bg-opacity-60 rounded-r-lg"
|
||||
>
|
||||
<span class="text-center pl-1 pr-2">sec</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<span class="text-sm text-slate-300 text-opacity-60"
|
||||
>sleep time</span
|
||||
>
|
||||
<div class="flex">
|
||||
<input
|
||||
type="number"
|
||||
class="bg-slate-600 bg-opacity-60 rounded-l-lg appearance-none [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:appearance-none"
|
||||
/>
|
||||
<div
|
||||
class="bg-slate-500 bg-opacity-60 rounded-r-lg"
|
||||
>
|
||||
<span class="text-center pl-1 pr-2">sec</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="col-span-full">
|
||||
<h2 class=" text-2xl font-bolc">Hardware</h2>
|
||||
<div class="flex">
|
||||
<div class="text-sm text-slate-300 text-opacity-60 text">Use custom values</div>
|
||||
<input bind:checked={useCustom} class="ml-2 checkbox checkbox-xs my-auto" type="checkbox" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{#if !useCustom}
|
||||
<select bind:value={selectedText} class="select select-sm select-bordered w-full max-w-xs">
|
||||
<option disabled value="">Select a text</option>
|
||||
{#each options as option}
|
||||
<option value={option}>{option}</option>
|
||||
{/each}
|
||||
</select>
|
||||
|
||||
{#if selectedText}
|
||||
<p class="mt-4 text-lg">You selected: {selectedText}</p>
|
||||
{/if}
|
||||
{:else}
|
||||
<div>
|
||||
<span class="text-sm text-slate-300 text-opacity-60"
|
||||
>Duration of code execution</span
|
||||
>
|
||||
<div class="flex">
|
||||
<input
|
||||
type="number"
|
||||
class="bg-slate-600 bg-opacity-60 rounded-l-lg appearance-none [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:appearance-none"
|
||||
/>
|
||||
<div class="bg-slate-500 bg-opacity-60 rounded-r-lg">
|
||||
<span class="text-center pl-1 pr-2">sec</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<span class="text-sm text-slate-300 text-opacity-60"
|
||||
>sleep time</span
|
||||
>
|
||||
<div class="flex">
|
||||
<input
|
||||
type="number"
|
||||
class="bg-slate-600 bg-opacity-60 rounded-l-lg appearance-none [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:appearance-none"
|
||||
/>
|
||||
<div class="bg-slate-500 bg-opacity-60 rounded-r-lg">
|
||||
<span class="text-center pl-1 pr-2">sec</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mt-auto align-text-bottom text-center">
|
||||
Source for the calculations is at this
|
||||
<a
|
||||
class="text-blue-500 underline"
|
||||
href="https://github.com/simonneutert/batterylife-calculator"
|
||||
>github</a
|
||||
>
|
||||
and the
|
||||
<a
|
||||
class="text-blue-500 underline"
|
||||
href="https://www.of-things.de/battery-life-calculator.php"
|
||||
>original website.</a
|
||||
>
|
||||
I just mearly made additions.
|
||||
</div>
|
||||
</div>
|
||||
</A4>
|
||||
</div>
|
||||
|
||||
<style>
|
||||
/* Hide the spinner for Chrome, Safari, Edge, Opera */
|
||||
input::-webkit-outer-spin-button,
|
||||
input::-webkit-inner-spin-button {
|
||||
-webkit-appearance: none;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
/* Hide the spinner for Firefox */
|
||||
input[type="number"] {
|
||||
-moz-appearance: textfield;
|
||||
}
|
||||
</style>
|
108
src/routes/tools/battery-life-calculator/comps/BatteryCalc.js
Normal file
108
src/routes/tools/battery-life-calculator/comps/BatteryCalc.js
Normal file
|
@ -0,0 +1,108 @@
|
|||
export class BatteryLifeCalculator {
|
||||
constructor(
|
||||
timeRunSeconds,
|
||||
timeSleepSeconds,
|
||||
consumptionActiveMilliAmpHours,
|
||||
consumptionSleepMilliAmpHours,
|
||||
powerBatteryTotalMilliAmpHours,
|
||||
powerBatteryBufferBeforeEmptyPercent = 20) {
|
||||
this.timeRunSeconds = timeRunSeconds
|
||||
this.timeSleepSeconds = timeSleepSeconds
|
||||
this.consumptionActiveMilliAmpHours = consumptionActiveMilliAmpHours
|
||||
this.consumptionSleepMilliAmpHours = consumptionSleepMilliAmpHours
|
||||
this.powerBatteryTotalMilliAmpHours = powerBatteryTotalMilliAmpHours
|
||||
this.powerBatteryBufferBeforeEmptyPercent = powerBatteryBufferBeforeEmptyPercent
|
||||
|
||||
console.log("The source of this battery calc is here: https://github.com/simonneutert/batterylife-calculator\nI was too lazy to make the math myself.");
|
||||
}
|
||||
|
||||
// public API
|
||||
|
||||
milliAmpToMicroAmp(milliAmps) {
|
||||
return milliAmps * 1000
|
||||
}
|
||||
|
||||
microAmpToMilliAmp(milliAmps) {
|
||||
return milliAmps * 0.001
|
||||
}
|
||||
|
||||
calculate() {
|
||||
return {
|
||||
powerAveragePerHour: this.powerEstimatedHourly(),
|
||||
runtimeHoursEstimated: this.runtimeHoursEstimated(),
|
||||
runtimeDaysEstimated: this.runtimeDaysEstimated(),
|
||||
runtimeDaysRemainingHoursEstimated: this.runtimeDaysRemainingHoursEstimated()
|
||||
}
|
||||
}
|
||||
|
||||
powerEstimatedHourly() {
|
||||
return this.calcPowerEst(
|
||||
this.powerRun(),
|
||||
this.consumptionActiveMilliAmpHours,
|
||||
this.powerSleep(),
|
||||
this.consumptionSleepMilliAmpHours
|
||||
)
|
||||
}
|
||||
|
||||
runtimeHoursEstimated() {
|
||||
return parseInt(this.powerLipo() / this.powerEstimatedHourly())
|
||||
}
|
||||
|
||||
runtimeDaysEstimated() {
|
||||
return parseInt(this.runtimeHoursEstimated() / 24)
|
||||
}
|
||||
|
||||
runtimeDaysRemainingHoursEstimated() {
|
||||
return parseInt(this.runtimeHoursEstimated() % 24)
|
||||
}
|
||||
|
||||
// private
|
||||
|
||||
roundOff(x) {
|
||||
return Math.round(x * 100.0) / 100.0
|
||||
}
|
||||
|
||||
calcPowerLipo(x, y) {
|
||||
return parseFloat((x * (100 - y)) / 100)
|
||||
}
|
||||
|
||||
calcRuns(x, y) {
|
||||
return parseFloat(60 / (x + y))
|
||||
}
|
||||
|
||||
calcRunsHour(x, y) {
|
||||
return parseFloat(3600 / (x + y))
|
||||
}
|
||||
|
||||
calcPowerRun(x, y) {
|
||||
return parseFloat((x / (x + y)) * 3600)
|
||||
}
|
||||
|
||||
calcPowerSleep(x, y) {
|
||||
return parseFloat((y / (x + y)) * 3600)
|
||||
}
|
||||
|
||||
powerLipo() {
|
||||
return this.calcPowerLipo(this.powerBatteryTotalMilliAmpHours, this.powerBatteryBufferBeforeEmptyPercent)
|
||||
}
|
||||
|
||||
runs() {
|
||||
return this.calcRuns(this.timeRunSeconds, this.timeSleepSeconds)
|
||||
}
|
||||
|
||||
runsHour() {
|
||||
return this.calcRunsHour(this.timeRunSeconds, this.timeSleepSeconds)
|
||||
}
|
||||
|
||||
powerRun() {
|
||||
return this.calcPowerRun(this.timeRunSeconds, this.timeSleepSeconds)
|
||||
}
|
||||
|
||||
powerSleep() {
|
||||
return this.calcPowerSleep(this.timeRunSeconds, this.timeSleepSeconds)
|
||||
}
|
||||
|
||||
calcPowerEst(a, b, c, d) {
|
||||
return parseFloat((a / 3600) * b + (c / 3600) * d)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue