Permalink
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
:EndIf
rReset Clear the serial buffer
Unmake
:Implements Destructor
:Trap 0
device ⎕NUNTIE tie Close the device
tie0
:EndTrap
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
r''
start⎕AI[3] Start time
:If 0=⎕NC 'timeout' timeout0 :EndIf
⎕RTL×timeout Time out every second if we're checking
:Repeat
r,⎕UCS (tie tie)⎕ARBIN
stop(timeout0)timeout<0.001×⎕AI[3]-start
:Until stopprompt(-nprompt)r
'TIMEOUT' ⎕SIGNAL stop/999
r(rLF)(-n+2)r Drop Echo, Drop prompt and preceding CRLF
rClear;t
Read everything in the serial input queue
:Access Public
r''
:Repeat
r,t⎕UCS (tie tie)⎕ARBIN Read next line (⍬ means up to LF)
:Until 0=t Repeat until no response
rReset
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
:Else
'TIMEOUT: micro:bit Python REPL not issuing >>>' ⎕SIGNAL 11
:EndTrap
rSetTTY device
Initialise TTY device
r''
:If ~⎕NEXISTS device
r'Unable to connect to micro:bit via ',device,': device does not exist or is inaccessable'
0
:EndIf
: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
:Else
r'Problems initialising connection to micro:bit via ',device
:EndTrap
{z}clear_buttons
:Access Public
zwas_pressed¨'ab'
show image;z
:Access Public
zPyREPL 'display.show(Image.',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
:EndClass