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.

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