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.

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