Code Monkey home page Code Monkey logo

avrbro's Introduction

Avrbro

This library use the Serial Api available in Chrome (with experimental flag enabled) to upload compiled .hex files on Arduino boards without Avrdude, directly from a web page!

For the moment it works only with stk500v1 protocol. That means it (should) works with Uno and Nano boards, but not with Leonardo or Mega boards).

Install

# with npm
npm i avrbro --save

# or with yarn:
yarn add avrbro

Exemple

import avrbro from 'avrbro'
const { parseHex, openSerial, flash, reset } = avrbro

// Here's just an helper function to use async/await with FileReader...
const readFileAsync = (file) => {
  return new Promise((resolve, reject) => {
    let reader = new FileReader()
    reader.onload = () => {
      resolve(reader.result)
    }
    reader.onerror = reject
    reader.readAsArrayBuffer(file)
  })
}

const flashMyBoard = async () => {
  // connect to device using web serial API
  const serial = await openSerial()
  if (serial) {
  
    // get .hex buffer
    const response = await fetch('bin/my-firmware.hex')
    const data = await response.blob()
    const fileData = await readFileAsync(data)
    const hexBuffer = parseHex(new TextDecoder("utf-8").decode(fileData))
    
    // reset the board
    await reset(serial)
    
    // upload .hex file
    const success = await flash(serial, hexBuffer, { boardName: 'nano' })
    if (success) {
      console.log('.hex file uploaded on board successfully!')
    } else {
      console.log('an error has occurred :(')
    }
  } else {
    console.log('operation canceled by user')
  }
}

flashMyBoard()

Api

isAvailable()

Allow to know if the serial API is available.

Returns: Boolean


openSerial(options)

Open serial connection with device. Once called, the browser will open a dedicated modal window to choose the device.

Params

  • options object
    • baudrate Number - defaults to 57600
    • vendorId Number - defaults to 0x2341

Returns: Object or null if cancelled. The Object will match {port, reader, writer} where:

  • port SerialPort
  • reader ReadableStream
  • writer WritableStream

closeSerial(serial)

Close serial connection with device.

Params

  • serial object
    • port SerialPort
    • reader ReadableStream
    • writer WritableStream

parseHex(data)

Use Intel HEX file format to parse given data and prepare buffer for upload.

Params

  • data Object - string in ASCII format or Buffer

Returns: Buffer


flash(serial, hexBuffer, options)

Flash the device connected on the given serial port with the given .hex file buffer.

Params

  • data Object - string in ASCII format or Buffer Object
  • hexBuffer Buffer - the .hex buffer returned by the parseHex function
  • options Object
    • boardName String - Name of the target board (check src/board.js to find available names)
    • debug Boolean - Allow to display logs during flash process

Returns: Boolean


reset(serial)

Reset board with cycle DTR.

Params

  • serial object
    • port SerialPort
    • reader ReadableStream
    • writer WritableStream

Contributing

Contributions in any form are welcome! If you find a bug, please file an issue.

License

This project is licensed under the MIT license. See the LICENSE file for more details.

Acknowledgements

All this stuff was made possible thanks to:

avrbro's People

Contributors

kaelhem avatar

Watchers

 avatar

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.