-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReactionTimer.dyalog
78 lines (65 loc) · 2.48 KB
/
ReactionTimer.dyalog
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
:Namespace ReactionTimer
⍝ Reaction timer using the LEDs & buttons on a BBC micro:bit
⍝ Assumes the micro:bit is exposing the MicroPython REPL
⍝∇:require =microbit
∇ {mb}←InitMB
⍝ Ensure existence of and return ref to instance of micro:bit class
:If 0=⎕NC '#.mb'
#.mb←⎕NEW #.microbit ''
:EndIf
mb←#.mb
∇
∇ r←Play;mb;start;time;z
mb←InitMB
r←⍬
:Repeat
mb.clear_buttons
mb.show 'CLOCK1'
z←⎕DL 1+0.1×?40 ⍝ Delay 1-5 seconds
:If mb.was_pressed 'a'
⎕←'naughty!'
:EndIf
mb.show 'HAPPY'
start←⎕AI[3]
:Repeat ⋄ ⎕DL 0.01
:Until ∨/mb.is_pressed¨'ab'
⎕←(time←⎕AI[3]-start)'ms'
r,←time
:Until mb.was_pressed 'b'
r←¯1↓r ⍝ drop time to press 'b'
⎕←'Bye...'
mb.show 'SAD'
∇
AsciiChart←{
scale←25 ⍝ size of each row
tiks←4 ⍝ tik spacing
(max min)←(⌈/ , ⌊/) ⍵ ⍝ maximum and minimum
base←⌊min÷scale ⍝ round down to nearest scale unit
rb←base+0,⍳⌈(max-min)÷scale ⍝ row base values
r←' *'[1+rb∘.=⌊⍵÷scale] ⍝ our chart
r←((≢rb)⍴'+',(tiks-1)⍴'|'),' ',r ⍝ add tiks
r←(⍕⍪rb×scale),r ⍝ add base values
⊖r ⍝ low values last (humans!)
}
∇ SPHistogram times;heading;renderHtml;sp;svg;z
heading←'Reaction Times'
renderHtml←3500⌶ ⍝ Render HTML in Window
:If 0=⎕NC '#.SharpPlot' ⋄ #.⎕CY 'sharpplot.dws' ⋄ :EndIf
sp←⎕NEW #.SharpPlot (432 250)
sp.Heading←heading
⍝ Draw histogram
sp.ClassInterval←25
sp.SetXTickMarks 25
sp.HistogramStyle←#.HistogramStyles.SurfaceShading
sp.SetFillStyles #.FillStyle.Opacity30
sp.DrawHistogram ⊂times
⍝ Add observations using ScatterPlot
sp.SetMarkers #.Marker.UpTick
sp.SetPenWidths 1
sp.SetMarkerScales 3
sp.DrawScatterPlot(times×0)(times)
⍝ Render SVG and display in window
svg←sp.RenderSvg #.SvgMode.FixedAspect
z←heading renderHtml svg
∇
:EndNamespace