Last active
September 29, 2017 07:56
-
-
Save nrobinson2000/6be1dbf6ef77bb947e42 to your computer and use it in GitHub Desktop.
Particle Offline Utility: A handy script for installing and using the Particle Toolchain on Ubuntu-based Distros and OSX
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Particle Offline Utility | |
| if [ "$1" == "help" ]; | |
| then | |
| echo " po-util 1.1 | |
| To install dependencies run \"po-util install\" | |
| To correctly format a project folder run \"po-util init\" | |
| To build a binary run \"po-util DEVICE build\", with DEVICE as either \"photon\" | |
| or \"electron\" | |
| To build a binary and upload (flash) to the connected device using dfu-util | |
| run \"po-util DEVICE flash\" | |
| If you need to reset the build directory run \"po-util DEVICE clean\" | |
| Summary: Code in the firmware/ directory is compiled and saved as a binary. | |
| The binary can be flashed to a connected device over dfu-util." | |
| exit | |
| fi | |
| CWD=$(pwd) | |
| if [ "$1" == "install" ]; | |
| then | |
| mkdir ~/github | |
| cd ~/github | |
| git clone https://github.com/spark/firmware.git | |
| if [ "$(uname -s)" == "Linux" ]; | |
| then | |
| cd ~/github || exit | |
| # Install dependencies | |
| echo "installing dependencies..." | |
| curl -sL https://deb.nodesource.com/setup_5.x | sudo -E bash - | |
| sudo apt-get remove -y node modemmanager gcc-arm-none-eabi | |
| sudo apt-get install -y nodejs python-software-properties python g++ make build-essential libusb-1.0-0-dev | |
| # Install dfu-util | |
| wget "https://sourceforge.net/projects/dfu-util/files/dfu-util-0.9.tar.gz/download" | |
| tar -xzvf download | |
| rm download | |
| cd dfu-util-0.9 || exit | |
| ./configure | |
| make | |
| sudo make install | |
| cd .. | |
| rm -rf dfu-util-0.9 | |
| # Install gcc-arm-embedded | |
| sudo apt-add-repository ppa:terry.guo/gcc-arm-embedded | |
| sudo apt-get update | |
| sudo apt-get install gcc-arm-none-eabi | |
| # clone firmware repository | |
| cd ~/github || exit | |
| git clone https://github.com/spark/firmware.git | |
| # install particle-cli | |
| sudo npm install -g node-pre-gyp npm | |
| sudo npm install -g particle-cli | |
| # create udev rules file | |
| wget https://gist.github.com/monkbroc/b283bb4da8c10228a61e/raw/e59c77021b460748a9c80ef6a3d62e17f5947be1/50-particle.rules | |
| sudo mv 50-particle.rules /etc/udev/rules.d/50-particle.rules | |
| fi | |
| if [ "$(uname -s)" == "Darwin" ]; | |
| then | |
| /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" | |
| brew tap PX4/homebrew-px4 | |
| brew update | |
| brew install gcc-arm-none-eabi-49 dfu-util wget | |
| wget https://nodejs.org/dist/v5.8.0/node-v5.8.0.pkg | |
| sudo installer -pkg node-v5.8.0.pkg -target / | |
| rm node-v5.8.0.pkg | |
| sudo npm install -g node-pre-gyp npm | |
| sudo npm install -g particle-cli | |
| fi | |
| echo "Sucessfully Installed!" && exit | |
| fi | |
| if [ "$1" == "install-helper" ]; | |
| then | |
| cd ~ | |
| curl -O https://gist.github.com/nrobinson2000/7f9dfaef66dccc6c34834ca2ef5dd8ca/raw/d02769d732801965e8bddbd89887755d6e7bc4d5/po-helper-linux.py | |
| chmod +x po-helper-linux.py | |
| fi | |
| if [ "$1" == "init" ]; | |
| then | |
| mkdir firmware/ | |
| cp *.cpp firmware/ | |
| cp *.h firmware/ | |
| ls firmware/ | grep -v "particle.include" | cat > firmware/particle.include | |
| echo "Copied c++ files into firmware directory. Setup complete." | |
| exit | |
| fi | |
| if [ -d "firmware" ]; | |
| then echo | |
| else echo "Please run with \"init\" to setup this project directory." && exit | |
| fi | |
| if [ "$1" == "photon" ] || [ "$1" == "electron" ]; | |
| then echo "$1 selected." | |
| else echo "Please select photon or electron. Try \"po-util help\" for help." && exit | |
| fi | |
| if [ "$2" == "build" ] || [ "$2" == "flash" ] || [ "$2" == "clean" ] || [ "$2" == "update" ] || [ "$2" == "upload" ] || [ "$2" == "dfu" ]; | |
| then echo "$2 selected." | |
| else echo "Please decide whether to update, dfu, upload, clean, build, or build and flash. Try \"po-util help\" for | |
| help." && exit | |
| fi | |
| cd ~/github/firmware || exit | |
| if [ "$1" == "photon" ]; | |
| then git checkout latest | |
| fi | |
| if [ "$1" == "electron" ]; | |
| then git checkout prereleases/electron-v0.4.8-rc.6 | |
| fi | |
| if [ "$2" == "update" ]; | |
| then git pull | |
| fi | |
| if [ "$2" == "clean" ]; | |
| then make clean | |
| fi | |
| if [ "$2" == "upload" ]; | |
| then | |
| particle flash "$3" "$CWD/bin/firmware.bin" | |
| fi | |
| if [ "$2" == "build" ]; | |
| then make all -s -C ~/github/firmware APPDIR="$CWD/firmware" TARGET_DIR="$CWD/bin" PLATFORM="$1" | |
| echo "Binary saved to $CWD/bin/firmware.bin" | |
| fi | |
| if [ "$2" == "flash" ]; | |
| then | |
| if [ "$(uname -s)" == "Darwin" ]; | |
| then | |
| stty -f /dev/tty.usbmodem1411 14400 | |
| make all -s -C ~/github/firmware APPDIR="$CWD/firmware" TARGET_DIR="$CWD/bin" PLATFORM="$1" | |
| dfu-util -d 2b04:d006 -a 0 -i 0 -s 0x080A0000:leave -D "$CWD/bin/firmware.bin" | |
| else | |
| ~/po-helper-linux.py | |
| make all -s -C ~/github/firmware APPDIR="$CWD/firmware" TARGET_DIR="$CWD/bin" PLATFORM="$1" | |
| dfu-util -d 2b04:d006 -a 0 -i 0 -s 0x080A0000:leave -D "$CWD/bin/firmware.bin" | |
| fi | |
| fi | |
| if [ "$2" == "dfu" ]; | |
| then | |
| if [ "$(uname -s)" == "Darwin" ]; | |
| then | |
| stty -f /dev/tty.usbmodem1411 14400 | |
| dfu-util -d 2b04:d006 -a 0 -i 0 -s 0x080A0000:leave -D "$CWD/bin/firmware.bin" | |
| else | |
| ~/po-helper-linux.py | |
| dfu-util -d 2b04:d006 -a 0 -i 0 -s 0x080A0000:leave -D "$CWD/bin/firmware.bin" | |
| fi | |
| fi |
Author
This is very nice script!
Thank you for it!
Saves a ton of time!
This is one fantastic tool! I've been using it on a side project for about a month. I'm totally impressed!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
UPDATE:
This is now a repository. https://github.com/nrobinson2000/po-util/
Notes:
To fully make use of this script you must first download and save it to your home folder.
Make the script executable and create an alias for it in your .bashrc.
You next have to install the Particle toolchain and dependecies. All of this can be taken care with:
To format your working directory into a project folder run:
Next, put your device into dfu mode and install the firmware patch with:
replace _DEVICE_ with either _photon_ or _electron_
To compile and test your firmware run:
To compile and automagically upload your firmware with dfu-util run:
Discuss on Particle Forums