-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_project.sh
executable file
·163 lines (145 loc) · 3.93 KB
/
build_project.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#! /bin/bash
PARENT_DIR="$(pwd)"
#Add a path to all libraries
MYSENSORS_LIB="libraries/mysensors_github/libraries/MySensors"
DHT_LIB="libraries/mysensors_github/libraries/DHT"
#DHT_LIB="libraries/arduino_github/libraries/DHTlib"
function CHECK_DEPENDENCIES {
if ! which platformio >/dev/null; then
echo "Install platfromio!"
echo "http://platformio.org/#!/get-started"
exit
fi
if [ -n "$DISPLAY" ]; then
if which arduino > /dev/null; then
ARDUINO=$(which arduino)
else
echo "Install arduino"
exit
fi
else
if which arduino-headless > /dev/null; then
ARDUINO=$(which arduino-headless)
else
echo "Install arduino-headless"
echo "Create arduino-headless and copy:"
echo "
#!/bin/bash
Xvfb :1 -nolisten tcp -screen :1 1280x800x24 &
xvfb=\"$!\"
DISPLAY=:1 arduino \$@
kill -9 \$xvfb
"
echo "and make arduino-headless runnable (/usr/local/bin/arduino-headless)"
exit
fi
fi
}
###
# List all nodes/gateways that are possible to build
###
function LIST_DIR {
local i=0
dirs=( $(find . -maxdepth 1 -type d -printf '%P\n') )
for dir in "${dirs[@]}"; do
if [ "${dir}" == ".git" ]; then
unset dirs[$i]
elif [ "${dir}" == "libraries" ]; then
unset dirs[$i]
fi
i=$i+1
done
dirs=("${dirs[@]}" "Quit")
echo "Which project do you want to build? "
select dir in "${dirs[@]}"; do
if [ "${dir}" == "Quit" ]; then
echo "Quit"
break
elif [[ $REPLY -gt 0 && $REPLY < ${#dirs[@]} ]] ; then
echo "${dir} selected"
BUILD_WITH_PLATFORMIO "${dir}"
#BUILD_WITH_ARDUINO "${dir}"
LIST_DIR
else
echo "Select between 0 and ${#dirs[@]}"
fi
done
}
###
# Use the the arduino cli for building.
# TODO: Add interface for build with differnet boards and 8Mhz/16Mhz
###
BUILD_WITH_ARDUINO() {
LINK_HEADER "${dir}" "arduino"
${ARDUINO} --upload -v --board arduino:avr:pro --pref build.f_cpu=8000000 --pref build.mcu=atmega328p --pref compiler.warning_level=all --pref sketchbook.path="${PWD}/${dir}" "${PWD}/${dir}/${dir}.ino"
}
###
# TODO: Platformio dosen't work with MySensors lib.
###
BUILD_WITH_PLATFORMIO() {
LINK_HEADER "${dir}" "platformio"
(cd "${1}" && platformio run --target clean)
(cd "${1}" && platformio run)
if [ "$?" -ne "0" ]; then
echo "Compiliing failed "
echo "Exiting"
exit
fi
read -r -p "Want to upload? [Y/n]" response
response=${response,,} # tolower
if [ "${response}" = "" ]; then
response='yes'
fi
if [[ $response =~ ^(yes|y| ) ]]; then
echo "Upload"
(cd "${1}" && platformio run --target upload)
else
echo "Not upload"
fi
}
###
# Checks which includes the sketch wants and creates a symlink to
# the lib
# TODO: Platformio dosen't work with MySensors lib.
###
LINK_HEADER() {
cd "${1}"
echo "Link_header "
# Scan the ino file for #include <lib.h>
if [ "${2}" = "arduino" ]; then
local header_files=($(grep "include" ./*.ino | sed 's/\(#include <\)\(.*\)\(.h>\)/\2/'))
lib_dir=libraries
else
echo "platformio"
local header_files=($(grep "include" src/*.ino | sed 's/\(#include <\)\(.*\)\(.h>\)/\2/'))
lib_dir=lib
fi
if [ ! -d "$lib_dir" ]; then
mkdir "$lib_dir"
else
rm -r ${lib_dir}/*
fi
for file in "${header_files[@]}"; do
case $file in
"MySensor")
# Fix path to mysensors lib in platformio.ini
if [ "${2}" = "platformio" ]; then
sed -i "s:\(build_flags\s*=\)\(.*\):\1 \-I$PARENT_DIR/$MYSENSORS_LIB:" platformio.ini
cat platformio.ini
elif [[ ! -d "$lib_dir/${file}" ]]; then
ln -s "${PARENT_DIR}/${MYSENSORS_LIB}" "$lib_dir/${file}"
fi
;;
"DHT")
if [[ ! -d "$lib_dir/${file}" ]]; then
ln -s "${PARENT_DIR}/${DHT_LIB}" "$lib_dir/${file}"
fi
;;
*)
echo "Nothing ${file}"
esac
done
cd "${PARENT_DIR}"
}
CHECK_DEPENDENCIES
LIST_DIR