Created
June 21, 2020 18:50
-
-
Save jahkeup/d0ce2ab5d45269ff1517f74c8adb9670 to your computer and use it in GitHub Desktop.
Helper script to watch a device and connect when it becomes available.
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
| #!/usr/bin/env bash | |
| if ! hash platformio logger; then | |
| echo "ERROR: missing required commands" >&2 | |
| exit 1 | |
| fi | |
| log() { | |
| local level="${1:?log level required}" | |
| shift 1 | |
| logger --no-act --stderr --skip-empty \ | |
| --socket-errors=off \ | |
| --tag "${level^^}" \ | |
| "${@}" | |
| } | |
| log_period_secs=15 | |
| wait_period_secs=1 | |
| wait_for() { | |
| local dev="${1:?device to wait for is required}" | |
| local last=0 | |
| until [[ -c "$dev" ]]; do | |
| now_sec="$(date +'%s')" | |
| if [[ "$((now_sec - last))" -ge "$log_period_secs" ]]; then | |
| last="$now_sec" | |
| log "info" "waiting for device at $dev" | |
| fi | |
| sleep "${wait_period_secs}s" | |
| done | |
| log "info" "detected device $dev" | |
| } | |
| connect() { | |
| local dev="${1:?device to connect to is required}" | |
| local -a extra_flags | |
| shift 1 | |
| extra_flags=("$@") | |
| log "info" "connecting to $dev" | |
| ( | |
| set -x; | |
| platformio device monitor --port "$dev" "${extra_flags[@]}" || : | |
| ) | |
| } | |
| connect_loop() { | |
| local dev="${1:?device to monitor is required}" | |
| local -a extra_flags | |
| shift 1 | |
| extra_flags=( "$@" ) | |
| while :; do | |
| wait_for "$dev" | |
| connect "$dev" "${extra_flags[@]}" | |
| log "info" "device dropped connection" | |
| done | |
| } | |
| TTY_DEV="${1:-/dev/ttyACM1}" | |
| PIO_EXTRA_FLAGS=() | |
| if [[ "$#" -gt 2 ]]; then | |
| shift 2 | |
| # Allow '--' as a separator between device and passthru args. | |
| if [[ "$1" = "--" ]]; then | |
| shift 1 | |
| fi | |
| PIO_EXTRA_FLAGS=( "$@" ) | |
| log "debug" <<EOF | |
| connect configured with additional args: | |
| $(printf -- '- %q\n' "${PIO_EXTRA_FLAGS[@]}") | |
| EOF | |
| fi | |
| log "info" "starting monitor loop for $TTY_DEV" | |
| log "debug" <<EOF | |
| Toolchain: $(platformio --version) | |
| Revision: $(git describe --dirty --always --long --abbrev=8 --all 2>/dev/null) | |
| EOF | |
| connect_loop "$TTY_DEV" "${PIO_EXTRA_FLAGS[@]}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment