Skip to content

Commit

Permalink
fix rate profile migration for partial profiles like templates
Browse files Browse the repository at this point in the history
  • Loading branch information
bkleiner committed Aug 5, 2022
1 parent 461b558 commit ba1374e
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 67 deletions.
31 changes: 23 additions & 8 deletions src/panel/StickRatesLegacy.vue
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@
</div>
<div
class="columns is-multiline"
v-for="(val, key) in profile.rate[currentMode.text.toLowerCase()]"
v-for="(val, key) in profile.rate[
currentMode.text.toLowerCase()
]"
:key="key"
>
<div class="column is-4">
Expand All @@ -75,7 +77,9 @@
type="number"
step="10"
v-model.number="
profile.rate[currentMode.text.toLowerCase()][key][0]
profile.rate[currentMode.text.toLowerCase()][
key
][0]
"
/>
</div>
Expand All @@ -86,7 +90,9 @@
type="number"
step="10"
v-model.number="
profile.rate[currentMode.text.toLowerCase()][key][1]
profile.rate[currentMode.text.toLowerCase()][
key
][1]
"
/>
</div>
Expand All @@ -97,7 +103,9 @@
type="number"
step="10"
v-model.number="
profile.rate[currentMode.text.toLowerCase()][key][2]
profile.rate[currentMode.text.toLowerCase()][
key
][2]
"
/>
</div>
Expand Down Expand Up @@ -156,7 +164,9 @@

