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.

225 lines
4.9 KiB

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