-
Notifications
You must be signed in to change notification settings - Fork 10
/
po-util.sh
executable file
·2329 lines (2012 loc) · 57.4 KB
/
po-util.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# __ __ __
# / | / |/ |
# ______ ______ __ __ _██ |_ ██/ ██ |
# / \ / \ ______ / | / |/ ██ | / |██ |
# /██████ |/██████ |/ |██ | ██ |██████/ ██ |██ |
# ██ | ██ |██ | ██ |██████/ ██ | ██ | ██ | __ ██ |██ |
# ██ |__██ |██ \__██ | ██ \__██ | ██ |/ |██ |██ |
# ██ ██/ ██ ██/ ██ ██/ ██ ██/ ██ |██ |
# ███████/ ██████/ ██████/ ████/ ██/ ██/
# ██ |
# ██ |
# ██/ https://po-util.com
#
# po-util - The Ultimate Local Particle Experience for Linux and macOS
# Copyright (C) 2018 Nathan Robinson
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Helper functions
pause()
{
read -rp "$*"
}
print_logo()
{
if [ "$1" ];
then
TEXTLINE="$1"
else
TEXTLINE="https://po-util.com"
fi
LOGO=" __ __ __
/ | / |/ |
______ ______ __ __ _██ |_ ██/ ██ |
/ \ / \ ______ / | / |/ ██ | / |██ |
/██████ |/██████ |/ |██ | ██ |██████/ ██ |██ |
██ | ██ |██ | ██ |██████/ ██ | ██ | ██ | __ ██ |██ |
██ |__██ |██ \__██ | ██ \__██ | ██ |/ |██ |██ |
██ ██/ ██ ██/ ██ ██/ ██ ██/ ██ |██ |
███████/ ██████/ ██████/ ████/ ██/ ██/
██ |
██ |
██/ $TEXTLINE
"
if [ -t 1 ];
then
echo "$(tput setaf 6)$(tput bold)$LOGO$(tput sgr0)"
else
echo "$LOGO"
fi
}
blue_echo()
{
if [ -t 1 ];
then
echo "$(tput setaf 6)$(tput bold)$1$(tput sgr0)"
else
echo "$1"
fi
}
green_echo()
{
if [ -t 1 ];
then
echo "$(tput setaf 2)$(tput bold)$1$(tput sgr0)"
else
echo "$1"
fi
}
red_echo()
{
if [ -t 1 ];
then
echo "$(tput setaf 1)$(tput bold)$1$(tput sgr0)"
else
echo "$1"
fi
}
find_objects() #Consolidated function
{
if [ "$1" != "" ];
then
case "$1" in
*/)
#"has slash"
DEVICESFILE="${1%?}"
FIRMWAREDIR="${1%?}"
FIRMWAREBIN="${1%?}"
DIRECTORY="${1%?}"
;;
*)
echo "doesn't have a slash" > /dev/null
DEVICESFILE="$1"
FIRMWAREDIR="$1"
FIRMWAREBIN="$1"
DIRECTORY="$1"
;;
esac
if [ -f "$CWD/$DEVICESFILE/devices.txt" ] || [ -d "$CWD/$FIRMWAREDIR/firmware" ] || [ -f "$CWD/$FIRMWAREBIN/bin/firmware.bin" ];
then
DEVICESFILE="$CWD/$DEVICESFILE/devices.txt"
FIRMWAREDIR="$CWD/$FIRMWAREDIR/firmware"
FIRMWAREBIN="$CWD/$FIRMWAREBIN/bin/firmware.bin"
else
if [ -d "$DIRECTORY" ] && [ -d "$DIRECTORY/firmware" ];
then
DEVICESFILE="$DIRECTORY/devices.txt"
FIRMWAREDIR="$DIRECTORY/firmware"
FIRMWAREBIN="$DIRECTORY/bin/firmware.bin"
else
if [ -d "$CWD/$DIRECTORY" ] && [ -d "$CWD/firmware" ];
then
DEVICESFILE="$CWD/$DIRECTORY/../devices.txt"
FIRMWAREDIR="$CWD/$DIRECTORY"
FIRMWAREBIN="$CWD/$DIRECTORY/../bin/firmware.bin"
else
if [ "$DIRECTORY" == "." ] && [ -f "$CWD/main.cpp" ];
then
cd "$CWD/.." || exit
DEVICESFILE="$PWD/devices.txt"
FIRMWAREDIR="$CWD"
FIRMWAREBIN="$PWD/bin/firmware.bin"
else
echo
red_echo "Firmware not found!"
blue_echo "Please run \"po init DEVICE FOLDER\" to setup a project,
or choose a valid directory."
echo
exit
fi
fi
fi
fi
else
DEVICESFILE="$CWD/devices.txt"
FIRMWAREDIR="$CWD/firmware"
FIRMWAREBIN="$CWD/bin/firmware.bin"
fi
if [ -d "$FIRMWAREDIR" ];
then
FIRMWAREDIR="$FIRMWAREDIR"
else
if [ "$DIRWARNING" == "true" ];
then
echo
red_echo "Firmware directory not found!"
blue_echo "Please run \"po init DEVICE FOLDER\" to setup a project,
or choose a valid directory."
echo
exit
fi
FINDDIRFAIL="true"
fi
if [ -f "$DEVICESFILE" ];
then
DEVICES="$(cat $DEVICESFILE)"
else
if [ "$DEVICEWARNING" == "true" ];
then
echo
red_echo "devices.txt not found!"
blue_echo "You need to create a \"devices.txt\" file in your project directory with the names
of your devices on each line."
green_echo "Example:"
echo " product1
product2
product3
"
fi
FINDDEVICESFAIL="true"
fi
if [ -f "$FIRMWAREBIN" ];
then
FIRMWAREBIN="$FIRMWAREBIN"
else
if [ "$BINWARNING" == "true" ];
then
echo
red_echo "Firmware Binary not found!"
blue_echo "Perhaps you need to build your firmware?"
echo
fi
FINDBINFAIL="true"
fi
}
validateDevicePlatform()
{
# Make sure we are using photon, P1, electron, pi, core, or duo
if [ "$1" == "photon" ] || [ "$1" == "P1" ] || [ "$1" == "electron" ] || [ "$1" == "pi" ] || [ "$1" == "core" ] || [ "$1" == "duo" ];
then
DEVICE_TYPE="$1"
if [ "$DEVICE_TYPE" == "duo" ];
then
cd "$FIRMWARE_DUO/firmware" || exit
switch_branch $BRANCH_DUO &> /dev/null
fi
if [ "$DEVICE_TYPE" == "pi" ];
then
cd "$FIRMWARE_PI/firmware" || exit
switch_branch "feature/raspberry-pi" &> /dev/null
else
cd "$FIRMWARE_PARTICLE/firmware" || exit
switch_branch &> /dev/null
fi
else
echo
if [ "$1" == "redbear" ] || [ "$1" == "bluz" ] || [ "$1" == "oak" ];
then
red_echo "This compound is not supported yet. Find out more here: https://git.io/vMTAw"
echo
fi
red_echo "Please choose \"photon\", \"P1\", \"electron\", \"core\", \"pi\", or \"duo\",
or choose a proper command."
common_commands
exit
fi
if [ "$DEVICE_TYPE" == "photon" ];
then
DFU_ADDRESS1="2b04:d006"
DFU_ADDRESS2="0x080A0000"
fi
if [ "$DEVICE_TYPE" == "P1" ];
then
DFU_ADDRESS1="2b04:d008"
DFU_ADDRESS2="0x080A0000"
fi
if [ "$DEVICE_TYPE" == "electron" ];
then
DFU_ADDRESS1="2b04:d00a"
DFU_ADDRESS2="0x08080000"
fi
if [ "$DEVICE_TYPE" == "core" ];
then
DFU_ADDRESS1="1d50:607f"
DFU_ADDRESS2="0x08005000"
fi
if [ "$DEVICE_TYPE" == "duo" ];
then
DFU_ADDRESS1="2b04:d058"
DFU_ADDRESS2="0x80C0000"
fi
}
build_message()
{
echo
cd "$FIRMWAREDIR"/.. || exit
BINARYDIR="$PWD/bin"
green_echo "Binary saved to $BINARYDIR/firmware.bin"
echo
exit
}
dfu_open()
{
DFU_LIST="$(dfu-util -l)"
if echo "$DFU_LIST" | grep "2b04:d006" > /dev/null || echo "$DFU_LIST" | grep "2b04:d008" > /dev/null || echo "$DFU_LIST" | grep "2b04:d00a" > /dev/null || echo "$DFU_LIST" | grep "1d50:607f" > /dev/null || echo "$DFU_LIST" | grep "2b04:d058" > /dev/null ;
then
blue_echo "
Already found a device in DFU mode!
"
return
fi
if [ "$1" == "-d" ] || [ "$1" == "--device" ];
then
if [[ -e "$2" ]]; # Check if serial port is avaliable
then
blue_echo "
Placing device $2 into DFU mode...
"
custom-baud "$2" "$DFUBAUDRATE" > /dev/null 2>&1
return
else
red_echo "
Could not find a device on $2
"
fi
return
fi
if [ "$DEVICE_TYPE" == "duo" ];
then
if [ -e "$MODEM_DUO" ];
then
MODEM="$MODEM_DUO"
else
echo
red_echo "Device not found!"
echo
blue_echo "Your device must be connected by USB."
echo
exit
fi
else
if [ -e "$MODEM" ];
then
MODEM="$MODEM"
else
echo
red_echo "Device not found!"
echo
blue_echo "Your device must be connected by USB."
echo
exit
fi
fi
if [ -e "$MODEM" ];
then
custom-baud "$MODEM" "$DFUBAUDRATE" > /dev/null 2>&1
fi
}
serial_open()
{
if [ "$1" == "-d" ] || [ "$1" == "--device" ];
then
if [[ -e "$2" ]]; # Check if serial port is avaliable
then
blue_echo "
Placing device $2 into listening mode...
"
custom-baud "$2" "28800" > /dev/null 2>&1
return
else
red_echo "
Could not find a device on $2
"
fi
return
fi
if [ "$DEVICE_TYPE" == "duo" ];
then
if [ -e "$MODEM_DUO" ];
then
MODEM="$MODEM_DUO"
else
echo
red_echo "Device not found!"
echo
blue_echo "Your device must be connected by USB."
echo
exit
fi
else
if [ -e "$MODEM" ];
then
MODEM="$MODEM"
else
echo
red_echo "Device not found!"
echo
blue_echo "Your device must be connected by USB."
echo
exit
fi
fi
if [ -e "$MODEM" ];
then
custom-baud "$MODEM" "28800" > /dev/null 2>&1
fi
}
switch_branch()
{
if [ "$1" != "" ];
then
if [ "$(git rev-parse --abbrev-ref HEAD)" != "$1" ];
then
git checkout "$1" > /dev/null
fi
else
if [ "$(git rev-parse --abbrev-ref HEAD)" != "$BRANCH" ];
then
git checkout "$BRANCH" > /dev/null
fi
fi
}
common_commands() #List common commands
{
echo
blue_echo "Common commands include:
build, flash, clean, ota, dfu, serial, init, config, setup, library"
echo
}
cleanFirmware()
{
if [ "$DEVICE_TYPE" == "duo" ];
then
cd "$FIRMWARE_DUO"/firmware || exit
else
cd "$FIRMWARE_PARTICLE"/firmware || exit
fi
if [ "$DEVICE_TYPE" == "pi" ];
then
switch_branch "feature/raspberry-pi" &> /dev/null
elif [ "$DEVICE_TYPE" == "duo" ];
then
switch_branch "$BRANCH_DUO" &> /dev/null
else
switch_branch &> /dev/null
fi
git stash &> /dev/null
echo
if [ "$DEVICE_TYPE" == "pi" ];
then
make clean -s 2>&1 /dev/null
else
make clean -s PLATFORM="$DEVICE_TYPE" 2>&1 /dev/null
fi
if [ "$FIRMWAREDIR/../bin" != "$HOME/bin" ];
then
rm -rf "$FIRMWAREDIR/../bin"
fi
blue_echo "Sucessfully cleaned."
echo
}
store_build_parameters() # Keep track of $DEVICE_TYPE and $FIRMWAREDIR
{
PARAMETERS="$HOME/.po-util/buildParameters"
[[ -f "$PARAMETERS" ]] && . "$PARAMETERS"
[[ "$STORED_DEVICE_TYPE" != "$DEVICE_TYPE" ]] && DEVICE_TYPE_CHANGED="true"
[[ "$STORED_FIRMWAREDIR" != "$FIRMWAREDIR" ]] && FIRMWAREDIR_CHANGED="true"
if [[ "$STORED_DEVICE_TYPE" != "$DEVICE_TYPE" ]] || [[ "$STORED_FIRMWAREDIR" != "$FIRMWAREDIR" ]]; then
echo "STORED_DEVICE_TYPE=$DEVICE_TYPE" > "$PARAMETERS"
echo "STORED_FIRMWAREDIR=$FIRMWAREDIR" >> "$PARAMETERS"
fi
if [[ "$DEVICE_TYPE_CHANGED" ]] && [[ "$FIRMWAREDIR_CHANGED" ]]; then
blue_echo "Detected change of platform and project. Cleaning before attempting build..."
cleanFirmware
elif [[ "$DEVICE_TYPE_CHANGED" ]]; then
blue_echo "Detected change of platform. Cleaning before attempting build..."
cleanFirmware
elif [[ "$FIRMWAREDIR_CHANGED" ]]; then
blue_echo "Detected change of project. Cleaning before attempting build..."
cleanFirmware
fi
}
printSizes()
{
if [[ "$DEVICE_TYPE" == "core" ]]; then
FLASHTOTAL="110592"
RAMTOTAL="20480"
else
FLASHTOTAL="125000"
RAMTOTAL="60000"
fi
FLASH="$(( $1 + $2 ))"
FLASHPERCENT=$(bc -l <<< "scale = 4; $FLASH / $FLASHTOTAL * 100")
RAM="$(( $3 + $2 ))"
RAMPERCENT=$(bc -l <<< "scale = 4; $RAM / $RAMTOTAL * 100")
echo
echo "$(tput setaf 6)$(tput bold)Flash Used:$(tput sgr0) $FLASH / $FLASHTOTAL ${FLASHPERCENT%??} %"
echo "$(tput setaf 6)$(tput bold)RAM Used: $(tput sgr0) $RAM / $RAMTOTAL ${RAMPERCENT%??} %"
}
build_firmware()
{
store_build_parameters
print_logo "Building firmware for $DEVICE_TYPE..."
if [ "$DEVICE_TYPE" == "duo" ];
then
make all -s -C "$FIRMWARE_DUO/firmware/main" APPDIR="$FIRMWAREDIR" TARGET_DIR="$FIRMWAREDIR/../bin" PLATFORM="$DEVICE_TYPE"
else
if [ "$DEVICE_TYPE" == "pi" ];
then
if hash docker 2>/dev/null;
then
if docker run --rm -i -v $FIRMWARE_PI/firmware:/firmware -v $FIRMWAREDIR:/input -v $FIRMWAREDIR/../bin:/output particle/buildpack-raspberrypi 2> echo;
then
echo
blue_echo "Successfully built firmware for Raspberry Pi"
else
echo
red_echo "Build failed."
echo
exit 1
fi
else
red_echo "Docker not found. Please install docker to build firmware for Raspberry Pi"
echo
exit 1
fi
else
make all -s -C "$FIRMWARE_PARTICLE/firmware/main" APPDIR="$FIRMWAREDIR" TARGET_DIR="$FIRMWAREDIR/../bin" PLATFORM="$DEVICE_TYPE"
fi
fi
}
ota() # device firmware
{
find_objects "$2"
DIRWARNING="true"
BINWARNING="true"
if [ "$FINDDIRFAIL" == "true" ] || [ "$FINDBINFAIL" == "true" ];
then
exit
fi
if [ "$1" == "" ];
then
echo
red_echo "Please specify which device to flash ota."
echo
exit
fi
if [ "$1" == "--multi" ] || [ "$1" == "-m" ] || [ "$1" == "-ota" ];
then
DEVICEWARNING="true"
if [ "$FINDDEVICESFAIL" == "true" ] || [[ -z "$DEVICES" ]];
then
cd "$CWD" || exit
echo "" > devices.txt
red_echo "Please list your devices in devices.txt"
exit 1
fi
for DEVICE in $DEVICES ; do
echo
blue_echo "Flashing to device $DEVICE..."
particle flash "$DEVICE" "$FIRMWAREBIN" || {
red_echo "Your device must be online in order to flash firmware OTA." && echo && exit 1
}
done
echo
exit
fi
echo
blue_echo "Flashing to device $1..."
particle flash "$1" "$FIRMWAREBIN" || {
red_echo "Try using \"particle flash\" if you are having issues." && echo && exit 1
}
echo
exit
}
config()
{
if [[ -d "$HOME/.po-util" ]]; then
echo "Exists!" > /dev/null
else
mkdir -p "$HOME/.po-util"
fi
SETTINGS=~/.po-util/config
BASE_DIR=~/.po-util/src
FIRMWARE_PARTICLE=$BASE_DIR/particle
FIRMWARE_DUO=$BASE_DIR/redbearduo
FIRMWARE_PI=$BASE_DIR/pi
BRANCH="release/stable"
BRANCH_DUO="duo"
BRANCH_PI="feature/raspberry-pi"
GCC_ARM_PATH=$BINDIR/gcc-arm-embedded/$GCC_ARM_VER/bin/
MODEM_DUO=$MODEM_DUO
echo BASE_DIR="$BASE_DIR" >> $SETTINGS
echo FIRMWARE_PARTICLE="$FIRMWARE_PARTICLE" >> $SETTINGS
echo FIRMWARE_DUO="$FIRMWARE_DUO" >> $SETTINGS
echo FIRMWARE_PI="$FIRMWARE_PI" >> $SETTINGS
echo "export PARTICLE_DEVELOP=1" >> $SETTINGS
echo BINDIR="$BINDIR" >> $SETTINGS
# Particle
echo
blue_echo "Which branch of the Particle firmware would you like to use?
You can find the branches at https://github.com/spark/firmware/branches
If you are unsure, please enter \"release/stable\""
read -rp "Branch: " branch_variable
BRANCH="$branch_variable"
echo BRANCH="$BRANCH" >> $SETTINGS
echo
# RedBear DUO
blue_echo "Which branch of the RedBear DUO firmware would you like to use?
You can find the branches at https://github.com/redbear/Duo/branches
If you are unsure, please enter \"duo\""
read -rp "Branch: " branch_variable
BRANCH_DUO="$branch_variable"
echo BRANCH_DUO="$BRANCH_DUO" >> $SETTINGS
echo
blue_echo "Shoud po-util automatically add and remove headers when using
libraries?"
read -rp "(yes/no): " response
if [ "$response" == "yes" ] || [ "$response" == "y" ] || [ "$response" == "Y" ];
then
AUTO_HEADER="true"
else
AUTO_HEADER="false"
fi
echo AUTO_HEADER="$AUTO_HEADER" >> $SETTINGS
echo
}
getAddedLibs()
{
find_objects "$1"
if [ "$FINDDIRFAIL" == "true" ]; then
exit
fi
for i in $FIRMWAREDIR/*/; do basename "$i"; done
}
getLibURL()
{
TOKEN="$(grep 'token' ~/.particle/particle.config.json | grep -oE '([0-Z])\w+' | grep -v 'token')"
DATA=$(curl -sLH "Authorization: Bearer $TOKEN" "https://api.particle.io/v1/libraries/$1" | json_pp)
LIBURL=$(echo "$DATA" | grep "url" | grep -oE '"((?:\\.|[^"\\])*)"' | grep "http" | tr -d '"')
}
getLib() # "$i" "$LIB_NAME"
{
if [ "$2" == "" ];
then
LIB_QUERY="$1"
else
LIB_QUERY="$2"
fi
if [[ -d "$LIBRARY/$LIB_QUERY" ]];
then
echo
blue_echo "Library $LIB_QUERY is already installed..."
else
if grep -q "://" <<<"$1";
then
GIT_ARGS=($1)
if [ "${GIT_ARGS[1]}" == "" ];
then
git clone "${GIT_ARGS[0]}" || ( echo ; red_echo "Could not download Library. Please supply a valid URL to a git repository." )
else
git clone "${GIT_ARGS[0]}" "${GIT_ARGS[1]}" || ( echo ; red_echo "Could not download Library. Please supply a valid URL to a git repository." )
fi
else
echo
getLibURL "$LIB_QUERY"
if ( echo "$LIBURL" | grep "github" ) > /dev/null ;
then
green_echo "$LIB_QUERY is availiable on GitHub!"
read -rp "Would you prefer to download it this way? (yes/no): " answer
if [ "$answer" == "yes" ] || [ "$answer" == "y" ] || [ "$answer" == "Y" ];
then
echo
cd "$LIBRARY" || exit
git clone "$LIBURL" "$LIB_QUERY"
echo
blue_echo "Downloaded $LIB_QUERY from GitHub."
return 0
fi
echo
fi
blue_echo "Attempting to download $LIB_QUERY using Particle Libraries 2.0..."
echo
cd "$LIBRARY/.." || exit
particle library copy "$LIB_QUERY" || ( echo && particle library search "$LIB_QUERY" && echo && return 1 )
echo
fi
fi
}
addLib()
{
if [[ ! -d "$LIBRARY/$LIB_NAME" ]]; # Try to automatically get library if not found
then
getLib "$LIB_NAME" || ( echo && exit)
fi
if [ -f "$FIRMWAREDIR/$LIB_NAME.cpp" ] || [ -f "$FIRMWAREDIR/$LIB_NAME.h" ] || [ -d "$FIRMWAREDIR/$LIB_NAME" ];
then
echo
red_echo "Library $LIB_NAME is already added to this project..."
else
echo
green_echo "Adding library $LIB_NAME to this project..."
# Include library as a folder full of symlinks -- This is the new feature
mkdir -p "$FIRMWAREDIR/$LIB_NAME"
if [ -d "$LIBRARY/$LIB_NAME/firmware" ];
then
ln -s $LIBRARY/$LIB_NAME/firmware/* "$FIRMWAREDIR/$LIB_NAME"
else
if [ -d "$LIBRARY/$LIB_NAME/src" ];
then
ln -s $LIBRARY/$LIB_NAME/src/* "$FIRMWAREDIR/$LIB_NAME"
else
ln -s $LIBRARY/$LIB_NAME/* "$FIRMWAREDIR/$LIB_NAME"
fi
fi
fi
}
addHeaders()
{
[ "$1" != "" ] && HEADER="$1" || HEADER="$LIB_NAME"
if [ "$AUTO_HEADER" == "true" ];
then
if (grep "#include \"$HEADER/$HEADER.h\"" "$FIRMWAREDIR/main.cpp") &> /dev/null ;
then
echo "Already imported" &> /dev/null
else
echo "#include \"$HEADER/$HEADER.h\"" > "$FIRMWAREDIR/main.cpp.temp"
cat "$FIRMWAREDIR/main.cpp" >> "$FIRMWAREDIR/main.cpp.temp"
rm "$FIRMWAREDIR/main.cpp"
mv "$FIRMWAREDIR/main.cpp.temp" "$FIRMWAREDIR/main.cpp"
fi
fi
}
rmHeaders()
{
if [ "$AUTO_HEADER" == "true" ];
then
if (grep "#include \"$1/$1.h\"" "$FIRMWAREDIR/main.cpp") &> /dev/null ;
then
grep -v "#include \"$1/$1.h\"" "$FIRMWAREDIR/main.cpp" > "$FIRMWAREDIR/main.cpp.temp"
rm "$FIRMWAREDIR/main.cpp"
mv "$FIRMWAREDIR/main.cpp.temp" "$FIRMWAREDIR/main.cpp"
fi
if (grep "#include \"$1.h\"" "$FIRMWAREDIR/main.cpp") &> /dev/null ; # Backwards support
then
grep -v "#include \"$1.h\"" "$FIRMWAREDIR/main.cpp" > "$FIRMWAREDIR/main.cpp.temp"
rm "$FIRMWAREDIR/main.cpp"
mv "$FIRMWAREDIR/main.cpp.temp" "$FIRMWAREDIR/main.cpp"
fi
if (grep "#include <$1.h>" "$FIRMWAREDIR/main.cpp") &> /dev/null ; # Other support
then
grep -v "#include <$1.h>" "$FIRMWAREDIR/main.cpp" > "$FIRMWAREDIR/main.cpp.temp"
rm "$FIRMWAREDIR/main.cpp"
mv "$FIRMWAREDIR/main.cpp.temp" "$FIRMWAREDIR/main.cpp"
fi
fi
}
initProject()
{
if [[ "$FOLDER" == "/"* ]]; # Check for absolute or relative
then
FIRMWAREDIR="$FOLDER/firmware"
else
FIRMWAREDIR="$CWD/$FOLDER/firmware"
fi
if [ -d "$FIRMWAREDIR" ];
then
echo
green_echo "Directory is already Initialized!"
echo
exit
fi
mkdir -p "$FIRMWAREDIR"
echo "#include \"Particle.h\"
void setup() // Put setup code here to run once
{
}
void loop() // Put code here to loop forever
{
}" > "$FIRMWAREDIR/main.cpp"
cp "$HOME/.po-util/doc/po-util-README.md" "$FIRMWAREDIR/../README.md"
if [ "$DEVICE_TYPE" != "" ];
then
echo "---
cmd: po $DEVICE_TYPE build
targets:
Build:
args:
- $DEVICE_TYPE
- build
cmd: po
keymap: ctrl-alt-1
name: Build
Flash:
args:
- $DEVICE_TYPE
- flash
cmd: po
keymap: ctrl-alt-2
name: Flash
Clean:
args:
- $DEVICE_TYPE
- clean
cmd: po
keymap: ctrl-alt-3
name: Clean
DFU:
args:
- $DEVICE_TYPE
- dfu
cmd: po
keymap: ctrl-alt-4
name: DFU
OTA:
args:
- $DEVICE_TYPE
- ota
- --multi
cmd: po
keymap: ctrl-alt-5
name: DFU
" >> "$FIRMWAREDIR/../.atom-build.yml"
mkdir -p "$FIRMWAREDIR/../ci"
echo "dist: trusty
sudo: required
language: generic
script:
- ci/travis.sh
cache:
directories:
- $HOME/bin" > "$FIRMWAREDIR/../.travis.yml"
echo "#!/bin/bash
bash <(curl -sL https://master.po-util.com/ci-install)
po lib clean . -f &> /dev/null
yes \"no\" | po lib setup # change to \"yes\" to prefer libraries from GitHub
po $DEVICE_TYPE build" > "$FIRMWAREDIR/../ci/travis.sh"
chmod +x "$FIRMWAREDIR/../ci/travis.sh"
fi
echo "bin/*" > "$FIRMWAREDIR/../.gitignore"
cd "$FIRMWAREDIR/.." || exit
git init &> /dev/null
echo
green_echo "Directory initialized as a po-util project for $DEVICE_TYPE"
echo
exit
}
getLatestNodeVersion()
{
curl -Ss https://nodejs.org/dist/ > node-result.txt
grep "<a href=\"v" "node-result.txt" > node-new.txt
tail -1 node-new.txt > node-oneline.txt
sed -n 's/.*\"\(.*.\)\".*/\1/p' node-oneline.txt > node-version.txt
NODEVERSION="$(cat node-version.txt)"
NODEVERSION="${NODEVERSION%?}"
INSTALLVERSION="node-$NODEVERSION"
rm node-*.txt
}
#### End of helper functions
# po help
if [ "$1" == "" ] || [ "$1" == "help" ]; # Print help
then
print_logo
echo "Copyright (GPL) 2017 Nathan D. Robinson
Usage: po DEVICE_TYPE COMMAND DEVICE_NAME
po DFU_COMMAND
po install [full_install_path]
po library LIBRARY_COMMAND
Run \"man po\" for help.
"
exit
fi
# po setup-atom
if [ "$1" == "setup-atom" ];
then
echo
blue_echo "Installing Atom packages to enhance po-util experience..."
echo
apm install build minimap file-icons language-particle
echo
exit
fi
# Configuration file is created at "~/.po-util/config"
SETTINGS=~/.po-util/config
BASE_DIR=~/.po-util/src
FIRMWARE_PARTICLE=$BASE_DIR/particle
FIRMWARE_DUO=$BASE_DIR/redbearduo
FIRMWARE_PI=$BASE_DIR/pi
BRANCH="release/stable"
BRANCH_DUO="duo"
BRANCH_PI="feature/raspberry-pi"
BINDIR=~/.po-util/bin
DFUBAUDRATE=14400
CWD="$PWD" # Global Current Working Directory variable
for modem in /dev/ttyACM*
do
MODEM="$modem"
MODEM_DUO="$modem"
done
GCC_ARM_VER=gcc-arm-none-eabi-5_3-2016q1 # Updated to 5.3
GCC_ARM_URL=https://developer.arm.com/-/media/Files/downloads/gnu-rm/5_3-2016q1/gccarmnoneeabi532016q120160330linuxtar.bz2
GCC_ARM_PATH=$BINDIR/gcc-arm-embedded/$GCC_ARM_VER/bin/
CUSTOM_BAUD_PATH=$BINDIR/custom-baud
PATH="$PATH:$GCC_ARM_PATH"
PATH="$PATH:$CUSTOM_BAUD_PATH"
# po config
if [ "$1" == "config" ];
then
if [ -f "$SETTINGS" ];
then
rm "$SETTINGS"
fi
config
exit
fi
# Check if we have a saved settings file. If not, create it.
if [ ! -f $SETTINGS ]
then
echo
blue_echo "Your \"$SETTINGS\" configuration file is missing. Let's create it!"
config
fi
# Import our overrides from the ~/.po-util/config file.
source "$SETTINGS"
# po info
if [ "$1" == "info" ];
then
echo "