From b561fdaec9ea1a647cf66aec19fbf83237a2868c Mon Sep 17 00:00:00 2001 From: Takumi Sueda Date: Mon, 31 Jul 2023 18:34:50 +0900 Subject: [PATCH 1/2] buildroot: specify an override directory to add files into the buildroot rootfs --- buildroot | 2 +- os-buildroot/override/root/blink.sh | 74 +++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100755 os-buildroot/override/root/blink.sh diff --git a/buildroot b/buildroot index e39044f..cf3ea90 160000 --- a/buildroot +++ b/buildroot @@ -1 +1 @@ -Subproject commit e39044fa44394042e22f503fb53ba96122956bce +Subproject commit cf3ea901082f470449bd174272d8407e039522b2 diff --git a/os-buildroot/override/root/blink.sh b/os-buildroot/override/root/blink.sh new file mode 100755 index 0000000..00f125e --- /dev/null +++ b/os-buildroot/override/root/blink.sh @@ -0,0 +1,74 @@ +#!/bin/sh + +set -u + +if [ $# -ne 3 ]; then + echo "Usage: blink.sh RANGE_FROM RANGE_TO SLEEP" + exit 1 +fi + +if [ "$(id -u)" -ne "0" ]; then + echo "Error: please run as root" + exit 1 +fi + +FROM=$1 +TO=$2 +SLEEP=$3 +GPIOS=$(seq $FROM $TO) +AVAILABLE_GPIOS="" + +export_gpio() { + echo $1 > /sys/class/gpio/export +} + +set_direction() { + echo out > /sys/class/gpio/gpio$1/direction +} + +set_value() { + echo $2 > /sys/class/gpio/gpio$1/value +} + +for i in $GPIOS; do + if [ ! -e "/sys/class/gpio/gpio$i" ]; then + export_gpio $i 2>/dev/null + if [ $? -ne 0 ]; then + echo "Error: failed to export the pin $i" + continue + fi + fi + + set_direction $i 2>/dev/null + if [ $? -ne 0 ]; then + # Ignore the failure if the actual direction is out + if grep -vq "out" /dsys/class/gpio/gpio$i/direction; then + echo "Error: failed to set the direction of the pin $i to out" + continue + fi + fi + + AVAILABLE_GPIOS="$AVAILABLE_GPIOS$i " +done + +echo "Available GPIOs: $AVAILABLE_GPIOS" + +while [ 1 ]; do + for i in $AVAILABLE_GPIOS; do + set_value $i 1 2>/dev/null + if [ $? -ne 0 ]; then + echo "Warning: failed to set the value of the pin $i to high" + fi + done + + sleep $SLEEP + + for i in $AVAILABLE_GPIOS; do + set_value $i 0 2>/dev/null + if [ $? -ne 0 ]; then + echo "Warning: failed to set the value of the pin $i to low" + fi + done + + sleep $SLEEP +done From ce8d780b336d4ac532ab47c471e93171e3d45d13 Mon Sep 17 00:00:00 2001 From: Takumi Sueda Date: Mon, 31 Jul 2023 22:09:11 +0900 Subject: [PATCH 2/2] Enhance blink.sh --- os-buildroot/override/root/blink.sh | 54 ++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 8 deletions(-) diff --git a/os-buildroot/override/root/blink.sh b/os-buildroot/override/root/blink.sh index 00f125e..4435b53 100755 --- a/os-buildroot/override/root/blink.sh +++ b/os-buildroot/override/root/blink.sh @@ -1,10 +1,51 @@ #!/bin/sh - set -u -if [ $# -ne 3 ]; then - echo "Usage: blink.sh RANGE_FROM RANGE_TO SLEEP" - exit 1 +VERBOSE=0 +PIN="" +SLEEP=1 +GPIOS="" + +while getopts "hvr:p:s:" OPT; do + case "$OPT" in + h) + echo "Usage: blink.sh [-hv] [-r PIN_RANGE_FROM-PIN_RANGE_TO] [-p PIN] [-s SLEEP_SEC]" + echo "Example: blink.sh -r 0-10 -p 12" + echo " (blink from GPIO 0 to 10 and 12)" + exit 0 + ;; + v) + VERBOSE=1 + ;; + r) + RE='^([0-9]+)-([0-9]+)$' + if echo $OPTARG | grep -qvE $RE; then + echo "Error: invalid range: $OPTARG" + exit 1 + fi + FROM=$(echo $OPTARG | sed -E "s/$RE/\\1/") + TO=$(echo $OPTARG | sed -E "s/$RE/\\2/") + GPIOS="$GPIOS$(seq -s " " $FROM $TO) " + ;; + p) + if echo $OPTARG | grep -qvE "^[0-9]+$"; then + echo "Error: invalid pin number: $OPTARG" + exit 1 + fi + GPIOS="$GPIOS$OPTARG " + ;; + s) + if echo $OPTARG | grep -qvE "^[0-9]+$"; then + echo "Error: invalid sleep duration: $OPTARG" + exit 1 + fi + SLEEP=$OPTARG + ;; + esac +done + +if [ $VERBOSE -eq 1 ]; then + echo "Pins to iterate over: $GPIOS" fi if [ "$(id -u)" -ne "0" ]; then @@ -12,10 +53,6 @@ if [ "$(id -u)" -ne "0" ]; then exit 1 fi -FROM=$1 -TO=$2 -SLEEP=$3 -GPIOS=$(seq $FROM $TO) AVAILABLE_GPIOS="" export_gpio() { @@ -72,3 +109,4 @@ while [ 1 ]; do sleep $SLEEP done +