From 0477b30e59b0e58f51489aa00c401b9d41c7c04e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Millet?= Date: Sun, 18 Feb 2024 14:02:18 +0100 Subject: [PATCH] Removes symlinks to make arduino-lint happy - arduino-lint complains about symlinks in the repo and this leads to github updates NOT being taken into account by the Arduino Library Manager. The symlinks were all about am2 and Makefile. Although result is not ideal, I've replaced symlinks with hardlinks. --- am2 | 10 +- examples/01_main/Makefile | 23 +- examples/01_main/am2 | 347 +++++++++++++++++- examples/02_output-received-code/Makefile | 23 +- examples/02_output-received-code/am2 | 347 +++++++++++++++++- examples/03_output-signal-timings/Makefile | 23 +- examples/03_output-signal-timings/am2 | 347 +++++++++++++++++- examples/04_react_on_code/Makefile | 23 +- examples/04_react_on_code/am2 | 347 +++++++++++++++++- examples/05_callback/Makefile | 23 +- examples/05_callback/am2 | 347 +++++++++++++++++- extras/testplan/simul/am2 | 347 +++++++++++++++++- .../testplan/simul/issue-15-tuya-blinds.txt | 49 +++ extras/testplan/simul/simul.ino | 11 +- extras/testplan/simul/simul.sh | 2 +- .../simul/spiriapbergeron-20231121.txt | 104 ++++++ extras/testplan/test/am2 | 347 +++++++++++++++++- extras/testplan/test/test.ino | 49 +++ extras/testplan/tt.sh | 2 +- 19 files changed, 2752 insertions(+), 19 deletions(-) mode change 120000 => 100644 examples/01_main/Makefile mode change 120000 => 100755 examples/01_main/am2 mode change 120000 => 100644 examples/02_output-received-code/Makefile mode change 120000 => 100755 examples/02_output-received-code/am2 mode change 120000 => 100644 examples/03_output-signal-timings/Makefile mode change 120000 => 100755 examples/03_output-signal-timings/am2 mode change 120000 => 100644 examples/04_react_on_code/Makefile mode change 120000 => 100755 examples/04_react_on_code/am2 mode change 120000 => 100644 examples/05_callback/Makefile mode change 120000 => 100755 examples/05_callback/am2 mode change 120000 => 100755 extras/testplan/simul/am2 create mode 100644 extras/testplan/simul/issue-15-tuya-blinds.txt create mode 100644 extras/testplan/simul/spiriapbergeron-20231121.txt mode change 120000 => 100755 extras/testplan/test/am2 diff --git a/am2 b/am2 index fbdff52..ef045ff 100755 --- a/am2 +++ b/am2 @@ -37,6 +37,7 @@ READSPEED= RECORDFILE= UPLOAD="no" +REMOVE="no" VERBOSE="no" CATUSB="no" STTY="no" @@ -71,6 +72,7 @@ function usage { echo " -V --version Output version information and quit" echo " -v --verbose Be more talkative" echo " -u --upload Upload compiled code into Arduino" + echo " -R --remove Remove /tmp/arduino-core-cache and ./buid" echo " -b --board Board, either 'uno' or 'nano'" echo " -p --port Port, for ex. '/dev/ttyUSB0'" echo " -s --speed Upload speed, for ex. 115200" @@ -100,7 +102,7 @@ function version { exit } -OPTS=$(getopt -o hVvub:p:s:B:d:crnt: --long help,version,verbose,upload,board:,port:,speed:,fqbn:,builddir:,catusb,stty,no-color,recordusb,nocompile,readspeed:,recordfile:,testplan: -n 'am' -- "$@") +OPTS=$(getopt -o hVvuRb:p:s:B:d:crnt: --long help,version,verbose,upload,remove,board:,port:,speed:,fqbn:,builddir:,catusb,stty,no-color,recordusb,nocompile,readspeed:,recordfile:,testplan: -n 'am' -- "$@") eval set -- "$OPTS" @@ -110,6 +112,7 @@ while true; do -V | --version ) version; shift ;; -v | --verbose ) VERBOSE="yes"; shift ;; -u | --upload ) UPLOAD="yes"; shift ;; + -R | --remove ) REMOVE="yes"; shift ;; -b | --board ) BOARD="$2"; shift 2 ;; -p | --port ) PORT="$2"; shift 2 ;; -s | --speed ) SPEED="$2"; shift 2 ;; @@ -258,6 +261,7 @@ if [ "${VERBOSE}" == "yes" ]; then echo "Speed: ${SPEED}" echo "Fqbn: ${FQBN}" echo "Upload: ${UPLOAD}" + echo "Remove: ${REMOVE}" echo "Catusb: ${CATUSB}" echo "Recordusb: ${RECORDUSB}" echo "Record file: ${RECORDFILE}" @@ -268,6 +272,10 @@ fi set -e +if [ "${REMOVE}" == "yes" ]; then + rm -rf /tmp/arduino-core-cache +fi + if [ "${COMPILE}" == "yes" ]; then echo "-- Compile" diff --git a/examples/01_main/Makefile b/examples/01_main/Makefile deleted file mode 120000 index d0b0e8e..0000000 --- a/examples/01_main/Makefile +++ /dev/null @@ -1 +0,0 @@ -../Makefile \ No newline at end of file diff --git a/examples/01_main/Makefile b/examples/01_main/Makefile new file mode 100644 index 0000000..6cf19c3 --- /dev/null +++ b/examples/01_main/Makefile @@ -0,0 +1,22 @@ +# This Makefile is to be symbolic-linked inside subfolders. +# It is generic enough that, to the asumption the subfolder contains one .ino +# file and only one, it'll work as expected. + +source = $(wildcard *.ino) +target = build/$(source).with_bootloader.hex +opt = + +ALL: $(target) + +$(target): $(source) +ifdef color + ./am2 $(opt) $< +else + GCC_COLORS="" ./am2 --no-color $(opt) $< +endif + +clean: + rm -rf build + +mrproper: + rm -rf build out diff --git a/examples/01_main/am2 b/examples/01_main/am2 deleted file mode 120000 index 270bb52..0000000 --- a/examples/01_main/am2 +++ /dev/null @@ -1 +0,0 @@ -../../am2 \ No newline at end of file diff --git a/examples/01_main/am2 b/examples/01_main/am2 new file mode 100755 index 0000000..ef045ff --- /dev/null +++ b/examples/01_main/am2 @@ -0,0 +1,346 @@ +#!/usr/bin/bash + +# am2 + +# Copyright 2019, 2020, 2021, 2022 Sébastien Millet + +# Can perform the following: +# 1. Compile the code +# 2. Upload to Arduino +# 3. Read (continually) what is arriving from the USB port the Arduino is +# connected to + +# Versions history (as of 1.3) +# 1.3 Output from Arduino is recorded in files named with numbers instead of +# date-time string. +# 1.4 Adds -t (--testplan) option, to set TESTPLAN macro +# 1.5 -t (or --testplan) now comes with a value, so as to manage multiple test +# plans. +# 1.6 Updated to work fine with Arch arduino package instead of the manually +# installed (from tar.gz source) package used so far. +# 1.7 Renames archlinux-arduino back to arduino, and created corresponding +# symlink (was cleaner to do s). +# 2.0 Replaces arduino-builder with arduino-cli +# 2.1 Output warning if ARDUINO_USER_LIBS environment variable is not set + +set -euo pipefail + +VERSION=2.1 + +PORT= +BOARD= +SPEED= +FQBN= +BUILDDIR= +RECORDDIR=out +READSPEED= +RECORDFILE= + +UPLOAD="no" +REMOVE="no" +VERBOSE="no" +CATUSB="no" +STTY="no" +RECORDUSB="no" +COMPILE="yes" +TESTPLAN= +NOCOLOR= + +DISPLAYSEP=no + +function finish { + if [ "${DISPLAYSEP}" == "yes" ]; then + echo "-----END ARDUINO OUTPUT-----" | tee -a "${RECORDFILE}" + fi +} + +trap finish EXIT + +function usage { + echo "Usage:" + echo " am [OPTIONS...] FILE" + echo "Compile FILE using arduino-builder." + echo "Example: am sketch.ino" + echo "" + echo "ENVIRONMENT VARIABLES" + echo " If ARDUINO_USER_LIBS is defined and non empty, then arduino-builder" + echo " is called with the supplementary option -libraries followed by" + echo " ARDUINO_USER_LIBS' value." + echo "" + echo "OPTIONS" + echo " -h --help Display this help screen" + echo " -V --version Output version information and quit" + echo " -v --verbose Be more talkative" + echo " -u --upload Upload compiled code into Arduino" + echo " -R --remove Remove /tmp/arduino-core-cache and ./buid" + echo " -b --board Board, either 'uno' or 'nano'" + echo " -p --port Port, for ex. '/dev/ttyUSB0'" + echo " -s --speed Upload speed, for ex. 115200" + echo " Normally, speed is infered from device type:" + echo " 115200 for Uno, 57600 for Nano" + echo " -B --fqbn Board Fully Qualified Name, like 'arduino:avr:uno'" + echo " -d --builddir Build directory" + echo " -c --catusb Display (continually) what Arduino writes on USB" + echo " --stty Tune stty properly for later communication (implied" + echo " by --catusb)" + echo " -r --recordusb Write USB (continually) to a file (implies -c)" + echo " --recordfile Output file if -r option is set" + echo " -n --nocompile Don't compile code" + echo " --readspeed Read speed of USB. If not specified, this script" + echo " will try to infere it from source file. If it" + echo " fails, it'll fallback to 9600." + echo " This option is useful only if USB is read" + echo " (-c or --stty option set)" + echo " -t --testplan Set TESTPLAN macro value" + echo " (as if #define TESTPLAN VALUE)" + echo " --no-color Pass on the --no-color option to arduino-cli" + exit 1 +} + +function version { + echo "am version ${VERSION}" + exit +} + +OPTS=$(getopt -o hVvuRb:p:s:B:d:crnt: --long help,version,verbose,upload,remove,board:,port:,speed:,fqbn:,builddir:,catusb,stty,no-color,recordusb,nocompile,readspeed:,recordfile:,testplan: -n 'am' -- "$@") + +eval set -- "$OPTS" + +while true; do + case "$1" in + -h | --help ) usage; shift ;; + -V | --version ) version; shift ;; + -v | --verbose ) VERBOSE="yes"; shift ;; + -u | --upload ) UPLOAD="yes"; shift ;; + -R | --remove ) REMOVE="yes"; shift ;; + -b | --board ) BOARD="$2"; shift 2 ;; + -p | --port ) PORT="$2"; shift 2 ;; + -s | --speed ) SPEED="$2"; shift 2 ;; + -B | --fqbn ) FQBN="$2"; shift 2 ;; + -d | --builddir ) BUILDDIR="$2"; shift 2 ;; + -c | --catusb ) CATUSB="yes"; shift ;; + -r | --recordusb ) RECORDUSB="yes"; CATUSB="yes"; shift ;; + -n | --nocompile ) COMPILE="no"; shift ;; + --readspeed ) READSPEED="$2"; shift 2 ;; + --recordfile ) RECORDFILE="$2"; shift 2 ;; + --stty ) STTY="yes"; shift ;; + --no-color ) NOCOLOR="--no-color"; shift ;; + -t | --testplan ) TESTPLAN="$2"; shift 2 ;; + -- ) shift; break ;; + * ) break ;; + esac +done + +FILE=${1:-} +TRAILINGOPTS=${2:-} + +if [ -n "${TRAILINGOPTS}" ]; then + echo "Error: trailing options" + exit 1; +fi +if [ -z "${FILE}" ]; then + echo "Error: no input file" + exit 1; +fi + +set +e + +if [ -n "${BOARD}" ]; then + if [ "${BOARD}" != "uno" ] && [ "${BOARD}" != "nano" ]; then + echo "Error: board '${BOARD}' unknown" + exit 1 + fi +fi + +ARDUINODIR=/usr/share/arduino + +COUNTUNO=$(compgen -G '/dev/ttyACM*' | wc -l) +COUNTNANO=$(compgen -G '/dev/ttyUSB*' | wc -l) + +if [ -z "${BOARD}" ]; then + if [ "${COUNTUNO}" -ge 1 ] && [ "${COUNTNANO}" -ge 1 ]; then + echo "Error: cannot guess board, found ${COUNTUNO} uno(s), ${COUNTNANO} nano(s)" + exit 10 + fi + if [ "${COUNTUNO}" -ge 1 ]; then + BOARD=uno + elif [ "${COUNTNANO}" -ge 1 ]; then + BOARD=nano + fi + if [ -z "${BOARD}" ]; then + echo "Error: cannot guess board, none found"; + exit 10 + fi +fi + +if [ "${UPLOAD}" == "yes" ] || [ "${CATUSB}" == "yes" ]; then + if [ -z "${PORT}" ]; then + if [ "${BOARD}" == "uno" ]; then + COUNT=${COUNTUNO} + PORT=$(compgen -G '/dev/ttyACM*') + elif [ "${BOARD}" == "nano" ]; then + COUNT=${COUNTNANO} + PORT=$(compgen -G '/dev/ttyUSB*') + else + echo "FATAL #001, CHECK THIS CODE" + exit 99 + fi + + if [ "${COUNT}" -ge 2 ]; then + echo "Error: cannot guess port, more than 1 board '${BOARD}' found" + exit 10 + fi + if [ -z "${PORT}" ]; then + echo "Error: cannot guess port, none found" + exit 10 + fi + fi + + if [ -z "${SPEED}" ]; then + if [ "${BOARD}" == "uno" ]; then + SPEED=115200 + elif [ "${BOARD}" == "nano" ]; then + SPEED=57600 + else + echo "FATAL #002, CHECK THIS CODE" + exit 99 + fi + fi + + if [ ! -e "${PORT}" ]; then + echo "Error: port not found" + exit 10 + fi +fi + +if [ -z "${FQBN}" ]; then + if [ "${BOARD}" == "uno" ]; then + FQBN="arduino:avr:uno" + elif [ "${BOARD}" == "nano" ]; then + FQBN="arduino:avr:nano:cpu=atmega328old" + else + echo "FATAL #003, CHECK THIS CODE" + exit 99 + fi +fi + +if [ -z "${BUILDDIR}" ]; then + if [[ "${FILE}" == */* ]]; then + BUILDDIR=${FILE%/*} + BUILDDIR="${BUILDDIR%/}/build" + else + BUILDDIR=build + fi +fi + +if [ "${RECORDUSB}" == "yes" ]; then + if [ -z "${RECORDFILE}" ]; then + V=${FILE##*/} + V=${V%.*} + V=${V:-out} + PREV= + for i in {15..00}; do + F="${RECORDDIR}/${V}-$i.txt" + if [ -e "${F}" ] && [ -n "${PREV}" ]; then + mv "${F}" "${PREV}" + fi + PREV="${F}" + done + RECORDFILE="${F}" + mkdir -p "${RECORDDIR}" + fi +else + RECORDFILE="/dev/null" +fi + +if [ "${VERBOSE}" == "yes" ]; then + echo "-- Settings" + echo "Arduino dir: ${ARDUINODIR}" + echo "Board: ${BOARD}" + echo "Port: ${PORT}" + echo "Speed: ${SPEED}" + echo "Fqbn: ${FQBN}" + echo "Upload: ${UPLOAD}" + echo "Remove: ${REMOVE}" + echo "Catusb: ${CATUSB}" + echo "Recordusb: ${RECORDUSB}" + echo "Record file: ${RECORDFILE}" + echo "Verbose: ${VERBOSE}" + echo "File: ${FILE}" + echo "Build dir: ${BUILDDIR}" +fi + +set -e + +if [ "${REMOVE}" == "yes" ]; then + rm -rf /tmp/arduino-core-cache +fi + +if [ "${COMPILE}" == "yes" ]; then + echo "-- Compile" + + mkdir -p "${BUILDDIR}" + + OPT_LIB= + TMP_ULIB=${ARDUINO_USER_LIBS:-} + if [ -n "${TMP_ULIB}" ]; then + OPT_LIB="--libraries ""${TMP_ULIB}""" + else + echo + echo "***********************************************************" + echo "* WARNING *" + echo "* The environment variable ARDUINO_USER_LIBS is not set *" + echo "***********************************************************" + echo + fi + + TESTPLAN_OPT="" + if [ -n "${TESTPLAN}" ]; then + TESTPLAN_OPT="--build-property build.extra_flags=-DRF433ANY_TESTPLAN=${TESTPLAN}" + fi + + # shellcheck disable=SC2086 + # (We don't want to quote OPT_LIB as it can contain multiple options.) + arduino-cli compile -b "${FQBN}" ${NOCOLOR} --build-path "${BUILDDIR}" ${OPT_LIB} ${TESTPLAN_OPT} "${FILE}" +fi + +FILEBASENAME=${FILE##*/} + +if [ "${UPLOAD}" == "yes" ]; then + echo "-- Upload" + arduino-cli upload -v -b "${FQBN}" --input-dir "${BUILDDIR}" -p "${PORT}" +fi + +if [ "${CATUSB}" == "yes" ] || [ "${STTY}" == "yes" ]; then + if [ -z "${READSPEED}" ]; then + TFILE=$(mktemp) + gcc -fpreprocessed -dD -x c++ -E "${FILE}" > "${TFILE}" + for sp in 9600 19200 28800 38400 57600 115200; do + if grep ${sp} "${TFILE}" > /dev/null; then + READSPEED=${sp} + fi + done + READSPEED=${READSPEED:-9600} + rm "${TFILE}" + fi + + stty -F "${PORT}" -hupcl -echo "${READSPEED}" + echo "-- usb setup with speed ${READSPEED}" +fi + +if [ "${CATUSB}" == "yes" ]; then + echo "-- Read usb (Ctrl-C to quit)" + DISPLAYSEP=yes + { + echo "speed=${READSPEED}" + echo "fqbn=${FQBN}" + echo "port=${PORT}" + echo "file=${FILE}" + echo "filedate=$(date +"%Y-%m-%dT%H:%M:%SZ" -d @$(stat -c '%Y' "${FILE}"))" + echo "date=$(date +'%Y-%m-%dT%H:%M:%SZ')" + echo "" + echo "-----BEGIN ARDUINO OUTPUT-----" + } | tee "${RECORDFILE}" + tee -a "${RECORDFILE}" < "${PORT}" +fi + diff --git a/examples/02_output-received-code/Makefile b/examples/02_output-received-code/Makefile deleted file mode 120000 index d0b0e8e..0000000 --- a/examples/02_output-received-code/Makefile +++ /dev/null @@ -1 +0,0 @@ -../Makefile \ No newline at end of file diff --git a/examples/02_output-received-code/Makefile b/examples/02_output-received-code/Makefile new file mode 100644 index 0000000..6cf19c3 --- /dev/null +++ b/examples/02_output-received-code/Makefile @@ -0,0 +1,22 @@ +# This Makefile is to be symbolic-linked inside subfolders. +# It is generic enough that, to the asumption the subfolder contains one .ino +# file and only one, it'll work as expected. + +source = $(wildcard *.ino) +target = build/$(source).with_bootloader.hex +opt = + +ALL: $(target) + +$(target): $(source) +ifdef color + ./am2 $(opt) $< +else + GCC_COLORS="" ./am2 --no-color $(opt) $< +endif + +clean: + rm -rf build + +mrproper: + rm -rf build out diff --git a/examples/02_output-received-code/am2 b/examples/02_output-received-code/am2 deleted file mode 120000 index 270bb52..0000000 --- a/examples/02_output-received-code/am2 +++ /dev/null @@ -1 +0,0 @@ -../../am2 \ No newline at end of file diff --git a/examples/02_output-received-code/am2 b/examples/02_output-received-code/am2 new file mode 100755 index 0000000..ef045ff --- /dev/null +++ b/examples/02_output-received-code/am2 @@ -0,0 +1,346 @@ +#!/usr/bin/bash + +# am2 + +# Copyright 2019, 2020, 2021, 2022 Sébastien Millet + +# Can perform the following: +# 1. Compile the code +# 2. Upload to Arduino +# 3. Read (continually) what is arriving from the USB port the Arduino is +# connected to + +# Versions history (as of 1.3) +# 1.3 Output from Arduino is recorded in files named with numbers instead of +# date-time string. +# 1.4 Adds -t (--testplan) option, to set TESTPLAN macro +# 1.5 -t (or --testplan) now comes with a value, so as to manage multiple test +# plans. +# 1.6 Updated to work fine with Arch arduino package instead of the manually +# installed (from tar.gz source) package used so far. +# 1.7 Renames archlinux-arduino back to arduino, and created corresponding +# symlink (was cleaner to do s). +# 2.0 Replaces arduino-builder with arduino-cli +# 2.1 Output warning if ARDUINO_USER_LIBS environment variable is not set + +set -euo pipefail + +VERSION=2.1 + +PORT= +BOARD= +SPEED= +FQBN= +BUILDDIR= +RECORDDIR=out +READSPEED= +RECORDFILE= + +UPLOAD="no" +REMOVE="no" +VERBOSE="no" +CATUSB="no" +STTY="no" +RECORDUSB="no" +COMPILE="yes" +TESTPLAN= +NOCOLOR= + +DISPLAYSEP=no + +function finish { + if [ "${DISPLAYSEP}" == "yes" ]; then + echo "-----END ARDUINO OUTPUT-----" | tee -a "${RECORDFILE}" + fi +} + +trap finish EXIT + +function usage { + echo "Usage:" + echo " am [OPTIONS...] FILE" + echo "Compile FILE using arduino-builder." + echo "Example: am sketch.ino" + echo "" + echo "ENVIRONMENT VARIABLES" + echo " If ARDUINO_USER_LIBS is defined and non empty, then arduino-builder" + echo " is called with the supplementary option -libraries followed by" + echo " ARDUINO_USER_LIBS' value." + echo "" + echo "OPTIONS" + echo " -h --help Display this help screen" + echo " -V --version Output version information and quit" + echo " -v --verbose Be more talkative" + echo " -u --upload Upload compiled code into Arduino" + echo " -R --remove Remove /tmp/arduino-core-cache and ./buid" + echo " -b --board Board, either 'uno' or 'nano'" + echo " -p --port Port, for ex. '/dev/ttyUSB0'" + echo " -s --speed Upload speed, for ex. 115200" + echo " Normally, speed is infered from device type:" + echo " 115200 for Uno, 57600 for Nano" + echo " -B --fqbn Board Fully Qualified Name, like 'arduino:avr:uno'" + echo " -d --builddir Build directory" + echo " -c --catusb Display (continually) what Arduino writes on USB" + echo " --stty Tune stty properly for later communication (implied" + echo " by --catusb)" + echo " -r --recordusb Write USB (continually) to a file (implies -c)" + echo " --recordfile Output file if -r option is set" + echo " -n --nocompile Don't compile code" + echo " --readspeed Read speed of USB. If not specified, this script" + echo " will try to infere it from source file. If it" + echo " fails, it'll fallback to 9600." + echo " This option is useful only if USB is read" + echo " (-c or --stty option set)" + echo " -t --testplan Set TESTPLAN macro value" + echo " (as if #define TESTPLAN VALUE)" + echo " --no-color Pass on the --no-color option to arduino-cli" + exit 1 +} + +function version { + echo "am version ${VERSION}" + exit +} + +OPTS=$(getopt -o hVvuRb:p:s:B:d:crnt: --long help,version,verbose,upload,remove,board:,port:,speed:,fqbn:,builddir:,catusb,stty,no-color,recordusb,nocompile,readspeed:,recordfile:,testplan: -n 'am' -- "$@") + +eval set -- "$OPTS" + +while true; do + case "$1" in + -h | --help ) usage; shift ;; + -V | --version ) version; shift ;; + -v | --verbose ) VERBOSE="yes"; shift ;; + -u | --upload ) UPLOAD="yes"; shift ;; + -R | --remove ) REMOVE="yes"; shift ;; + -b | --board ) BOARD="$2"; shift 2 ;; + -p | --port ) PORT="$2"; shift 2 ;; + -s | --speed ) SPEED="$2"; shift 2 ;; + -B | --fqbn ) FQBN="$2"; shift 2 ;; + -d | --builddir ) BUILDDIR="$2"; shift 2 ;; + -c | --catusb ) CATUSB="yes"; shift ;; + -r | --recordusb ) RECORDUSB="yes"; CATUSB="yes"; shift ;; + -n | --nocompile ) COMPILE="no"; shift ;; + --readspeed ) READSPEED="$2"; shift 2 ;; + --recordfile ) RECORDFILE="$2"; shift 2 ;; + --stty ) STTY="yes"; shift ;; + --no-color ) NOCOLOR="--no-color"; shift ;; + -t | --testplan ) TESTPLAN="$2"; shift 2 ;; + -- ) shift; break ;; + * ) break ;; + esac +done + +FILE=${1:-} +TRAILINGOPTS=${2:-} + +if [ -n "${TRAILINGOPTS}" ]; then + echo "Error: trailing options" + exit 1; +fi +if [ -z "${FILE}" ]; then + echo "Error: no input file" + exit 1; +fi + +set +e + +if [ -n "${BOARD}" ]; then + if [ "${BOARD}" != "uno" ] && [ "${BOARD}" != "nano" ]; then + echo "Error: board '${BOARD}' unknown" + exit 1 + fi +fi + +ARDUINODIR=/usr/share/arduino + +COUNTUNO=$(compgen -G '/dev/ttyACM*' | wc -l) +COUNTNANO=$(compgen -G '/dev/ttyUSB*' | wc -l) + +if [ -z "${BOARD}" ]; then + if [ "${COUNTUNO}" -ge 1 ] && [ "${COUNTNANO}" -ge 1 ]; then + echo "Error: cannot guess board, found ${COUNTUNO} uno(s), ${COUNTNANO} nano(s)" + exit 10 + fi + if [ "${COUNTUNO}" -ge 1 ]; then + BOARD=uno + elif [ "${COUNTNANO}" -ge 1 ]; then + BOARD=nano + fi + if [ -z "${BOARD}" ]; then + echo "Error: cannot guess board, none found"; + exit 10 + fi +fi + +if [ "${UPLOAD}" == "yes" ] || [ "${CATUSB}" == "yes" ]; then + if [ -z "${PORT}" ]; then + if [ "${BOARD}" == "uno" ]; then + COUNT=${COUNTUNO} + PORT=$(compgen -G '/dev/ttyACM*') + elif [ "${BOARD}" == "nano" ]; then + COUNT=${COUNTNANO} + PORT=$(compgen -G '/dev/ttyUSB*') + else + echo "FATAL #001, CHECK THIS CODE" + exit 99 + fi + + if [ "${COUNT}" -ge 2 ]; then + echo "Error: cannot guess port, more than 1 board '${BOARD}' found" + exit 10 + fi + if [ -z "${PORT}" ]; then + echo "Error: cannot guess port, none found" + exit 10 + fi + fi + + if [ -z "${SPEED}" ]; then + if [ "${BOARD}" == "uno" ]; then + SPEED=115200 + elif [ "${BOARD}" == "nano" ]; then + SPEED=57600 + else + echo "FATAL #002, CHECK THIS CODE" + exit 99 + fi + fi + + if [ ! -e "${PORT}" ]; then + echo "Error: port not found" + exit 10 + fi +fi + +if [ -z "${FQBN}" ]; then + if [ "${BOARD}" == "uno" ]; then + FQBN="arduino:avr:uno" + elif [ "${BOARD}" == "nano" ]; then + FQBN="arduino:avr:nano:cpu=atmega328old" + else + echo "FATAL #003, CHECK THIS CODE" + exit 99 + fi +fi + +if [ -z "${BUILDDIR}" ]; then + if [[ "${FILE}" == */* ]]; then + BUILDDIR=${FILE%/*} + BUILDDIR="${BUILDDIR%/}/build" + else + BUILDDIR=build + fi +fi + +if [ "${RECORDUSB}" == "yes" ]; then + if [ -z "${RECORDFILE}" ]; then + V=${FILE##*/} + V=${V%.*} + V=${V:-out} + PREV= + for i in {15..00}; do + F="${RECORDDIR}/${V}-$i.txt" + if [ -e "${F}" ] && [ -n "${PREV}" ]; then + mv "${F}" "${PREV}" + fi + PREV="${F}" + done + RECORDFILE="${F}" + mkdir -p "${RECORDDIR}" + fi +else + RECORDFILE="/dev/null" +fi + +if [ "${VERBOSE}" == "yes" ]; then + echo "-- Settings" + echo "Arduino dir: ${ARDUINODIR}" + echo "Board: ${BOARD}" + echo "Port: ${PORT}" + echo "Speed: ${SPEED}" + echo "Fqbn: ${FQBN}" + echo "Upload: ${UPLOAD}" + echo "Remove: ${REMOVE}" + echo "Catusb: ${CATUSB}" + echo "Recordusb: ${RECORDUSB}" + echo "Record file: ${RECORDFILE}" + echo "Verbose: ${VERBOSE}" + echo "File: ${FILE}" + echo "Build dir: ${BUILDDIR}" +fi + +set -e + +if [ "${REMOVE}" == "yes" ]; then + rm -rf /tmp/arduino-core-cache +fi + +if [ "${COMPILE}" == "yes" ]; then + echo "-- Compile" + + mkdir -p "${BUILDDIR}" + + OPT_LIB= + TMP_ULIB=${ARDUINO_USER_LIBS:-} + if [ -n "${TMP_ULIB}" ]; then + OPT_LIB="--libraries ""${TMP_ULIB}""" + else + echo + echo "***********************************************************" + echo "* WARNING *" + echo "* The environment variable ARDUINO_USER_LIBS is not set *" + echo "***********************************************************" + echo + fi + + TESTPLAN_OPT="" + if [ -n "${TESTPLAN}" ]; then + TESTPLAN_OPT="--build-property build.extra_flags=-DRF433ANY_TESTPLAN=${TESTPLAN}" + fi + + # shellcheck disable=SC2086 + # (We don't want to quote OPT_LIB as it can contain multiple options.) + arduino-cli compile -b "${FQBN}" ${NOCOLOR} --build-path "${BUILDDIR}" ${OPT_LIB} ${TESTPLAN_OPT} "${FILE}" +fi + +FILEBASENAME=${FILE##*/} + +if [ "${UPLOAD}" == "yes" ]; then + echo "-- Upload" + arduino-cli upload -v -b "${FQBN}" --input-dir "${BUILDDIR}" -p "${PORT}" +fi + +if [ "${CATUSB}" == "yes" ] || [ "${STTY}" == "yes" ]; then + if [ -z "${READSPEED}" ]; then + TFILE=$(mktemp) + gcc -fpreprocessed -dD -x c++ -E "${FILE}" > "${TFILE}" + for sp in 9600 19200 28800 38400 57600 115200; do + if grep ${sp} "${TFILE}" > /dev/null; then + READSPEED=${sp} + fi + done + READSPEED=${READSPEED:-9600} + rm "${TFILE}" + fi + + stty -F "${PORT}" -hupcl -echo "${READSPEED}" + echo "-- usb setup with speed ${READSPEED}" +fi + +if [ "${CATUSB}" == "yes" ]; then + echo "-- Read usb (Ctrl-C to quit)" + DISPLAYSEP=yes + { + echo "speed=${READSPEED}" + echo "fqbn=${FQBN}" + echo "port=${PORT}" + echo "file=${FILE}" + echo "filedate=$(date +"%Y-%m-%dT%H:%M:%SZ" -d @$(stat -c '%Y' "${FILE}"))" + echo "date=$(date +'%Y-%m-%dT%H:%M:%SZ')" + echo "" + echo "-----BEGIN ARDUINO OUTPUT-----" + } | tee "${RECORDFILE}" + tee -a "${RECORDFILE}" < "${PORT}" +fi + diff --git a/examples/03_output-signal-timings/Makefile b/examples/03_output-signal-timings/Makefile deleted file mode 120000 index d0b0e8e..0000000 --- a/examples/03_output-signal-timings/Makefile +++ /dev/null @@ -1 +0,0 @@ -../Makefile \ No newline at end of file diff --git a/examples/03_output-signal-timings/Makefile b/examples/03_output-signal-timings/Makefile new file mode 100644 index 0000000..6cf19c3 --- /dev/null +++ b/examples/03_output-signal-timings/Makefile @@ -0,0 +1,22 @@ +# This Makefile is to be symbolic-linked inside subfolders. +# It is generic enough that, to the asumption the subfolder contains one .ino +# file and only one, it'll work as expected. + +source = $(wildcard *.ino) +target = build/$(source).with_bootloader.hex +opt = + +ALL: $(target) + +$(target): $(source) +ifdef color + ./am2 $(opt) $< +else + GCC_COLORS="" ./am2 --no-color $(opt) $< +endif + +clean: + rm -rf build + +mrproper: + rm -rf build out diff --git a/examples/03_output-signal-timings/am2 b/examples/03_output-signal-timings/am2 deleted file mode 120000 index 270bb52..0000000 --- a/examples/03_output-signal-timings/am2 +++ /dev/null @@ -1 +0,0 @@ -../../am2 \ No newline at end of file diff --git a/examples/03_output-signal-timings/am2 b/examples/03_output-signal-timings/am2 new file mode 100755 index 0000000..ef045ff --- /dev/null +++ b/examples/03_output-signal-timings/am2 @@ -0,0 +1,346 @@ +#!/usr/bin/bash + +# am2 + +# Copyright 2019, 2020, 2021, 2022 Sébastien Millet + +# Can perform the following: +# 1. Compile the code +# 2. Upload to Arduino +# 3. Read (continually) what is arriving from the USB port the Arduino is +# connected to + +# Versions history (as of 1.3) +# 1.3 Output from Arduino is recorded in files named with numbers instead of +# date-time string. +# 1.4 Adds -t (--testplan) option, to set TESTPLAN macro +# 1.5 -t (or --testplan) now comes with a value, so as to manage multiple test +# plans. +# 1.6 Updated to work fine with Arch arduino package instead of the manually +# installed (from tar.gz source) package used so far. +# 1.7 Renames archlinux-arduino back to arduino, and created corresponding +# symlink (was cleaner to do s). +# 2.0 Replaces arduino-builder with arduino-cli +# 2.1 Output warning if ARDUINO_USER_LIBS environment variable is not set + +set -euo pipefail + +VERSION=2.1 + +PORT= +BOARD= +SPEED= +FQBN= +BUILDDIR= +RECORDDIR=out +READSPEED= +RECORDFILE= + +UPLOAD="no" +REMOVE="no" +VERBOSE="no" +CATUSB="no" +STTY="no" +RECORDUSB="no" +COMPILE="yes" +TESTPLAN= +NOCOLOR= + +DISPLAYSEP=no + +function finish { + if [ "${DISPLAYSEP}" == "yes" ]; then + echo "-----END ARDUINO OUTPUT-----" | tee -a "${RECORDFILE}" + fi +} + +trap finish EXIT + +function usage { + echo "Usage:" + echo " am [OPTIONS...] FILE" + echo "Compile FILE using arduino-builder." + echo "Example: am sketch.ino" + echo "" + echo "ENVIRONMENT VARIABLES" + echo " If ARDUINO_USER_LIBS is defined and non empty, then arduino-builder" + echo " is called with the supplementary option -libraries followed by" + echo " ARDUINO_USER_LIBS' value." + echo "" + echo "OPTIONS" + echo " -h --help Display this help screen" + echo " -V --version Output version information and quit" + echo " -v --verbose Be more talkative" + echo " -u --upload Upload compiled code into Arduino" + echo " -R --remove Remove /tmp/arduino-core-cache and ./buid" + echo " -b --board Board, either 'uno' or 'nano'" + echo " -p --port Port, for ex. '/dev/ttyUSB0'" + echo " -s --speed Upload speed, for ex. 115200" + echo " Normally, speed is infered from device type:" + echo " 115200 for Uno, 57600 for Nano" + echo " -B --fqbn Board Fully Qualified Name, like 'arduino:avr:uno'" + echo " -d --builddir Build directory" + echo " -c --catusb Display (continually) what Arduino writes on USB" + echo " --stty Tune stty properly for later communication (implied" + echo " by --catusb)" + echo " -r --recordusb Write USB (continually) to a file (implies -c)" + echo " --recordfile Output file if -r option is set" + echo " -n --nocompile Don't compile code" + echo " --readspeed Read speed of USB. If not specified, this script" + echo " will try to infere it from source file. If it" + echo " fails, it'll fallback to 9600." + echo " This option is useful only if USB is read" + echo " (-c or --stty option set)" + echo " -t --testplan Set TESTPLAN macro value" + echo " (as if #define TESTPLAN VALUE)" + echo " --no-color Pass on the --no-color option to arduino-cli" + exit 1 +} + +function version { + echo "am version ${VERSION}" + exit +} + +OPTS=$(getopt -o hVvuRb:p:s:B:d:crnt: --long help,version,verbose,upload,remove,board:,port:,speed:,fqbn:,builddir:,catusb,stty,no-color,recordusb,nocompile,readspeed:,recordfile:,testplan: -n 'am' -- "$@") + +eval set -- "$OPTS" + +while true; do + case "$1" in + -h | --help ) usage; shift ;; + -V | --version ) version; shift ;; + -v | --verbose ) VERBOSE="yes"; shift ;; + -u | --upload ) UPLOAD="yes"; shift ;; + -R | --remove ) REMOVE="yes"; shift ;; + -b | --board ) BOARD="$2"; shift 2 ;; + -p | --port ) PORT="$2"; shift 2 ;; + -s | --speed ) SPEED="$2"; shift 2 ;; + -B | --fqbn ) FQBN="$2"; shift 2 ;; + -d | --builddir ) BUILDDIR="$2"; shift 2 ;; + -c | --catusb ) CATUSB="yes"; shift ;; + -r | --recordusb ) RECORDUSB="yes"; CATUSB="yes"; shift ;; + -n | --nocompile ) COMPILE="no"; shift ;; + --readspeed ) READSPEED="$2"; shift 2 ;; + --recordfile ) RECORDFILE="$2"; shift 2 ;; + --stty ) STTY="yes"; shift ;; + --no-color ) NOCOLOR="--no-color"; shift ;; + -t | --testplan ) TESTPLAN="$2"; shift 2 ;; + -- ) shift; break ;; + * ) break ;; + esac +done + +FILE=${1:-} +TRAILINGOPTS=${2:-} + +if [ -n "${TRAILINGOPTS}" ]; then + echo "Error: trailing options" + exit 1; +fi +if [ -z "${FILE}" ]; then + echo "Error: no input file" + exit 1; +fi + +set +e + +if [ -n "${BOARD}" ]; then + if [ "${BOARD}" != "uno" ] && [ "${BOARD}" != "nano" ]; then + echo "Error: board '${BOARD}' unknown" + exit 1 + fi +fi + +ARDUINODIR=/usr/share/arduino + +COUNTUNO=$(compgen -G '/dev/ttyACM*' | wc -l) +COUNTNANO=$(compgen -G '/dev/ttyUSB*' | wc -l) + +if [ -z "${BOARD}" ]; then + if [ "${COUNTUNO}" -ge 1 ] && [ "${COUNTNANO}" -ge 1 ]; then + echo "Error: cannot guess board, found ${COUNTUNO} uno(s), ${COUNTNANO} nano(s)" + exit 10 + fi + if [ "${COUNTUNO}" -ge 1 ]; then + BOARD=uno + elif [ "${COUNTNANO}" -ge 1 ]; then + BOARD=nano + fi + if [ -z "${BOARD}" ]; then + echo "Error: cannot guess board, none found"; + exit 10 + fi +fi + +if [ "${UPLOAD}" == "yes" ] || [ "${CATUSB}" == "yes" ]; then + if [ -z "${PORT}" ]; then + if [ "${BOARD}" == "uno" ]; then + COUNT=${COUNTUNO} + PORT=$(compgen -G '/dev/ttyACM*') + elif [ "${BOARD}" == "nano" ]; then + COUNT=${COUNTNANO} + PORT=$(compgen -G '/dev/ttyUSB*') + else + echo "FATAL #001, CHECK THIS CODE" + exit 99 + fi + + if [ "${COUNT}" -ge 2 ]; then + echo "Error: cannot guess port, more than 1 board '${BOARD}' found" + exit 10 + fi + if [ -z "${PORT}" ]; then + echo "Error: cannot guess port, none found" + exit 10 + fi + fi + + if [ -z "${SPEED}" ]; then + if [ "${BOARD}" == "uno" ]; then + SPEED=115200 + elif [ "${BOARD}" == "nano" ]; then + SPEED=57600 + else + echo "FATAL #002, CHECK THIS CODE" + exit 99 + fi + fi + + if [ ! -e "${PORT}" ]; then + echo "Error: port not found" + exit 10 + fi +fi + +if [ -z "${FQBN}" ]; then + if [ "${BOARD}" == "uno" ]; then + FQBN="arduino:avr:uno" + elif [ "${BOARD}" == "nano" ]; then + FQBN="arduino:avr:nano:cpu=atmega328old" + else + echo "FATAL #003, CHECK THIS CODE" + exit 99 + fi +fi + +if [ -z "${BUILDDIR}" ]; then + if [[ "${FILE}" == */* ]]; then + BUILDDIR=${FILE%/*} + BUILDDIR="${BUILDDIR%/}/build" + else + BUILDDIR=build + fi +fi + +if [ "${RECORDUSB}" == "yes" ]; then + if [ -z "${RECORDFILE}" ]; then + V=${FILE##*/} + V=${V%.*} + V=${V:-out} + PREV= + for i in {15..00}; do + F="${RECORDDIR}/${V}-$i.txt" + if [ -e "${F}" ] && [ -n "${PREV}" ]; then + mv "${F}" "${PREV}" + fi + PREV="${F}" + done + RECORDFILE="${F}" + mkdir -p "${RECORDDIR}" + fi +else + RECORDFILE="/dev/null" +fi + +if [ "${VERBOSE}" == "yes" ]; then + echo "-- Settings" + echo "Arduino dir: ${ARDUINODIR}" + echo "Board: ${BOARD}" + echo "Port: ${PORT}" + echo "Speed: ${SPEED}" + echo "Fqbn: ${FQBN}" + echo "Upload: ${UPLOAD}" + echo "Remove: ${REMOVE}" + echo "Catusb: ${CATUSB}" + echo "Recordusb: ${RECORDUSB}" + echo "Record file: ${RECORDFILE}" + echo "Verbose: ${VERBOSE}" + echo "File: ${FILE}" + echo "Build dir: ${BUILDDIR}" +fi + +set -e + +if [ "${REMOVE}" == "yes" ]; then + rm -rf /tmp/arduino-core-cache +fi + +if [ "${COMPILE}" == "yes" ]; then + echo "-- Compile" + + mkdir -p "${BUILDDIR}" + + OPT_LIB= + TMP_ULIB=${ARDUINO_USER_LIBS:-} + if [ -n "${TMP_ULIB}" ]; then + OPT_LIB="--libraries ""${TMP_ULIB}""" + else + echo + echo "***********************************************************" + echo "* WARNING *" + echo "* The environment variable ARDUINO_USER_LIBS is not set *" + echo "***********************************************************" + echo + fi + + TESTPLAN_OPT="" + if [ -n "${TESTPLAN}" ]; then + TESTPLAN_OPT="--build-property build.extra_flags=-DRF433ANY_TESTPLAN=${TESTPLAN}" + fi + + # shellcheck disable=SC2086 + # (We don't want to quote OPT_LIB as it can contain multiple options.) + arduino-cli compile -b "${FQBN}" ${NOCOLOR} --build-path "${BUILDDIR}" ${OPT_LIB} ${TESTPLAN_OPT} "${FILE}" +fi + +FILEBASENAME=${FILE##*/} + +if [ "${UPLOAD}" == "yes" ]; then + echo "-- Upload" + arduino-cli upload -v -b "${FQBN}" --input-dir "${BUILDDIR}" -p "${PORT}" +fi + +if [ "${CATUSB}" == "yes" ] || [ "${STTY}" == "yes" ]; then + if [ -z "${READSPEED}" ]; then + TFILE=$(mktemp) + gcc -fpreprocessed -dD -x c++ -E "${FILE}" > "${TFILE}" + for sp in 9600 19200 28800 38400 57600 115200; do + if grep ${sp} "${TFILE}" > /dev/null; then + READSPEED=${sp} + fi + done + READSPEED=${READSPEED:-9600} + rm "${TFILE}" + fi + + stty -F "${PORT}" -hupcl -echo "${READSPEED}" + echo "-- usb setup with speed ${READSPEED}" +fi + +if [ "${CATUSB}" == "yes" ]; then + echo "-- Read usb (Ctrl-C to quit)" + DISPLAYSEP=yes + { + echo "speed=${READSPEED}" + echo "fqbn=${FQBN}" + echo "port=${PORT}" + echo "file=${FILE}" + echo "filedate=$(date +"%Y-%m-%dT%H:%M:%SZ" -d @$(stat -c '%Y' "${FILE}"))" + echo "date=$(date +'%Y-%m-%dT%H:%M:%SZ')" + echo "" + echo "-----BEGIN ARDUINO OUTPUT-----" + } | tee "${RECORDFILE}" + tee -a "${RECORDFILE}" < "${PORT}" +fi + diff --git a/examples/04_react_on_code/Makefile b/examples/04_react_on_code/Makefile deleted file mode 120000 index d0b0e8e..0000000 --- a/examples/04_react_on_code/Makefile +++ /dev/null @@ -1 +0,0 @@ -../Makefile \ No newline at end of file diff --git a/examples/04_react_on_code/Makefile b/examples/04_react_on_code/Makefile new file mode 100644 index 0000000..6cf19c3 --- /dev/null +++ b/examples/04_react_on_code/Makefile @@ -0,0 +1,22 @@ +# This Makefile is to be symbolic-linked inside subfolders. +# It is generic enough that, to the asumption the subfolder contains one .ino +# file and only one, it'll work as expected. + +source = $(wildcard *.ino) +target = build/$(source).with_bootloader.hex +opt = + +ALL: $(target) + +$(target): $(source) +ifdef color + ./am2 $(opt) $< +else + GCC_COLORS="" ./am2 --no-color $(opt) $< +endif + +clean: + rm -rf build + +mrproper: + rm -rf build out diff --git a/examples/04_react_on_code/am2 b/examples/04_react_on_code/am2 deleted file mode 120000 index 270bb52..0000000 --- a/examples/04_react_on_code/am2 +++ /dev/null @@ -1 +0,0 @@ -../../am2 \ No newline at end of file diff --git a/examples/04_react_on_code/am2 b/examples/04_react_on_code/am2 new file mode 100755 index 0000000..ef045ff --- /dev/null +++ b/examples/04_react_on_code/am2 @@ -0,0 +1,346 @@ +#!/usr/bin/bash + +# am2 + +# Copyright 2019, 2020, 2021, 2022 Sébastien Millet + +# Can perform the following: +# 1. Compile the code +# 2. Upload to Arduino +# 3. Read (continually) what is arriving from the USB port the Arduino is +# connected to + +# Versions history (as of 1.3) +# 1.3 Output from Arduino is recorded in files named with numbers instead of +# date-time string. +# 1.4 Adds -t (--testplan) option, to set TESTPLAN macro +# 1.5 -t (or --testplan) now comes with a value, so as to manage multiple test +# plans. +# 1.6 Updated to work fine with Arch arduino package instead of the manually +# installed (from tar.gz source) package used so far. +# 1.7 Renames archlinux-arduino back to arduino, and created corresponding +# symlink (was cleaner to do s). +# 2.0 Replaces arduino-builder with arduino-cli +# 2.1 Output warning if ARDUINO_USER_LIBS environment variable is not set + +set -euo pipefail + +VERSION=2.1 + +PORT= +BOARD= +SPEED= +FQBN= +BUILDDIR= +RECORDDIR=out +READSPEED= +RECORDFILE= + +UPLOAD="no" +REMOVE="no" +VERBOSE="no" +CATUSB="no" +STTY="no" +RECORDUSB="no" +COMPILE="yes" +TESTPLAN= +NOCOLOR= + +DISPLAYSEP=no + +function finish { + if [ "${DISPLAYSEP}" == "yes" ]; then + echo "-----END ARDUINO OUTPUT-----" | tee -a "${RECORDFILE}" + fi +} + +trap finish EXIT + +function usage { + echo "Usage:" + echo " am [OPTIONS...] FILE" + echo "Compile FILE using arduino-builder." + echo "Example: am sketch.ino" + echo "" + echo "ENVIRONMENT VARIABLES" + echo " If ARDUINO_USER_LIBS is defined and non empty, then arduino-builder" + echo " is called with the supplementary option -libraries followed by" + echo " ARDUINO_USER_LIBS' value." + echo "" + echo "OPTIONS" + echo " -h --help Display this help screen" + echo " -V --version Output version information and quit" + echo " -v --verbose Be more talkative" + echo " -u --upload Upload compiled code into Arduino" + echo " -R --remove Remove /tmp/arduino-core-cache and ./buid" + echo " -b --board Board, either 'uno' or 'nano'" + echo " -p --port Port, for ex. '/dev/ttyUSB0'" + echo " -s --speed Upload speed, for ex. 115200" + echo " Normally, speed is infered from device type:" + echo " 115200 for Uno, 57600 for Nano" + echo " -B --fqbn Board Fully Qualified Name, like 'arduino:avr:uno'" + echo " -d --builddir Build directory" + echo " -c --catusb Display (continually) what Arduino writes on USB" + echo " --stty Tune stty properly for later communication (implied" + echo " by --catusb)" + echo " -r --recordusb Write USB (continually) to a file (implies -c)" + echo " --recordfile Output file if -r option is set" + echo " -n --nocompile Don't compile code" + echo " --readspeed Read speed of USB. If not specified, this script" + echo " will try to infere it from source file. If it" + echo " fails, it'll fallback to 9600." + echo " This option is useful only if USB is read" + echo " (-c or --stty option set)" + echo " -t --testplan Set TESTPLAN macro value" + echo " (as if #define TESTPLAN VALUE)" + echo " --no-color Pass on the --no-color option to arduino-cli" + exit 1 +} + +function version { + echo "am version ${VERSION}" + exit +} + +OPTS=$(getopt -o hVvuRb:p:s:B:d:crnt: --long help,version,verbose,upload,remove,board:,port:,speed:,fqbn:,builddir:,catusb,stty,no-color,recordusb,nocompile,readspeed:,recordfile:,testplan: -n 'am' -- "$@") + +eval set -- "$OPTS" + +while true; do + case "$1" in + -h | --help ) usage; shift ;; + -V | --version ) version; shift ;; + -v | --verbose ) VERBOSE="yes"; shift ;; + -u | --upload ) UPLOAD="yes"; shift ;; + -R | --remove ) REMOVE="yes"; shift ;; + -b | --board ) BOARD="$2"; shift 2 ;; + -p | --port ) PORT="$2"; shift 2 ;; + -s | --speed ) SPEED="$2"; shift 2 ;; + -B | --fqbn ) FQBN="$2"; shift 2 ;; + -d | --builddir ) BUILDDIR="$2"; shift 2 ;; + -c | --catusb ) CATUSB="yes"; shift ;; + -r | --recordusb ) RECORDUSB="yes"; CATUSB="yes"; shift ;; + -n | --nocompile ) COMPILE="no"; shift ;; + --readspeed ) READSPEED="$2"; shift 2 ;; + --recordfile ) RECORDFILE="$2"; shift 2 ;; + --stty ) STTY="yes"; shift ;; + --no-color ) NOCOLOR="--no-color"; shift ;; + -t | --testplan ) TESTPLAN="$2"; shift 2 ;; + -- ) shift; break ;; + * ) break ;; + esac +done + +FILE=${1:-} +TRAILINGOPTS=${2:-} + +if [ -n "${TRAILINGOPTS}" ]; then + echo "Error: trailing options" + exit 1; +fi +if [ -z "${FILE}" ]; then + echo "Error: no input file" + exit 1; +fi + +set +e + +if [ -n "${BOARD}" ]; then + if [ "${BOARD}" != "uno" ] && [ "${BOARD}" != "nano" ]; then + echo "Error: board '${BOARD}' unknown" + exit 1 + fi +fi + +ARDUINODIR=/usr/share/arduino + +COUNTUNO=$(compgen -G '/dev/ttyACM*' | wc -l) +COUNTNANO=$(compgen -G '/dev/ttyUSB*' | wc -l) + +if [ -z "${BOARD}" ]; then + if [ "${COUNTUNO}" -ge 1 ] && [ "${COUNTNANO}" -ge 1 ]; then + echo "Error: cannot guess board, found ${COUNTUNO} uno(s), ${COUNTNANO} nano(s)" + exit 10 + fi + if [ "${COUNTUNO}" -ge 1 ]; then + BOARD=uno + elif [ "${COUNTNANO}" -ge 1 ]; then + BOARD=nano + fi + if [ -z "${BOARD}" ]; then + echo "Error: cannot guess board, none found"; + exit 10 + fi +fi + +if [ "${UPLOAD}" == "yes" ] || [ "${CATUSB}" == "yes" ]; then + if [ -z "${PORT}" ]; then + if [ "${BOARD}" == "uno" ]; then + COUNT=${COUNTUNO} + PORT=$(compgen -G '/dev/ttyACM*') + elif [ "${BOARD}" == "nano" ]; then + COUNT=${COUNTNANO} + PORT=$(compgen -G '/dev/ttyUSB*') + else + echo "FATAL #001, CHECK THIS CODE" + exit 99 + fi + + if [ "${COUNT}" -ge 2 ]; then + echo "Error: cannot guess port, more than 1 board '${BOARD}' found" + exit 10 + fi + if [ -z "${PORT}" ]; then + echo "Error: cannot guess port, none found" + exit 10 + fi + fi + + if [ -z "${SPEED}" ]; then + if [ "${BOARD}" == "uno" ]; then + SPEED=115200 + elif [ "${BOARD}" == "nano" ]; then + SPEED=57600 + else + echo "FATAL #002, CHECK THIS CODE" + exit 99 + fi + fi + + if [ ! -e "${PORT}" ]; then + echo "Error: port not found" + exit 10 + fi +fi + +if [ -z "${FQBN}" ]; then + if [ "${BOARD}" == "uno" ]; then + FQBN="arduino:avr:uno" + elif [ "${BOARD}" == "nano" ]; then + FQBN="arduino:avr:nano:cpu=atmega328old" + else + echo "FATAL #003, CHECK THIS CODE" + exit 99 + fi +fi + +if [ -z "${BUILDDIR}" ]; then + if [[ "${FILE}" == */* ]]; then + BUILDDIR=${FILE%/*} + BUILDDIR="${BUILDDIR%/}/build" + else + BUILDDIR=build + fi +fi + +if [ "${RECORDUSB}" == "yes" ]; then + if [ -z "${RECORDFILE}" ]; then + V=${FILE##*/} + V=${V%.*} + V=${V:-out} + PREV= + for i in {15..00}; do + F="${RECORDDIR}/${V}-$i.txt" + if [ -e "${F}" ] && [ -n "${PREV}" ]; then + mv "${F}" "${PREV}" + fi + PREV="${F}" + done + RECORDFILE="${F}" + mkdir -p "${RECORDDIR}" + fi +else + RECORDFILE="/dev/null" +fi + +if [ "${VERBOSE}" == "yes" ]; then + echo "-- Settings" + echo "Arduino dir: ${ARDUINODIR}" + echo "Board: ${BOARD}" + echo "Port: ${PORT}" + echo "Speed: ${SPEED}" + echo "Fqbn: ${FQBN}" + echo "Upload: ${UPLOAD}" + echo "Remove: ${REMOVE}" + echo "Catusb: ${CATUSB}" + echo "Recordusb: ${RECORDUSB}" + echo "Record file: ${RECORDFILE}" + echo "Verbose: ${VERBOSE}" + echo "File: ${FILE}" + echo "Build dir: ${BUILDDIR}" +fi + +set -e + +if [ "${REMOVE}" == "yes" ]; then + rm -rf /tmp/arduino-core-cache +fi + +if [ "${COMPILE}" == "yes" ]; then + echo "-- Compile" + + mkdir -p "${BUILDDIR}" + + OPT_LIB= + TMP_ULIB=${ARDUINO_USER_LIBS:-} + if [ -n "${TMP_ULIB}" ]; then + OPT_LIB="--libraries ""${TMP_ULIB}""" + else + echo + echo "***********************************************************" + echo "* WARNING *" + echo "* The environment variable ARDUINO_USER_LIBS is not set *" + echo "***********************************************************" + echo + fi + + TESTPLAN_OPT="" + if [ -n "${TESTPLAN}" ]; then + TESTPLAN_OPT="--build-property build.extra_flags=-DRF433ANY_TESTPLAN=${TESTPLAN}" + fi + + # shellcheck disable=SC2086 + # (We don't want to quote OPT_LIB as it can contain multiple options.) + arduino-cli compile -b "${FQBN}" ${NOCOLOR} --build-path "${BUILDDIR}" ${OPT_LIB} ${TESTPLAN_OPT} "${FILE}" +fi + +FILEBASENAME=${FILE##*/} + +if [ "${UPLOAD}" == "yes" ]; then + echo "-- Upload" + arduino-cli upload -v -b "${FQBN}" --input-dir "${BUILDDIR}" -p "${PORT}" +fi + +if [ "${CATUSB}" == "yes" ] || [ "${STTY}" == "yes" ]; then + if [ -z "${READSPEED}" ]; then + TFILE=$(mktemp) + gcc -fpreprocessed -dD -x c++ -E "${FILE}" > "${TFILE}" + for sp in 9600 19200 28800 38400 57600 115200; do + if grep ${sp} "${TFILE}" > /dev/null; then + READSPEED=${sp} + fi + done + READSPEED=${READSPEED:-9600} + rm "${TFILE}" + fi + + stty -F "${PORT}" -hupcl -echo "${READSPEED}" + echo "-- usb setup with speed ${READSPEED}" +fi + +if [ "${CATUSB}" == "yes" ]; then + echo "-- Read usb (Ctrl-C to quit)" + DISPLAYSEP=yes + { + echo "speed=${READSPEED}" + echo "fqbn=${FQBN}" + echo "port=${PORT}" + echo "file=${FILE}" + echo "filedate=$(date +"%Y-%m-%dT%H:%M:%SZ" -d @$(stat -c '%Y' "${FILE}"))" + echo "date=$(date +'%Y-%m-%dT%H:%M:%SZ')" + echo "" + echo "-----BEGIN ARDUINO OUTPUT-----" + } | tee "${RECORDFILE}" + tee -a "${RECORDFILE}" < "${PORT}" +fi + diff --git a/examples/05_callback/Makefile b/examples/05_callback/Makefile deleted file mode 120000 index d0b0e8e..0000000 --- a/examples/05_callback/Makefile +++ /dev/null @@ -1 +0,0 @@ -../Makefile \ No newline at end of file diff --git a/examples/05_callback/Makefile b/examples/05_callback/Makefile new file mode 100644 index 0000000..6cf19c3 --- /dev/null +++ b/examples/05_callback/Makefile @@ -0,0 +1,22 @@ +# This Makefile is to be symbolic-linked inside subfolders. +# It is generic enough that, to the asumption the subfolder contains one .ino +# file and only one, it'll work as expected. + +source = $(wildcard *.ino) +target = build/$(source).with_bootloader.hex +opt = + +ALL: $(target) + +$(target): $(source) +ifdef color + ./am2 $(opt) $< +else + GCC_COLORS="" ./am2 --no-color $(opt) $< +endif + +clean: + rm -rf build + +mrproper: + rm -rf build out diff --git a/examples/05_callback/am2 b/examples/05_callback/am2 deleted file mode 120000 index 270bb52..0000000 --- a/examples/05_callback/am2 +++ /dev/null @@ -1 +0,0 @@ -../../am2 \ No newline at end of file diff --git a/examples/05_callback/am2 b/examples/05_callback/am2 new file mode 100755 index 0000000..ef045ff --- /dev/null +++ b/examples/05_callback/am2 @@ -0,0 +1,346 @@ +#!/usr/bin/bash + +# am2 + +# Copyright 2019, 2020, 2021, 2022 Sébastien Millet + +# Can perform the following: +# 1. Compile the code +# 2. Upload to Arduino +# 3. Read (continually) what is arriving from the USB port the Arduino is +# connected to + +# Versions history (as of 1.3) +# 1.3 Output from Arduino is recorded in files named with numbers instead of +# date-time string. +# 1.4 Adds -t (--testplan) option, to set TESTPLAN macro +# 1.5 -t (or --testplan) now comes with a value, so as to manage multiple test +# plans. +# 1.6 Updated to work fine with Arch arduino package instead of the manually +# installed (from tar.gz source) package used so far. +# 1.7 Renames archlinux-arduino back to arduino, and created corresponding +# symlink (was cleaner to do s). +# 2.0 Replaces arduino-builder with arduino-cli +# 2.1 Output warning if ARDUINO_USER_LIBS environment variable is not set + +set -euo pipefail + +VERSION=2.1 + +PORT= +BOARD= +SPEED= +FQBN= +BUILDDIR= +RECORDDIR=out +READSPEED= +RECORDFILE= + +UPLOAD="no" +REMOVE="no" +VERBOSE="no" +CATUSB="no" +STTY="no" +RECORDUSB="no" +COMPILE="yes" +TESTPLAN= +NOCOLOR= + +DISPLAYSEP=no + +function finish { + if [ "${DISPLAYSEP}" == "yes" ]; then + echo "-----END ARDUINO OUTPUT-----" | tee -a "${RECORDFILE}" + fi +} + +trap finish EXIT + +function usage { + echo "Usage:" + echo " am [OPTIONS...] FILE" + echo "Compile FILE using arduino-builder." + echo "Example: am sketch.ino" + echo "" + echo "ENVIRONMENT VARIABLES" + echo " If ARDUINO_USER_LIBS is defined and non empty, then arduino-builder" + echo " is called with the supplementary option -libraries followed by" + echo " ARDUINO_USER_LIBS' value." + echo "" + echo "OPTIONS" + echo " -h --help Display this help screen" + echo " -V --version Output version information and quit" + echo " -v --verbose Be more talkative" + echo " -u --upload Upload compiled code into Arduino" + echo " -R --remove Remove /tmp/arduino-core-cache and ./buid" + echo " -b --board Board, either 'uno' or 'nano'" + echo " -p --port Port, for ex. '/dev/ttyUSB0'" + echo " -s --speed Upload speed, for ex. 115200" + echo " Normally, speed is infered from device type:" + echo " 115200 for Uno, 57600 for Nano" + echo " -B --fqbn Board Fully Qualified Name, like 'arduino:avr:uno'" + echo " -d --builddir Build directory" + echo " -c --catusb Display (continually) what Arduino writes on USB" + echo " --stty Tune stty properly for later communication (implied" + echo " by --catusb)" + echo " -r --recordusb Write USB (continually) to a file (implies -c)" + echo " --recordfile Output file if -r option is set" + echo " -n --nocompile Don't compile code" + echo " --readspeed Read speed of USB. If not specified, this script" + echo " will try to infere it from source file. If it" + echo " fails, it'll fallback to 9600." + echo " This option is useful only if USB is read" + echo " (-c or --stty option set)" + echo " -t --testplan Set TESTPLAN macro value" + echo " (as if #define TESTPLAN VALUE)" + echo " --no-color Pass on the --no-color option to arduino-cli" + exit 1 +} + +function version { + echo "am version ${VERSION}" + exit +} + +OPTS=$(getopt -o hVvuRb:p:s:B:d:crnt: --long help,version,verbose,upload,remove,board:,port:,speed:,fqbn:,builddir:,catusb,stty,no-color,recordusb,nocompile,readspeed:,recordfile:,testplan: -n 'am' -- "$@") + +eval set -- "$OPTS" + +while true; do + case "$1" in + -h | --help ) usage; shift ;; + -V | --version ) version; shift ;; + -v | --verbose ) VERBOSE="yes"; shift ;; + -u | --upload ) UPLOAD="yes"; shift ;; + -R | --remove ) REMOVE="yes"; shift ;; + -b | --board ) BOARD="$2"; shift 2 ;; + -p | --port ) PORT="$2"; shift 2 ;; + -s | --speed ) SPEED="$2"; shift 2 ;; + -B | --fqbn ) FQBN="$2"; shift 2 ;; + -d | --builddir ) BUILDDIR="$2"; shift 2 ;; + -c | --catusb ) CATUSB="yes"; shift ;; + -r | --recordusb ) RECORDUSB="yes"; CATUSB="yes"; shift ;; + -n | --nocompile ) COMPILE="no"; shift ;; + --readspeed ) READSPEED="$2"; shift 2 ;; + --recordfile ) RECORDFILE="$2"; shift 2 ;; + --stty ) STTY="yes"; shift ;; + --no-color ) NOCOLOR="--no-color"; shift ;; + -t | --testplan ) TESTPLAN="$2"; shift 2 ;; + -- ) shift; break ;; + * ) break ;; + esac +done + +FILE=${1:-} +TRAILINGOPTS=${2:-} + +if [ -n "${TRAILINGOPTS}" ]; then + echo "Error: trailing options" + exit 1; +fi +if [ -z "${FILE}" ]; then + echo "Error: no input file" + exit 1; +fi + +set +e + +if [ -n "${BOARD}" ]; then + if [ "${BOARD}" != "uno" ] && [ "${BOARD}" != "nano" ]; then + echo "Error: board '${BOARD}' unknown" + exit 1 + fi +fi + +ARDUINODIR=/usr/share/arduino + +COUNTUNO=$(compgen -G '/dev/ttyACM*' | wc -l) +COUNTNANO=$(compgen -G '/dev/ttyUSB*' | wc -l) + +if [ -z "${BOARD}" ]; then + if [ "${COUNTUNO}" -ge 1 ] && [ "${COUNTNANO}" -ge 1 ]; then + echo "Error: cannot guess board, found ${COUNTUNO} uno(s), ${COUNTNANO} nano(s)" + exit 10 + fi + if [ "${COUNTUNO}" -ge 1 ]; then + BOARD=uno + elif [ "${COUNTNANO}" -ge 1 ]; then + BOARD=nano + fi + if [ -z "${BOARD}" ]; then + echo "Error: cannot guess board, none found"; + exit 10 + fi +fi + +if [ "${UPLOAD}" == "yes" ] || [ "${CATUSB}" == "yes" ]; then + if [ -z "${PORT}" ]; then + if [ "${BOARD}" == "uno" ]; then + COUNT=${COUNTUNO} + PORT=$(compgen -G '/dev/ttyACM*') + elif [ "${BOARD}" == "nano" ]; then + COUNT=${COUNTNANO} + PORT=$(compgen -G '/dev/ttyUSB*') + else + echo "FATAL #001, CHECK THIS CODE" + exit 99 + fi + + if [ "${COUNT}" -ge 2 ]; then + echo "Error: cannot guess port, more than 1 board '${BOARD}' found" + exit 10 + fi + if [ -z "${PORT}" ]; then + echo "Error: cannot guess port, none found" + exit 10 + fi + fi + + if [ -z "${SPEED}" ]; then + if [ "${BOARD}" == "uno" ]; then + SPEED=115200 + elif [ "${BOARD}" == "nano" ]; then + SPEED=57600 + else + echo "FATAL #002, CHECK THIS CODE" + exit 99 + fi + fi + + if [ ! -e "${PORT}" ]; then + echo "Error: port not found" + exit 10 + fi +fi + +if [ -z "${FQBN}" ]; then + if [ "${BOARD}" == "uno" ]; then + FQBN="arduino:avr:uno" + elif [ "${BOARD}" == "nano" ]; then + FQBN="arduino:avr:nano:cpu=atmega328old" + else + echo "FATAL #003, CHECK THIS CODE" + exit 99 + fi +fi + +if [ -z "${BUILDDIR}" ]; then + if [[ "${FILE}" == */* ]]; then + BUILDDIR=${FILE%/*} + BUILDDIR="${BUILDDIR%/}/build" + else + BUILDDIR=build + fi +fi + +if [ "${RECORDUSB}" == "yes" ]; then + if [ -z "${RECORDFILE}" ]; then + V=${FILE##*/} + V=${V%.*} + V=${V:-out} + PREV= + for i in {15..00}; do + F="${RECORDDIR}/${V}-$i.txt" + if [ -e "${F}" ] && [ -n "${PREV}" ]; then + mv "${F}" "${PREV}" + fi + PREV="${F}" + done + RECORDFILE="${F}" + mkdir -p "${RECORDDIR}" + fi +else + RECORDFILE="/dev/null" +fi + +if [ "${VERBOSE}" == "yes" ]; then + echo "-- Settings" + echo "Arduino dir: ${ARDUINODIR}" + echo "Board: ${BOARD}" + echo "Port: ${PORT}" + echo "Speed: ${SPEED}" + echo "Fqbn: ${FQBN}" + echo "Upload: ${UPLOAD}" + echo "Remove: ${REMOVE}" + echo "Catusb: ${CATUSB}" + echo "Recordusb: ${RECORDUSB}" + echo "Record file: ${RECORDFILE}" + echo "Verbose: ${VERBOSE}" + echo "File: ${FILE}" + echo "Build dir: ${BUILDDIR}" +fi + +set -e + +if [ "${REMOVE}" == "yes" ]; then + rm -rf /tmp/arduino-core-cache +fi + +if [ "${COMPILE}" == "yes" ]; then + echo "-- Compile" + + mkdir -p "${BUILDDIR}" + + OPT_LIB= + TMP_ULIB=${ARDUINO_USER_LIBS:-} + if [ -n "${TMP_ULIB}" ]; then + OPT_LIB="--libraries ""${TMP_ULIB}""" + else + echo + echo "***********************************************************" + echo "* WARNING *" + echo "* The environment variable ARDUINO_USER_LIBS is not set *" + echo "***********************************************************" + echo + fi + + TESTPLAN_OPT="" + if [ -n "${TESTPLAN}" ]; then + TESTPLAN_OPT="--build-property build.extra_flags=-DRF433ANY_TESTPLAN=${TESTPLAN}" + fi + + # shellcheck disable=SC2086 + # (We don't want to quote OPT_LIB as it can contain multiple options.) + arduino-cli compile -b "${FQBN}" ${NOCOLOR} --build-path "${BUILDDIR}" ${OPT_LIB} ${TESTPLAN_OPT} "${FILE}" +fi + +FILEBASENAME=${FILE##*/} + +if [ "${UPLOAD}" == "yes" ]; then + echo "-- Upload" + arduino-cli upload -v -b "${FQBN}" --input-dir "${BUILDDIR}" -p "${PORT}" +fi + +if [ "${CATUSB}" == "yes" ] || [ "${STTY}" == "yes" ]; then + if [ -z "${READSPEED}" ]; then + TFILE=$(mktemp) + gcc -fpreprocessed -dD -x c++ -E "${FILE}" > "${TFILE}" + for sp in 9600 19200 28800 38400 57600 115200; do + if grep ${sp} "${TFILE}" > /dev/null; then + READSPEED=${sp} + fi + done + READSPEED=${READSPEED:-9600} + rm "${TFILE}" + fi + + stty -F "${PORT}" -hupcl -echo "${READSPEED}" + echo "-- usb setup with speed ${READSPEED}" +fi + +if [ "${CATUSB}" == "yes" ]; then + echo "-- Read usb (Ctrl-C to quit)" + DISPLAYSEP=yes + { + echo "speed=${READSPEED}" + echo "fqbn=${FQBN}" + echo "port=${PORT}" + echo "file=${FILE}" + echo "filedate=$(date +"%Y-%m-%dT%H:%M:%SZ" -d @$(stat -c '%Y' "${FILE}"))" + echo "date=$(date +'%Y-%m-%dT%H:%M:%SZ')" + echo "" + echo "-----BEGIN ARDUINO OUTPUT-----" + } | tee "${RECORDFILE}" + tee -a "${RECORDFILE}" < "${PORT}" +fi + diff --git a/extras/testplan/simul/am2 b/extras/testplan/simul/am2 deleted file mode 120000 index 3149a53..0000000 --- a/extras/testplan/simul/am2 +++ /dev/null @@ -1 +0,0 @@ -../../../am2 \ No newline at end of file diff --git a/extras/testplan/simul/am2 b/extras/testplan/simul/am2 new file mode 100755 index 0000000..ef045ff --- /dev/null +++ b/extras/testplan/simul/am2 @@ -0,0 +1,346 @@ +#!/usr/bin/bash + +# am2 + +# Copyright 2019, 2020, 2021, 2022 Sébastien Millet + +# Can perform the following: +# 1. Compile the code +# 2. Upload to Arduino +# 3. Read (continually) what is arriving from the USB port the Arduino is +# connected to + +# Versions history (as of 1.3) +# 1.3 Output from Arduino is recorded in files named with numbers instead of +# date-time string. +# 1.4 Adds -t (--testplan) option, to set TESTPLAN macro +# 1.5 -t (or --testplan) now comes with a value, so as to manage multiple test +# plans. +# 1.6 Updated to work fine with Arch arduino package instead of the manually +# installed (from tar.gz source) package used so far. +# 1.7 Renames archlinux-arduino back to arduino, and created corresponding +# symlink (was cleaner to do s). +# 2.0 Replaces arduino-builder with arduino-cli +# 2.1 Output warning if ARDUINO_USER_LIBS environment variable is not set + +set -euo pipefail + +VERSION=2.1 + +PORT= +BOARD= +SPEED= +FQBN= +BUILDDIR= +RECORDDIR=out +READSPEED= +RECORDFILE= + +UPLOAD="no" +REMOVE="no" +VERBOSE="no" +CATUSB="no" +STTY="no" +RECORDUSB="no" +COMPILE="yes" +TESTPLAN= +NOCOLOR= + +DISPLAYSEP=no + +function finish { + if [ "${DISPLAYSEP}" == "yes" ]; then + echo "-----END ARDUINO OUTPUT-----" | tee -a "${RECORDFILE}" + fi +} + +trap finish EXIT + +function usage { + echo "Usage:" + echo " am [OPTIONS...] FILE" + echo "Compile FILE using arduino-builder." + echo "Example: am sketch.ino" + echo "" + echo "ENVIRONMENT VARIABLES" + echo " If ARDUINO_USER_LIBS is defined and non empty, then arduino-builder" + echo " is called with the supplementary option -libraries followed by" + echo " ARDUINO_USER_LIBS' value." + echo "" + echo "OPTIONS" + echo " -h --help Display this help screen" + echo " -V --version Output version information and quit" + echo " -v --verbose Be more talkative" + echo " -u --upload Upload compiled code into Arduino" + echo " -R --remove Remove /tmp/arduino-core-cache and ./buid" + echo " -b --board Board, either 'uno' or 'nano'" + echo " -p --port Port, for ex. '/dev/ttyUSB0'" + echo " -s --speed Upload speed, for ex. 115200" + echo " Normally, speed is infered from device type:" + echo " 115200 for Uno, 57600 for Nano" + echo " -B --fqbn Board Fully Qualified Name, like 'arduino:avr:uno'" + echo " -d --builddir Build directory" + echo " -c --catusb Display (continually) what Arduino writes on USB" + echo " --stty Tune stty properly for later communication (implied" + echo " by --catusb)" + echo " -r --recordusb Write USB (continually) to a file (implies -c)" + echo " --recordfile Output file if -r option is set" + echo " -n --nocompile Don't compile code" + echo " --readspeed Read speed of USB. If not specified, this script" + echo " will try to infere it from source file. If it" + echo " fails, it'll fallback to 9600." + echo " This option is useful only if USB is read" + echo " (-c or --stty option set)" + echo " -t --testplan Set TESTPLAN macro value" + echo " (as if #define TESTPLAN VALUE)" + echo " --no-color Pass on the --no-color option to arduino-cli" + exit 1 +} + +function version { + echo "am version ${VERSION}" + exit +} + +OPTS=$(getopt -o hVvuRb:p:s:B:d:crnt: --long help,version,verbose,upload,remove,board:,port:,speed:,fqbn:,builddir:,catusb,stty,no-color,recordusb,nocompile,readspeed:,recordfile:,testplan: -n 'am' -- "$@") + +eval set -- "$OPTS" + +while true; do + case "$1" in + -h | --help ) usage; shift ;; + -V | --version ) version; shift ;; + -v | --verbose ) VERBOSE="yes"; shift ;; + -u | --upload ) UPLOAD="yes"; shift ;; + -R | --remove ) REMOVE="yes"; shift ;; + -b | --board ) BOARD="$2"; shift 2 ;; + -p | --port ) PORT="$2"; shift 2 ;; + -s | --speed ) SPEED="$2"; shift 2 ;; + -B | --fqbn ) FQBN="$2"; shift 2 ;; + -d | --builddir ) BUILDDIR="$2"; shift 2 ;; + -c | --catusb ) CATUSB="yes"; shift ;; + -r | --recordusb ) RECORDUSB="yes"; CATUSB="yes"; shift ;; + -n | --nocompile ) COMPILE="no"; shift ;; + --readspeed ) READSPEED="$2"; shift 2 ;; + --recordfile ) RECORDFILE="$2"; shift 2 ;; + --stty ) STTY="yes"; shift ;; + --no-color ) NOCOLOR="--no-color"; shift ;; + -t | --testplan ) TESTPLAN="$2"; shift 2 ;; + -- ) shift; break ;; + * ) break ;; + esac +done + +FILE=${1:-} +TRAILINGOPTS=${2:-} + +if [ -n "${TRAILINGOPTS}" ]; then + echo "Error: trailing options" + exit 1; +fi +if [ -z "${FILE}" ]; then + echo "Error: no input file" + exit 1; +fi + +set +e + +if [ -n "${BOARD}" ]; then + if [ "${BOARD}" != "uno" ] && [ "${BOARD}" != "nano" ]; then + echo "Error: board '${BOARD}' unknown" + exit 1 + fi +fi + +ARDUINODIR=/usr/share/arduino + +COUNTUNO=$(compgen -G '/dev/ttyACM*' | wc -l) +COUNTNANO=$(compgen -G '/dev/ttyUSB*' | wc -l) + +if [ -z "${BOARD}" ]; then + if [ "${COUNTUNO}" -ge 1 ] && [ "${COUNTNANO}" -ge 1 ]; then + echo "Error: cannot guess board, found ${COUNTUNO} uno(s), ${COUNTNANO} nano(s)" + exit 10 + fi + if [ "${COUNTUNO}" -ge 1 ]; then + BOARD=uno + elif [ "${COUNTNANO}" -ge 1 ]; then + BOARD=nano + fi + if [ -z "${BOARD}" ]; then + echo "Error: cannot guess board, none found"; + exit 10 + fi +fi + +if [ "${UPLOAD}" == "yes" ] || [ "${CATUSB}" == "yes" ]; then + if [ -z "${PORT}" ]; then + if [ "${BOARD}" == "uno" ]; then + COUNT=${COUNTUNO} + PORT=$(compgen -G '/dev/ttyACM*') + elif [ "${BOARD}" == "nano" ]; then + COUNT=${COUNTNANO} + PORT=$(compgen -G '/dev/ttyUSB*') + else + echo "FATAL #001, CHECK THIS CODE" + exit 99 + fi + + if [ "${COUNT}" -ge 2 ]; then + echo "Error: cannot guess port, more than 1 board '${BOARD}' found" + exit 10 + fi + if [ -z "${PORT}" ]; then + echo "Error: cannot guess port, none found" + exit 10 + fi + fi + + if [ -z "${SPEED}" ]; then + if [ "${BOARD}" == "uno" ]; then + SPEED=115200 + elif [ "${BOARD}" == "nano" ]; then + SPEED=57600 + else + echo "FATAL #002, CHECK THIS CODE" + exit 99 + fi + fi + + if [ ! -e "${PORT}" ]; then + echo "Error: port not found" + exit 10 + fi +fi + +if [ -z "${FQBN}" ]; then + if [ "${BOARD}" == "uno" ]; then + FQBN="arduino:avr:uno" + elif [ "${BOARD}" == "nano" ]; then + FQBN="arduino:avr:nano:cpu=atmega328old" + else + echo "FATAL #003, CHECK THIS CODE" + exit 99 + fi +fi + +if [ -z "${BUILDDIR}" ]; then + if [[ "${FILE}" == */* ]]; then + BUILDDIR=${FILE%/*} + BUILDDIR="${BUILDDIR%/}/build" + else + BUILDDIR=build + fi +fi + +if [ "${RECORDUSB}" == "yes" ]; then + if [ -z "${RECORDFILE}" ]; then + V=${FILE##*/} + V=${V%.*} + V=${V:-out} + PREV= + for i in {15..00}; do + F="${RECORDDIR}/${V}-$i.txt" + if [ -e "${F}" ] && [ -n "${PREV}" ]; then + mv "${F}" "${PREV}" + fi + PREV="${F}" + done + RECORDFILE="${F}" + mkdir -p "${RECORDDIR}" + fi +else + RECORDFILE="/dev/null" +fi + +if [ "${VERBOSE}" == "yes" ]; then + echo "-- Settings" + echo "Arduino dir: ${ARDUINODIR}" + echo "Board: ${BOARD}" + echo "Port: ${PORT}" + echo "Speed: ${SPEED}" + echo "Fqbn: ${FQBN}" + echo "Upload: ${UPLOAD}" + echo "Remove: ${REMOVE}" + echo "Catusb: ${CATUSB}" + echo "Recordusb: ${RECORDUSB}" + echo "Record file: ${RECORDFILE}" + echo "Verbose: ${VERBOSE}" + echo "File: ${FILE}" + echo "Build dir: ${BUILDDIR}" +fi + +set -e + +if [ "${REMOVE}" == "yes" ]; then + rm -rf /tmp/arduino-core-cache +fi + +if [ "${COMPILE}" == "yes" ]; then + echo "-- Compile" + + mkdir -p "${BUILDDIR}" + + OPT_LIB= + TMP_ULIB=${ARDUINO_USER_LIBS:-} + if [ -n "${TMP_ULIB}" ]; then + OPT_LIB="--libraries ""${TMP_ULIB}""" + else + echo + echo "***********************************************************" + echo "* WARNING *" + echo "* The environment variable ARDUINO_USER_LIBS is not set *" + echo "***********************************************************" + echo + fi + + TESTPLAN_OPT="" + if [ -n "${TESTPLAN}" ]; then + TESTPLAN_OPT="--build-property build.extra_flags=-DRF433ANY_TESTPLAN=${TESTPLAN}" + fi + + # shellcheck disable=SC2086 + # (We don't want to quote OPT_LIB as it can contain multiple options.) + arduino-cli compile -b "${FQBN}" ${NOCOLOR} --build-path "${BUILDDIR}" ${OPT_LIB} ${TESTPLAN_OPT} "${FILE}" +fi + +FILEBASENAME=${FILE##*/} + +if [ "${UPLOAD}" == "yes" ]; then + echo "-- Upload" + arduino-cli upload -v -b "${FQBN}" --input-dir "${BUILDDIR}" -p "${PORT}" +fi + +if [ "${CATUSB}" == "yes" ] || [ "${STTY}" == "yes" ]; then + if [ -z "${READSPEED}" ]; then + TFILE=$(mktemp) + gcc -fpreprocessed -dD -x c++ -E "${FILE}" > "${TFILE}" + for sp in 9600 19200 28800 38400 57600 115200; do + if grep ${sp} "${TFILE}" > /dev/null; then + READSPEED=${sp} + fi + done + READSPEED=${READSPEED:-9600} + rm "${TFILE}" + fi + + stty -F "${PORT}" -hupcl -echo "${READSPEED}" + echo "-- usb setup with speed ${READSPEED}" +fi + +if [ "${CATUSB}" == "yes" ]; then + echo "-- Read usb (Ctrl-C to quit)" + DISPLAYSEP=yes + { + echo "speed=${READSPEED}" + echo "fqbn=${FQBN}" + echo "port=${PORT}" + echo "file=${FILE}" + echo "filedate=$(date +"%Y-%m-%dT%H:%M:%SZ" -d @$(stat -c '%Y' "${FILE}"))" + echo "date=$(date +'%Y-%m-%dT%H:%M:%SZ')" + echo "" + echo "-----BEGIN ARDUINO OUTPUT-----" + } | tee "${RECORDFILE}" + tee -a "${RECORDFILE}" < "${PORT}" +fi + diff --git a/extras/testplan/simul/issue-15-tuya-blinds.txt b/extras/testplan/simul/issue-15-tuya-blinds.txt new file mode 100644 index 0000000..52d7b07 --- /dev/null +++ b/extras/testplan/simul/issue-15-tuya-blinds.txt @@ -0,0 +1,49 @@ +0, 10696 +352, 424 +564, 1036 +560, 1028 +568, 1028 +560, 1028 +564, 1032 +360, 420 +576, 1020 +368, 412 +368, 412 +376, 412 +376, 420 +364, 420 +368, 428 +360, 424 +568, 1036 +360, 428 +568, 1028 +372, 412 +372, 412 +380, 408 +580, 1028 +576, 1028 +368, 420 +568, 1036 +368, 412 +380, 412 +372, 412 +376, 412 +376, 412 +584, 1020 +576, 1028 +368, 420 +572, 1028 +576, 1020 +384, 408 +380, 408 +580, 1024 +584, 1020 +576, 1028 +372, 416 +572, 1028 +376, 412 +584, 1020 +376, 412 +584, 1024 +372, 412 +376, 416 diff --git a/extras/testplan/simul/simul.ino b/extras/testplan/simul/simul.ino index fa00c74..c86f5c7 100644 --- a/extras/testplan/simul/simul.ino +++ b/extras/testplan/simul/simul.ino @@ -27,6 +27,7 @@ #define PIN_RFINPUT 2 #include "RF433any.h" + #include "RF433Serial.h" #include @@ -91,6 +92,8 @@ void read_simulated_timings_from_usb() { if (!strlen(buffer)) continue; + dbgf("==READ LINE [%s]", buffer); + char *p = buffer; while (*p != ',' && *p != '\0') ++p; @@ -140,10 +143,10 @@ void output_decoder(Decoder *pdec) { #endif const char *encoding_names[] = { - "RFMOD_TRIBIT", // T - "RFMOD_TRIBIT_INVERTED", // N - "RFMOD_MANCHESTER", // M - "" // Anything else + "R", // T + "R", // N + "R", // M + "<" // Anything else }; const char *id_letter_to_encoding_name(char c) { diff --git a/extras/testplan/simul/simul.sh b/extras/testplan/simul/simul.sh index 2ff04f8..e5dd371 100755 --- a/extras/testplan/simul/simul.sh +++ b/extras/testplan/simul/simul.sh @@ -24,7 +24,7 @@ if [ "${INP_FILE}" == "" ]; then exit 1 fi -./am2 -t 3 simul.ino -u +./am2 -R -t 4 simul.ino -u stty -F "${PORT}" -hupcl -echo 115200 cat "${PORT}" & diff --git a/extras/testplan/simul/spiriapbergeron-20231121.txt b/extras/testplan/simul/spiriapbergeron-20231121.txt new file mode 100644 index 0000000..6ea27c0 --- /dev/null +++ b/extras/testplan/simul/spiriapbergeron-20231121.txt @@ -0,0 +1,104 @@ +0,183488 +7600, 2508 +7272, 1084 +684, 320 +300, 724 +300, 724 +676, 332 +688, 344 +292, 716 +684, 340 +676, 352 +676, 352 +676, 352 +288, 740 +676, 352 +676, 352 +288, 740 +668, 348 +292, 744 +292, 744 +280, 736 +288, 736 +292, 732 +292, 736 +668, 356 +668, 356 +284, 752 +284, 748 +280, 744 +280, 748 +676, 352 +280, 744 +284, 748 +284, 744 +280, 744 +284, 748 +672, 364 +272, 744 +676, 360 +280, 740 +280, 752 +280, 748 +664, 372 +276, 748 +664, 360 +668, 364 +664, 368 +272, 756 +660, 368 +272, 756 +660, 380 +272, 716 +7232, 1112 +660, 368 +272, 756 +264, 764 +656, 368 +656, 372 +260, 764 +656, 368 +656, 380 +648, 384 +652, 372 +260, 764 +656, 372 +644, 380 +260, 772 +648, 380 +256, 780 +256, 768 +256, 760 +268, 756 +268, 760 +268, 756 +660, 376 +648, 376 +264, 764 +272, 756 +272, 756 +264, 764 +648, 376 +264, 768 +264, 764 +260, 764 +264, 776 +260, 768 +648, 380 +260, 764 +644, 384 +264, 764 +260, 764 +252, 764 +656, 380 +260, 764 +656, 372 +656, 372 +656, 380 +256, 760 +656, 384 +256, 760 +656, 380 +268, 728 +7212, 1120 +640, 388 diff --git a/extras/testplan/test/am2 b/extras/testplan/test/am2 deleted file mode 120000 index 3149a53..0000000 --- a/extras/testplan/test/am2 +++ /dev/null @@ -1 +0,0 @@ -../../../am2 \ No newline at end of file diff --git a/extras/testplan/test/am2 b/extras/testplan/test/am2 new file mode 100755 index 0000000..ef045ff --- /dev/null +++ b/extras/testplan/test/am2 @@ -0,0 +1,346 @@ +#!/usr/bin/bash + +# am2 + +# Copyright 2019, 2020, 2021, 2022 Sébastien Millet + +# Can perform the following: +# 1. Compile the code +# 2. Upload to Arduino +# 3. Read (continually) what is arriving from the USB port the Arduino is +# connected to + +# Versions history (as of 1.3) +# 1.3 Output from Arduino is recorded in files named with numbers instead of +# date-time string. +# 1.4 Adds -t (--testplan) option, to set TESTPLAN macro +# 1.5 -t (or --testplan) now comes with a value, so as to manage multiple test +# plans. +# 1.6 Updated to work fine with Arch arduino package instead of the manually +# installed (from tar.gz source) package used so far. +# 1.7 Renames archlinux-arduino back to arduino, and created corresponding +# symlink (was cleaner to do s). +# 2.0 Replaces arduino-builder with arduino-cli +# 2.1 Output warning if ARDUINO_USER_LIBS environment variable is not set + +set -euo pipefail + +VERSION=2.1 + +PORT= +BOARD= +SPEED= +FQBN= +BUILDDIR= +RECORDDIR=out +READSPEED= +RECORDFILE= + +UPLOAD="no" +REMOVE="no" +VERBOSE="no" +CATUSB="no" +STTY="no" +RECORDUSB="no" +COMPILE="yes" +TESTPLAN= +NOCOLOR= + +DISPLAYSEP=no + +function finish { + if [ "${DISPLAYSEP}" == "yes" ]; then + echo "-----END ARDUINO OUTPUT-----" | tee -a "${RECORDFILE}" + fi +} + +trap finish EXIT + +function usage { + echo "Usage:" + echo " am [OPTIONS...] FILE" + echo "Compile FILE using arduino-builder." + echo "Example: am sketch.ino" + echo "" + echo "ENVIRONMENT VARIABLES" + echo " If ARDUINO_USER_LIBS is defined and non empty, then arduino-builder" + echo " is called with the supplementary option -libraries followed by" + echo " ARDUINO_USER_LIBS' value." + echo "" + echo "OPTIONS" + echo " -h --help Display this help screen" + echo " -V --version Output version information and quit" + echo " -v --verbose Be more talkative" + echo " -u --upload Upload compiled code into Arduino" + echo " -R --remove Remove /tmp/arduino-core-cache and ./buid" + echo " -b --board Board, either 'uno' or 'nano'" + echo " -p --port Port, for ex. '/dev/ttyUSB0'" + echo " -s --speed Upload speed, for ex. 115200" + echo " Normally, speed is infered from device type:" + echo " 115200 for Uno, 57600 for Nano" + echo " -B --fqbn Board Fully Qualified Name, like 'arduino:avr:uno'" + echo " -d --builddir Build directory" + echo " -c --catusb Display (continually) what Arduino writes on USB" + echo " --stty Tune stty properly for later communication (implied" + echo " by --catusb)" + echo " -r --recordusb Write USB (continually) to a file (implies -c)" + echo " --recordfile Output file if -r option is set" + echo " -n --nocompile Don't compile code" + echo " --readspeed Read speed of USB. If not specified, this script" + echo " will try to infere it from source file. If it" + echo " fails, it'll fallback to 9600." + echo " This option is useful only if USB is read" + echo " (-c or --stty option set)" + echo " -t --testplan Set TESTPLAN macro value" + echo " (as if #define TESTPLAN VALUE)" + echo " --no-color Pass on the --no-color option to arduino-cli" + exit 1 +} + +function version { + echo "am version ${VERSION}" + exit +} + +OPTS=$(getopt -o hVvuRb:p:s:B:d:crnt: --long help,version,verbose,upload,remove,board:,port:,speed:,fqbn:,builddir:,catusb,stty,no-color,recordusb,nocompile,readspeed:,recordfile:,testplan: -n 'am' -- "$@") + +eval set -- "$OPTS" + +while true; do + case "$1" in + -h | --help ) usage; shift ;; + -V | --version ) version; shift ;; + -v | --verbose ) VERBOSE="yes"; shift ;; + -u | --upload ) UPLOAD="yes"; shift ;; + -R | --remove ) REMOVE="yes"; shift ;; + -b | --board ) BOARD="$2"; shift 2 ;; + -p | --port ) PORT="$2"; shift 2 ;; + -s | --speed ) SPEED="$2"; shift 2 ;; + -B | --fqbn ) FQBN="$2"; shift 2 ;; + -d | --builddir ) BUILDDIR="$2"; shift 2 ;; + -c | --catusb ) CATUSB="yes"; shift ;; + -r | --recordusb ) RECORDUSB="yes"; CATUSB="yes"; shift ;; + -n | --nocompile ) COMPILE="no"; shift ;; + --readspeed ) READSPEED="$2"; shift 2 ;; + --recordfile ) RECORDFILE="$2"; shift 2 ;; + --stty ) STTY="yes"; shift ;; + --no-color ) NOCOLOR="--no-color"; shift ;; + -t | --testplan ) TESTPLAN="$2"; shift 2 ;; + -- ) shift; break ;; + * ) break ;; + esac +done + +FILE=${1:-} +TRAILINGOPTS=${2:-} + +if [ -n "${TRAILINGOPTS}" ]; then + echo "Error: trailing options" + exit 1; +fi +if [ -z "${FILE}" ]; then + echo "Error: no input file" + exit 1; +fi + +set +e + +if [ -n "${BOARD}" ]; then + if [ "${BOARD}" != "uno" ] && [ "${BOARD}" != "nano" ]; then + echo "Error: board '${BOARD}' unknown" + exit 1 + fi +fi + +ARDUINODIR=/usr/share/arduino + +COUNTUNO=$(compgen -G '/dev/ttyACM*' | wc -l) +COUNTNANO=$(compgen -G '/dev/ttyUSB*' | wc -l) + +if [ -z "${BOARD}" ]; then + if [ "${COUNTUNO}" -ge 1 ] && [ "${COUNTNANO}" -ge 1 ]; then + echo "Error: cannot guess board, found ${COUNTUNO} uno(s), ${COUNTNANO} nano(s)" + exit 10 + fi + if [ "${COUNTUNO}" -ge 1 ]; then + BOARD=uno + elif [ "${COUNTNANO}" -ge 1 ]; then + BOARD=nano + fi + if [ -z "${BOARD}" ]; then + echo "Error: cannot guess board, none found"; + exit 10 + fi +fi + +if [ "${UPLOAD}" == "yes" ] || [ "${CATUSB}" == "yes" ]; then + if [ -z "${PORT}" ]; then + if [ "${BOARD}" == "uno" ]; then + COUNT=${COUNTUNO} + PORT=$(compgen -G '/dev/ttyACM*') + elif [ "${BOARD}" == "nano" ]; then + COUNT=${COUNTNANO} + PORT=$(compgen -G '/dev/ttyUSB*') + else + echo "FATAL #001, CHECK THIS CODE" + exit 99 + fi + + if [ "${COUNT}" -ge 2 ]; then + echo "Error: cannot guess port, more than 1 board '${BOARD}' found" + exit 10 + fi + if [ -z "${PORT}" ]; then + echo "Error: cannot guess port, none found" + exit 10 + fi + fi + + if [ -z "${SPEED}" ]; then + if [ "${BOARD}" == "uno" ]; then + SPEED=115200 + elif [ "${BOARD}" == "nano" ]; then + SPEED=57600 + else + echo "FATAL #002, CHECK THIS CODE" + exit 99 + fi + fi + + if [ ! -e "${PORT}" ]; then + echo "Error: port not found" + exit 10 + fi +fi + +if [ -z "${FQBN}" ]; then + if [ "${BOARD}" == "uno" ]; then + FQBN="arduino:avr:uno" + elif [ "${BOARD}" == "nano" ]; then + FQBN="arduino:avr:nano:cpu=atmega328old" + else + echo "FATAL #003, CHECK THIS CODE" + exit 99 + fi +fi + +if [ -z "${BUILDDIR}" ]; then + if [[ "${FILE}" == */* ]]; then + BUILDDIR=${FILE%/*} + BUILDDIR="${BUILDDIR%/}/build" + else + BUILDDIR=build + fi +fi + +if [ "${RECORDUSB}" == "yes" ]; then + if [ -z "${RECORDFILE}" ]; then + V=${FILE##*/} + V=${V%.*} + V=${V:-out} + PREV= + for i in {15..00}; do + F="${RECORDDIR}/${V}-$i.txt" + if [ -e "${F}" ] && [ -n "${PREV}" ]; then + mv "${F}" "${PREV}" + fi + PREV="${F}" + done + RECORDFILE="${F}" + mkdir -p "${RECORDDIR}" + fi +else + RECORDFILE="/dev/null" +fi + +if [ "${VERBOSE}" == "yes" ]; then + echo "-- Settings" + echo "Arduino dir: ${ARDUINODIR}" + echo "Board: ${BOARD}" + echo "Port: ${PORT}" + echo "Speed: ${SPEED}" + echo "Fqbn: ${FQBN}" + echo "Upload: ${UPLOAD}" + echo "Remove: ${REMOVE}" + echo "Catusb: ${CATUSB}" + echo "Recordusb: ${RECORDUSB}" + echo "Record file: ${RECORDFILE}" + echo "Verbose: ${VERBOSE}" + echo "File: ${FILE}" + echo "Build dir: ${BUILDDIR}" +fi + +set -e + +if [ "${REMOVE}" == "yes" ]; then + rm -rf /tmp/arduino-core-cache +fi + +if [ "${COMPILE}" == "yes" ]; then + echo "-- Compile" + + mkdir -p "${BUILDDIR}" + + OPT_LIB= + TMP_ULIB=${ARDUINO_USER_LIBS:-} + if [ -n "${TMP_ULIB}" ]; then + OPT_LIB="--libraries ""${TMP_ULIB}""" + else + echo + echo "***********************************************************" + echo "* WARNING *" + echo "* The environment variable ARDUINO_USER_LIBS is not set *" + echo "***********************************************************" + echo + fi + + TESTPLAN_OPT="" + if [ -n "${TESTPLAN}" ]; then + TESTPLAN_OPT="--build-property build.extra_flags=-DRF433ANY_TESTPLAN=${TESTPLAN}" + fi + + # shellcheck disable=SC2086 + # (We don't want to quote OPT_LIB as it can contain multiple options.) + arduino-cli compile -b "${FQBN}" ${NOCOLOR} --build-path "${BUILDDIR}" ${OPT_LIB} ${TESTPLAN_OPT} "${FILE}" +fi + +FILEBASENAME=${FILE##*/} + +if [ "${UPLOAD}" == "yes" ]; then + echo "-- Upload" + arduino-cli upload -v -b "${FQBN}" --input-dir "${BUILDDIR}" -p "${PORT}" +fi + +if [ "${CATUSB}" == "yes" ] || [ "${STTY}" == "yes" ]; then + if [ -z "${READSPEED}" ]; then + TFILE=$(mktemp) + gcc -fpreprocessed -dD -x c++ -E "${FILE}" > "${TFILE}" + for sp in 9600 19200 28800 38400 57600 115200; do + if grep ${sp} "${TFILE}" > /dev/null; then + READSPEED=${sp} + fi + done + READSPEED=${READSPEED:-9600} + rm "${TFILE}" + fi + + stty -F "${PORT}" -hupcl -echo "${READSPEED}" + echo "-- usb setup with speed ${READSPEED}" +fi + +if [ "${CATUSB}" == "yes" ]; then + echo "-- Read usb (Ctrl-C to quit)" + DISPLAYSEP=yes + { + echo "speed=${READSPEED}" + echo "fqbn=${FQBN}" + echo "port=${PORT}" + echo "file=${FILE}" + echo "filedate=$(date +"%Y-%m-%dT%H:%M:%SZ" -d @$(stat -c '%Y' "${FILE}"))" + echo "date=$(date +'%Y-%m-%dT%H:%M:%SZ')" + echo "" + echo "-----BEGIN ARDUINO OUTPUT-----" + } | tee "${RECORDFILE}" + tee -a "${RECORDFILE}" < "${PORT}" +fi + diff --git a/extras/testplan/test/test.ino b/extras/testplan/test/test.ino index 1d3b1fa..2867869 100644 --- a/extras/testplan/test/test.ino +++ b/extras/testplan/test/test.ino @@ -138,6 +138,51 @@ void output_decoder(Decoder *pdec) { } #endif +const char *encoding_names[] = { + "RFMOD_TRIBIT", // T + "RFMOD_TRIBIT_INVERTED", // N + "RFMOD_MANCHESTER", // M + "" // Anything else +}; + +const char *id_letter_to_encoding_name(char c) { + if (c == 'T') + return encoding_names[0]; + else if (c == 'N') + return encoding_names[1]; + else if (c == 'M') + return encoding_names[2]; + + return encoding_names[3]; +} + +void output_timings(Decoder *pdec, byte nb_bits) { + TimingsExt tsext; + if (!pdec) + return; + pdec->get_tsext(&tsext); + + const char *enc_name = id_letter_to_encoding_name(pdec->get_id_letter()); + + dbg("\n-----CODE START-----"); + dbg("// [WRITE THE DEVICE NAME HERE]\n" + "rf.register_Receiver("); + dbgf("\t%s, // mod", enc_name); + dbgf("\t%u, // initseq", tsext.initseq); + dbgf("\t%u, // lo_prefix", tsext.first_low); + dbgf("\t%u, // hi_prefix", tsext.first_high); + dbgf("\t%u, // first_lo_ign", tsext.first_low_ignored); + dbgf("\t%u, // lo_short", tsext.low_short); + dbgf("\t%u, // lo_long", tsext.low_long); + dbgf("\t%u, // hi_short (0 => take lo_short)", tsext.high_short); + dbgf("\t%u, // hi_long (0 => take lo_long)", tsext.high_long); + dbgf("\t%u, // lo_last", tsext.last_low); + dbgf("\t%u, // sep", tsext.sep); + dbgf("\t%u // nb_bits", nb_bits); + dbg(");"); + dbg("-----CODE END-----\n"); +} + void loop() { if (sim_int_count >= sim_timings_count) read_simulated_timings_from_usb(); @@ -176,6 +221,10 @@ void loop() { if (pdec) { #ifdef RF433ANY_DBG_DECODER pdec->dbg_decoder(2); +// if (pdec) { +// int nb_bits = pdec->get_nb_bits(); +// output_timings(pdec, nb_bits); +// } #endif #if RF433ANY_TESTPLAN == 5 output_decoder(pdec); diff --git a/extras/testplan/tt.sh b/extras/testplan/tt.sh index 8bfea11..92fa42c 100755 --- a/extras/testplan/tt.sh +++ b/extras/testplan/tt.sh @@ -44,7 +44,7 @@ for ((i=START; i<=STOP; i++)); do echo "== ROUND $i" - testplan/test/am2 testplan/test/test.ino -u --stty -t "${i}" + testplan/test/am2 -R testplan/test/test.ino -u --stty -t "${i}" sleep 2