Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
126 lines (101 sloc) 3.7 KB
:Class microbit
:Field Public device'/dev/ttyACM0'
:Field Public tie0
(CR LF)⎕UCS 13 10
Make dev;r
:Implements Constructor
:Access Public
devicedev,(0=dev)/device Default if not provided
tiedevice ⎕NTIE 0 Get a file handle
:If 0rSetTTY device Initalise serial tty comms
r ⎕SIGNAL 11
rReset Clear the serial buffer
:Implements Destructor
:Trap 0
device ⎕NUNTIE tie Close the device
rPyREPL cmd
Send command to Python REPL and return response
:Access Public
rClear Empty buffer
Send cmd,CR send command + CR
rReadUntil '>>> ' Read until >>>
Send cmd
:Access Public
tie ⎕ARBOUT ⎕UCS cmd
r{timeout} ReadUntil prompt;n;start;stop;⎕RTL
Read up to next prompt
:Access Public
start⎕AI[3] Start time
:If 0=⎕NC 'timeout' timeout0 :EndIf
⎕RTL×timeout Time out every second if we're checking
r,⎕UCS (tie tie)⎕ARBIN
:Until stopprompt(-nprompt)r
'TIMEOUT' ⎕SIGNAL stop/999
r(rLF)(-n+2)r Drop Echo, Drop prompt and preceding CRLF
Read everything in the serial input queue
:Access Public
r,t⎕UCS (tie tie)⎕ARBIN Read next line (⍬ means up to LF)
:Until 0=t Repeat until no response
Send CTRL+D for soft reboot
:Access Public
Send ⎕UCS 4 CTRL+D = Soft Reboot
⎕DL 0.5 Give it half a sec
:Trap 999
r5 ReadUntil '>>> ' Allow 5 seconds to get input prompt
'TIMEOUT: micro:bit Python REPL not issuing >>>' ⎕SIGNAL 11
rSetTTY device
Initialise TTY device
:If ~⎕NEXISTS device
r'Unable to connect to micro:bit via ',device,': device does not exist or is inaccessable'
:Trap 0
{}⎕SH'stty 115200 -parenb -parodd -cmspar cs8 hupcl -cstopb cread clocal -crtscts -ignbrk -brkint ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl -ixon -ixoff -iuclc -ixany -imaxbel -iutf8 -opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0 -isig -icanon -iexten -echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke <',device
{}⎕SH'stty min 0 <',device
r'Problems initialising connection to micro:bit via ',device
:Access Public
show image;z
:Access Public
zPyREPL '',image,')'
ris_pressed button
:Access Public
ris_true 'button_',button,'.is_pressed()'
rwas_pressed button
:Access Public
ris_true 'button_',button,'.was_pressed()'
ris_true expr;t;z
:Access Public
r1t'True' 'False'zPyREPL expr
:If ~/t 'True/False expected, got: ',z ∘∘∘ :EndIf