<div class="field is-horizontal">
<div class="field-label">
<label class="label" for="sticks-deadband">SticksDeadband</label>
<label class="label" for="sticks-deadband"
>SticksDeadband</label
>
</div>
<div class="field-body">
<div class="field">
Expand Down Expand Up @@ -233,7 +243,6 @@ export default defineComponent({
watch: {
"profile.rate": {
handler(val) {
console.log(val);
this.update();
},
deep: true,
Expand Down Expand Up @@ -285,7 +294,11 @@ export default defineComponent({
1.0 / this.constrainf(1.0 - rcCommandfAbs * superExpo, 0.01, 1.0);
angleRate *= rcSuperfactor;
}
return this.constrainf(angleRate, -SETPOINT_RATE_LIMIT, SETPOINT_RATE_LIMIT);
return this.constrainf(
angleRate,
-SETPOINT_RATE_LIMIT,
SETPOINT_RATE_LIMIT
);
},
update() {
const axis = [
Expand All @@ -305,7 +318,9 @@ export default defineComponent({
const labels = [] as string[];
const rateMulit = this.plotLowRates ? this.profile.rate.low_rate_mulitplier : 1.0;
const rateMulit = this.plotLowRates
? this.profile.rate.low_rate_mulitplier
: 1.0;
for (let i = -100; i <= 100; i++) {
const input = i / 100.0;
Expand Down
115 changes: 72 additions & 43 deletions src/store/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ import semver from "semver";
import { decodeSemver } from "./util";
import { useRootStore } from "./root";

function mergeDeep(target, source) {
for (const [key, val] of Object.entries(source)) {
if (val !== null && typeof val === `object`) {
if (target[key] === undefined) {
target[key] = new (val as any).__proto__.constructor();
}
mergeDeep(target[key], val);
} else {
target[key] = val;
}
}
// we're replacing in-situ, so this is more for chaining than anything else
return target;
}

function makeSemver(major, minor, patch) {
return (major << 16) | (minor << 8) | patch;
}
Expand All @@ -19,47 +34,62 @@ function ensureMinVersion(version) {
return version;
}

function migrateProfile(profile, version) {
function migrateProfileVersion(profile, version) {
switch (version) {
case makeSemver(0, 1, 0):
profile.osd = {};
case makeSemver(0, 1, 0): {
const silverware = {
mode: 0,
rate: [
profile.rate.silverware?.max_rate || [860, 860, 500],
profile.rate.silverware?.acro_expo || [0.8, 0.8, 0.6],
profile.rate.silverware?.angle_expo || [0.55, 0, 0.55],
],
};
const betaflight = {
mode: 1,
rate: [
profile.rate.betaflight?.rc_rate || [1.3, 1.3, 1.3],
profile.rate.betaflight?.super_rate || [0.7, 0.7, 0.7],
profile.rate.betaflight?.expo || [0.4, 0.4, 0.4],
],
};

profile.rate.profile = 0;
profile.rate.rates = [
{
mode: profile.rate.mode,
rate: [
profile.rate.mode == 1
? profile.rate.betaflight.rc_rate
: profile.rate.silverware.max_rate,
profile.rate.mode == 1
? profile.rate.betaflight.super_rate
: profile.rate.silverware.acro_expo,
profile.rate.mode == 1
? profile.rate.betaflight.expo
: profile.rate.silverware.angle_expo,
],
},
{
mode: profile.rate.mode == 1 ? 0 : 1,
rate: [
profile.rate.mode == 0
? profile.rate.betaflight.rc_rate
: profile.rate.silverware.max_rate,
profile.rate.mode == 0
? profile.rate.betaflight.super_rate
: profile.rate.silverware.acro_expo,
profile.rate.mode == 0
? profile.rate.betaflight.expo
: profile.rate.silverware.angle_expo,
],
},
];

if (profile.rate.mode == 0) {
profile.rate.rates = [silverware, betaflight];
} else {
profile.rate.rates = [betaflight, silverware];
}

break;
}
}

profile.meta.name = profile.meta.name.replace(/\0/g, "");
if (profile.osd?.callsign) {
profile.osd.callsign = profile.osd.callsign.replace(/\0/g, "");
}

return profile;
}

function migrateProfile(profile) {
const default_profile = useDefaultProfileStore();

const firmwareVersion = ensureMinVersion(default_profile.meta.version);
const profileVersion = ensureMinVersion(profile.meta.version);

let p = JSON.parse(JSON.stringify(profile));
if (firmwareVersion != profileVersion) {
p = migrateProfileVersion(p, profileVersion);
}

p.meta.datetime = Math.floor(Date.now() / 1000);

return p;
}

export const useProfileStore = defineStore("profile", {
state: () => ({
semver: "v0.0.0",
Expand Down Expand Up @@ -170,19 +200,18 @@ export const useProfileStore = defineStore("profile", {
fetch_profile() {
return serial.get(QuicVal.Profile).then((p) => this.set_profile(p));
},
apply_profile(profile) {
const root = useRootStore();
const default_profile = useDefaultProfileStore();
async merge_profile(profile) {
const lhs = migrateProfile(await serial.get(QuicVal.Profile));
const rhs = migrateProfile(profile);

const firmwareVersion = ensureMinVersion(default_profile.meta.version);
const profileVersion = ensureMinVersion(profile.meta.version);
const p = mergeDeep(lhs, rhs);

let p = profile;
if (firmwareVersion != profileVersion) {
p = migrateProfile(p, profileVersion);
}
return this.apply_profile(p);
},
apply_profile(profile) {
const root = useRootStore();

p.meta.datetime = Math.floor(Date.now() / 1000);
const p = migrateProfile(profile);

return serial
.set(QuicVal.Profile, p)
Expand Down
31 changes: 15 additions & 16 deletions src/views/Templates.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<template>
<div class="columns is-multiline">
<div class="column is-one-third" v-for="tmpl of templates.index" :key="tmpl.name">
<div
class="column is-one-third"
v-for="tmpl of templates.index"
:key="tmpl.name"
>
<div class="card template-card">
<div class="card-image">
<figure class="image is-square">
Expand All @@ -20,7 +24,10 @@
</div>
<footer class="card-footer">
<span class="card-footer-item"></span>
<spinner-btn class="card-footer-item is-white" @click="applyProfile(tmpl)">
<spinner-btn
class="card-footer-item is-white"
@click="applyProfile(tmpl)"
>
Apply
</spinner-btn>
</footer>
Expand All @@ -31,8 +38,6 @@

<script lang="ts">
import { defineComponent } from "vue";
import { serial } from "../store/serial/serial";
import { QuicVal } from "@/store/serial/quic";
import YAML from "yaml";
import { useTemplatesStore } from "@/store/templates";
import { useProfileStore } from "@/store/profile";
Expand All @@ -47,18 +52,12 @@ export default defineComponent({
},
methods: {
applyProfile(p) {
return Promise.all([
fetch(p.profile)
.then((res) => res.text())
.then((t) => YAML.parse(t)),
serial.get(QuicVal.Profile),
]).then(([patch, profile]) => {
const p = {
...profile,
...patch,
};
return this.profile.apply_profile(p);
});
return fetch(p.profile)
.then((res) => res.text())
.then((t) => YAML.parse(t))
.then((patch) => {
return this.profile.merge_profile(patch);
});
},
},
created() {
Expand Down

0 comments on commit ba1374e

Please sign in to comment.