Skip to content

Instantly share code, notes, and snippets.

@joegoggins
Last active December 30, 2015 04:39
Show Gist options
  • Save joegoggins/7777824 to your computer and use it in GitHub Desktop.
Save joegoggins/7777824 to your computer and use it in GitHub Desktop.
This bash script is used to invoke API commands to a spark programmed with the "SPARK RC CAR KIT" sample code on this page: http://docs.spark.io/#/shields
# About
# =====
#
# Control a Spark Core remote control car with a bash script. Why not?
#
# Spark Core Firmware Assumptions
# -------------------------------
#
# * You have registered a function in your firmware that will allow you to hit it via the API,
# something like:
#
# `Spark.function("rccar", rcCarControl);`
#
# See the RC Car docs here for this example: http://docs.spark.io/#/shields for more details
#
# Usage
# -------
#
# * Download this file
# * Open up a bash terminal (note this doesn't work in zsh for some reason).
# * Change the SPARK_CORE_DEVICE_ID and SPARK_CORE_ACCESS_TOKEN variables
# * Type `source spark_core_rc_car_bash_script.sh`
# * Type `rc_while`
# * Hit characters and watch API commands go (and hopefully your car spin around)!
#
# f=forward, b=back, l=left, r=right, s=stop
#
#
# Change these!!!
export SPARK_CORE_DEVICE_ID=YOUR_DEVICE_ID_GOES_HERE
export SPARK_CORE_ACCESS_TOKEN=YOUR_ACCESS_TOKEN_GOES_HERE
# a loop that calls "rc" with a single character as input.
function rc_while() {
while read -n 1 _c; do
rc "$_c"
done
}
# A function that maps a single character to a curl request.
function rc() {
_base_command='curl https://api.spark.io/v1/devices/'"$SPARK_CORE_DEVICE_ID"'/rccar -d access_token='"$SPARK_CORE_ACCESS_TOKEN"' -d "params=rc,__CMD__"'
case "$1" in
f) echo "Forward march!"
_command=$(echo "${_base_command}" | sed 's/__CMD__/FORWARD/')
;;
b) echo "Come on back now"
_command=$(echo "${_base_command}" | sed 's/__CMD__/BACK/')
;;
l) echo "Hang a lu lu"
_command=$(echo "${_base_command}" | sed 's/__CMD__/LEFT/')
;;
r) echo "Hang a rubarb"
_command=$(echo "${_base_command}" | sed 's/__CMD__/RIGHT/')
;;
s) echo "Stop!"
_command=$(echo "${_base_command}" | sed 's/__CMD__/STOP/')
;;
*) echo "Don't know what to do with $1 : f=forward, b=back, l=left, r=right, s=stop"
;;
esac
echo $_command
echo $_command | bash
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment