Giter Site home page Giter Site logo

stm32dfu's People

Contributors

kischang avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

stm32dfu's Issues

Example is not working

Hello,
I'm trying to implement DFU in my react project.

I've tried a lot of different things without any success.

My code:

import React, { Component } from 'react';
import stm32dfu from 'stm32dfu';

import { Buffer } from "buffer"

class DfuUploader extends Component {
  constructor(props) {
    super(props);
    this.state = {
      dfuImage: null,
      error: null,
      loading: true,
      deviceSettings: null,
    };
    this.flashFirmware = this.flashFirmware.bind(this);
    this.initializeDevice = this.initializeDevice.bind(this);
  }

  async componentDidMount() {
    await this.fetchDfuFile();
  }

  async fetchDfuFile() {
    try {
      const response = await fetch('/public/firmwares/firmware.bin');

      if (!response.ok) {
        throw new Error('Network response was not ok');
      }

      const fileBlob = await response.blob();
      console.log('File Blob:', fileBlob);

      const arrayBuffer = await fileBlob.arrayBuffer().then();
      console.log('Array Buffer:', arrayBuffer);

      if (!arrayBuffer) {
        throw new Error('Failed to convert Blob to ArrayBuffer');
      }

      // Pass the Uint8Array to parseDfuImage
      const parsedDfuImage = stm32dfu.parseDfuImage(arrayBuffer);
      console.log('Parsed DFU Image:', parsedDfuImage);

      this.setState({
        dfuImage: parsedDfuImage,
        loading: false,
      });
    } catch (error) {
      console.error('Error fetching or parsing DFU file:', error);
      this.setState({
        error: error.message,
        loading: false,
      });
    }
  }

  async initializeDevice() {
    try {
      const deviceSettings = await stm32dfu.findAllStm32Device(0x0483); // Replace with the actual vendorId

      if (!deviceSettings.length) {
        throw new Error('No STM32 devices found');
      }

      const device = await stm32dfu.getDfu(deviceSettings[0].device, deviceSettings[0]);

      this.setState({ deviceSettings, device });
    } catch (error) {
      console.error('Error initializing device:', error);
      this.setState({
        error: error.message,
      });
    }
  }

  async flashFirmware() {
    const { deviceSettings, dfuImage } = this.state;

    if (!deviceSettings || !dfuImage) {
      console.error('Device settings or DFU image is not available.');
      return;
    }

    const flashSetting = {
      logger: {
        debug: (...data) => { console.log(data); },
        info: (...data) => { console.log(data); },
        warn: (...data) => { console.log(data); },
        error: (...data) => { console.log(data); },
      },
      handler: (done, total) => {
        console.log(`${done} / ${total}`);
      },
    };

    try {
      await stm32dfu.flash(deviceSettings[0], dfuImage, flashSetting);
      console.log('Firmware flashed successfully!');
    } catch (error) {
      console.error('Error flashing firmware:', error);
    }
  }

  render() {
    const { error, loading } = this.state;

    return (
      <div>
        <h1>DFU Uploader</h1>
        {error ? (
          <p>Error: {error}</p>
        ) : loading ? (
          <p>Loading DFU file...</p>
        ) : (
          <div>
            <p>DFU Image successfully parsed!</p>
            <button onClick={this.initializeDevice}>Initialize Device</button>
            <button onClick={this.flashFirmware}>Flash Firmware</button>
          </div>
        )}
      </div>
    );
  }
}

export default DfuUploader;

I'm always getting this error : Error: e2.slice(...).readInt32LE is not a function ... I'm pretty sure I missed something, but I can't find out.
I'm guessing that my firmwareFile is not in the correct buffer format to pass it but idk ...
Does one have an idea ?

Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.