Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
4.0 KiB

  1. #!/bin/bash
  2. ip=
  3. board=
  4. auth=
  5. flags=
  6. export boards=()
  7. ips=""
  8. exists() {
  9. command -v "$1" >/dev/null 2>&1
  10. }
  11. echo_pad() {
  12. string=$1
  13. pad=$2
  14. printf '%s' "$string"
  15. printf '%*s' $(( $pad - ${#string} ))
  16. }
  17. useAvahi() {
  18. echo_pad "#" 4
  19. echo_pad "HOSTNAME" 20
  20. echo_pad "IP" 20
  21. echo_pad "DEVICE" 30
  22. echo_pad "VERSION" 10
  23. echo
  24. printf -v line '%*s\n' 84
  25. echo ${line// /-}
  26. counter=0
  27. ip_file="/tmp/espurna.flash.ips"
  28. board_file="/tmp/espurna.flash.boards"
  29. count_file="/tmp/espurna.flash.count"
  30. echo -n "" > $ip_file
  31. echo -n "" > $board_file
  32. echo -n "$counter" > $count_file
  33. avahi-browse -t -r -p "_arduino._tcp" 2>/dev/null | grep ^= | while read line; do
  34. (( counter++ ))
  35. echo "$counter" > $count_file
  36. hostname=`echo $line | cut -d ';' -f4`
  37. ip=`echo $line | cut -d ';' -f8`
  38. txt=`echo $line | cut -d ';' -f10`
  39. board=`echo $txt | sed -n "s/.*espurna_board=\([^\"]*\).*/\1/p"`
  40. version=`echo $txt | sed -n "s/.*espurna_version=\([^\"]*\).*/\1/p"`
  41. echo -n "$ip;" >> $ip_file
  42. echo -n "$board;" >> $board_file
  43. echo_pad "$counter" 4
  44. echo_pad "$hostname" 20
  45. echo_pad "$ip" 20
  46. echo_pad "$board" 30
  47. echo_pad "$version" 10
  48. echo
  49. done
  50. echo
  51. read -p "Choose the board you want to flash (empty if none of these): " num
  52. # None of these
  53. if [ "$num" == "" ]; then
  54. return
  55. fi
  56. # Check boundaries
  57. counter=`cat $count_file`
  58. if [ $num -lt 1 ] || [ $num -gt $counter ]; then
  59. echo "Board number must be between 1 and $counter"
  60. exit 1
  61. fi
  62. # Fill the fields
  63. ip=`cat $ip_file | cut -d ';' -f$num`
  64. board=`cat $board_file | cut -d ';' -f$num`
  65. }
  66. getBoard() {
  67. boards=(`cat espurna/config/hardware.h | grep "defined" | sed "s/.*(\(.*\)).*/\1/" | sort`)
  68. echo_pad "#" 4
  69. echo_pad "DEVICE" 30
  70. echo
  71. printf -v line '%*s\n' 34
  72. echo ${line// /-}
  73. counter=0
  74. for board in "${boards[@]}"; do
  75. (( counter++ ))
  76. echo_pad "$counter" 4
  77. echo_pad "$board" 30
  78. echo
  79. done
  80. echo
  81. read -p "Choose the board you want to flash (empty if none of these): " num
  82. # None of these
  83. if [ "$num" == "" ]; then
  84. return
  85. fi
  86. # Check boundaries
  87. counter=${#boards[*]}
  88. if [ $num -lt 1 ] || [ $num -gt $counter ]; then
  89. echo "Board code must be between 1 and $counter"
  90. exit 1
  91. fi
  92. # Fill the fields
  93. (( num -- ))
  94. board=${boards[$num]}
  95. }
  96. # ------------------------------------------------------------------------------
  97. # Welcome
  98. echo
  99. echo "--------------------------------------------------------------"
  100. echo "ESPURNA FIRMWARE OTA FLASHER"
  101. # Get current version
  102. version=`cat espurna/config/version.h | grep APP_VERSION | awk '{print $3}' | sed 's/"//g'`
  103. echo "Building for version $version"
  104. echo "--------------------------------------------------------------"
  105. echo
  106. if exists avahi-browse; then
  107. useAvahi
  108. fi
  109. if [ "$board" == "" ]; then
  110. getBoard
  111. fi
  112. if [ "$board" == "" ]; then
  113. read -p "Board type of the device to flash: " -e -i "NODEMCU_LOLIN" board
  114. fi
  115. if [ "$board" == "" ]; then
  116. echo "You must define the board type"
  117. exit 2
  118. fi
  119. if [ "$ip" == "" ]; then
  120. read -p "IP of the device to flash: " -e -i 192.168.4.1 ip
  121. fi
  122. if [ "$ip" == "" ]; then
  123. echo "You must define the IP of the device"
  124. exit 2
  125. fi
  126. if [ "$auth" == "" ]; then
  127. read -p "Authorization key of the device to flash: " auth
  128. fi
  129. if [ "$flags" == "" ]; then
  130. read -p "Extra flags for the build: " -e -i "-DTELNET_ONLY_AP=0" flags
  131. fi
  132. read -p "Environment to build: " -e -i "esp8266-1m-ota" env
  133. echo
  134. echo "ESPURNA_IP = $ip"
  135. echo "ESPURNA_BOARD = $board"
  136. echo "ESPURNA_AUTH = $auth"
  137. echo "ESPURNA_FLAGS = $flags"
  138. echo "ESPURNA_ENV = $env"
  139. echo
  140. echo -n "Are these values corrent [y/N]: "
  141. read response
  142. if [ "$response" != "y" ]; then
  143. exit
  144. fi
  145. export ESPURNA_IP=$ip
  146. export ESPURNA_BOARD=$board
  147. export ESPURNA_AUTH=$auth
  148. export ESPURNA_FLAGS=$flags
  149. pio run -e $env -t upload