Skip to content

Instantly share code, notes, and snippets.

@mdahlstrom
Created June 12, 2019 12:33
Show Gist options
  • Save mdahlstrom/08dacdb82ebe1e881b11e91a312dd6ac to your computer and use it in GitHub Desktop.
Save mdahlstrom/08dacdb82ebe1e881b11e91a312dd6ac to your computer and use it in GitHub Desktop.
Vue.js component that uses the package dfu-js to update nrf 51/52 MCU via Web Bluetooth
<template lang="html">
<form v-on:submit.prevent>
<fieldset v-if="dfuIdle">
<input type="file" @change="processFile" />
<button @click="upload">Update Device</button>
</fieldset>
<fieldset v-if="dfuInProgress">
<progress class="progress is-primary" :value="dfuProgress" max="1.0">
</progress>
<button style="margin-top: 0.5rem;" @click="cancel">Cancel</button>
</fieldset>
<fieldset v-if="dfuFailed">
<p>Device Firmware Update failed</p>
<button @click="cancel">Try Again</button>
</fieldset>
</form>
</template>
<script type="text/javascript">
import { MixinDFU, Firmware } from 'dfu-js'
import JSZip from 'jszip'
export default {
name: 'DFUComponent',
mixins: [MixinDFU],
props: {
packetPoint: {
required: true,
validator: val => {
// eslint-disable-next-line
console.log(val)
return (
val === undefined ||
val.constructor.name === 'BluetoothRemoteGATTCharacteristic'
)
}
},
controlPoint: {
required: true,
validator: val => {
return (
val === undefined ||
val.constructor.name === 'BluetoothRemoteGATTCharacteristic'
)
}
}
},
data() {
return { fileName: '' }
},
computed: {
webBluetoothPacketPoint() {
return this.packetPoint
},
webBluetoothControlPoint() {
return this.controlPoint
}
},
methods: {
async processFile(ev) {
const files = ev.target.files
try {
const zip = new JSZip()
await zip.loadAsync(files[0])
await this.firmwareFromZip(zip)
this.fileName = files[0].name
} catch (e) {
// eslint-disable-next-line
console.error(e)
}
},
upload() {
if (this.dfuFirmware instanceof Firmware) {
this.performDFU()
}
},
cancel() {
this.resetDFU()
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment