Skip to content

Commit 6bd2230

Browse files
committed
Migrate to new API. All working
1 parent b99e323 commit 6bd2230

File tree

10 files changed

+2652
-802
lines changed

10 files changed

+2652
-802
lines changed

compile_examples.sh

Lines changed: 359 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,359 @@
1+
#!/bin/bash
2+
3+
# AFE4490 Arduino Library - Example Compilation Script
4+
# This script compiles all examples for multiple Arduino platforms
5+
# Usage: ./compile_examples.sh [platform]
6+
# Example: ./compile_examples.sh all (compiles for all platforms)
7+
# ./compile_examples.sh uno (compiles for Arduino Uno only)
8+
9+
set -e # Exit on any error
10+
11+
# Colors for output
12+
RED='\033[0;31m'
13+
GREEN='\033[0;32m'
14+
YELLOW='\033[1;33m'
15+
BLUE='\033[0;34m'
16+
NC='\033[0m' # No Color
17+
18+
# Configuration
19+
LIBRARY_PATH="."
20+
EXAMPLES_DIR="examples"
21+
LIBRARY_NAME="protocentral-afe4490-arduino"
22+
23+
# Supported platforms and their FQBNs
24+
declare -A PLATFORMS=(
25+
["uno"]="arduino:avr:uno"
26+
["nano"]="arduino:avr:nano"
27+
["mega"]="arduino:avr:mega"
28+
["leonardo"]="arduino:avr:leonardo"
29+
["esp32"]="esp32:esp32:esp32"
30+
["esp32s3"]="esp32:esp32:esp32s3"
31+
)
32+
33+
# Platform descriptions for better output
34+
declare -A PLATFORM_NAMES=(
35+
["uno"]="Arduino Uno"
36+
["nano"]="Arduino Nano"
37+
["mega"]="Arduino Mega"
38+
["leonardo"]="Arduino Leonardo"
39+
["esp32"]="ESP32 Dev Module"
40+
["esp32s3"]="ESP32-S3 Dev Module"
41+
)
42+
43+
# Statistics tracking
44+
TOTAL_COMPILATIONS=0
45+
SUCCESSFUL_COMPILATIONS=0
46+
FAILED_COMPILATIONS=0
47+
declare -a COMPILATION_RESULTS=()
48+
declare -a FAILED_DETAILS=()
49+
50+
# Function to print colored output
51+
print_status() {
52+
local status=$1
53+
local message=$2
54+
case $status in
55+
"INFO")
56+
echo -e "${BLUE}[INFO]${NC} $message"
57+
;;
58+
"SUCCESS")
59+
echo -e "${GREEN}[SUCCESS]${NC} $message"
60+
;;
61+
"ERROR")
62+
echo -e "${RED}[ERROR]${NC} $message"
63+
;;
64+
"WARNING")
65+
echo -e "${YELLOW}[WARNING]${NC} $message"
66+
;;
67+
esac
68+
}
69+
70+
# Function to print section headers
71+
print_header() {
72+
echo
73+
echo -e "${BLUE}============================================================${NC}"
74+
echo -e "${BLUE} $1${NC}"
75+
echo -e "${BLUE}============================================================${NC}"
76+
}
77+
78+
# Function to check if arduino-cli is installed
79+
check_arduino_cli() {
80+
if ! command -v arduino-cli &> /dev/null; then
81+
print_status "ERROR" "arduino-cli is not installed or not in PATH"
82+
print_status "INFO" "Please install arduino-cli from: https://arduino.github.io/arduino-cli/"
83+
exit 1
84+
fi
85+
86+
local version=$(arduino-cli version | head -n1)
87+
print_status "INFO" "Found $version"
88+
}
89+
90+
# Function to check if required cores are installed
91+
check_cores() {
92+
print_status "INFO" "Checking installed Arduino cores..."
93+
94+
local cores_output=$(arduino-cli core list)
95+
96+
if [[ $cores_output == *"arduino:avr"* ]]; then
97+
print_status "SUCCESS" "Arduino AVR core is installed"
98+
else
99+
print_status "WARNING" "Arduino AVR core not found. Installing..."
100+
arduino-cli core install arduino:avr
101+
fi
102+
103+
if [[ $cores_output == *"esp32:esp32"* ]]; then
104+
print_status "SUCCESS" "ESP32 core is installed"
105+
else
106+
print_status "WARNING" "ESP32 core not found. You may need to install it manually for ESP32 compilation"
107+
print_status "INFO" "To install ESP32 core: arduino-cli core install esp32:esp32"
108+
fi
109+
}
110+
111+
# Function to find all example sketches
112+
find_examples() {
113+
local examples=()
114+
while IFS= read -r -d '' file; do
115+
examples+=("$file")
116+
done < <(find "$EXAMPLES_DIR" -name "*.ino" -print0 2>/dev/null)
117+
118+
if [ ${#examples[@]} -eq 0 ]; then
119+
print_status "ERROR" "No example sketches found in $EXAMPLES_DIR"
120+
exit 1
121+
fi
122+
123+
print_status "INFO" "Found ${#examples[@]} example(s):"
124+
for example in "${examples[@]}"; do
125+
local example_name=$(basename "$example" .ino)
126+
print_status "INFO" " - $example_name"
127+
done
128+
129+
echo "${examples[@]}"
130+
}
131+
132+
# Function to get memory usage from compilation output
133+
extract_memory_usage() {
134+
local output="$1"
135+
local flash_usage=""
136+
local ram_usage=""
137+
138+
# Extract flash usage
139+
if [[ $output =~ Sketch\ uses\ ([0-9]+)\ bytes\ \(([0-9]+)%\)\ of\ program\ storage ]]; then
140+
flash_usage="${BASH_REMATCH[1]} bytes (${BASH_REMATCH[2]}%)"
141+
fi
142+
143+
# Extract RAM usage
144+
if [[ $output =~ Global\ variables\ use\ ([0-9]+)\ bytes\ \(([0-9]+)%\)\ of\ dynamic\ memory ]]; then
145+
ram_usage="${BASH_REMATCH[1]} bytes (${BASH_REMATCH[2]}%)"
146+
fi
147+
148+
if [[ -n "$flash_usage" && -n "$ram_usage" ]]; then
149+
echo "Flash: $flash_usage, RAM: $ram_usage"
150+
elif [[ -n "$flash_usage" ]]; then
151+
echo "Flash: $flash_usage"
152+
else
153+
echo "Memory usage not available"
154+
fi
155+
}
156+
157+
# Function to compile a single example for a single platform
158+
compile_example() {
159+
local example_path="$1"
160+
local platform_key="$2"
161+
local platform_fqbn="${PLATFORMS[$platform_key]}"
162+
local platform_name="${PLATFORM_NAMES[$platform_key]}"
163+
164+
local example_name=$(basename "$(dirname "$example_path")")
165+
166+
print_status "INFO" "Compiling $example_name for $platform_name..."
167+
168+
TOTAL_COMPILATIONS=$((TOTAL_COMPILATIONS + 1))
169+
170+
# Compile the example
171+
local compile_output
172+
local compile_result=0
173+
174+
compile_output=$(arduino-cli compile --fqbn "$platform_fqbn" "$example_path" --libraries "$LIBRARY_PATH" 2>&1) || compile_result=$?
175+
176+
if [ $compile_result -eq 0 ]; then
177+
SUCCESSFUL_COMPILATIONS=$((SUCCESSFUL_COMPILATIONS + 1))
178+
local memory_usage=$(extract_memory_usage "$compile_output")
179+
print_status "SUCCESS" "$example_name ✓ [$platform_name] - $memory_usage"
180+
COMPILATION_RESULTS+=("$example_name [$platform_name] - $memory_usage")
181+
else
182+
FAILED_COMPILATIONS=$((FAILED_COMPILATIONS + 1))
183+
print_status "ERROR" "$example_name ✗ [$platform_name] - Compilation failed"
184+
COMPILATION_RESULTS+=("$example_name [$platform_name] - FAILED")
185+
FAILED_DETAILS+=("=== $example_name [$platform_name] ===" "$compile_output" "")
186+
fi
187+
188+
return $compile_result
189+
}
190+
191+
# Function to compile all examples for specified platforms
192+
compile_examples() {
193+
local target_platforms=("$@")
194+
195+
if [ ${#target_platforms[@]} -eq 0 ]; then
196+
print_status "ERROR" "No platforms specified"
197+
return 1
198+
fi
199+
200+
# Find all examples
201+
local examples_array=($(find_examples))
202+
203+
print_header "COMPILING EXAMPLES"
204+
205+
# Compile each example for each platform
206+
local any_failed=0
207+
for example in "${examples_array[@]}"; do
208+
for platform in "${target_platforms[@]}"; do
209+
if [[ -z "${PLATFORMS[$platform]}" ]]; then
210+
print_status "WARNING" "Unknown platform: $platform (skipping)"
211+
continue
212+
fi
213+
214+
compile_example "$example" "$platform" || any_failed=1
215+
done
216+
echo # Add spacing between examples
217+
done
218+
219+
return $any_failed
220+
}
221+
222+
# Function to print compilation summary
223+
print_summary() {
224+
print_header "COMPILATION SUMMARY"
225+
226+
print_status "INFO" "Total compilations: $TOTAL_COMPILATIONS"
227+
print_status "SUCCESS" "Successful: $SUCCESSFUL_COMPILATIONS"
228+
229+
if [ $FAILED_COMPILATIONS -gt 0 ]; then
230+
print_status "ERROR" "Failed: $FAILED_COMPILATIONS"
231+
else
232+
print_status "SUCCESS" "Failed: $FAILED_COMPILATIONS"
233+
fi
234+
235+
echo
236+
print_status "INFO" "Detailed results:"
237+
for result in "${COMPILATION_RESULTS[@]}"; do
238+
if [[ $result ==* ]]; then
239+
echo -e " ${GREEN}$result${NC}"
240+
else
241+
echo -e " ${RED}$result${NC}"
242+
fi
243+
done
244+
245+
# Print failed compilation details if any
246+
if [ ${#FAILED_DETAILS[@]} -gt 0 ]; then
247+
echo
248+
print_header "FAILURE DETAILS"
249+
for detail in "${FAILED_DETAILS[@]}"; do
250+
echo "$detail"
251+
done
252+
fi
253+
254+
echo
255+
if [ $FAILED_COMPILATIONS -eq 0 ]; then
256+
print_status "SUCCESS" "All examples compiled successfully! 🎉"
257+
return 0
258+
else
259+
print_status "ERROR" "Some compilations failed. Please check the details above."
260+
return 1
261+
fi
262+
}
263+
264+
# Function to show usage
265+
show_usage() {
266+
echo "AFE4490 Arduino Library - Example Compilation Script"
267+
echo
268+
echo "Usage: $0 [platform1] [platform2] ..."
269+
echo " $0 all"
270+
echo " $0 avr"
271+
echo " $0 esp"
272+
echo
273+
echo "Available platforms:"
274+
for platform in "${!PLATFORM_NAMES[@]}"; do
275+
printf " %-10s - %s\n" "$platform" "${PLATFORM_NAMES[$platform]}"
276+
done
277+
echo
278+
echo "Platform groups:"
279+
echo " all - All supported platforms"
280+
echo " avr - All Arduino AVR platforms (uno, nano, mega, leonardo)"
281+
echo " esp - All ESP platforms (esp32, esp32s3)"
282+
echo
283+
echo "Examples:"
284+
echo " $0 uno # Compile for Arduino Uno only"
285+
echo " $0 uno esp32 # Compile for Arduino Uno and ESP32"
286+
echo " $0 avr # Compile for all AVR platforms"
287+
echo " $0 all # Compile for all platforms"
288+
}
289+
290+
# Main function
291+
main() {
292+
print_header "AFE4490 ARDUINO LIBRARY - EXAMPLE COMPILATION"
293+
294+
# Check if help is requested
295+
if [[ "$1" == "-h" || "$1" == "--help" || "$1" == "help" ]]; then
296+
show_usage
297+
exit 0
298+
fi
299+
300+
# Check prerequisites
301+
check_arduino_cli
302+
check_cores
303+
304+
# Determine target platforms
305+
local target_platforms=()
306+
307+
if [ $# -eq 0 ]; then
308+
print_status "INFO" "No platforms specified, using default: uno"
309+
target_platforms=("uno")
310+
else
311+
for arg in "$@"; do
312+
case "$arg" in
313+
"all")
314+
target_platforms=(${!PLATFORMS[@]})
315+
break
316+
;;
317+
"avr")
318+
target_platforms+=("uno" "nano" "mega" "leonardo")
319+
;;
320+
"esp")
321+
target_platforms+=("esp32" "esp32s3")
322+
;;
323+
*)
324+
if [[ -n "${PLATFORMS[$arg]}" ]]; then
325+
target_platforms+=("$arg")
326+
else
327+
print_status "ERROR" "Unknown platform or group: $arg"
328+
show_usage
329+
exit 1
330+
fi
331+
;;
332+
esac
333+
done
334+
fi
335+
336+
# Remove duplicates
337+
target_platforms=($(printf "%s\n" "${target_platforms[@]}" | sort -u))
338+
339+
print_status "INFO" "Target platforms: ${target_platforms[*]}"
340+
341+
# Change to library directory
342+
if [ ! -d "$EXAMPLES_DIR" ]; then
343+
print_status "ERROR" "Examples directory not found: $EXAMPLES_DIR"
344+
print_status "INFO" "Please run this script from the library root directory"
345+
exit 1
346+
fi
347+
348+
# Compile examples
349+
local compilation_result=0
350+
compile_examples "${target_platforms[@]}" || compilation_result=$?
351+
352+
# Print summary
353+
print_summary
354+
355+
exit $compilation_result
356+
}
357+
358+
# Run main function with all arguments
359+
main "$@"

0 commit comments

Comments
 (0)