diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..fad9c10 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,27 @@ +# Copyright 2025 Enactic, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# https://editorconfig.org/ + +root = true + +[*] +charset = utf-8 +insert_final_newline = true +spelling_language = en +trim_trailing_whitespace = true + +[*.sh] +indent_size = 4 +indent_style = space diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 84f6438..935026b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -39,4 +39,22 @@ repos: hooks: - id: meson-fmt alias: python - args: ['--inplace'] + args: ["--inplace"] + exclude: ".editorconfig$" + - repo: https://github.com/koalaman/shellcheck-precommit + rev: v0.10.0 + hooks: + - id: shellcheck + alias: shell + - repo: https://github.com/scop/pre-commit-shfmt + # v3.11.0-1 or later requires pre-commit 3.2.0 or later but Ubuntu + # 22.04 ships pre-commit 2.17.0. We can update this rev after + # Ubuntu 22.04 reached EOL (June 2027). + rev: v3.10.0-1 + hooks: + - id: shfmt + alias: shell + args: + # The default args is "--write --simplify" but we don't use + # "--simplify". Because it's conflicted will ShellCheck. + - "--write" diff --git a/python/build.sh b/python/build.sh index 018fffe..6af191c 100755 --- a/python/build.sh +++ b/python/build.sh @@ -16,7 +16,7 @@ # # Build script for OpenArm Python bindings -set -e +set -eu # Get script directory SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" @@ -36,11 +36,13 @@ fi # Build the C++ library first if needed if [ ! -d "$PROJECT_ROOT/build" ]; then echo "Building C++ library..." - mkdir -p "$PROJECT_ROOT/build" - cd "$PROJECT_ROOT/build" - cmake .. - make -j$(nproc) - cd "$SCRIPT_DIR" + cmake \ + -S "$PROJECT_ROOT" \ + -B "$PROJECT_ROOT/build" \ + -DCMAKE_BUILD_TYPE=Debug \ + -GNinja + cmake --build "$PROJECT_ROOT/build" + cmake --install "$PROJECT_ROOT/build" fi # Install in development mode diff --git a/setup/configure_socketcan.sh b/setup/configure_socketcan.sh index ab22770..4d94734 100755 --- a/setup/configure_socketcan.sh +++ b/setup/configure_socketcan.sh @@ -14,6 +14,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +set -eu + # Simple CAN Interface Setup Script # Usage: script/configure_socketcan.sh [options] @@ -52,32 +54,32 @@ shift # Parse options while [[ $# -gt 0 ]]; do case $1 in - -fd) - FD_MODE=true - shift - ;; - -b) - BITRATE="$2" - shift 2 - ;; - -d) - DBITRATE="$2" - shift 2 - ;; - -h) - usage - exit 0 - ;; - *) - echo "Error: Unknown option '$1'" - usage - exit 1 - ;; + -fd) + FD_MODE=true + shift + ;; + -b) + BITRATE="$2" + shift 2 + ;; + -d) + DBITRATE="$2" + shift 2 + ;; + -h) + usage + exit 0 + ;; + *) + echo "Error: Unknown option '$1'" + usage + exit 1 + ;; esac done # Check if interface exists -if ! ip link show $CAN_IF &>/dev/null; then +if ! ip link show "$CAN_IF" &>/dev/null; then echo "Error: CAN interface '$CAN_IF' not found" echo "Available interfaces:" ip link show | grep -E "can[0-9]" | cut -d: -f2 | tr -d ' ' || echo " No CAN interfaces found" @@ -87,26 +89,26 @@ fi # Configure CAN interface echo "Configuring $CAN_IF..." -if ! sudo ip link set $CAN_IF down; then +if ! sudo ip link set "$CAN_IF" down; then echo "Error: Failed to bring down $CAN_IF" exit 1 fi if [ "$FD_MODE" = true ]; then - if ! sudo ip link set $CAN_IF type can bitrate $BITRATE dbitrate $DBITRATE fd on; then + if ! sudo ip link set "$CAN_IF" type can bitrate "$BITRATE" dbitrate "$DBITRATE" fd on; then echo "Error: Failed to configure CAN FD mode" exit 1 fi echo "$CAN_IF is now set to CAN FD mode (${BITRATE} bps / ${DBITRATE} bps)" else - if ! sudo ip link set $CAN_IF type can bitrate $BITRATE; then + if ! sudo ip link set "$CAN_IF" type can bitrate "$BITRATE"; then echo "Error: Failed to configure CAN 2.0 mode" exit 1 fi echo "$CAN_IF is now set to CAN 2.0 mode (${BITRATE} bps)" fi -if ! sudo ip link set $CAN_IF up; then +if ! sudo ip link set "$CAN_IF" up; then echo "Error: Failed to bring up $CAN_IF" exit 1 fi diff --git a/setup/set_zero.sh b/setup/set_zero.sh index f35f5df..bc2983f 100755 --- a/setup/set_zero.sh +++ b/setup/set_zero.sh @@ -14,6 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +set -eu # CAN Interface Script # Usage: scripts/set_zero.sh [CAN_ID] [-all] @@ -42,7 +43,8 @@ check_can_interface() { fi # Check if interface is up - local state=$(ip link show "$interface" | grep -o "state [A-Z]*" | cut -d' ' -f2) + local state + state=$(ip link show "$interface" | grep -o "state [A-Z]*" | cut -d' ' -f2) if [ "$state" != "UP" ]; then echo "Error: CAN interface $interface is not UP (current state: $state)" return 1 @@ -52,14 +54,16 @@ check_can_interface() { # Try to get baudrate information if command -v ethtool &>/dev/null; then - local baudrate=$(ethtool "$interface" 2>/dev/null | grep -i speed | cut -d: -f2 | tr -d ' ') + local baudrate + baudrate=$(ethtool "$interface" 2>/dev/null | grep -i speed | cut -d: -f2 | tr -d ' ') if [ -n "$baudrate" ]; then echo "Baudrate: $baudrate" fi fi # Alternative method using ip command - local bitrate=$(ip -details link show "$interface" 2>/dev/null | grep -o "bitrate [0-9]*" | cut -d' ' -f2) + local bitrate + bitrate=$(ip -details link show "$interface" 2>/dev/null | grep -o "bitrate [0-9]*" | cut -d' ' -f2) if [ -n "$bitrate" ]; then echo "Bitrate: ${bitrate} bps" fi @@ -146,7 +150,8 @@ main() { echo "Sending set zero messages to all motor with CAN IDs from 001 to 008" echo "==========================================" for i in {1..8}; do - local padded_id=$(printf "%03d" $i) + local padded_id + padded_id=$(printf "%03d" "$i") send_can_messages "$padded_id" "$CAN_IF" done else @@ -157,4 +162,4 @@ main() { } # Run main function with all arguments -main "$@" \ No newline at end of file +main "$@"