seeedfarm: introduce a nicer UI that works over telnet - brcon2025-hackathons - Bitreichcon 2025 Hackathons
 (HTM) git clone git://bitreich.org/brcon2025-hackathons git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/brcon2025-hackathons
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Tags
       ---
 (DIR) commit 8a6dd4b51a856d549863836b240d2a6d0c495043
 (DIR) parent b01932dc1daf4868cfbe4125d0ebed7b7274473c
 (HTM) Author: Josuah Demangeon <me@josuah.net>
       Date:   Thu, 31 Jul 2025 10:02:24 +0200
       
       seeedfarm: introduce a nicer UI that works over telnet
       
       Diffstat:
         M seeedfarming/example.py             |      13 +++++++++----
         M seeedfarming/session.sh             |      85 +++++++++++++++++++++++--------
       
       2 files changed, 73 insertions(+), 25 deletions(-)
       ---
 (DIR) diff --git a/seeedfarming/example.py b/seeedfarming/example.py
       @@ -1,16 +1,21 @@
        #!/usr/bin/env python
        # coding=utf-8
        #
       -# this is an example script that you can load into MicroPython prompt to
       -# connect yourself to the WiFi access point
       -import network, socket
       +# This is an example script that you can load into MicroPython prompt to
       +# connect yourself to the WiFi access point.
       +import network
       +import socket
       +import time
        
        wlan = network.WLAN()
        wlan.active(True)
        wlan.scan()
        wlan.connect('seeeder', 'seeedrooots')
        
       -# Then wait until wlan.isconnected() returns True
       +# Then wait until internet is up
       +while not wlan.isconnected():
       +    print('.', end='')
       +print(' connected')
        
        myip = wlan.ipconfig('addr4')[0]
        print(myip)
 (DIR) diff --git a/seeedfarming/session.sh b/seeedfarming/session.sh
       @@ -1,31 +1,74 @@
        #!/bin/sh
        
       -# You do not need to run this, but have a look if you are curious
       +SEEED_SCRIPTS=/tmp/seeed_scripts
        
       -# Initial string sent to reboot the board and start from a clean session every time
       -# Ctrl+C to cancel anything running
       -# Ctrl+B to come back to the normal prompt mode
       -# Then a python script to connect to hardware-reset the board and connect to wifi
       -init="$(printf '\x03\x02'; printf '%s\r' 'import machine' 'machine.reset()')"
       +echo "Welcome to the MicroPython Seeed farm!"
        
       -{
       -        echo "Welcome to the MicroPython Seeed farm!"
       +mkdir -p "$SEEED_SCRIPTS"
       +
       +num=0
       +
       +while true; do
       +        echo "What action do you want to run?"
                for tty in /dev/ttyACM*; do
       -                if pgrep -fal "picocom --quiet --no-escape .* $tty"; then
       -                        echo "- $tty busy"
       +                s="$SEEED_SCRIPTS/${tty#/dev/}.py"
       +                i=${tty#/dev/ttyACM}
       +
       +                if [ -f "$s" ]; then
       +                        echo " [$i]  select $tty (running $s)"
       +                elif pgrep -fal "picocom --quiet --no-escape .* $tty"; then
       +                        echo " [$i]  select $tty (busy)"
                        else
       -                        echo "- $tty free"
       +                        echo " [$i]  select $tty (free)"
                        fi
                done
       -} | sed "s/$/$(printf '\r')/"
       +        echo " [l]  Load a new script instead of the current one (if any)"
       +        echo " [p]  Get a python prompt on the current script"
       +        echo " [r]  Reset the board and remove the script"
       +        echo " [v]  View the script "
       +        echo -n "ttyACM$num>>> "
       +        read action || exit
        
       -i=0
       -while [ "$i" -lt 100 ]; do
       -        port=$(printf 1%04d $i)
       -        picocom --quiet --no-escape --initstring="$init" --baud=115200 /dev/ttyACM$i && break
       -        i=$((i + 1))
       -done
       +        case $action in
       +        ([0-9])
       +                num=$action
       +                script=$SEEED_SCRIPTS/ttyACM$num.py
       +                ;;
       +        (l)
       +                echo "Paste your script and finish it by a dot character ('.') alone on its own line:"
       +                rm -f "$script"
       +                while read line; do
       +                        if [ "$line" = "." ]; then break; fi
       +                        echo "$line" >>$script
       +                        echo "$line"
       +                done
       +
       +                # Set the terminal in "paste" mode and load the script
       +                picocom --quiet --baud="115200" --initstring="$reset" "/dev/ttyACM$num" <$script
        
       -if [ "$?" != 0 ]; then
       -        echo "Failed to find any MicroPython board available... Ask for a farm fixing the chat!"
       -fi
       +                # Set the terminal back to "normal" mode and show the output of the script
       +                reset=$(printf '\x03''import machine; machine.reset()\r''\x05')
       +                done=$(printf '\x04\r')
       +                tr '\n' '\r' | picocom --quiet --baud="115200" --initstring="$done" "/dev/ttyACM$num"
       +                ;;
       +        (p)
       +                # Just connect without resetting
       +                tr '\n' '\r' | picocom --imap="lfcrlf" --quiet --baud="115200" "/dev/ttyACM$num"
       +                ;;
       +        (e)
       +                cat
       +                ;;
       +        (r)
       +                # Just connect without resetting or sending a script to see the ongoing execution
       +                reset=$(printf '\x03''import machine; machine.reset()\r')
       +                picocom --quiet --baud="115200" --initstring="$reset" --exit-after="500" "/dev/ttyACM$num"
       +                rm -f "$script"
       +                ;;
       +        (v)
       +                cat "$script"
       +                ;;
       +        (*)
       +                echo "error: unknown command '$action' selected"
       +                ;;
       +        esac
       +done